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 Zach Copley <zach@status.net>
25 * @copyright 2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET')) {
34 require_once INSTALLDIR.'/lib/apiauth.php';
37 * Show a list of direct messages from or to the authenticating user
41 * @author Zach Copley <zach@status.net>
42 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43 * @link http://status.net/
46 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
69 function prepare($args)
71 parent::prepare($args);
73 $this->user = $this->auth_user;
75 if (empty($this->user)) {
76 $this->clientError(_('No such user!'), 404, $this->format);
80 $this->page = (int)$this->arg('page', 1);
81 $this->count = (int)$this->arg('count', 20);
82 $this->max_id = (int)$this->arg('max_id', 0);
83 $this->since_id = (int)$this->arg('since_id', 0);
84 $this->since = $this->arg('since');
86 $server = common_root_url();
87 $taguribase = common_config('integration', 'taguri');
89 if ($this->arg('sent')) {
91 // Action was called by /api/direct_messages/sent.format
93 $this->title = sprintf(
94 _("Direct messages from %s"),
97 $this->subtitle = sprintf(
98 _("All the direct messages sent from %s"),
101 $this->link = $server . $this->user->nickname . '/outbox';
102 $this->selfuri_base = common_root_url() . 'api/direct_messages/sent';
103 $this->id = "tag:$taguribase:SentDirectMessages:" . $this->user->id;
105 $this->title = sprintf(
106 _("Direct messages to %s"),
107 $this->user->nickname
109 $this->subtitle = sprintf(
110 _("All the direct messages sent to %s"),
111 $this->user->nickname
113 $this->link = $server . $this->user->nickname . '/inbox';
114 $this->selfuri_base = common_root_url() . 'api/direct_messages';
115 $this->id = "tag:$taguribase:DirectMessages:" . $this->user->id;
118 $this->messages = $this->getMessages();
128 * @param array $args $_REQUEST data (unused)
133 function handle($args)
135 parent::handle($args);
136 $this->showMessages();
145 function showMessages()
147 switch($this->format) {
149 $this->showXmlDirectMessages();
152 $this->showRssDirectMessages();
155 $this->showAtomDirectMessages();
158 $this->showJsonDirectMessages();
161 $this->clientError(_('API method not found!'), $code = 404);
169 * @return array notices
172 function getMessages()
174 $message = new Message();
176 if ($this->arg('sent')) {
177 $message->from_profile = $this->user->id;
179 $message->to_profile = $this->user->id;
182 if (!empty($this->max_id)) {
183 $message->whereAdd('id <= ' . $this->max_id);
186 if (!empty($this->since_id)) {
187 $message->whereAdd('id > ' . $this->since_id);
190 if (!empty($since)) {
191 $d = date('Y-m-d H:i:s', $this->since);
192 $message->whereAdd("created > '$d'");
195 $message->orderBy('created DESC, id DESC');
196 $message->limit((($this->page - 1) * $this->count), $this->count);
201 while ($message->fetch()) {
202 $messages[] = clone($message);
209 * Is this action read only?
211 * @param array $args other arguments
213 * @return boolean true
216 function isReadOnly($args)
222 * When was this notice last modified?
224 * @return string datestamp of the latest notice in the stream
227 function lastModified()
229 if (!empty($this->messages)) {
230 return strtotime($this->messages[0]->created);
237 * Shows a list of direct messages as Twitter-style XML array
242 function showXmlDirectMessages()
244 $this->init_document('xml');
245 $this->elementStart('direct-messages', array('type' => 'array'));
247 foreach ($this->messages as $m) {
248 $dm_array = $this->directMessageArray($m);
249 $this->showXmlDirectMessage($dm_array);
252 $this->elementEnd('direct-messages');
253 $this->end_document('xml');
257 * Shows a list of direct messages as a JSON encoded array
262 function showJsonDirectMessages()
264 $this->init_document('json');
268 foreach ($this->messages as $m) {
269 $dm_array = $this->directMessageArray($m);
270 array_push($dmsgs, $dm_array);
273 $this->show_json_objects($dmsgs);
274 $this->end_document('json');
278 * Shows a list of direct messages as RSS items
283 function showRssDirectMessages()
285 $this->init_document('rss');
287 $this->element('title', null, $this->title);
289 $this->element('link', null, $this->link);
290 $this->element('description', null, $this->subtitle);
291 $this->element('language', null, 'en-us');
296 'type' => 'application/rss+xml',
297 'href' => $this->selfuri_base . '.rss',
302 $this->element('ttl', null, '40');
304 foreach ($this->messages as $m) {
305 $entry = $this->rssDirectMessageArray($m);
306 $this->show_twitter_rss_item($entry);
309 $this->end_twitter_rss();
313 * Shows a list of direct messages as Atom entries
318 function showAtomDirectMessages()
320 $this->init_document('atom');
322 $this->element('title', null, $this->title);
323 $this->element('id', null, $this->id);
325 $selfuri = common_root_url() . 'api/direct_messages.atom';
329 'href' => $this->link,
330 'rel' => 'alternate',
331 'type' => 'text/html'),
336 'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
337 'type' => 'application/atom+xml'),
340 $this->element('updated', null, common_date_iso8601('now'));
341 $this->element('subtitle', null, $this->subtitle);
343 foreach ($this->messages as $m) {
344 $entry = $this->rssDirectMessageArray($m);
345 $this->showTwitterAtomEntry($entry);
348 $this->end_document('atom');
352 * An entity tag for this notice
354 * Returns an Etag based on the action name, language, and
355 * timestamps of the notice
357 * @return string etag
362 if (!empty($this->messages)) {
364 $last = count($this->messages) - 1;
366 return '"' . implode(
368 array($this->arg('action'),
370 strtotime($this->messages[0]->created),
371 strtotime($this->messages[$last]->created)