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