]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DirectMessage/actions/apidirectmessage.php
Maybe this message will be needed some day ...
[quix0rs-gnu-social.git] / plugins / DirectMessage / 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     // BEGIN import from lib/apiaction.php
222
223     function showSingleXmlDirectMessage($message)
224     {
225         $this->initDocument('xml');
226         $dmsg = $this->directMessageArray($message);
227         $this->showXmlDirectMessage($dmsg, true);
228         $this->endDocument('xml');
229     }
230
231     function showSingleJsonDirectMessage($message)
232     {
233         $this->initDocument('json');
234         $dmsg = $this->directMessageArray($message);
235         $this->showJsonObjects($dmsg);
236         $this->endDocument('json');
237     }
238
239     function showXmlDirectMessage($dm, $namespaces=false)
240     {
241         $attrs = array();
242         if ($namespaces) {
243             $attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
244         }
245         $this->elementStart('direct_message', $attrs);
246         foreach($dm as $element => $value) {
247             switch ($element) {
248             case 'sender':
249             case 'recipient':
250                 $this->showTwitterXmlUser($value, $element);
251                 break;
252             case 'text':
253                 $this->element($element, null, common_xml_safe_str($value));
254                 break;
255             default:
256                 $this->element($element, null, $value);
257                 break;
258             }
259         }
260         $this->elementEnd('direct_message');
261     }
262
263     function directMessageArray($message)
264     {
265         $dmsg = array();
266
267         $from_profile = $message->getFrom();
268         $to_profile = $message->getTo();
269
270         $dmsg['id'] = intval($message->id);
271         $dmsg['sender_id'] = intval($from_profile->id);
272         $dmsg['text'] = trim($message->content);
273         $dmsg['recipient_id'] = intval($to_profile->id);
274         $dmsg['created_at'] = $this->dateTwitter($message->created);
275         $dmsg['sender_screen_name'] = $from_profile->nickname;
276         $dmsg['recipient_screen_name'] = $to_profile->nickname;
277         $dmsg['sender'] = $this->twitterUserArray($from_profile, false);
278         $dmsg['recipient'] = $this->twitterUserArray($to_profile, false);
279
280         return $dmsg;
281     }
282
283     function rssDirectMessageArray($message)
284     {
285         $entry = array();
286
287         $from = $message->getFrom();
288
289         $entry['title'] = sprintf('Message from %1$s to %2$s',
290             $from->nickname, $message->getTo()->nickname);
291
292         $entry['content'] = common_xml_safe_str($message->rendered);
293         $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
294         $entry['published'] = common_date_iso8601($message->created);
295
296         $taguribase = TagURI::base();
297
298         $entry['id'] = "tag:$taguribase:$entry[link]";
299         $entry['updated'] = $entry['published'];
300
301         $entry['author-name'] = $from->getBestName();
302         $entry['author-uri'] = $from->homepage;
303
304         $entry['avatar'] = $from->avatarUrl(AVATAR_STREAM_SIZE);
305         try {
306             $avatar = $from->getAvatar(AVATAR_STREAM_SIZE);
307             $entry['avatar-type'] = $avatar->mediatype;
308         } catch (Exception $e) {
309             $entry['avatar-type'] = 'image/png';
310         }
311
312         // RSS item specific
313
314         $entry['description'] = $entry['content'];
315         $entry['pubDate'] = common_date_rfc2822($message->created);
316         $entry['guid'] = $entry['link'];
317
318         return $entry;
319     }
320
321     // END import from lib/apiaction.php
322
323     /**
324      * Shows a list of direct messages as Twitter-style XML array
325      *
326      * @return void
327      */
328     function showXmlDirectMessages()
329     {
330         $this->initDocument('xml');
331         $this->elementStart('direct-messages', array('type' => 'array',
332                                                      'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
333
334         foreach ($this->messages as $m) {
335             $dm_array = $this->directMessageArray($m);
336             $this->showXmlDirectMessage($dm_array);
337         }
338
339         $this->elementEnd('direct-messages');
340         $this->endDocument('xml');
341     }
342
343     /**
344      * Shows a list of direct messages as a JSON encoded array
345      *
346      * @return void
347      */
348     function showJsonDirectMessages()
349     {
350         $this->initDocument('json');
351
352         $dmsgs = array();
353
354         foreach ($this->messages as $m) {
355             $dm_array = $this->directMessageArray($m);
356             array_push($dmsgs, $dm_array);
357         }
358
359         $this->showJsonObjects($dmsgs);
360         $this->endDocument('json');
361     }
362
363     /**
364      * Shows a list of direct messages as RSS items
365      *
366      * @return void
367      */
368     function showRssDirectMessages()
369     {
370         $this->initDocument('rss');
371
372         $this->element('title', null, $this->title);
373
374         $this->element('link', null, $this->link);
375         $this->element('description', null, $this->subtitle);
376         $this->element('language', null, 'en-us');
377
378         $this->element(
379             'atom:link',
380             array(
381                 'type' => 'application/rss+xml',
382                 'href' => $this->selfuri_base . '.rss',
383                 'rel' => self
384                 ),
385             null
386         );
387         $this->element('ttl', null, '40');
388
389         foreach ($this->messages as $m) {
390             $entry = $this->rssDirectMessageArray($m);
391             $this->showTwitterRssItem($entry);
392         }
393
394         $this->endTwitterRss();
395     }
396
397     /**
398      * Shows a list of direct messages as Atom entries
399      *
400      * @return void
401      */
402     function showAtomDirectMessages()
403     {
404         $this->initDocument('atom');
405
406         $this->element('title', null, $this->title);
407         $this->element('id', null, $this->id);
408
409         $selfuri = common_root_url() . 'api/direct_messages.atom';
410
411         $this->element(
412             'link', array(
413             'href' => $this->link,
414             'rel' => 'alternate',
415             'type' => 'text/html'),
416             null
417         );
418         $this->element(
419             'link', array(
420             'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
421             'type' => 'application/atom+xml'),
422             null
423         );
424         $this->element('updated', null, common_date_iso8601('now'));
425         $this->element('subtitle', null, $this->subtitle);
426
427         foreach ($this->messages as $m) {
428             $entry = $this->rssDirectMessageArray($m);
429             $this->showTwitterAtomEntry($entry);
430         }
431
432         $this->endDocument('atom');
433     }
434
435     /**
436      * An entity tag for this notice
437      *
438      * Returns an Etag based on the action name, language, and
439      * timestamps of the notice
440      *
441      * @return string etag
442      */
443     function etag()
444     {
445         if (!empty($this->messages)) {
446
447             $last = count($this->messages) - 1;
448
449             return '"' . implode(
450                 ':',
451                 array($this->arg('action'),
452                       common_user_cache_hash($this->auth_user),
453                       common_language(),
454                       strtotime($this->messages[0]->created),
455                       strtotime($this->messages[$last]->created)
456                 )
457             )
458             . '"';
459         }
460
461         return null;
462     }
463 }