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/
51 class ApiDirectMessageAction extends ApiAuthAction
57 var $selfuri_base = null;
61 * Take arguments for running
63 * @param array $args $_REQUEST args
65 * @return boolean success flag
67 function prepare($args)
69 parent::prepare($args);
71 $this->user = $this->auth_user;
73 if (empty($this->user)) {
74 // TRANS: Client error given when a user was not found (404).
75 $this->clientError(_('No such user.'), 404, $this->format);
79 $server = common_root_url();
80 $taguribase = TagURI::base();
82 if ($this->arg('sent')) {
84 // Action was called by /api/direct_messages/sent.format
86 $this->title = sprintf(
87 // TRANS: Title. %s is a user nickname.
88 _("Direct messages from %s"),
91 $this->subtitle = sprintf(
92 // TRANS: Subtitle. %s is a user nickname.
93 _("All the direct messages sent from %s"),
96 $this->link = $server . $this->user->nickname . '/outbox';
97 $this->selfuri_base = common_root_url() . 'api/direct_messages/sent';
98 $this->id = "tag:$taguribase:SentDirectMessages:" . $this->user->id;
100 $this->title = sprintf(
101 // TRANS: Title. %s is a user nickname.
102 _("Direct messages to %s"),
103 $this->user->nickname
105 $this->subtitle = sprintf(
106 // TRANS: Subtitle. %s is a user nickname.
107 _("All the direct messages sent to %s"),
108 $this->user->nickname
110 $this->link = $server . $this->user->nickname . '/inbox';
111 $this->selfuri_base = common_root_url() . 'api/direct_messages';
112 $this->id = "tag:$taguribase:DirectMessages:" . $this->user->id;
115 $this->messages = $this->getMessages();
125 * @param array $args $_REQUEST data (unused)
129 function handle($args)
131 parent::handle($args);
132 $this->showMessages();
140 function showMessages()
142 switch($this->format) {
144 $this->showXmlDirectMessages();
147 $this->showRssDirectMessages();
150 $this->showAtomDirectMessages();
153 $this->showJsonDirectMessages();
156 // TRANS: Client error displayed when coming across a non-supported API method.
157 $this->clientError(_('API method not found.'), $code = 404);
165 * @return array notices
167 function getMessages()
169 $message = new Message();
171 if ($this->arg('sent')) {
172 $message->from_profile = $this->user->id;
174 $message->to_profile = $this->user->id;
177 if (!empty($this->max_id)) {
178 $message->whereAdd('id <= ' . $this->max_id);
181 if (!empty($this->since_id)) {
182 $message->whereAdd('id > ' . $this->since_id);
185 $message->orderBy('created DESC, id DESC');
186 $message->limit((($this->page - 1) * $this->count), $this->count);
191 while ($message->fetch()) {
192 $messages[] = clone($message);
199 * Is this action read only?
201 * @param array $args other arguments
203 * @return boolean true
205 function isReadOnly($args)
211 * When was this notice last modified?
213 * @return string datestamp of the latest notice in the stream
215 function lastModified()
217 if (!empty($this->messages)) {
218 return strtotime($this->messages[0]->created);
225 * Shows a list of direct messages as Twitter-style XML array
229 function showXmlDirectMessages()
231 $this->initDocument('xml');
232 $this->elementStart('direct-messages', array('type' => 'array',
233 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
235 foreach ($this->messages as $m) {
236 $dm_array = $this->directMessageArray($m);
237 $this->showXmlDirectMessage($dm_array);
240 $this->elementEnd('direct-messages');
241 $this->endDocument('xml');
245 * Shows a list of direct messages as a JSON encoded array
249 function showJsonDirectMessages()
251 $this->initDocument('json');
255 foreach ($this->messages as $m) {
256 $dm_array = $this->directMessageArray($m);
257 array_push($dmsgs, $dm_array);
260 $this->showJsonObjects($dmsgs);
261 $this->endDocument('json');
265 * Shows a list of direct messages as RSS items
269 function showRssDirectMessages()
271 $this->initDocument('rss');
273 $this->element('title', null, $this->title);
275 $this->element('link', null, $this->link);
276 $this->element('description', null, $this->subtitle);
277 $this->element('language', null, 'en-us');
282 'type' => 'application/rss+xml',
283 'href' => $this->selfuri_base . '.rss',
288 $this->element('ttl', null, '40');
290 foreach ($this->messages as $m) {
291 $entry = $this->rssDirectMessageArray($m);
292 $this->showTwitterRssItem($entry);
295 $this->endTwitterRss();
299 * Shows a list of direct messages as Atom entries
303 function showAtomDirectMessages()
305 $this->initDocument('atom');
307 $this->element('title', null, $this->title);
308 $this->element('id', null, $this->id);
310 $selfuri = common_root_url() . 'api/direct_messages.atom';
314 'href' => $this->link,
315 'rel' => 'alternate',
316 'type' => 'text/html'),
321 'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
322 'type' => 'application/atom+xml'),
325 $this->element('updated', null, common_date_iso8601('now'));
326 $this->element('subtitle', null, $this->subtitle);
328 foreach ($this->messages as $m) {
329 $entry = $this->rssDirectMessageArray($m);
330 $this->showTwitterAtomEntry($entry);
333 $this->endDocument('atom');
337 * An entity tag for this notice
339 * Returns an Etag based on the action name, language, and
340 * timestamps of the notice
342 * @return string etag
346 if (!empty($this->messages)) {
348 $last = count($this->messages) - 1;
350 return '"' . implode(
352 array($this->arg('action'),
353 common_user_cache_hash($this->auth_user),
355 strtotime($this->messages[0]->created),
356 strtotime($this->messages[$last]->created)