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
52 var $selfuri_base = null;
56 * Take arguments for running
58 * @param array $args $_REQUEST args
60 * @return boolean success flag
64 function prepare($args)
66 parent::prepare($args);
68 $this->user = $this->auth_user;
70 if (empty($this->user)) {
71 $this->clientError(_('No such user!'), 404, $this->format);
75 $server = common_root_url();
76 $taguribase = common_config('integration', 'taguri');
78 if ($this->arg('sent')) {
80 // Action was called by /api/direct_messages/sent.format
82 $this->title = sprintf(
83 _("Direct messages from %s"),
86 $this->subtitle = sprintf(
87 _("All the direct messages sent from %s"),
90 $this->link = $server . $this->user->nickname . '/outbox';
91 $this->selfuri_base = common_root_url() . 'api/direct_messages/sent';
92 $this->id = "tag:$taguribase:SentDirectMessages:" . $this->user->id;
94 $this->title = sprintf(
95 _("Direct messages to %s"),
98 $this->subtitle = sprintf(
99 _("All the direct messages sent to %s"),
100 $this->user->nickname
102 $this->link = $server . $this->user->nickname . '/inbox';
103 $this->selfuri_base = common_root_url() . 'api/direct_messages';
104 $this->id = "tag:$taguribase:DirectMessages:" . $this->user->id;
107 $this->messages = $this->getMessages();
117 * @param array $args $_REQUEST data (unused)
122 function handle($args)
124 parent::handle($args);
125 $this->showMessages();
134 function showMessages()
136 switch($this->format) {
138 $this->showXmlDirectMessages();
141 $this->showRssDirectMessages();
144 $this->showAtomDirectMessages();
147 $this->showJsonDirectMessages();
150 $this->clientError(_('API method not found!'), $code = 404);
158 * @return array notices
161 function getMessages()
163 $message = new Message();
165 if ($this->arg('sent')) {
166 $message->from_profile = $this->user->id;
168 $message->to_profile = $this->user->id;
171 if (!empty($this->max_id)) {
172 $message->whereAdd('id <= ' . $this->max_id);
175 if (!empty($this->since_id)) {
176 $message->whereAdd('id > ' . $this->since_id);
179 if (!empty($since)) {
180 $d = date('Y-m-d H:i:s', $this->since);
181 $message->whereAdd("created > '$d'");
184 $message->orderBy('created DESC, id DESC');
185 $message->limit((($this->page - 1) * $this->count), $this->count);
190 while ($message->fetch()) {
191 $messages[] = clone($message);
198 * Is this action read only?
200 * @param array $args other arguments
202 * @return boolean true
205 function isReadOnly($args)
211 * When was this notice last modified?
213 * @return string datestamp of the latest notice in the stream
216 function lastModified()
218 if (!empty($this->messages)) {
219 return strtotime($this->messages[0]->created);
226 * Shows a list of direct messages as Twitter-style XML array
231 function showXmlDirectMessages()
233 $this->initDocument('xml');
234 $this->elementStart('direct-messages', array('type' => 'array'));
236 foreach ($this->messages as $m) {
237 $dm_array = $this->directMessageArray($m);
238 $this->showXmlDirectMessage($dm_array);
241 $this->elementEnd('direct-messages');
242 $this->endDocument('xml');
246 * Shows a list of direct messages as a JSON encoded array
251 function showJsonDirectMessages()
253 $this->initDocument('json');
257 foreach ($this->messages as $m) {
258 $dm_array = $this->directMessageArray($m);
259 array_push($dmsgs, $dm_array);
262 $this->showJsonObjects($dmsgs);
263 $this->endDocument('json');
267 * Shows a list of direct messages as RSS items
272 function showRssDirectMessages()
274 $this->initDocument('rss');
276 $this->element('title', null, $this->title);
278 $this->element('link', null, $this->link);
279 $this->element('description', null, $this->subtitle);
280 $this->element('language', null, 'en-us');
285 'type' => 'application/rss+xml',
286 'href' => $this->selfuri_base . '.rss',
291 $this->element('ttl', null, '40');
293 foreach ($this->messages as $m) {
294 $entry = $this->rssDirectMessageArray($m);
295 $this->showTwitterRssItem($entry);
298 $this->endTwitterRss();
302 * Shows a list of direct messages as Atom entries
307 function showAtomDirectMessages()
309 $this->initDocument('atom');
311 $this->element('title', null, $this->title);
312 $this->element('id', null, $this->id);
314 $selfuri = common_root_url() . 'api/direct_messages.atom';
318 'href' => $this->link,
319 'rel' => 'alternate',
320 'type' => 'text/html'),
325 'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
326 'type' => 'application/atom+xml'),
329 $this->element('updated', null, common_date_iso8601('now'));
330 $this->element('subtitle', null, $this->subtitle);
332 foreach ($this->messages as $m) {
333 $entry = $this->rssDirectMessageArray($m);
334 $this->showTwitterAtomEntry($entry);
337 $this->endDocument('atom');
341 * An entity tag for this notice
343 * Returns an Etag based on the action name, language, and
344 * timestamps of the notice
346 * @return string etag
351 if (!empty($this->messages)) {
353 $last = count($this->messages) - 1;
355 return '"' . implode(
357 array($this->arg('action'),
359 strtotime($this->messages[0]->created),
360 strtotime($this->messages[$last]->created)