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
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 if ($this->requiresAuth()) {
75 if ($this->checkBasicAuthUser() == false) {
80 $this->user = $this->auth_user;
82 if (empty($this->user)) {
83 $this->clientError(_('No such user!'), 404, $this->format);
87 $this->page = (int)$this->arg('page', 1);
88 $this->count = (int)$this->arg('count', 20);
89 $this->max_id = (int)$this->arg('max_id', 0);
90 $this->since_id = (int)$this->arg('since_id', 0);
91 $this->since = $this->arg('since');
93 $server = common_root_url();
94 $taguribase = common_config('integration', 'taguri');
96 if ($this->arg('sent')) {
98 // Action was called by /api/direct_messages/sent.format
100 $this->title = sprintf(
101 _("Direct messages from %s"),
102 $this->user->nickname
104 $this->subtitle = sprintf(
105 _("All the direct messages sent from %s"),
106 $this->user->nickname
108 $this->link = $server . $this->user->nickname . '/outbox';
109 $this->selfuri_base = common_root_url() . 'api/direct_messages/sent';
110 $this->id = "tag:$taguribase:SentDirectMessages:" . $this->user->id;
112 $this->title = sprintf(
113 _("Direct messages to %s"),
114 $this->user->nickname
116 $this->subtitle = sprintf(
117 _("All the direct messages sent to %s"),
118 $this->user->nickname
120 $this->link = $server . $this->user->nickname . '/inbox';
121 $this->selfuri_base = common_root_url() . 'api/direct_messages';
122 $this->id = "tag:$taguribase:DirectMessages:" . $this->user->id;
125 $this->format = $this->arg('format');
127 $this->messages = $this->getMessages();
137 * @param array $args $_REQUEST data (unused)
142 function handle($args)
144 parent::handle($args);
145 $this->showMessages();
154 function showMessages()
156 switch($this->format) {
158 $this->showXmlDirectMessages();
161 $this->showRssDirectMessages();
164 $this->showAtomDirectMessages();
167 $this->showJsonDirectMessages();
170 $this->clientError(_('API method not found!'), $code = 404);
178 * @return array notices
181 function getMessages()
183 $message = new Message();
185 if ($this->arg('sent')) {
186 $message->from_profile = $this->user->id;
188 $message->to_profile = $this->user->id;
191 if (!empty($this->max_id)) {
192 $message->whereAdd('id <= ' . $this->max_id);
195 if (!empty($this->since_id)) {
196 $message->whereAdd('id > ' . $this->since_id);
199 if (!empty($since)) {
200 $d = date('Y-m-d H:i:s', $this->since);
201 $message->whereAdd("created > '$d'");
204 $message->orderBy('created DESC, id DESC');
205 $message->limit((($this->page - 1) * $this->count), $this->count);
210 while ($message->fetch()) {
211 $messages[] = clone($message);
218 * Is this action read only?
220 * @param array $args other arguments
222 * @return boolean true
225 function isReadOnly($args)
231 * When was this notice last modified?
233 * @return string datestamp of the latest notice in the stream
236 function lastModified()
238 if (!empty($this->messages)) {
239 return strtotime($this->messages[0]->created);
246 * Shows a list of direct messages as Twitter-style XML array
251 function showXmlDirectMessages()
253 $this->init_document('xml');
254 $this->elementStart('direct-messages', array('type' => 'array'));
256 foreach ($this->messages as $m) {
257 $dm_array = $this->directMessageArray($m);
258 $this->showXmlDirectMessage($dm_array);
261 $this->elementEnd('direct-messages');
262 $this->end_document('xml');
266 * Shows a list of direct messages as a JSON encoded array
271 function showJsonDirectMessages()
273 $this->init_document('json');
277 foreach ($this->messages as $m) {
278 $dm_array = $this->directMessageArray($m);
279 array_push($dmsgs, $dm_array);
282 $this->show_json_objects($dmsgs);
283 $this->end_document('json');
287 * Shows a list of direct messages as RSS items
292 function showRssDirectMessages()
294 $this->init_document('rss');
296 $this->element('title', null, $this->title);
298 $this->element('link', null, $this->link);
299 $this->element('description', null, $this->subtitle);
300 $this->element('language', null, 'en-us');
305 'type' => 'application/rss+xml',
306 'href' => $this->selfuri_base . '.rss',
311 $this->element('ttl', null, '40');
313 foreach ($this->messages as $m) {
314 $entry = $this->rssDirectMessageArray($m);
315 $this->show_twitter_rss_item($entry);
318 $this->end_twitter_rss();
322 * Shows a list of direct messages as Atom entries
327 function showAtomDirectMessages()
329 $this->init_document('atom');
331 $this->element('title', null, $this->title);
332 $this->element('id', null, $this->id);
334 $selfuri = common_root_url() . 'api/direct_messages.atom';
338 'href' => $this->link,
339 'rel' => 'alternate',
340 'type' => 'text/html'),
345 'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
346 'type' => 'application/atom+xml'),
349 $this->element('updated', null, common_date_iso8601('now'));
350 $this->element('subtitle', null, $this->subtitle);
352 foreach ($this->messages as $m) {
353 $entry = $this->rssDirectMessageArray($m);
354 $this->showTwitterAtomEntry($entry);
357 $this->end_document('atom');
361 * An entity tag for this notice
363 * Returns an Etag based on the action name, language, and
364 * timestamps of the notice
366 * @return string etag
371 if (!empty($this->messages)) {
373 $last = count($this->messages) - 1;
375 return '"' . implode(
377 array($this->arg('action'),
379 strtotime($this->messages[0]->created),
380 strtotime($this->messages[$last]->created)