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 if ($this->requiresAuth()) {
74 if ($this->checkBasicAuthUser() == false) {
79 $this->user = $this->auth_user;
81 if (empty($this->user)) {
82 $this->clientError(_('No such user!'), 404, $this->format);
86 $this->page = (int)$this->arg('page', 1);
87 $this->count = (int)$this->arg('count', 20);
88 $this->max_id = (int)$this->arg('max_id', 0);
89 $this->since_id = (int)$this->arg('since_id', 0);
90 $this->since = $this->arg('since');
92 $server = common_root_url();
93 $taguribase = common_config('integration', 'taguri');
95 if ($this->arg('sent')) {
97 // Action was called by /api/direct_messages/sent.format
99 $this->title = sprintf(
100 _("Direct messages from %s"),
101 $this->user->nickname
103 $this->subtitle = sprintf(
104 _("All the direct messages sent from %s"),
105 $this->user->nickname
107 $this->link = $server . $this->user->nickname . '/outbox';
108 $this->selfuri_base = common_root_url() . 'api/direct_messages/sent';
109 $this->id = "tag:$taguribase:SentDirectMessages:" . $this->user->id;
111 $this->title = sprintf(
112 _("Direct messages to %s"),
113 $this->user->nickname
115 $this->subtitle = sprintf(
116 _("All the direct messages sent to %s"),
117 $this->user->nickname
119 $this->link = $server . $this->user->nickname . '/inbox';
120 $this->selfuri_base = common_root_url() . 'api/direct_messages';
121 $this->id = "tag:$taguribase:DirectMessages:" . $this->user->id;
124 $this->messages = $this->getMessages();
134 * @param array $args $_REQUEST data (unused)
139 function handle($args)
141 parent::handle($args);
142 $this->showMessages();
151 function showMessages()
153 switch($this->format) {
155 $this->showXmlDirectMessages();
158 $this->showRssDirectMessages();
161 $this->showAtomDirectMessages();
164 $this->showJsonDirectMessages();
167 $this->clientError(_('API method not found!'), $code = 404);
175 * @return array notices
178 function getMessages()
180 $message = new Message();
182 if ($this->arg('sent')) {
183 $message->from_profile = $this->user->id;
185 $message->to_profile = $this->user->id;
188 if (!empty($this->max_id)) {
189 $message->whereAdd('id <= ' . $this->max_id);
192 if (!empty($this->since_id)) {
193 $message->whereAdd('id > ' . $this->since_id);
196 if (!empty($since)) {
197 $d = date('Y-m-d H:i:s', $this->since);
198 $message->whereAdd("created > '$d'");
201 $message->orderBy('created DESC, id DESC');
202 $message->limit((($this->page - 1) * $this->count), $this->count);
207 while ($message->fetch()) {
208 $messages[] = clone($message);
215 * Is this action read only?
217 * @param array $args other arguments
219 * @return boolean true
222 function isReadOnly($args)
228 * When was this notice last modified?
230 * @return string datestamp of the latest notice in the stream
233 function lastModified()
235 if (!empty($this->messages)) {
236 return strtotime($this->messages[0]->created);
243 * Shows a list of direct messages as Twitter-style XML array
248 function showXmlDirectMessages()
250 $this->init_document('xml');
251 $this->elementStart('direct-messages', array('type' => 'array'));
253 foreach ($this->messages as $m) {
254 $dm_array = $this->directMessageArray($m);
255 $this->showXmlDirectMessage($dm_array);
258 $this->elementEnd('direct-messages');
259 $this->end_document('xml');
263 * Shows a list of direct messages as a JSON encoded array
268 function showJsonDirectMessages()
270 $this->init_document('json');
274 foreach ($this->messages as $m) {
275 $dm_array = $this->directMessageArray($m);
276 array_push($dmsgs, $dm_array);
279 $this->show_json_objects($dmsgs);
280 $this->end_document('json');
284 * Shows a list of direct messages as RSS items
289 function showRssDirectMessages()
291 $this->init_document('rss');
293 $this->element('title', null, $this->title);
295 $this->element('link', null, $this->link);
296 $this->element('description', null, $this->subtitle);
297 $this->element('language', null, 'en-us');
302 'type' => 'application/rss+xml',
303 'href' => $this->selfuri_base . '.rss',
308 $this->element('ttl', null, '40');
310 foreach ($this->messages as $m) {
311 $entry = $this->rssDirectMessageArray($m);
312 $this->show_twitter_rss_item($entry);
315 $this->end_twitter_rss();
319 * Shows a list of direct messages as Atom entries
324 function showAtomDirectMessages()
326 $this->init_document('atom');
328 $this->element('title', null, $this->title);
329 $this->element('id', null, $this->id);
331 $selfuri = common_root_url() . 'api/direct_messages.atom';
335 'href' => $this->link,
336 'rel' => 'alternate',
337 'type' => 'text/html'),
342 'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
343 'type' => 'application/atom+xml'),
346 $this->element('updated', null, common_date_iso8601('now'));
347 $this->element('subtitle', null, $this->subtitle);
349 foreach ($this->messages as $m) {
350 $entry = $this->rssDirectMessageArray($m);
351 $this->showTwitterAtomEntry($entry);
354 $this->end_document('atom');
358 * An entity tag for this notice
360 * Returns an Etag based on the action name, language, and
361 * timestamps of the notice
363 * @return string etag
368 if (!empty($this->messages)) {
370 $last = count($this->messages) - 1;
372 return '"' . implode(
374 array($this->arg('action'),
376 strtotime($this->messages[0]->created),
377 strtotime($this->messages[$last]->created)