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')) {
37 require_once INSTALLDIR . '/lib/apiauth.php';
40 * Show a list of direct messages from or to the authenticating user
44 * @author Adrian Lang <mail@adrianlang.de>
45 * @author Evan Prodromou <evan@status.net>
46 * @author Robin Millette <robin@millette.info>
47 * @author Zach Copley <zach@status.net>
48 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
49 * @link http://status.net/
52 class ApiDirectMessageAction extends ApiAuthAction
58 var $selfuri_base = null;
62 * Take arguments for running
64 * @param array $args $_REQUEST args
66 * @return boolean success flag
70 function prepare($args)
72 parent::prepare($args);
74 $this->user = $this->auth_user;
76 if (empty($this->user)) {
77 // TRANS: Client error given when a user was not found (404).
78 $this->clientError(_('No such user.'), 404, $this->format);
82 $server = common_root_url();
83 $taguribase = TagURI::base();
85 if ($this->arg('sent')) {
87 // Action was called by /api/direct_messages/sent.format
89 $this->title = sprintf(
90 // TRANS: %s is a user nickname.
91 _("Direct messages from %s"),
94 $this->subtitle = sprintf(
95 // TRANS: %s is a user nickname.
96 _("All the direct messages sent from %s"),
99 $this->link = $server . $this->user->nickname . '/outbox';
100 $this->selfuri_base = common_root_url() . 'api/direct_messages/sent';
101 $this->id = "tag:$taguribase:SentDirectMessages:" . $this->user->id;
103 $this->title = sprintf(
104 // TRANS: %s is a user nickname.
105 _("Direct messages to %s"),
106 $this->user->nickname
108 $this->subtitle = sprintf(
109 // TRANS: %s is a user nickname.
110 _("All the direct messages sent to %s"),
111 $this->user->nickname
113 $this->link = $server . $this->user->nickname . '/inbox';
114 $this->selfuri_base = common_root_url() . 'api/direct_messages';
115 $this->id = "tag:$taguribase:DirectMessages:" . $this->user->id;
118 $this->messages = $this->getMessages();
128 * @param array $args $_REQUEST data (unused)
133 function handle($args)
135 parent::handle($args);
136 $this->showMessages();
145 function showMessages()
147 switch($this->format) {
149 $this->showXmlDirectMessages();
152 $this->showRssDirectMessages();
155 $this->showAtomDirectMessages();
158 $this->showJsonDirectMessages();
161 // TRANS: Client error given when an API method was not found (404).
162 $this->clientError(_('API method not found.'), $code = 404);
170 * @return array notices
173 function getMessages()
175 $message = new Message();
177 if ($this->arg('sent')) {
178 $message->from_profile = $this->user->id;
180 $message->to_profile = $this->user->id;
183 if (!empty($this->max_id)) {
184 $message->whereAdd('id <= ' . $this->max_id);
187 if (!empty($this->since_id)) {
188 $message->whereAdd('id > ' . $this->since_id);
191 $message->orderBy('created DESC, id DESC');
192 $message->limit((($this->page - 1) * $this->count), $this->count);
197 while ($message->fetch()) {
198 $messages[] = clone($message);
205 * Is this action read only?
207 * @param array $args other arguments
209 * @return boolean true
212 function isReadOnly($args)
218 * When was this notice last modified?
220 * @return string datestamp of the latest notice in the stream
223 function lastModified()
225 if (!empty($this->messages)) {
226 return strtotime($this->messages[0]->created);
233 * Shows a list of direct messages as Twitter-style XML array
238 function showXmlDirectMessages()
240 $this->initDocument('xml');
241 $this->elementStart('direct-messages', array('type' => 'array',
242 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
244 foreach ($this->messages as $m) {
245 $dm_array = $this->directMessageArray($m);
246 $this->showXmlDirectMessage($dm_array);
249 $this->elementEnd('direct-messages');
250 $this->endDocument('xml');
254 * Shows a list of direct messages as a JSON encoded array
259 function showJsonDirectMessages()
261 $this->initDocument('json');
265 foreach ($this->messages as $m) {
266 $dm_array = $this->directMessageArray($m);
267 array_push($dmsgs, $dm_array);
270 $this->showJsonObjects($dmsgs);
271 $this->endDocument('json');
275 * Shows a list of direct messages as RSS items
280 function showRssDirectMessages()
282 $this->initDocument('rss');
284 $this->element('title', null, $this->title);
286 $this->element('link', null, $this->link);
287 $this->element('description', null, $this->subtitle);
288 $this->element('language', null, 'en-us');
293 'type' => 'application/rss+xml',
294 'href' => $this->selfuri_base . '.rss',
299 $this->element('ttl', null, '40');
301 foreach ($this->messages as $m) {
302 $entry = $this->rssDirectMessageArray($m);
303 $this->showTwitterRssItem($entry);
306 $this->endTwitterRss();
310 * Shows a list of direct messages as Atom entries
315 function showAtomDirectMessages()
317 $this->initDocument('atom');
319 $this->element('title', null, $this->title);
320 $this->element('id', null, $this->id);
322 $selfuri = common_root_url() . 'api/direct_messages.atom';
326 'href' => $this->link,
327 'rel' => 'alternate',
328 'type' => 'text/html'),
333 'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
334 'type' => 'application/atom+xml'),
337 $this->element('updated', null, common_date_iso8601('now'));
338 $this->element('subtitle', null, $this->subtitle);
340 foreach ($this->messages as $m) {
341 $entry = $this->rssDirectMessageArray($m);
342 $this->showTwitterAtomEntry($entry);
345 $this->endDocument('atom');
349 * An entity tag for this notice
351 * Returns an Etag based on the action name, language, and
352 * timestamps of the notice
354 * @return string etag
359 if (!empty($this->messages)) {
361 $last = count($this->messages) - 1;
363 return '"' . implode(
365 array($this->arg('action'),
366 common_user_cache_hash($this->auth_user),
368 strtotime($this->messages[0]->created),
369 strtotime($this->messages[$last]->created)