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 = common_config('integration', 'taguri');
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 if (!empty($since)) {
186 $d = date('Y-m-d H:i:s', $this->since);
187 $message->whereAdd("created > '$d'");
190 $message->orderBy('created DESC, id DESC');
191 $message->limit((($this->page - 1) * $this->count), $this->count);
196 while ($message->fetch()) {
197 $messages[] = clone($message);
204 * Is this action read only?
206 * @param array $args other arguments
208 * @return boolean true
211 function isReadOnly($args)
217 * When was this notice last modified?
219 * @return string datestamp of the latest notice in the stream
222 function lastModified()
224 if (!empty($this->messages)) {
225 return strtotime($this->messages[0]->created);
232 * Shows a list of direct messages as Twitter-style XML array
237 function showXmlDirectMessages()
239 $this->initDocument('xml');
240 $this->elementStart('direct-messages', array('type' => 'array'));
242 foreach ($this->messages as $m) {
243 $dm_array = $this->directMessageArray($m);
244 $this->showXmlDirectMessage($dm_array);
247 $this->elementEnd('direct-messages');
248 $this->endDocument('xml');
252 * Shows a list of direct messages as a JSON encoded array
257 function showJsonDirectMessages()
259 $this->initDocument('json');
263 foreach ($this->messages as $m) {
264 $dm_array = $this->directMessageArray($m);
265 array_push($dmsgs, $dm_array);
268 $this->showJsonObjects($dmsgs);
269 $this->endDocument('json');
273 * Shows a list of direct messages as RSS items
278 function showRssDirectMessages()
280 $this->initDocument('rss');
282 $this->element('title', null, $this->title);
284 $this->element('link', null, $this->link);
285 $this->element('description', null, $this->subtitle);
286 $this->element('language', null, 'en-us');
291 'type' => 'application/rss+xml',
292 'href' => $this->selfuri_base . '.rss',
297 $this->element('ttl', null, '40');
299 foreach ($this->messages as $m) {
300 $entry = $this->rssDirectMessageArray($m);
301 $this->showTwitterRssItem($entry);
304 $this->endTwitterRss();
308 * Shows a list of direct messages as Atom entries
313 function showAtomDirectMessages()
315 $this->initDocument('atom');
317 $this->element('title', null, $this->title);
318 $this->element('id', null, $this->id);
320 $selfuri = common_root_url() . 'api/direct_messages.atom';
324 'href' => $this->link,
325 'rel' => 'alternate',
326 'type' => 'text/html'),
331 'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
332 'type' => 'application/atom+xml'),
335 $this->element('updated', null, common_date_iso8601('now'));
336 $this->element('subtitle', null, $this->subtitle);
338 foreach ($this->messages as $m) {
339 $entry = $this->rssDirectMessageArray($m);
340 $this->showTwitterAtomEntry($entry);
343 $this->endDocument('atom');
347 * An entity tag for this notice
349 * Returns an Etag based on the action name, language, and
350 * timestamps of the notice
352 * @return string etag
357 if (!empty($this->messages)) {
359 $last = count($this->messages) - 1;
361 return '"' . implode(
363 array($this->arg('action'),
365 strtotime($this->messages[0]->created),
366 strtotime($this->messages[$last]->created)