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