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 <<<<<<< .merge_file_m7N4pd
64 protected function prepare(array $args=array())
66 function prepare(array $args=array())
67 >>>>>>> .merge_file_lMcE3c
69 parent::prepare($args);
71 if (!$this->scoped instanceof Profile) {
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"),
86 $this->scoped->getNickname()
88 $this->subtitle = sprintf(
89 // TRANS: Subtitle. %s is a user nickname.
90 _("All the direct messages sent from %s"),
91 $this->scoped->getNickname()
93 $this->link = $server . $this->scoped->getNickname() . '/outbox';
94 $this->selfuri_base = common_root_url() . 'api/direct_messages/sent';
95 $this->id = "tag:$taguribase:SentDirectMessages:" . $this->scoped->getID();
97 $this->title = sprintf(
98 // TRANS: Title. %s is a user nickname.
99 _("Direct messages to %s"),
100 $this->scoped->getNickname()
102 $this->subtitle = sprintf(
103 // TRANS: Subtitle. %s is a user nickname.
104 _("All the direct messages sent to %s"),
105 $this->scoped->getNickname()
107 $this->link = $server . $this->scoped->getNickname() . '/inbox';
108 $this->selfuri_base = common_root_url() . 'api/direct_messages';
109 $this->id = "tag:$taguribase:DirectMessages:" . $this->scoped->getID();
112 $this->messages = $this->getMessages();
117 <<<<<<< .merge_file_m7N4pd
118 protected function handle()
125 * @param array $args $_REQUEST data (unused)
129 function handle(array $args=array())
130 >>>>>>> .merge_file_lMcE3c
133 $this->showMessages();
141 function showMessages()
143 switch($this->format) {
145 $this->showXmlDirectMessages();
148 $this->showRssDirectMessages();
151 $this->showAtomDirectMessages();
154 $this->showJsonDirectMessages();
157 // TRANS: Client error displayed when coming across a non-supported API method.
158 $this->clientError(_('API method not found.'), $code = 404);
166 * @return array notices
168 function getMessages()
170 $message = new Message();
172 if ($this->arg('sent')) {
173 $message->from_profile = $this->scoped->getID();
175 $message->to_profile = $this->scoped->getID();
178 if (!empty($this->max_id)) {
179 $message->whereAdd('id <= ' . $this->max_id);
182 if (!empty($this->since_id)) {
183 $message->whereAdd('id > ' . $this->since_id);
186 $message->orderBy('created DESC, id DESC');
187 $message->limit((($this->page - 1) * $this->count), $this->count);
192 while ($message->fetch()) {
193 $messages[] = clone($message);
200 * Is this action read only?
202 * @param array $args other arguments
204 * @return boolean true
206 function isReadOnly(array $args=array())
212 * When was this notice last modified?
214 * @return string datestamp of the latest notice in the stream
216 function lastModified()
218 if (!empty($this->messages)) {
219 return strtotime($this->messages[0]->created);
225 // BEGIN import from lib/apiaction.php
227 function showSingleXmlDirectMessage($message)
229 $this->initDocument('xml');
230 $dmsg = $this->directMessageArray($message);
231 $this->showXmlDirectMessage($dmsg, true);
232 $this->endDocument('xml');
235 function showSingleJsonDirectMessage($message)
237 $this->initDocument('json');
238 $dmsg = $this->directMessageArray($message);
239 $this->showJsonObjects($dmsg);
240 $this->endDocument('json');
243 function showXmlDirectMessage($dm, $namespaces=false)
247 $attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
249 $this->elementStart('direct_message', $attrs);
250 foreach($dm as $element => $value) {
254 $this->showTwitterXmlUser($value, $element);
257 $this->element($element, null, common_xml_safe_str($value));
260 $this->element($element, null, $value);
264 $this->elementEnd('direct_message');
267 function directMessageArray($message)
271 $from_profile = $message->getFrom();
272 $to_profile = $message->getTo();
274 $dmsg['id'] = intval($message->id);
275 $dmsg['sender_id'] = intval($from_profile->id);
276 $dmsg['text'] = trim($message->content);
277 $dmsg['recipient_id'] = intval($to_profile->id);
278 $dmsg['created_at'] = $this->dateTwitter($message->created);
279 $dmsg['sender_screen_name'] = $from_profile->nickname;
280 $dmsg['recipient_screen_name'] = $to_profile->nickname;
281 $dmsg['sender'] = $this->twitterUserArray($from_profile, false);
282 $dmsg['recipient'] = $this->twitterUserArray($to_profile, false);
287 function rssDirectMessageArray($message)
291 $from = $message->getFrom();
293 $entry['title'] = sprintf('Message from %1$s to %2$s',
294 $from->nickname, $message->getTo()->nickname);
296 $entry['content'] = common_xml_safe_str($message->rendered);
297 $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
298 $entry['published'] = common_date_iso8601($message->created);
300 $taguribase = TagURI::base();
302 $entry['id'] = "tag:$taguribase:$entry[link]";
303 $entry['updated'] = $entry['published'];
305 $entry['author-name'] = $from->getBestName();
306 $entry['author-uri'] = $from->homepage;
308 $entry['avatar'] = $from->avatarUrl(AVATAR_STREAM_SIZE);
310 $avatar = $from->getAvatar(AVATAR_STREAM_SIZE);
311 $entry['avatar-type'] = $avatar->mediatype;
312 } catch (Exception $e) {
313 $entry['avatar-type'] = 'image/png';
318 $entry['description'] = $entry['content'];
319 $entry['pubDate'] = common_date_rfc2822($message->created);
320 $entry['guid'] = $entry['link'];
325 // END import from lib/apiaction.php
328 * Shows a list of direct messages as Twitter-style XML array
332 function showXmlDirectMessages()
334 $this->initDocument('xml');
335 $this->elementStart('direct-messages', array('type' => 'array',
336 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
338 foreach ($this->messages as $m) {
339 $dm_array = $this->directMessageArray($m);
340 $this->showXmlDirectMessage($dm_array);
343 $this->elementEnd('direct-messages');
344 $this->endDocument('xml');
348 * Shows a list of direct messages as a JSON encoded array
352 function showJsonDirectMessages()
354 $this->initDocument('json');
358 foreach ($this->messages as $m) {
359 $dm_array = $this->directMessageArray($m);
360 array_push($dmsgs, $dm_array);
363 $this->showJsonObjects($dmsgs);
364 $this->endDocument('json');
368 * Shows a list of direct messages as RSS items
372 function showRssDirectMessages()
374 $this->initDocument('rss');
376 $this->element('title', null, $this->title);
378 $this->element('link', null, $this->link);
379 $this->element('description', null, $this->subtitle);
380 $this->element('language', null, 'en-us');
385 'type' => 'application/rss+xml',
386 'href' => $this->selfuri_base . '.rss',
391 $this->element('ttl', null, '40');
393 foreach ($this->messages as $m) {
394 $entry = $this->rssDirectMessageArray($m);
395 $this->showTwitterRssItem($entry);
398 $this->endTwitterRss();
402 * Shows a list of direct messages as Atom entries
406 function showAtomDirectMessages()
408 $this->initDocument('atom');
410 $this->element('title', null, $this->title);
411 $this->element('id', null, $this->id);
413 $selfuri = common_root_url() . 'api/direct_messages.atom';
417 'href' => $this->link,
418 'rel' => 'alternate',
419 'type' => 'text/html'),
424 'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
425 'type' => 'application/atom+xml'),
428 $this->element('updated', null, common_date_iso8601('now'));
429 $this->element('subtitle', null, $this->subtitle);
431 foreach ($this->messages as $m) {
432 $entry = $this->rssDirectMessageArray($m);
433 $this->showTwitterAtomEntry($entry);
436 $this->endDocument('atom');
440 * An entity tag for this notice
442 * Returns an Etag based on the action name, language, and
443 * timestamps of the notice
445 * @return string etag
449 if (!empty($this->messages)) {
451 $last = count($this->messages) - 1;
453 return '"' . implode(
455 array($this->arg('action'),
456 common_user_cache_hash($this->auth_user),
458 strtotime($this->messages[0]->created),
459 strtotime($this->messages[$last]->created)