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();
118 * @param array $args $_REQUEST data (unused)
122 function handle($args)
124 parent::handle($args);
125 $this->showMessages();
133 function showMessages()
135 switch($this->format) {
137 $this->showXmlDirectMessages();
140 $this->showRssDirectMessages();
143 $this->showAtomDirectMessages();
146 $this->showJsonDirectMessages();
149 // TRANS: Client error displayed when coming across a non-supported API method.
150 $this->clientError(_('API method not found.'), $code = 404);
158 * @return array notices
160 function getMessages()
162 $message = new Message();
164 if ($this->arg('sent')) {
165 $message->from_profile = $this->scoped->getID();
167 $message->to_profile = $this->scoped->getID();
170 if (!empty($this->max_id)) {
171 $message->whereAdd('id <= ' . $this->max_id);
174 if (!empty($this->since_id)) {
175 $message->whereAdd('id > ' . $this->since_id);
178 $message->orderBy('created DESC, id DESC');
179 $message->limit((($this->page - 1) * $this->count), $this->count);
184 while ($message->fetch()) {
185 $messages[] = clone($message);
192 * Is this action read only?
194 * @param array $args other arguments
196 * @return boolean true
198 function isReadOnly($args)
204 * When was this notice last modified?
206 * @return string datestamp of the latest notice in the stream
208 function lastModified()
210 if (!empty($this->messages)) {
211 return strtotime($this->messages[0]->created);
217 // BEGIN import from lib/apiaction.php
219 function showSingleXmlDirectMessage($message)
221 $this->initDocument('xml');
222 $dmsg = $this->directMessageArray($message);
223 $this->showXmlDirectMessage($dmsg, true);
224 $this->endDocument('xml');
227 function showSingleJsonDirectMessage($message)
229 $this->initDocument('json');
230 $dmsg = $this->directMessageArray($message);
231 $this->showJsonObjects($dmsg);
232 $this->endDocument('json');
235 function showXmlDirectMessage($dm, $namespaces=false)
239 $attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
241 $this->elementStart('direct_message', $attrs);
242 foreach($dm as $element => $value) {
246 $this->showTwitterXmlUser($value, $element);
249 $this->element($element, null, common_xml_safe_str($value));
252 $this->element($element, null, $value);
256 $this->elementEnd('direct_message');
259 function directMessageArray($message)
263 $from_profile = $message->getFrom();
264 $to_profile = $message->getTo();
266 $dmsg['id'] = intval($message->id);
267 $dmsg['sender_id'] = intval($from_profile->id);
268 $dmsg['text'] = trim($message->content);
269 $dmsg['recipient_id'] = intval($to_profile->id);
270 $dmsg['created_at'] = $this->dateTwitter($message->created);
271 $dmsg['sender_screen_name'] = $from_profile->nickname;
272 $dmsg['recipient_screen_name'] = $to_profile->nickname;
273 $dmsg['sender'] = $this->twitterUserArray($from_profile, false);
274 $dmsg['recipient'] = $this->twitterUserArray($to_profile, false);
279 function rssDirectMessageArray($message)
283 $from = $message->getFrom();
285 $entry['title'] = sprintf('Message from %1$s to %2$s',
286 $from->nickname, $message->getTo()->nickname);
288 $entry['content'] = common_xml_safe_str($message->rendered);
289 $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
290 $entry['published'] = common_date_iso8601($message->created);
292 $taguribase = TagURI::base();
294 $entry['id'] = "tag:$taguribase:$entry[link]";
295 $entry['updated'] = $entry['published'];
297 $entry['author-name'] = $from->getBestName();
298 $entry['author-uri'] = $from->homepage;
300 $entry['avatar'] = $from->avatarUrl(AVATAR_STREAM_SIZE);
302 $avatar = $from->getAvatar(AVATAR_STREAM_SIZE);
303 $entry['avatar-type'] = $avatar->mediatype;
304 } catch (Exception $e) {
305 $entry['avatar-type'] = 'image/png';
310 $entry['description'] = $entry['content'];
311 $entry['pubDate'] = common_date_rfc2822($message->created);
312 $entry['guid'] = $entry['link'];
317 // END import from lib/apiaction.php
320 * Shows a list of direct messages as Twitter-style XML array
324 function showXmlDirectMessages()
326 $this->initDocument('xml');
327 $this->elementStart('direct-messages', array('type' => 'array',
328 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
330 foreach ($this->messages as $m) {
331 $dm_array = $this->directMessageArray($m);
332 $this->showXmlDirectMessage($dm_array);
335 $this->elementEnd('direct-messages');
336 $this->endDocument('xml');
340 * Shows a list of direct messages as a JSON encoded array
344 function showJsonDirectMessages()
346 $this->initDocument('json');
350 foreach ($this->messages as $m) {
351 $dm_array = $this->directMessageArray($m);
352 array_push($dmsgs, $dm_array);
355 $this->showJsonObjects($dmsgs);
356 $this->endDocument('json');
360 * Shows a list of direct messages as RSS items
364 function showRssDirectMessages()
366 $this->initDocument('rss');
368 $this->element('title', null, $this->title);
370 $this->element('link', null, $this->link);
371 $this->element('description', null, $this->subtitle);
372 $this->element('language', null, 'en-us');
377 'type' => 'application/rss+xml',
378 'href' => $this->selfuri_base . '.rss',
383 $this->element('ttl', null, '40');
385 foreach ($this->messages as $m) {
386 $entry = $this->rssDirectMessageArray($m);
387 $this->showTwitterRssItem($entry);
390 $this->endTwitterRss();
394 * Shows a list of direct messages as Atom entries
398 function showAtomDirectMessages()
400 $this->initDocument('atom');
402 $this->element('title', null, $this->title);
403 $this->element('id', null, $this->id);
405 $selfuri = common_root_url() . 'api/direct_messages.atom';
409 'href' => $this->link,
410 'rel' => 'alternate',
411 'type' => 'text/html'),
416 'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
417 'type' => 'application/atom+xml'),
420 $this->element('updated', null, common_date_iso8601('now'));
421 $this->element('subtitle', null, $this->subtitle);
423 foreach ($this->messages as $m) {
424 $entry = $this->rssDirectMessageArray($m);
425 $this->showTwitterAtomEntry($entry);
428 $this->endDocument('atom');
432 * An entity tag for this notice
434 * Returns an Etag based on the action name, language, and
435 * timestamps of the notice
437 * @return string etag
441 if (!empty($this->messages)) {
443 $last = count($this->messages) - 1;
445 return '"' . implode(
447 array($this->arg('action'),
448 common_user_cache_hash($this->auth_user),
450 strtotime($this->messages[0]->created),
451 strtotime($this->messages[$last]->created)