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