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