]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapidirect_messages.php
70e3ef64ea26be8d2a595553ae656fab86764696
[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('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 (mb_strlen($content_shortened) > 140) {
145                 $this->clientError(_('That\'s too long. Max message size is 140 chars.'),
146                     $code = 406, $apidata['content-type']);
147                 return;
148             }
149         }
150
151         $other = $this->get_user($this->trimmed('user'));
152
153         if (empty($other)) {
154             $this->clientError(_('Recipient user not found.'),
155                 $code = 403, $apidata['content-type']);
156             return;
157         } else if (!$user->mutuallySubscribed($other)) {
158             $this->clientError(_('Can\'t send direct messages to users who aren\'t your friend.'),
159                 $code = 403, $apidata['content-type']);
160             return;
161         } else if ($user->id == $other->id) {
162             // Sending msgs to yourself is allowed by Twitter
163             $this->clientError(_('Don\'t send a message to yourself; just say it to yourself quietly instead.'),
164                 $code = 403, $apidata['content-type']);
165             return;
166         }
167
168         $message = Message::saveNew($user->id, $other->id,
169             html_entity_decode($content, ENT_NOQUOTES, 'UTF-8'), $source);
170
171         if (is_string($message)) {
172             $this->serverError($message);
173             return;
174         }
175
176         $this->notify($user, $other, $message);
177
178         if ($apidata['content-type'] == 'xml') {
179             $this->show_single_xml_dmsg($message);
180         } elseif ($apidata['content-type'] == 'json') {
181             $this->show_single_json_dmsg($message);
182         }
183
184     }
185
186     function destroy($args, $apidata)
187     {
188         parent::handle($args);
189         $this->serverError(_('API method under construction.'), $code=501);
190     }
191
192     function show_xml_dmsgs($message)
193     {
194
195         $this->init_document('xml');
196         $this->elementStart('direct-messages', array('type' => 'array'));
197
198         if (is_array($message)) {
199             foreach ($message as $m) {
200                 $twitter_dm = $this->twitter_dmsg_array($m);
201                 $this->show_twitter_xml_dmsg($twitter_dm);
202             }
203         } else {
204             while ($message->fetch()) {
205                 $twitter_dm = $this->twitter_dmsg_array($message);
206                 $this->show_twitter_xml_dmsg($twitter_dm);
207             }
208         }
209
210         $this->elementEnd('direct-messages');
211         $this->end_document('xml');
212
213     }
214
215     function show_json_dmsgs($message)
216     {
217
218         $this->init_document('json');
219
220         $dmsgs = array();
221
222         if (is_array($message)) {
223             foreach ($message as $m) {
224                 $twitter_dm = $this->twitter_dmsg_array($m);
225                 array_push($dmsgs, $twitter_dm);
226             }
227         } else {
228             while ($message->fetch()) {
229                 $twitter_dm = $this->twitter_dmsg_array($message);
230                 array_push($dmsgs, $twitter_dm);
231             }
232         }
233
234         $this->show_json_objects($dmsgs);
235         $this->end_document('json');
236
237     }
238
239     function show_rss_dmsgs($message, $title, $link, $subtitle)
240     {
241
242         $this->init_document('rss');
243
244         $this->elementStart('channel');
245         $this->element('title', null, $title);
246
247         $this->element('link', null, $link);
248         $this->element('description', null, $subtitle);
249         $this->element('language', null, 'en-us');
250         $this->element('ttl', null, '40');
251
252         if (is_array($message)) {
253             foreach ($message as $m) {
254                 $entry = $this->twitter_rss_dmsg_array($m);
255                 $this->show_twitter_rss_item($entry);
256             }
257         } else {
258             while ($message->fetch()) {
259                 $entry = $this->twitter_rss_dmsg_array($message);
260                 $this->show_twitter_rss_item($entry);
261             }
262         }
263
264         $this->elementEnd('channel');
265         $this->end_twitter_rss();
266
267     }
268
269     function show_atom_dmsgs($message, $title, $link, $subtitle, $selfuri, $id)
270     {
271
272         $this->init_document('atom');
273
274         $this->element('title', null, $title);
275         $this->element('id', null, $id);
276         $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
277         $this->element('link', array('href' => $selfuri, 'rel' => 'self',
278             'type' => 'application/atom+xml'), null);
279         $this->element('updated', null, common_date_iso8601('now'));
280         $this->element('subtitle', null, $subtitle);
281
282         if (is_array($message)) {
283             foreach ($message as $m) {
284                 $entry = $this->twitter_rss_dmsg_array($m);
285                 $this->show_twitter_atom_entry($entry);
286             }
287         } else {
288             while ($message->fetch()) {
289                 $entry = $this->twitter_rss_dmsg_array($message);
290                 $this->show_twitter_atom_entry($entry);
291             }
292         }
293
294         $this->end_document('atom');
295     }
296
297     // swiped from MessageAction. Should it be place in util.php?
298     function notify($from, $to, $message)
299     {
300         mail_notify_message($message, $from, $to);
301         # XXX: Jabber, SMS notifications... probably queued
302     }
303
304 }