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($args)
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, $this->format);
77 $server = common_root_url();
78 $taguribase = TagURI::base();
80 if ($this->arg('sent')) {
82 // Action was called by /api/direct_messages/sent.format
84 $this->title = sprintf(
85 // TRANS: Title. %s is a user nickname.
86 _("Direct messages from %s"),
89 $this->subtitle = sprintf(
90 // TRANS: Subtitle. %s is a user nickname.
91 _("All the direct messages sent from %s"),
94 $this->link = $server . $this->user->nickname . '/outbox';
95 $this->selfuri_base = common_root_url() . 'api/direct_messages/sent';
96 $this->id = "tag:$taguribase:SentDirectMessages:" . $this->user->id;
98 $this->title = sprintf(
99 // TRANS: Title. %s is a user nickname.
100 _("Direct messages to %s"),
101 $this->user->nickname
103 $this->subtitle = sprintf(
104 // TRANS: Subtitle. %s is a user nickname.
105 _("All the direct messages sent to %s"),
106 $this->user->nickname
108 $this->link = $server . $this->user->nickname . '/inbox';
109 $this->selfuri_base = common_root_url() . 'api/direct_messages';
110 $this->id = "tag:$taguribase:DirectMessages:" . $this->user->id;
113 $this->messages = $this->getMessages();
123 * @param array $args $_REQUEST data (unused)
127 function handle($args)
129 parent::handle($args);
130 $this->showMessages();
138 function showMessages()
140 switch($this->format) {
142 $this->showXmlDirectMessages();
145 $this->showRssDirectMessages();
148 $this->showAtomDirectMessages();
151 $this->showJsonDirectMessages();
154 // TRANS: Client error displayed when coming across a non-supported API method.
155 $this->clientError(_('API method not found.'), $code = 404);
163 * @return array notices
165 function getMessages()
167 $message = new Message();
169 if ($this->arg('sent')) {
170 $message->from_profile = $this->user->id;
172 $message->to_profile = $this->user->id;
175 if (!empty($this->max_id)) {
176 $message->whereAdd('id <= ' . $this->max_id);
179 if (!empty($this->since_id)) {
180 $message->whereAdd('id > ' . $this->since_id);
183 $message->orderBy('created DESC, id DESC');
184 $message->limit((($this->page - 1) * $this->count), $this->count);
189 while ($message->fetch()) {
190 $messages[] = clone($message);
197 * Is this action read only?
199 * @param array $args other arguments
201 * @return boolean true
203 function isReadOnly($args)
209 * When was this notice last modified?
211 * @return string datestamp of the latest notice in the stream
213 function lastModified()
215 if (!empty($this->messages)) {
216 return strtotime($this->messages[0]->created);
223 * Shows a list of direct messages as Twitter-style XML array
227 function showXmlDirectMessages()
229 $this->initDocument('xml');
230 $this->elementStart('direct-messages', array('type' => 'array',
231 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
233 foreach ($this->messages as $m) {
234 $dm_array = $this->directMessageArray($m);
235 $this->showXmlDirectMessage($dm_array);
238 $this->elementEnd('direct-messages');
239 $this->endDocument('xml');
243 * Shows a list of direct messages as a JSON encoded array
247 function showJsonDirectMessages()
249 $this->initDocument('json');
253 foreach ($this->messages as $m) {
254 $dm_array = $this->directMessageArray($m);
255 array_push($dmsgs, $dm_array);
258 $this->showJsonObjects($dmsgs);
259 $this->endDocument('json');
263 * Shows a list of direct messages as RSS items
267 function showRssDirectMessages()
269 $this->initDocument('rss');
271 $this->element('title', null, $this->title);
273 $this->element('link', null, $this->link);
274 $this->element('description', null, $this->subtitle);
275 $this->element('language', null, 'en-us');
280 'type' => 'application/rss+xml',
281 'href' => $this->selfuri_base . '.rss',
286 $this->element('ttl', null, '40');
288 foreach ($this->messages as $m) {
289 $entry = $this->rssDirectMessageArray($m);
290 $this->showTwitterRssItem($entry);
293 $this->endTwitterRss();
297 * Shows a list of direct messages as Atom entries
301 function showAtomDirectMessages()
303 $this->initDocument('atom');
305 $this->element('title', null, $this->title);
306 $this->element('id', null, $this->id);
308 $selfuri = common_root_url() . 'api/direct_messages.atom';
312 'href' => $this->link,
313 'rel' => 'alternate',
314 'type' => 'text/html'),
319 'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
320 'type' => 'application/atom+xml'),
323 $this->element('updated', null, common_date_iso8601('now'));
324 $this->element('subtitle', null, $this->subtitle);
326 foreach ($this->messages as $m) {
327 $entry = $this->rssDirectMessageArray($m);
328 $this->showTwitterAtomEntry($entry);
331 $this->endDocument('atom');
335 * An entity tag for this notice
337 * Returns an Etag based on the action name, language, and
338 * timestamps of the notice
340 * @return string etag
344 if (!empty($this->messages)) {
346 $last = count($this->messages) - 1;
348 return '"' . implode(
350 array($this->arg('action'),
351 common_user_cache_hash($this->auth_user),
353 strtotime($this->messages[0]->created),
354 strtotime($this->messages[$last]->created)