3 * StatusNet, the distributed open-source microblogging tool
5 * Show a the direct messages from or to a user
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Adrian Lang <mail@adrianlang.de>
25 * @author Evan Prodromou <evan@status.net>
26 * @author Robin Millette <robin@millette.info>
27 * @author Zach Copley <zach@status.net>
28 * @copyright 2009 StatusNet, Inc.
29 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30 * @link http://status.net/
33 if (!defined('STATUSNET')) {
38 * Show a list of direct messages from or to the authenticating user
42 * @author Adrian Lang <mail@adrianlang.de>
43 * @author Evan Prodromou <evan@status.net>
44 * @author Robin Millette <robin@millette.info>
45 * @author Zach Copley <zach@status.net>
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47 * @link http://status.net/
49 class ApiDirectMessageAction extends ApiAuthAction
55 var $selfuri_base = null;
59 * Take arguments for running
61 * @param array $args $_REQUEST args
63 * @return boolean success flag
65 function prepare(array $args=array())
67 parent::prepare($args);
69 $this->user = $this->auth_user;
71 if (empty($this->user)) {
72 // TRANS: Client error given when a user was not found (404).
73 $this->clientError(_('No such user.'), 404);
76 $server = common_root_url();
77 $taguribase = TagURI::base();
79 if ($this->arg('sent')) {
81 // Action was called by /api/direct_messages/sent.format
83 $this->title = sprintf(
84 // TRANS: Title. %s is a user nickname.
85 _("Direct messages from %s"),
88 $this->subtitle = sprintf(
89 // TRANS: Subtitle. %s is a user nickname.
90 _("All the direct messages sent from %s"),
93 $this->link = $server . $this->user->nickname . '/outbox';
94 $this->selfuri_base = common_root_url() . 'api/direct_messages/sent';
95 $this->id = "tag:$taguribase:SentDirectMessages:" . $this->user->id;
97 $this->title = sprintf(
98 // TRANS: Title. %s is a user nickname.
99 _("Direct messages to %s"),
100 $this->user->nickname
102 $this->subtitle = sprintf(
103 // TRANS: Subtitle. %s is a user nickname.
104 _("All the direct messages sent to %s"),
105 $this->user->nickname
107 $this->link = $server . $this->user->nickname . '/inbox';
108 $this->selfuri_base = common_root_url() . 'api/direct_messages';
109 $this->id = "tag:$taguribase:DirectMessages:" . $this->user->id;
112 $this->messages = $this->getMessages();
122 * @param array $args $_REQUEST data (unused)
126 function handle(array $args=array())
128 parent::handle($args);
129 $this->showMessages();
137 function showMessages()
139 switch($this->format) {
141 $this->showXmlDirectMessages();
144 $this->showRssDirectMessages();
147 $this->showAtomDirectMessages();
150 $this->showJsonDirectMessages();
153 // TRANS: Client error displayed when coming across a non-supported API method.
154 $this->clientError(_('API method not found.'), $code = 404);
162 * @return array notices
164 function getMessages()
166 $message = new Message();
168 if ($this->arg('sent')) {
169 $message->from_profile = $this->user->id;
171 $message->to_profile = $this->user->id;
174 if (!empty($this->max_id)) {
175 $message->whereAdd('id <= ' . $this->max_id);
178 if (!empty($this->since_id)) {
179 $message->whereAdd('id > ' . $this->since_id);
182 $message->orderBy('created DESC, id DESC');
183 $message->limit((($this->page - 1) * $this->count), $this->count);
188 while ($message->fetch()) {
189 $messages[] = clone($message);
196 * Is this action read only?
198 * @param array $args other arguments
200 * @return boolean true
202 function isReadOnly(array $args=array())
208 * When was this notice last modified?
210 * @return string datestamp of the latest notice in the stream
212 function lastModified()
214 if (!empty($this->messages)) {
215 return strtotime($this->messages[0]->created);
221 // BEGIN import from lib/apiaction.php
223 function showSingleXmlDirectMessage($message)
225 $this->initDocument('xml');
226 $dmsg = $this->directMessageArray($message);
227 $this->showXmlDirectMessage($dmsg, true);
228 $this->endDocument('xml');
231 function showSingleJsonDirectMessage($message)
233 $this->initDocument('json');
234 $dmsg = $this->directMessageArray($message);
235 $this->showJsonObjects($dmsg);
236 $this->endDocument('json');
239 function showXmlDirectMessage($dm, $namespaces=false)
243 $attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
245 $this->elementStart('direct_message', $attrs);
246 foreach($dm as $element => $value) {
250 $this->showTwitterXmlUser($value, $element);
253 $this->element($element, null, common_xml_safe_str($value));
256 $this->element($element, null, $value);
260 $this->elementEnd('direct_message');
263 function directMessageArray($message)
267 $from_profile = $message->getFrom();
268 $to_profile = $message->getTo();
270 $dmsg['id'] = intval($message->id);
271 $dmsg['sender_id'] = intval($from_profile->id);
272 $dmsg['text'] = trim($message->content);
273 $dmsg['recipient_id'] = intval($to_profile->id);
274 $dmsg['created_at'] = $this->dateTwitter($message->created);
275 $dmsg['sender_screen_name'] = $from_profile->nickname;
276 $dmsg['recipient_screen_name'] = $to_profile->nickname;
277 $dmsg['sender'] = $this->twitterUserArray($from_profile, false);
278 $dmsg['recipient'] = $this->twitterUserArray($to_profile, false);
283 function rssDirectMessageArray($message)
287 $from = $message->getFrom();
289 $entry['title'] = sprintf('Message from %1$s to %2$s',
290 $from->nickname, $message->getTo()->nickname);
292 $entry['content'] = common_xml_safe_str($message->rendered);
293 $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
294 $entry['published'] = common_date_iso8601($message->created);
296 $taguribase = TagURI::base();
298 $entry['id'] = "tag:$taguribase:$entry[link]";
299 $entry['updated'] = $entry['published'];
301 $entry['author-name'] = $from->getBestName();
302 $entry['author-uri'] = $from->homepage;
304 $entry['avatar'] = $from->avatarUrl(AVATAR_STREAM_SIZE);
306 $avatar = $from->getAvatar(AVATAR_STREAM_SIZE);
307 $entry['avatar-type'] = $avatar->mediatype;
308 } catch (Exception $e) {
309 $entry['avatar-type'] = 'image/png';
314 $entry['description'] = $entry['content'];
315 $entry['pubDate'] = common_date_rfc2822($message->created);
316 $entry['guid'] = $entry['link'];
321 // END import from lib/apiaction.php
324 * Shows a list of direct messages as Twitter-style XML array
328 function showXmlDirectMessages()
330 $this->initDocument('xml');
331 $this->elementStart('direct-messages', array('type' => 'array',
332 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
334 foreach ($this->messages as $m) {
335 $dm_array = $this->directMessageArray($m);
336 $this->showXmlDirectMessage($dm_array);
339 $this->elementEnd('direct-messages');
340 $this->endDocument('xml');
344 * Shows a list of direct messages as a JSON encoded array
348 function showJsonDirectMessages()
350 $this->initDocument('json');
354 foreach ($this->messages as $m) {
355 $dm_array = $this->directMessageArray($m);
356 array_push($dmsgs, $dm_array);
359 $this->showJsonObjects($dmsgs);
360 $this->endDocument('json');
364 * Shows a list of direct messages as RSS items
368 function showRssDirectMessages()
370 $this->initDocument('rss');
372 $this->element('title', null, $this->title);
374 $this->element('link', null, $this->link);
375 $this->element('description', null, $this->subtitle);
376 $this->element('language', null, 'en-us');
381 'type' => 'application/rss+xml',
382 'href' => $this->selfuri_base . '.rss',
387 $this->element('ttl', null, '40');
389 foreach ($this->messages as $m) {
390 $entry = $this->rssDirectMessageArray($m);
391 $this->showTwitterRssItem($entry);
394 $this->endTwitterRss();
398 * Shows a list of direct messages as Atom entries
402 function showAtomDirectMessages()
404 $this->initDocument('atom');
406 $this->element('title', null, $this->title);
407 $this->element('id', null, $this->id);
409 $selfuri = common_root_url() . 'api/direct_messages.atom';
413 'href' => $this->link,
414 'rel' => 'alternate',
415 'type' => 'text/html'),
420 'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
421 'type' => 'application/atom+xml'),
424 $this->element('updated', null, common_date_iso8601('now'));
425 $this->element('subtitle', null, $this->subtitle);
427 foreach ($this->messages as $m) {
428 $entry = $this->rssDirectMessageArray($m);
429 $this->showTwitterAtomEntry($entry);
432 $this->endDocument('atom');
436 * An entity tag for this notice
438 * Returns an Etag based on the action name, language, and
439 * timestamps of the notice
441 * @return string etag
445 if (!empty($this->messages)) {
447 $last = count($this->messages) - 1;
449 return '"' . implode(
451 array($this->arg('action'),
452 common_user_cache_hash($this->auth_user),
454 strtotime($this->messages[0]->created),
455 strtotime($this->messages[$last]->created)