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