]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apidirectmessage.php
Merge branch '0.9.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
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             // TRANS: Client error given when a user was not found (404).
78             $this->clientError(_('No such user.'), 404, $this->format);
79             return;
80         }
81
82         $server   = common_root_url();
83         $taguribase = TagURI::base();
84
85         if ($this->arg('sent')) {
86
87             // Action was called by /api/direct_messages/sent.format
88
89             $this->title = sprintf(
90                 // TRANS: %s is a user nickname.
91                 _("Direct messages from %s"),
92                 $this->user->nickname
93             );
94             $this->subtitle = sprintf(
95                 // TRANS: %s is a user nickname.
96                 _("All the direct messages sent from %s"),
97                 $this->user->nickname
98             );
99             $this->link = $server . $this->user->nickname . '/outbox';
100             $this->selfuri_base = common_root_url() . 'api/direct_messages/sent';
101             $this->id = "tag:$taguribase:SentDirectMessages:" . $this->user->id;
102         } else {
103             $this->title = sprintf(
104                 // TRANS: %s is a user nickname.
105                 _("Direct messages to %s"),
106                 $this->user->nickname
107             );
108             $this->subtitle = sprintf(
109                 // TRANS: %s is a user nickname.
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             // TRANS: Client error given when an API method was not found (404).
162             $this->clientError(_('API method not found.'), $code = 404);
163             break;
164         }
165     }
166
167     /**
168      * Get notices
169      *
170      * @return array notices
171      */
172
173     function getMessages()
174     {
175         $message  = new Message();
176
177         if ($this->arg('sent')) {
178             $message->from_profile = $this->user->id;
179         } else {
180             $message->to_profile = $this->user->id;
181         }
182
183         if (!empty($this->max_id)) {
184             $message->whereAdd('id <= ' . $this->max_id);
185         }
186
187         if (!empty($this->since_id)) {
188             $message->whereAdd('id > ' . $this->since_id);
189         }
190
191         $message->orderBy('created DESC, id DESC');
192         $message->limit((($this->page - 1) * $this->count), $this->count);
193         $message->find();
194
195         $messages = array();
196
197         while ($message->fetch()) {
198             $messages[] = clone($message);
199         }
200
201         return $messages;
202     }
203
204     /**
205      * Is this action read only?
206      *
207      * @param array $args other arguments
208      *
209      * @return boolean true
210      */
211
212     function isReadOnly($args)
213     {
214         return true;
215     }
216
217     /**
218      * When was this notice last modified?
219      *
220      * @return string datestamp of the latest notice in the stream
221      */
222
223     function lastModified()
224     {
225         if (!empty($this->messages)) {
226             return strtotime($this->messages[0]->created);
227         }
228
229         return null;
230     }
231
232     /**
233      * Shows a list of direct messages as Twitter-style XML array
234      *
235      * @return void
236      */
237
238     function showXmlDirectMessages()
239     {
240         $this->initDocument('xml');
241         $this->elementStart('direct-messages', array('type' => 'array',
242                                                      'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
243
244         foreach ($this->messages as $m) {
245             $dm_array = $this->directMessageArray($m);
246             $this->showXmlDirectMessage($dm_array);
247         }
248
249         $this->elementEnd('direct-messages');
250         $this->endDocument('xml');
251     }
252
253     /**
254      * Shows a list of direct messages as a JSON encoded array
255      *
256      * @return void
257      */
258
259     function showJsonDirectMessages()
260     {
261         $this->initDocument('json');
262
263         $dmsgs = array();
264
265         foreach ($this->messages as $m) {
266             $dm_array = $this->directMessageArray($m);
267             array_push($dmsgs, $dm_array);
268         }
269
270         $this->showJsonObjects($dmsgs);
271         $this->endDocument('json');
272     }
273
274     /**
275      * Shows a list of direct messages as RSS items
276      *
277      * @return void
278      */
279
280     function showRssDirectMessages()
281     {
282         $this->initDocument('rss');
283
284         $this->element('title', null, $this->title);
285
286         $this->element('link', null, $this->link);
287         $this->element('description', null, $this->subtitle);
288         $this->element('language', null, 'en-us');
289
290         $this->element(
291             'atom:link',
292             array(
293                 'type' => 'application/rss+xml',
294                 'href' => $this->selfuri_base . '.rss',
295                 'rel' => self
296                 ),
297             null
298         );
299         $this->element('ttl', null, '40');
300
301         foreach ($this->messages as $m) {
302             $entry = $this->rssDirectMessageArray($m);
303             $this->showTwitterRssItem($entry);
304         }
305
306         $this->endTwitterRss();
307     }
308
309     /**
310      * Shows a list of direct messages as Atom entries
311      *
312      * @return void
313      */
314
315     function showAtomDirectMessages()
316     {
317         $this->initDocument('atom');
318
319         $this->element('title', null, $this->title);
320         $this->element('id', null, $this->id);
321
322         $selfuri = common_root_url() . 'api/direct_messages.atom';
323
324         $this->element(
325             'link', array(
326             'href' => $this->link,
327             'rel' => 'alternate',
328             'type' => 'text/html'),
329             null
330         );
331         $this->element(
332             'link', array(
333             'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
334             'type' => 'application/atom+xml'),
335             null
336         );
337         $this->element('updated', null, common_date_iso8601('now'));
338         $this->element('subtitle', null, $this->subtitle);
339
340         foreach ($this->messages as $m) {
341             $entry = $this->rssDirectMessageArray($m);
342             $this->showTwitterAtomEntry($entry);
343         }
344
345         $this->endDocument('atom');
346     }
347
348     /**
349      * An entity tag for this notice
350      *
351      * Returns an Etag based on the action name, language, and
352      * timestamps of the notice
353      *
354      * @return string etag
355      */
356
357     function etag()
358     {
359         if (!empty($this->messages)) {
360
361             $last = count($this->messages) - 1;
362
363             return '"' . implode(
364                 ':',
365                 array($this->arg('action'),
366                       common_user_cache_hash($this->auth_user),
367                       common_language(),
368                       strtotime($this->messages[0]->created),
369                       strtotime($this->messages[$last]->created)
370                 )
371             )
372             . '"';
373         }
374
375         return null;
376     }
377
378 }