]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apidirectmessage.php
Merge branch '0.9.x' of git://gitorious.org/statusnet/mainline into 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             $this->clientError(_('No such user.'), 404, $this->format);
78             return;
79         }
80
81         $server   = common_root_url();
82         $taguribase = common_config('integration', 'taguri');
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         if (!empty($since)) {
186             $d = date('Y-m-d H:i:s', $this->since);
187             $message->whereAdd("created > '$d'");
188         }
189
190         $message->orderBy('created DESC, id DESC');
191         $message->limit((($this->page - 1) * $this->count), $this->count);
192         $message->find();
193
194         $messages = array();
195
196         while ($message->fetch()) {
197             $messages[] = clone($message);
198         }
199
200         return $messages;
201     }
202
203     /**
204      * Is this action read only?
205      *
206      * @param array $args other arguments
207      *
208      * @return boolean true
209      */
210
211     function isReadOnly($args)
212     {
213         return true;
214     }
215
216     /**
217      * When was this notice last modified?
218      *
219      * @return string datestamp of the latest notice in the stream
220      */
221
222     function lastModified()
223     {
224         if (!empty($this->messages)) {
225             return strtotime($this->messages[0]->created);
226         }
227
228         return null;
229     }
230
231     /**
232      * Shows a list of direct messages as Twitter-style XML array
233      *
234      * @return void
235      */
236
237     function showXmlDirectMessages()
238     {
239         $this->initDocument('xml');
240         $this->elementStart('direct-messages', array('type' => 'array'));
241
242         foreach ($this->messages as $m) {
243             $dm_array = $this->directMessageArray($m);
244             $this->showXmlDirectMessage($dm_array);
245         }
246
247         $this->elementEnd('direct-messages');
248         $this->endDocument('xml');
249     }
250
251     /**
252      * Shows a list of direct messages as a JSON encoded array
253      *
254      * @return void
255      */
256
257     function showJsonDirectMessages()
258     {
259         $this->initDocument('json');
260
261         $dmsgs = array();
262
263         foreach ($this->messages as $m) {
264             $dm_array = $this->directMessageArray($m);
265             array_push($dmsgs, $dm_array);
266         }
267
268         $this->showJsonObjects($dmsgs);
269         $this->endDocument('json');
270     }
271
272     /**
273      * Shows a list of direct messages as RSS items
274      *
275      * @return void
276      */
277
278     function showRssDirectMessages()
279     {
280         $this->initDocument('rss');
281
282         $this->element('title', null, $this->title);
283
284         $this->element('link', null, $this->link);
285         $this->element('description', null, $this->subtitle);
286         $this->element('language', null, 'en-us');
287
288         $this->element(
289             'atom:link',
290             array(
291                 'type' => 'application/rss+xml',
292                 'href' => $this->selfuri_base . '.rss',
293                 'rel' => self
294                 ),
295             null
296         );
297         $this->element('ttl', null, '40');
298
299         foreach ($this->messages as $m) {
300             $entry = $this->rssDirectMessageArray($m);
301             $this->showTwitterRssItem($entry);
302         }
303
304         $this->endTwitterRss();
305     }
306
307     /**
308      * Shows a list of direct messages as Atom entries
309      *
310      * @return void
311      */
312
313     function showAtomDirectMessages()
314     {
315         $this->initDocument('atom');
316
317         $this->element('title', null, $this->title);
318         $this->element('id', null, $this->id);
319
320         $selfuri = common_root_url() . 'api/direct_messages.atom';
321
322         $this->element(
323             'link', array(
324             'href' => $this->link,
325             'rel' => 'alternate',
326             'type' => 'text/html'),
327             null
328         );
329         $this->element(
330             'link', array(
331             'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
332             'type' => 'application/atom+xml'),
333             null
334         );
335         $this->element('updated', null, common_date_iso8601('now'));
336         $this->element('subtitle', null, $this->subtitle);
337
338         foreach ($this->messages as $m) {
339             $entry = $this->rssDirectMessageArray($m);
340             $this->showTwitterAtomEntry($entry);
341         }
342
343         $this->endDocument('atom');
344     }
345
346     /**
347      * An entity tag for this notice
348      *
349      * Returns an Etag based on the action name, language, and
350      * timestamps of the notice
351      *
352      * @return string etag
353      */
354
355     function etag()
356     {
357         if (!empty($this->messages)) {
358
359             $last = count($this->messages) - 1;
360
361             return '"' . implode(
362                 ':',
363                 array($this->arg('action'),
364                       common_language(),
365                       strtotime($this->messages[0]->created),
366                       strtotime($this->messages[$last]->created)
367                 )
368             )
369             . '"';
370         }
371
372         return null;
373     }
374
375 }