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 $this->clientError(_('No such user.'), 404, $this->format);
81 $server = common_root_url();
82 $taguribase = TagURI::base();
84 if ($this->arg('sent')) {
86 // Action was called by /api/direct_messages/sent.format
88 $this->title = sprintf(
89 _("Direct messages from %s"),
92 $this->subtitle = sprintf(
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 _("Direct messages to %s"),
102 $this->user->nickname
104 $this->subtitle = sprintf(
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)
128 function handle($args)
130 parent::handle($args);
131 $this->showMessages();
140 function showMessages()
142 switch($this->format) {
144 $this->showXmlDirectMessages();
147 $this->showRssDirectMessages();
150 $this->showAtomDirectMessages();
153 $this->showJsonDirectMessages();
156 $this->clientError(_('API method not found.'), $code = 404);
164 * @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
206 function isReadOnly($args)
212 * When was this notice last modified?
214 * @return string datestamp of the latest notice in the stream
217 function lastModified()
219 if (!empty($this->messages)) {
220 return strtotime($this->messages[0]->created);
227 * Shows a list of direct messages as Twitter-style XML array
232 function showXmlDirectMessages()
234 $this->initDocument('xml');
235 $this->elementStart('direct-messages', array('type' => 'array',
236 'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
238 foreach ($this->messages as $m) {
239 $dm_array = $this->directMessageArray($m);
240 $this->showXmlDirectMessage($dm_array);
243 $this->elementEnd('direct-messages');
244 $this->endDocument('xml');
248 * Shows a list of direct messages as a JSON encoded array
253 function showJsonDirectMessages()
255 $this->initDocument('json');
259 foreach ($this->messages as $m) {
260 $dm_array = $this->directMessageArray($m);
261 array_push($dmsgs, $dm_array);
264 $this->showJsonObjects($dmsgs);
265 $this->endDocument('json');
269 * Shows a list of direct messages as RSS items
274 function showRssDirectMessages()
276 $this->initDocument('rss');
278 $this->element('title', null, $this->title);
280 $this->element('link', null, $this->link);
281 $this->element('description', null, $this->subtitle);
282 $this->element('language', null, 'en-us');
287 'type' => 'application/rss+xml',
288 'href' => $this->selfuri_base . '.rss',
293 $this->element('ttl', null, '40');
295 foreach ($this->messages as $m) {
296 $entry = $this->rssDirectMessageArray($m);
297 $this->showTwitterRssItem($entry);
300 $this->endTwitterRss();
304 * Shows a list of direct messages as Atom entries
309 function showAtomDirectMessages()
311 $this->initDocument('atom');
313 $this->element('title', null, $this->title);
314 $this->element('id', null, $this->id);
316 $selfuri = common_root_url() . 'api/direct_messages.atom';
320 'href' => $this->link,
321 'rel' => 'alternate',
322 'type' => 'text/html'),
327 'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
328 'type' => 'application/atom+xml'),
331 $this->element('updated', null, common_date_iso8601('now'));
332 $this->element('subtitle', null, $this->subtitle);
334 foreach ($this->messages as $m) {
335 $entry = $this->rssDirectMessageArray($m);
336 $this->showTwitterAtomEntry($entry);
339 $this->endDocument('atom');
343 * An entity tag for this notice
345 * Returns an Etag based on the action name, language, and
346 * timestamps of the notice
348 * @return string etag
353 if (!empty($this->messages)) {
355 $last = count($this->messages) - 1;
357 return '"' . implode(
359 array($this->arg('action'),
361 strtotime($this->messages[0]->created),
362 strtotime($this->messages[$last]->created)