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