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('GNUSOCIAL')) { exit(1); }
36 * Show a list of direct messages from or to the authenticating user
40 * @author Adrian Lang <mail@adrianlang.de>
41 * @author Evan Prodromou <evan@status.net>
42 * @author Robin Millette <robin@millette.info>
43 * @author Zach Copley <zach@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
47 class ApiDirectMessageAction extends ApiAuthAction
53 var $selfuri_base = null;
57 * Take arguments for running
59 * @param array $args $_REQUEST args
61 * @return boolean success flag
63 protected function prepare(array $args=array())
65 parent::prepare($args);
67 if (!$this->scoped instanceof Profile) {
68 // TRANS: Client error given when a user was not found (404).
69 $this->clientError(_('No such user.'), 404);
72 $server = common_root_url();
73 $taguribase = TagURI::base();
75 if ($this->arg('sent')) {
77 // Action was called by /api/direct_messages/sent.format
79 $this->title = sprintf(
80 // TRANS: Title. %s is a user nickname.
81 _("Direct messages from %s"),
82 $this->scoped->getNickname()
84 $this->subtitle = sprintf(
85 // TRANS: Subtitle. %s is a user nickname.
86 _("All the direct messages sent from %s"),
87 $this->scoped->getNickname()
89 $this->link = $server . $this->scoped->getNickname() . '/outbox';
90 $this->selfuri_base = common_root_url() . 'api/direct_messages/sent';
91 $this->id = "tag:$taguribase:SentDirectMessages:" . $this->scoped->getID();
93 $this->title = sprintf(
94 // TRANS: Title. %s is a user nickname.
95 _("Direct messages to %s"),
96 $this->scoped->getNickname()
98 $this->subtitle = sprintf(
99 // TRANS: Subtitle. %s is a user nickname.
100 _("All the direct messages sent to %s"),
101 $this->scoped->getNickname()
103 $this->link = $server . $this->scoped->getNickname() . '/inbox';
104 $this->selfuri_base = common_root_url() . 'api/direct_messages';
105 $this->id = "tag:$taguribase:DirectMessages:" . $this->scoped->getID();
108 $this->messages = $this->getMessages();
113 protected function handle()
116 $this->showMessages();
124 function showMessages()
126 switch($this->format) {
128 $this->showXmlDirectMessages();
131 $this->showRssDirectMessages();
134 $this->showAtomDirectMessages();
137 $this->showJsonDirectMessages();
140 // TRANS: Client error displayed when coming across a non-supported API method.
141 $this->clientError(_('API method not found.'), $code = 404);
149 * @return array notices
151 function getMessages()
153 $message = new Message();
155 if ($this->arg('sent')) {
156 $message->from_profile = $this->scoped->getID();
158 $message->to_profile = $this->scoped->getID();
161 if (!empty($this->max_id)) {
162 $message->whereAdd('id <= ' . $this->max_id);
165 if (!empty($this->since_id)) {
166 $message->whereAdd('id > ' . $this->since_id);
169 $message->orderBy('created DESC, id DESC');
170 $message->limit((($this->page - 1) * $this->count), $this->count);
175 while ($message->fetch()) {
176 $messages[] = clone($message);
183 * Is this action read only?
185 * @param array $args other arguments
187 * @return boolean true
189 function isReadOnly($args)
195 * When was this notice last modified?
197 * @return string datestamp of the latest notice in the stream
199 function lastModified()
201 if (!empty($this->messages)) {
202 return strtotime($this->messages[0]->created);
208 // BEGIN import from lib/apiaction.php
210 function showSingleXmlDirectMessage($message)
212 $this->initDocument('xml');
213 $dmsg = $this->directMessageArray($message);
214 $this->showXmlDirectMessage($dmsg, true);
215 $this->endDocument('xml');
218 function showSingleJsonDirectMessage($message)
220 $this->initDocument('json');
221 $dmsg = $this->directMessageArray($message);
222 $this->showJsonObjects($dmsg);
223 $this->endDocument('json');
226 function showXmlDirectMessage($dm, $namespaces=false)
230 $attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
232 $this->elementStart('direct_message', $attrs);
233 foreach($dm as $element => $value) {
237 $this->showTwitterXmlUser($value, $element);
240 $this->element($element, null, common_xml_safe_str($value));
243 $this->element($element, null, $value);
247 $this->elementEnd('direct_message');
250 function directMessageArray($message)
254 $from_profile = $message->getFrom();
255 $to_profile = $message->getTo();
257 $dmsg['id'] = intval($message->id);
258 $dmsg['sender_id'] = intval($from_profile->id);
259 $dmsg['text'] = trim($message->content);
260 $dmsg['recipient_id'] = intval($to_profile->id);
261 $dmsg['created_at'] = $this->dateTwitter($message->created);
262 $dmsg['sender_screen_name'] = $from_profile->nickname;
263 $dmsg['recipient_screen_name'] = $to_profile->nickname;
264 $dmsg['sender'] = $this->twitterUserArray($from_profile, false);
265 $dmsg['recipient'] = $this->twitterUserArray($to_profile, false);
270 function rssDirectMessageArray($message)
274 $from = $message->getFrom();
276 $entry['title'] = sprintf('Message from %1$s to %2$s',
277 $from->nickname, $message->getTo()->nickname);
279 $entry['content'] = common_xml_safe_str($message->rendered);
280 $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
281 $entry['published'] = common_date_iso8601($message->created);
283 $taguribase = TagURI::base();
285 $entry['id'] = "tag:$taguribase:$entry[link]";
286 $entry['updated'] = $entry['published'];
288 $entry['author-name'] = $from->getBestName();
289 $entry['author-uri'] = $from->homepage;
291 $entry['avatar'] = $from->avatarUrl(AVATAR_STREAM_SIZE);
293 $avatar = $from->getAvatar(AVATAR_STREAM_SIZE);
294 $entry['avatar-type'] = $avatar->mediatype;
295 } catch (Exception $e) {
296 $entry['avatar-type'] = 'image/png';
301 $entry['description'] = $entry['content'];
302 $entry['pubDate'] = common_date_rfc2822($message->created);
303 $entry['guid'] = $entry['link'];
308 // END import from lib/apiaction.php
311 * Shows a list of direct messages as Twitter-style XML array
315 function showXmlDirectMessages()
317 $this->initDocument('xml');
318 $this->elementStart('direct-messages', array('type' => 'array',
319 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
321 foreach ($this->messages as $m) {
322 $dm_array = $this->directMessageArray($m);
323 $this->showXmlDirectMessage($dm_array);
326 $this->elementEnd('direct-messages');
327 $this->endDocument('xml');
331 * Shows a list of direct messages as a JSON encoded array
335 function showJsonDirectMessages()
337 $this->initDocument('json');
341 foreach ($this->messages as $m) {
342 $dm_array = $this->directMessageArray($m);
343 array_push($dmsgs, $dm_array);
346 $this->showJsonObjects($dmsgs);
347 $this->endDocument('json');
351 * Shows a list of direct messages as RSS items
355 function showRssDirectMessages()
357 $this->initDocument('rss');
359 $this->element('title', null, $this->title);
361 $this->element('link', null, $this->link);
362 $this->element('description', null, $this->subtitle);
363 $this->element('language', null, 'en-us');
368 'type' => 'application/rss+xml',
369 'href' => $this->selfuri_base . '.rss',
374 $this->element('ttl', null, '40');
376 foreach ($this->messages as $m) {
377 $entry = $this->rssDirectMessageArray($m);
378 $this->showTwitterRssItem($entry);
381 $this->endTwitterRss();
385 * Shows a list of direct messages as Atom entries
389 function showAtomDirectMessages()
391 $this->initDocument('atom');
393 $this->element('title', null, $this->title);
394 $this->element('id', null, $this->id);
396 $selfuri = common_root_url() . 'api/direct_messages.atom';
400 'href' => $this->link,
401 'rel' => 'alternate',
402 'type' => 'text/html'),
407 'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
408 'type' => 'application/atom+xml'),
411 $this->element('updated', null, common_date_iso8601('now'));
412 $this->element('subtitle', null, $this->subtitle);
414 foreach ($this->messages as $m) {
415 $entry = $this->rssDirectMessageArray($m);
416 $this->showTwitterAtomEntry($entry);
419 $this->endDocument('atom');
423 * An entity tag for this notice
425 * Returns an Etag based on the action name, language, and
426 * timestamps of the notice
428 * @return string etag
432 if (!empty($this->messages)) {
434 $last = count($this->messages) - 1;
436 return '"' . implode(
438 array($this->arg('action'),
439 common_user_cache_hash($this->auth_user),
441 strtotime($this->messages[0]->created),
442 strtotime($this->messages[$last]->created)