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