]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apidirectmessage.php
Move all basic auth output and processing to base classes
[quix0rs-gnu-social.git] / actions / apidirectmessage.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show a the direct messages from or to a user
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  API
23  * @package   StatusNet
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/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/apiauth.php';
35
36 /**
37  * Show a list of direct messages from or to the authenticating user
38  *
39  * @category API
40  * @package  StatusNet
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/
44  */
45
46 class ApiDirectMessageAction extends ApiAuthAction
47 {
48     var $messages     = null;
49     var $page         = null;
50     var $count        = null;
51     var $max_id       = null;
52     var $since_id     = null;
53     var $since        = null;
54     var $title        = null;
55     var $subtitle     = null;
56     var $link         = null;
57     var $selfuri_base = null;
58     var $id           = null;
59
60     /**
61      * Take arguments for running
62      *
63      * @param array $args $_REQUEST args
64      *
65      * @return boolean success flag
66      *
67      */
68
69     function prepare($args)
70     {
71         parent::prepare($args);
72
73         $this->user = $this->auth_user;
74
75         if (empty($this->user)) {
76             $this->clientError(_('No such user!'), 404, $this->format);
77             return;
78         }
79
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');
85
86         $server   = common_root_url();
87         $taguribase = common_config('integration', 'taguri');
88
89         if ($this->arg('sent')) {
90
91             // Action was called by /api/direct_messages/sent.format
92
93             $this->title = sprintf(
94                 _("Direct messages from %s"),
95                 $this->user->nickname
96             );
97             $this->subtitle = sprintf(
98                 _("All the direct messages sent from %s"),
99                 $this->user->nickname
100             );
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;
104         } else {
105             $this->title = sprintf(
106                 _("Direct messages to %s"),
107                 $this->user->nickname
108             );
109             $this->subtitle = sprintf(
110                 _("All the direct messages sent to %s"),
111                 $this->user->nickname
112             );
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;
116         }
117
118         $this->messages = $this->getMessages();
119
120         return true;
121     }
122
123     /**
124      * Handle the request
125      *
126      * Show the messages
127      *
128      * @param array $args $_REQUEST data (unused)
129      *
130      * @return void
131      */
132
133     function handle($args)
134     {
135         parent::handle($args);
136         $this->showMessages();
137     }
138
139     /**
140      * Show the messages
141      *
142      * @return void
143      */
144
145     function showMessages()
146     {
147         switch($this->format) {
148         case 'xml':
149             $this->showXmlDirectMessages();
150             break;
151         case 'rss':
152             $this->showRssDirectMessages();
153             break;
154         case 'atom':
155             $this->showAtomDirectMessages();
156             break;
157         case 'json':
158             $this->showJsonDirectMessages();
159             break;
160         default:
161             $this->clientError(_('API method not found!'), $code = 404);
162             break;
163         }
164     }
165
166     /**
167      * Get notices
168      *
169      * @return array notices
170      */
171
172     function getMessages()
173     {
174         $message  = new Message();
175
176         if ($this->arg('sent')) {
177             $message->from_profile = $this->user->id;
178         } else {
179             $message->to_profile = $this->user->id;
180         }
181
182         if (!empty($this->max_id)) {
183             $message->whereAdd('id <= ' . $this->max_id);
184         }
185
186         if (!empty($this->since_id)) {
187             $message->whereAdd('id > ' . $this->since_id);
188         }
189
190         if (!empty($since)) {
191             $d = date('Y-m-d H:i:s', $this->since);
192             $message->whereAdd("created > '$d'");
193         }
194
195         $message->orderBy('created DESC, id DESC');
196         $message->limit((($this->page - 1) * $this->count), $this->count);
197         $message->find();
198
199         $messages = array();
200
201         while ($message->fetch()) {
202             $messages[] = clone($message);
203         }
204
205         return $messages;
206     }
207
208     /**
209      * Is this action read only?
210      *
211      * @param array $args other arguments
212      *
213      * @return boolean true
214      */
215
216     function isReadOnly($args)
217     {
218         return true;
219     }
220
221     /**
222      * When was this notice last modified?
223      *
224      * @return string datestamp of the latest notice in the stream
225      */
226
227     function lastModified()
228     {
229         if (!empty($this->messages)) {
230             return strtotime($this->messages[0]->created);
231         }
232
233         return null;
234     }
235
236     /**
237      * Shows a list of direct messages as Twitter-style XML array
238      *
239      * @return void
240      */
241
242     function showXmlDirectMessages()
243     {
244         $this->init_document('xml');
245         $this->elementStart('direct-messages', array('type' => 'array'));
246
247         foreach ($this->messages as $m) {
248             $dm_array = $this->directMessageArray($m);
249             $this->showXmlDirectMessage($dm_array);
250         }
251
252         $this->elementEnd('direct-messages');
253         $this->end_document('xml');
254     }
255
256     /**
257      * Shows a list of direct messages as a JSON encoded array
258      *
259      * @return void
260      */
261
262     function showJsonDirectMessages()
263     {
264         $this->init_document('json');
265
266         $dmsgs = array();
267
268         foreach ($this->messages as $m) {
269             $dm_array = $this->directMessageArray($m);
270             array_push($dmsgs, $dm_array);
271         }
272
273         $this->show_json_objects($dmsgs);
274         $this->end_document('json');
275     }
276
277     /**
278      * Shows a list of direct messages as RSS items
279      *
280      * @return void
281      */
282
283     function showRssDirectMessages()
284     {
285         $this->init_document('rss');
286
287         $this->element('title', null, $this->title);
288
289         $this->element('link', null, $this->link);
290         $this->element('description', null, $this->subtitle);
291         $this->element('language', null, 'en-us');
292
293         $this->element(
294             'atom:link',
295             array(
296                 'type' => 'application/rss+xml',
297                 'href' => $this->selfuri_base . '.rss',
298                 'rel' => self
299                 ),
300             null
301         );
302         $this->element('ttl', null, '40');
303
304         foreach ($this->messages as $m) {
305             $entry = $this->rssDirectMessageArray($m);
306             $this->show_twitter_rss_item($entry);
307         }
308
309         $this->end_twitter_rss();
310     }
311
312     /**
313      * Shows a list of direct messages as Atom entries
314      *
315      * @return void
316      */
317
318     function showAtomDirectMessages()
319     {
320         $this->init_document('atom');
321
322         $this->element('title', null, $this->title);
323         $this->element('id', null, $this->id);
324
325         $selfuri = common_root_url() . 'api/direct_messages.atom';
326
327         $this->element(
328             'link', array(
329             'href' => $this->link,
330             'rel' => 'alternate',
331             'type' => 'text/html'),
332             null
333         );
334         $this->element(
335             'link', array(
336             'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
337             'type' => 'application/atom+xml'),
338             null
339         );
340         $this->element('updated', null, common_date_iso8601('now'));
341         $this->element('subtitle', null, $this->subtitle);
342
343         foreach ($this->messages as $m) {
344             $entry = $this->rssDirectMessageArray($m);
345             $this->showTwitterAtomEntry($entry);
346         }
347
348         $this->end_document('atom');
349     }
350
351     /**
352      * An entity tag for this notice
353      *
354      * Returns an Etag based on the action name, language, and
355      * timestamps of the notice
356      *
357      * @return string etag
358      */
359
360     function etag()
361     {
362         if (!empty($this->messages)) {
363
364             $last = count($this->messages) - 1;
365
366             return '"' . implode(
367                 ':',
368                 array($this->arg('action'),
369                       common_language(),
370                       strtotime($this->messages[0]->created),
371                       strtotime($this->messages[$last]->created)
372                 )
373             )
374             . '"';
375         }
376
377         return null;
378     }
379
380 }