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);
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"),
88 $this->subtitle = sprintf(
89 // TRANS: Subtitle. %s is a user nickname.
90 _("All the direct messages sent from %s"),
93 $this->link = $server . $this->user->nickname . '/outbox';
94 $this->selfuri_base = common_root_url() . 'api/direct_messages/sent';
95 $this->id = "tag:$taguribase:SentDirectMessages:" . $this->user->id;
97 $this->title = sprintf(
98 // TRANS: Title. %s is a user nickname.
99 _("Direct messages to %s"),
100 $this->user->nickname
102 $this->subtitle = sprintf(
103 // TRANS: Subtitle. %s is a user nickname.
104 _("All the direct messages sent to %s"),
105 $this->user->nickname
107 $this->link = $server . $this->user->nickname . '/inbox';
108 $this->selfuri_base = common_root_url() . 'api/direct_messages';
109 $this->id = "tag:$taguribase:DirectMessages:" . $this->user->id;
112 $this->messages = $this->getMessages();
122 * @param array $args $_REQUEST data (unused)
126 function handle($args)
128 parent::handle($args);
129 $this->showMessages();
137 function showMessages()
139 switch($this->format) {
141 $this->showXmlDirectMessages();
144 $this->showRssDirectMessages();
147 $this->showAtomDirectMessages();
150 $this->showJsonDirectMessages();
153 // TRANS: Client error displayed when coming across a non-supported API method.
154 $this->clientError(_('API method not found.'), $code = 404);
162 * @return array notices
164 function getMessages()
166 $message = new Message();
168 if ($this->arg('sent')) {
169 $message->from_profile = $this->user->id;
171 $message->to_profile = $this->user->id;
174 if (!empty($this->max_id)) {
175 $message->whereAdd('id <= ' . $this->max_id);
178 if (!empty($this->since_id)) {
179 $message->whereAdd('id > ' . $this->since_id);
182 $message->orderBy('created DESC, id DESC');
183 $message->limit((($this->page - 1) * $this->count), $this->count);
188 while ($message->fetch()) {
189 $messages[] = clone($message);
196 * Is this action read only?
198 * @param array $args other arguments
200 * @return boolean true
202 function isReadOnly($args)
208 * When was this notice last modified?
210 * @return string datestamp of the latest notice in the stream
212 function lastModified()
214 if (!empty($this->messages)) {
215 return strtotime($this->messages[0]->created);
222 * Shows a list of direct messages as Twitter-style XML array
226 function showXmlDirectMessages()
228 $this->initDocument('xml');
229 $this->elementStart('direct-messages', array('type' => 'array',
230 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
232 foreach ($this->messages as $m) {
233 $dm_array = $this->directMessageArray($m);
234 $this->showXmlDirectMessage($dm_array);
237 $this->elementEnd('direct-messages');
238 $this->endDocument('xml');
242 * Shows a list of direct messages as a JSON encoded array
246 function showJsonDirectMessages()
248 $this->initDocument('json');
252 foreach ($this->messages as $m) {
253 $dm_array = $this->directMessageArray($m);
254 array_push($dmsgs, $dm_array);
257 $this->showJsonObjects($dmsgs);
258 $this->endDocument('json');
262 * Shows a list of direct messages as RSS items
266 function showRssDirectMessages()
268 $this->initDocument('rss');
270 $this->element('title', null, $this->title);
272 $this->element('link', null, $this->link);
273 $this->element('description', null, $this->subtitle);
274 $this->element('language', null, 'en-us');
279 'type' => 'application/rss+xml',
280 'href' => $this->selfuri_base . '.rss',
285 $this->element('ttl', null, '40');
287 foreach ($this->messages as $m) {
288 $entry = $this->rssDirectMessageArray($m);
289 $this->showTwitterRssItem($entry);
292 $this->endTwitterRss();
296 * Shows a list of direct messages as Atom entries
300 function showAtomDirectMessages()
302 $this->initDocument('atom');
304 $this->element('title', null, $this->title);
305 $this->element('id', null, $this->id);
307 $selfuri = common_root_url() . 'api/direct_messages.atom';
311 'href' => $this->link,
312 'rel' => 'alternate',
313 'type' => 'text/html'),
318 'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
319 'type' => 'application/atom+xml'),
322 $this->element('updated', null, common_date_iso8601('now'));
323 $this->element('subtitle', null, $this->subtitle);
325 foreach ($this->messages as $m) {
326 $entry = $this->rssDirectMessageArray($m);
327 $this->showTwitterAtomEntry($entry);
330 $this->endDocument('atom');
334 * An entity tag for this notice
336 * Returns an Etag based on the action name, language, and
337 * timestamps of the notice
339 * @return string etag
343 if (!empty($this->messages)) {
345 $last = count($this->messages) - 1;
347 return '"' . implode(
349 array($this->arg('action'),
350 common_user_cache_hash($this->auth_user),
352 strtotime($this->messages[0]->created),
353 strtotime($this->messages[$last]->created)