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