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