3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/twitterapi.php');
24 class Twitapidirect_messagesAction extends TwitterapiAction {
26 function direct_messages($args, $apidata)
28 parent::handle($args);
29 return $this->show_messages($args, $apidata, 'received');
32 function sent($args, $apidata)
34 parent::handle($args);
35 return $this->show_messages($args, $apidata, 'sent');
38 function show_messages($args, $apidata, $type)
41 $user = $apidata['user'];
43 $count = $this->arg('count');
44 $since = $this->arg('since');
45 $since_id = $this->arg('since_id');
46 $before_id = $this->arg('before_id');
48 $page = $this->arg('page');
58 $message = new Message();
63 $server = common_root_url();
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';
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';
78 $message->whereAdd("id < $before_id");
82 $message->whereAdd("id > $since_id");
85 $since = strtotime($this->arg('since'));
88 $d = date('Y-m-d H:i:s', $since);
89 $message->whereAdd("created > '$d'");
92 $message->orderBy('created DESC, id DESC');
93 $message->limit((($page-1)*20), $count);
96 switch($apidata['content-type']) {
98 $this->show_xml_dmsgs($message);
101 $this->show_rss_dmsgs($message, $title, $link, $subtitle);
104 $this->show_atom_dmsgs($message, $title, $link, $subtitle);
107 $this->show_json_dmsgs($message);
110 common_user_error(_('API method not found!'), $code = 404);
115 // had to change this from "new" to "create" to avoid PHP reserved word
116 function create($args, $apidata)
118 parent::handle($args);
120 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
121 $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
125 $user = $apidata['user'];
126 $source = $this->trimmed('source'); // Not supported by Twitter.
128 $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
129 if (!$source || in_array($source, $reserved_sources)) {
133 $content = $this->trimmed('text');
136 $this->client_error(_('No message text!'), $code = 406, $apidata['content-type']);
138 $content_shortened = common_shorten_links($content);
139 if (mb_strlen($content_shortened) > 140) {
140 $this->client_error(_('That\'s too long. Max message size is 140 chars.'),
141 $code = 406, $apidata['content-type']);
146 $other = $this->get_user($this->trimmed('user'));
149 $this->client_error(_('Recipient user not found.'), $code = 403, $apidata['content-type']);
151 } else if (!$user->mutuallySubscribed($other)) {
152 $this->client_error(_('Can\'t send direct messages to users who aren\'t your friend.'),
153 $code = 403, $apidata['content-type']);
155 } else if ($user->id == $other->id) {
156 // Sending msgs to yourself is allowed by Twitter
157 $this->client_error(_('Don\'t send a message to yourself; just say it to yourself quietly instead.'),
158 $code = 403, $apidata['content-type']);
162 $message = Message::saveNew($user->id, $other->id,
163 html_entity_decode($content, ENT_NOQUOTES, 'UTF-8'), $source);
165 if (is_string($message)) {
166 $this->server_error($message);
170 $this->notify($user, $other, $message);
172 if ($apidata['content-type'] == 'xml') {
173 $this->show_single_xml_dmsg($message);
174 } elseif ($apidata['content-type'] == 'json') {
175 $this->show_single_json_dmsg($message);
180 function destroy($args, $apidata)
182 parent::handle($args);
183 common_server_error(_('API method under construction.'), $code=501);
186 function show_xml_dmsgs($message)
189 $this->init_document('xml');
190 common_element_start('direct-messages', array('type' => 'array'));
192 if (is_array($messages)) {
193 foreach ($message as $m) {
194 $twitter_dm = $this->twitter_dmsg_array($m);
195 $this->show_twitter_xml_dmsg($twitter_dm);
198 while ($message->fetch()) {
199 $twitter_dm = $this->twitter_dmsg_array($message);
200 $this->show_twitter_xml_dmsg($twitter_dm);
204 common_element_end('direct-messages');
205 $this->end_document('xml');
209 function show_json_dmsgs($message)
212 $this->init_document('json');
216 if (is_array($message)) {
217 foreach ($message as $m) {
218 $twitter_dm = $this->twitter_dmsg_array($m);
219 array_push($dmsgs, $twitter_dm);
222 while ($message->fetch()) {
223 $twitter_dm = $this->twitter_dmsg_array($message);
224 array_push($dmsgs, $twitter_dm);
228 $this->show_json_objects($dmsgs);
229 $this->end_document('json');
233 function show_rss_dmsgs($message, $title, $link, $subtitle)
236 $this->init_document('rss');
238 common_element_start('channel');
239 common_element('title', null, $title);
241 common_element('link', null, $link);
242 common_element('description', null, $subtitle);
243 common_element('language', null, 'en-us');
244 common_element('ttl', null, '40');
246 if (is_array($message)) {
247 foreach ($message as $m) {
248 $entry = $this->twitter_rss_dmsg_array($m);
249 $this->show_twitter_rss_item($entry);
252 while ($message->fetch()) {
253 $entry = $this->twitter_rss_dmsg_array($message);
254 $this->show_twitter_rss_item($entry);
258 common_element_end('channel');
259 $this->end_twitter_rss();
263 function show_atom_dmsgs($message, $title, $link, $subtitle)
266 $this->init_document('atom');
268 common_element('title', null, $title);
269 $siteserver = common_config('site', 'server');
270 common_element('id', null, "tag:$siteserver,2008:DirectMessage");
271 common_element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null);
272 common_element('updated', null, common_date_iso8601(strftime('%c')));
273 common_element('subtitle', null, $subtitle);
275 if (is_array($message)) {
276 foreach ($message as $m) {
277 $entry = $this->twitter_rss_dmsg_array($m);
278 $this->show_twitter_atom_entry($entry);
281 while ($message->fetch()) {
282 $entry = $this->twitter_rss_dmsg_array($message);
283 $this->show_twitter_atom_entry($entry);
287 $this->end_document('atom');
290 // swiped from MessageAction. Should it be place in util.php?
291 function notify($from, $to, $message)
293 mail_notify_message($message, $from, $to);
294 # XXX: Jabber, SMS notifications... probably queued