3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, Control Yourself, 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')) {
24 require_once(INSTALLDIR.'/lib/twitterapi.php');
26 class Twitapidirect_messagesAction extends TwitterapiAction
29 function direct_messages($args, $apidata)
31 parent::handle($args);
32 return $this->show_messages($args, $apidata, 'received');
35 function sent($args, $apidata)
37 parent::handle($args);
38 return $this->show_messages($args, $apidata, 'sent');
41 function show_messages($args, $apidata, $type)
43 $user = $apidata['user']; // Always the auth user
45 $message = new Message();
49 $server = common_root_url();
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"),
56 $link = $server . $user->nickname . '/inbox';
58 $message->from_profile = $user->id;
59 $title = _('Direct Messages You\'ve Sent');
60 $subtitle = sprintf(_("All the direct messages sent from %s"),
62 $link = $server . $user->nickname . '/outbox';
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');
72 $message->whereAdd("id <= $max_id");
76 $message->whereAdd("id > $since_id");
80 $d = date('Y-m-d H:i:s', $since);
81 $message->whereAdd("created > '$d'");
84 $message->orderBy('created DESC, id DESC');
85 $message->limit((($page-1)*$count), $count);
88 switch($apidata['content-type']) {
90 $this->show_xml_dmsgs($message);
93 $this->show_rss_dmsgs($message, $title, $link, $subtitle);
96 $selfuri = common_root_url() . 'api/direct_messages';
97 $selfuri .= ($type == 'received') ? '.atom' : '/sent.atom';
98 $taguribase = common_config('integration', 'taguri');
100 if ($type == 'sent') {
101 $id = "tag:$taguribase:SentDirectMessages:" . $user->id;
103 $id = "tag:$taguribase:DirectMessages:" . $user->id;
106 $this->show_atom_dmsgs($message, $title, $link, $subtitle,
110 $this->show_json_dmsgs($message);
113 $this->clientError(_('API method not found!'), $code = 404);
118 // had to change this from "new" to "create" to avoid PHP reserved word
119 function create($args, $apidata)
121 parent::handle($args);
123 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
124 $this->clientError(_('This method requires a POST.'),
125 400, $apidata['content-type']);
129 $user = $apidata['user'];
130 $source = $this->trimmed('source'); // Not supported by Twitter.
132 $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
133 if (empty($source) || in_array($source, $reserved_sources)) {
137 $content = $this->trimmed('text');
139 if (empty($content)) {
140 $this->clientError(_('No message text!'),
141 $code = 406, $apidata['content-type']);
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']);
151 $other = $this->get_user($this->trimmed('user'));
154 $this->clientError(_('Recipient user not found.'),
155 $code = 403, $apidata['content-type']);
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']);
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']);
168 $message = Message::saveNew($user->id, $other->id,
169 html_entity_decode($content, ENT_NOQUOTES, 'UTF-8'), $source);
171 if (is_string($message)) {
172 $this->serverError($message);
176 $this->notify($user, $other, $message);
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);
186 function destroy($args, $apidata)
188 parent::handle($args);
189 $this->serverError(_('API method under construction.'), $code=501);
192 function show_xml_dmsgs($message)
195 $this->init_document('xml');
196 $this->elementStart('direct-messages', array('type' => 'array'));
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);
204 while ($message->fetch()) {
205 $twitter_dm = $this->twitter_dmsg_array($message);
206 $this->show_twitter_xml_dmsg($twitter_dm);
210 $this->elementEnd('direct-messages');
211 $this->end_document('xml');
215 function show_json_dmsgs($message)
218 $this->init_document('json');
222 if (is_array($message)) {
223 foreach ($message as $m) {
224 $twitter_dm = $this->twitter_dmsg_array($m);
225 array_push($dmsgs, $twitter_dm);
228 while ($message->fetch()) {
229 $twitter_dm = $this->twitter_dmsg_array($message);
230 array_push($dmsgs, $twitter_dm);
234 $this->show_json_objects($dmsgs);
235 $this->end_document('json');
239 function show_rss_dmsgs($message, $title, $link, $subtitle)
242 $this->init_document('rss');
244 $this->elementStart('channel');
245 $this->element('title', null, $title);
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');
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);
258 while ($message->fetch()) {
259 $entry = $this->twitter_rss_dmsg_array($message);
260 $this->show_twitter_rss_item($entry);
264 $this->elementEnd('channel');
265 $this->end_twitter_rss();
269 function show_atom_dmsgs($message, $title, $link, $subtitle, $selfuri, $id)
272 $this->init_document('atom');
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);
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);
288 while ($message->fetch()) {
289 $entry = $this->twitter_rss_dmsg_array($message);
290 $this->show_twitter_atom_entry($entry);
294 $this->end_document('atom');
297 // swiped from MessageAction. Should it be place in util.php?
298 function notify($from, $to, $message)
300 mail_notify_message($message, $from, $to);
301 # XXX: Jabber, SMS notifications... probably queued