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