]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apidirectmessage.php
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into testing
[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
237         foreach ($this->messages as $m) {
238             $dm_array = $this->directMessageArray($m);
239             $this->showXmlDirectMessage($dm_array);
240         }
241
242         $this->elementEnd('direct-messages');
243         $this->endDocument('xml');
244     }
245
246     /**
247      * Shows a list of direct messages as a JSON encoded array
248      *
249      * @return void
250      */
251
252     function showJsonDirectMessages()
253     {
254         $this->initDocument('json');
255
256         $dmsgs = array();
257
258         foreach ($this->messages as $m) {
259             $dm_array = $this->directMessageArray($m);
260             array_push($dmsgs, $dm_array);
261         }
262
263         $this->showJsonObjects($dmsgs);
264         $this->endDocument('json');
265     }
266
267     /**
268      * Shows a list of direct messages as RSS items
269      *
270      * @return void
271      */
272
273     function showRssDirectMessages()
274     {
275         $this->initDocument('rss');
276
277         $this->element('title', null, $this->title);
278
279         $this->element('link', null, $this->link);
280         $this->element('description', null, $this->subtitle);
281         $this->element('language', null, 'en-us');
282
283         $this->element(
284             'atom:link',
285             array(
286                 'type' => 'application/rss+xml',
287                 'href' => $this->selfuri_base . '.rss',
288                 'rel' => self
289                 ),
290             null
291         );
292         $this->element('ttl', null, '40');
293
294         foreach ($this->messages as $m) {
295             $entry = $this->rssDirectMessageArray($m);
296             $this->showTwitterRssItem($entry);
297         }
298
299         $this->endTwitterRss();
300     }
301
302     /**
303      * Shows a list of direct messages as Atom entries
304      *
305      * @return void
306      */
307
308     function showAtomDirectMessages()
309     {
310         $this->initDocument('atom');
311
312         $this->element('title', null, $this->title);
313         $this->element('id', null, $this->id);
314
315         $selfuri = common_root_url() . 'api/direct_messages.atom';
316
317         $this->element(
318             'link', array(
319             'href' => $this->link,
320             'rel' => 'alternate',
321             'type' => 'text/html'),
322             null
323         );
324         $this->element(
325             'link', array(
326             'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
327             'type' => 'application/atom+xml'),
328             null
329         );
330         $this->element('updated', null, common_date_iso8601('now'));
331         $this->element('subtitle', null, $this->subtitle);
332
333         foreach ($this->messages as $m) {
334             $entry = $this->rssDirectMessageArray($m);
335             $this->showTwitterAtomEntry($entry);
336         }
337
338         $this->endDocument('atom');
339     }
340
341     /**
342      * An entity tag for this notice
343      *
344      * Returns an Etag based on the action name, language, and
345      * timestamps of the notice
346      *
347      * @return string etag
348      */
349
350     function etag()
351     {
352         if (!empty($this->messages)) {
353
354             $last = count($this->messages) - 1;
355
356             return '"' . implode(
357                 ':',
358                 array($this->arg('action'),
359                       common_language(),
360                       strtotime($this->messages[0]->created),
361                       strtotime($this->messages[$last]->created)
362                 )
363             )
364             . '"';
365         }
366
367         return null;
368     }
369
370 }