]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - _darcs/pristine/actions/twitapidirect_messages.php
replace NULL with null
[quix0rs-gnu-social.git] / _darcs / pristine / actions / twitapidirect_messages.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/twitterapi.php');
23
24 class Twitapidirect_messagesAction extends TwitterapiAction {
25
26     function direct_messages($args, $apidata) {
27         parent::handle($args);
28         return $this->show_messages($args, $apidata, 'received');
29     }
30
31     function sent($args, $apidata) {
32         parent::handle($args);
33         return $this->show_messages($args, $apidata, 'sent');
34     }
35
36     function show_messages($args, $apidata, $type) {
37
38         $user = $apidata['user'];
39
40         $count = $this->arg('count');
41         $since = $this->arg('since');
42         $since_id = $this->arg('since_id');
43         $before_id = $this->arg('before_id');
44
45         $page = $this->arg('page');
46
47         if (!$page) {
48             $page = 1;
49         }
50
51         if (!$count) {
52             $count = 20;
53         }
54
55         $message = new Message();
56
57         $title = null;
58         $subtitle = null;
59         $link = null;
60         $server = common_root_url();
61
62         if ($type == 'received') {
63             $message->to_profile = $user->id;
64             $title = sprintf(_("Direct messages to %s"), $user->nickname);
65             $subtitle = sprintf(_("All the direct messages sent to %s"), $user->nickname);
66             $link = $server . $user->nickname . '/inbox';
67         } else {
68             $message->from_profile = $user->id;
69             $title = _('Direct Messages You\'ve Sent');
70             $subtitle = sprintf(_("All the direct messages sent from %s"), $user->nickname);
71             $link = $server . $user->nickname . '/outbox';
72         }
73
74         if ($before_id) {
75             $message->whereAdd("id < $before_id");
76         }
77
78         if ($since_id) {
79             $message->whereAdd("id > $since_id");
80         }
81
82         $since = strtotime($this->arg('since'));
83
84         if ($since) {
85             $d = date('Y-m-d H:i:s', $since);
86             $message->whereAdd("created > '$d'");
87         }
88
89         $message->orderBy('created DESC, id DESC');
90         $message->limit((($page-1)*20), $count);
91         $message->find();
92
93         switch($apidata['content-type']) {
94          case 'xml':
95             $this->show_xml_dmsgs($message);
96             break;
97          case 'rss':
98             $this->show_rss_dmsgs($message, $title, $link, $subtitle);
99             break;
100          case 'atom':
101             $this->show_atom_dmsgs($message, $title, $link, $subtitle);
102             break;
103          case 'json':
104             $this->show_json_dmsgs($message);
105             break;
106          default:
107             common_user_error(_('API method not found!'), $code = 404);
108         }
109
110     }
111
112     // had to change this from "new" to "create" to avoid PHP reserved word
113     function create($args, $apidata) {
114         parent::handle($args);
115
116         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
117             $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
118             return;
119         }
120
121         $user = $apidata['user'];
122         $source = $this->trimmed('source');     // Not supported by Twitter.
123
124         $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
125         if (!$source || in_array($source, $reserved_sources)) {
126             $source = 'api';
127         }
128
129         $content = $this->trimmed('text');
130
131         if (!$content) {
132             $this->client_error(_('No message text!'), $code = 406, $apidata['content-type']);
133         } else {
134             $content_shortened = common_shorten_links($content);
135             if (mb_strlen($content_shortened) > 140) {
136                 $this->client_error(_('That\'s too long. Max message size is 140 chars.'),
137                     $code = 406, $apidata['content-type']);
138                 return;
139             }
140         }
141
142         $other = $this->get_user($this->trimmed('user'));
143
144         if (!$other) {
145             $this->client_error(_('Recipient user not found.'), $code = 403, $apidata['content-type']);
146             return;
147         } else if (!$user->mutuallySubscribed($other)) {
148             $this->client_error(_('Can\'t send direct messages to users who aren\'t your friend.'),
149                 $code = 403, $apidata['content-type']);
150             return;
151         } else if ($user->id == $other->id) {
152             // Sending msgs to yourself is allowed by Twitter
153             $this->client_error(_('Don\'t send a message to yourself; just say it to yourself quietly instead.'),
154                 $code = 403, $apidata['content-type']);
155             return;
156         }
157
158         $message = Message::saveNew($user->id, $other->id,
159             html_entity_decode($content, ENT_NOQUOTES, 'UTF-8'), $source);
160
161         if (is_string($message)) {
162             $this->server_error($message);
163             return;
164         }
165
166         $this->notify($user, $other, $message);
167
168         if ($apidata['content-type'] == 'xml') {
169             $this->show_single_xml_dmsg($message);
170         } elseif ($apidata['content-type'] == 'json') {
171             $this->show_single_json_dmsg($message);
172         }
173
174     }
175
176     function destroy($args, $apidata) {
177         parent::handle($args);
178         common_server_error(_('API method under construction.'), $code=501);
179     }
180
181     function show_xml_dmsgs($message) {
182
183         $this->init_document('xml');
184         common_element_start('direct-messages', array('type' => 'array'));
185
186         if (is_array($messages)) {
187             foreach ($message as $m) {
188                 $twitter_dm = $this->twitter_dmsg_array($m);
189                 $this->show_twitter_xml_dmsg($twitter_dm);
190             }
191         } else {
192             while ($message->fetch()) {
193                 $twitter_dm = $this->twitter_dmsg_array($message);
194                 $this->show_twitter_xml_dmsg($twitter_dm);
195             }
196         }
197
198         common_element_end('direct-messages');
199         $this->end_document('xml');
200
201     }
202
203     function show_json_dmsgs($message) {
204
205         $this->init_document('json');
206
207         $dmsgs = array();
208
209         if (is_array($message)) {
210             foreach ($message as $m) {
211                 $twitter_dm = $this->twitter_dmsg_array($m);
212                 array_push($dmsgs, $twitter_dm);
213             }
214         } else {
215             while ($message->fetch()) {
216                 $twitter_dm = $this->twitter_dmsg_array($message);
217                 array_push($dmsgs, $twitter_dm);
218             }
219         }
220
221         $this->show_json_objects($dmsgs);
222         $this->end_document('json');
223
224     }
225
226     function show_rss_dmsgs($message, $title, $link, $subtitle) {
227
228         $this->init_document('rss');
229
230         common_element_start('channel');
231         common_element('title', null, $title);
232
233         common_element('link', null, $link);
234         common_element('description', null, $subtitle);
235         common_element('language', null, 'en-us');
236         common_element('ttl', null, '40');
237
238         if (is_array($message)) {
239             foreach ($message as $m) {
240                 $entry = $this->twitter_rss_dmsg_array($m);
241                 $this->show_twitter_rss_item($entry);
242             }
243         } else {
244             while ($message->fetch()) {
245                 $entry = $this->twitter_rss_dmsg_array($message);
246                 $this->show_twitter_rss_item($entry);
247             }
248         }
249
250         common_element_end('channel');
251         $this->end_twitter_rss();
252
253     }
254
255     function show_atom_dmsgs($message, $title, $link, $subtitle) {
256
257         $this->init_document('atom');
258
259         common_element('title', null, $title);
260         $siteserver = common_config('site', 'server');
261         common_element('id', null, "tag:$siteserver,2008:DirectMessage");
262         common_element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
263         common_element('updated', null, common_date_iso8601(strftime('%c')));
264         common_element('subtitle', null, $subtitle);
265
266         if (is_array($message)) {
267             foreach ($message as $m) {
268                 $entry = $this->twitter_rss_dmsg_array($m);
269                 $this->show_twitter_atom_entry($entry);
270             }
271         } else {
272             while ($message->fetch()) {
273                 $entry = $this->twitter_rss_dmsg_array($message);
274                 $this->show_twitter_atom_entry($entry);
275             }
276         }
277
278         $this->end_document('atom');
279     }
280
281     // swiped from MessageAction. Should it be place in util.php?
282     function notify($from, $to, $message) {
283         mail_notify_message($message, $from, $to);
284         # XXX: Jabber, SMS notifications... probably queued
285     }
286
287 }