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