]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apidirectmessage.php
Merge branch 'qna' into 1.0.x
[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 class ApiDirectMessageAction extends ApiAuthAction
52 {
53     var $messages     = 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     function prepare($args)
68     {
69         parent::prepare($args);
70
71         $this->user = $this->auth_user;
72
73         if (empty($this->user)) {
74             // TRANS: Client error given when a user was not found (404).
75             $this->clientError(_('No such user.'), 404, $this->format);
76             return;
77         }
78
79         $server   = common_root_url();
80         $taguribase = TagURI::base();
81
82         if ($this->arg('sent')) {
83
84             // Action was called by /api/direct_messages/sent.format
85
86             $this->title = sprintf(
87                 // TRANS: Title. %s is a user nickname.
88                 _("Direct messages from %s"),
89                 $this->user->nickname
90             );
91             $this->subtitle = sprintf(
92                 // TRANS: Subtitle. %s is a user nickname.
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                 // TRANS: Title. %s is a user nickname.
102                 _("Direct messages to %s"),
103                 $this->user->nickname
104             );
105             $this->subtitle = sprintf(
106                 // TRANS: Subtitle. %s is a user nickname.
107                 _("All the direct messages sent to %s"),
108                 $this->user->nickname
109             );
110             $this->link = $server . $this->user->nickname . '/inbox';
111             $this->selfuri_base = common_root_url() . 'api/direct_messages';
112             $this->id = "tag:$taguribase:DirectMessages:" . $this->user->id;
113         }
114
115         $this->messages = $this->getMessages();
116
117         return true;
118     }
119
120     /**
121      * Handle the request
122      *
123      * Show the messages
124      *
125      * @param array $args $_REQUEST data (unused)
126      *
127      * @return void
128      */
129     function handle($args)
130     {
131         parent::handle($args);
132         $this->showMessages();
133     }
134
135     /**
136      * Show the messages
137      *
138      * @return void
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             // TRANS: Client error given when an API method was not found (404).
157             $this->clientError(_('API method not found.'), $code = 404);
158             break;
159         }
160     }
161
162     /**
163      * Get notices
164      *
165      * @return array notices
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     function isReadOnly($args)
206     {
207         return true;
208     }
209
210     /**
211      * When was this notice last modified?
212      *
213      * @return string datestamp of the latest notice in the stream
214      */
215     function lastModified()
216     {
217         if (!empty($this->messages)) {
218             return strtotime($this->messages[0]->created);
219         }
220
221         return null;
222     }
223
224     /**
225      * Shows a list of direct messages as Twitter-style XML array
226      *
227      * @return void
228      */
229     function showXmlDirectMessages()
230     {
231         $this->initDocument('xml');
232         $this->elementStart('direct-messages', array('type' => 'array',
233                                                      'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
234
235         foreach ($this->messages as $m) {
236             $dm_array = $this->directMessageArray($m);
237             $this->showXmlDirectMessage($dm_array);
238         }
239
240         $this->elementEnd('direct-messages');
241         $this->endDocument('xml');
242     }
243
244     /**
245      * Shows a list of direct messages as a JSON encoded array
246      *
247      * @return void
248      */
249     function showJsonDirectMessages()
250     {
251         $this->initDocument('json');
252
253         $dmsgs = array();
254
255         foreach ($this->messages as $m) {
256             $dm_array = $this->directMessageArray($m);
257             array_push($dmsgs, $dm_array);
258         }
259
260         $this->showJsonObjects($dmsgs);
261         $this->endDocument('json');
262     }
263
264     /**
265      * Shows a list of direct messages as RSS items
266      *
267      * @return void
268      */
269     function showRssDirectMessages()
270     {
271         $this->initDocument('rss');
272
273         $this->element('title', null, $this->title);
274
275         $this->element('link', null, $this->link);
276         $this->element('description', null, $this->subtitle);
277         $this->element('language', null, 'en-us');
278
279         $this->element(
280             'atom:link',
281             array(
282                 'type' => 'application/rss+xml',
283                 'href' => $this->selfuri_base . '.rss',
284                 'rel' => self
285                 ),
286             null
287         );
288         $this->element('ttl', null, '40');
289
290         foreach ($this->messages as $m) {
291             $entry = $this->rssDirectMessageArray($m);
292             $this->showTwitterRssItem($entry);
293         }
294
295         $this->endTwitterRss();
296     }
297
298     /**
299      * Shows a list of direct messages as Atom entries
300      *
301      * @return void
302      */
303     function showAtomDirectMessages()
304     {
305         $this->initDocument('atom');
306
307         $this->element('title', null, $this->title);
308         $this->element('id', null, $this->id);
309
310         $selfuri = common_root_url() . 'api/direct_messages.atom';
311
312         $this->element(
313             'link', array(
314             'href' => $this->link,
315             'rel' => 'alternate',
316             'type' => 'text/html'),
317             null
318         );
319         $this->element(
320             'link', array(
321             'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
322             'type' => 'application/atom+xml'),
323             null
324         );
325         $this->element('updated', null, common_date_iso8601('now'));
326         $this->element('subtitle', null, $this->subtitle);
327
328         foreach ($this->messages as $m) {
329             $entry = $this->rssDirectMessageArray($m);
330             $this->showTwitterAtomEntry($entry);
331         }
332
333         $this->endDocument('atom');
334     }
335
336     /**
337      * An entity tag for this notice
338      *
339      * Returns an Etag based on the action name, language, and
340      * timestamps of the notice
341      *
342      * @return string etag
343      */
344     function etag()
345     {
346         if (!empty($this->messages)) {
347
348             $last = count($this->messages) - 1;
349
350             return '"' . implode(
351                 ':',
352                 array($this->arg('action'),
353                       common_user_cache_hash($this->auth_user),
354                       common_language(),
355                       strtotime($this->messages[0]->created),
356                       strtotime($this->messages[$last]->created)
357                 )
358             )
359             . '"';
360         }
361
362         return null;
363     }
364 }