]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DirectMessage/actions/apidirectmessage.php
Merged
[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('GNUSOCIAL')) { exit(1); }
34
35 /**
36  * Show a list of direct messages from or to the authenticating user
37  *
38  * @category API
39  * @package  StatusNet
40  * @author   Adrian Lang <mail@adrianlang.de>
41  * @author   Evan Prodromou <evan@status.net>
42  * @author   Robin Millette <robin@millette.info>
43  * @author   Zach Copley <zach@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  */
47 class ApiDirectMessageAction extends ApiAuthAction
48 {
49     var $messages     = null;
50     var $title        = null;
51     var $subtitle     = null;
52     var $link         = null;
53     var $selfuri_base = null;
54     var $id           = null;
55
56     /**
57      * Take arguments for running
58      *
59      * @param array $args $_REQUEST args
60      *
61      * @return boolean success flag
62      */
63 <<<<<<< .merge_file_m7N4pd
64     protected function prepare(array $args=array())
65 =======
66     function prepare(array $args=array())
67 >>>>>>> .merge_file_lMcE3c
68     {
69         parent::prepare($args);
70
71         if (!$this->scoped instanceof Profile) {
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->scoped->getNickname()
87             );
88             $this->subtitle = sprintf(
89                 // TRANS: Subtitle. %s is a user nickname.
90                 _("All the direct messages sent from %s"),
91                 $this->scoped->getNickname()
92             );
93             $this->link = $server . $this->scoped->getNickname() . '/outbox';
94             $this->selfuri_base = common_root_url() . 'api/direct_messages/sent';
95             $this->id = "tag:$taguribase:SentDirectMessages:" . $this->scoped->getID();
96         } else {
97             $this->title = sprintf(
98                 // TRANS: Title. %s is a user nickname.
99                 _("Direct messages to %s"),
100                 $this->scoped->getNickname()
101             );
102             $this->subtitle = sprintf(
103                 // TRANS: Subtitle. %s is a user nickname.
104                 _("All the direct messages sent to %s"),
105                 $this->scoped->getNickname()
106             );
107             $this->link = $server . $this->scoped->getNickname() . '/inbox';
108             $this->selfuri_base = common_root_url() . 'api/direct_messages';
109             $this->id = "tag:$taguribase:DirectMessages:" . $this->scoped->getID();
110         }
111
112         $this->messages = $this->getMessages();
113
114         return true;
115     }
116
117 <<<<<<< .merge_file_m7N4pd
118     protected function handle()
119 =======
120     /**
121      * Handle the request
122      *
123      * Show the messages
124      *
125      * @param array $args $_REQUEST data (unused)
126      *
127      * @return void
128      */
129     function handle(array $args=array())
130 >>>>>>> .merge_file_lMcE3c
131     {
132         parent::handle();
133         $this->showMessages();
134     }
135
136     /**
137      * Show the messages
138      *
139      * @return void
140      */
141     function showMessages()
142     {
143         switch($this->format) {
144         case 'xml':
145             $this->showXmlDirectMessages();
146             break;
147         case 'rss':
148             $this->showRssDirectMessages();
149             break;
150         case 'atom':
151             $this->showAtomDirectMessages();
152             break;
153         case 'json':
154             $this->showJsonDirectMessages();
155             break;
156         default:
157             // TRANS: Client error displayed when coming across a non-supported API method.
158             $this->clientError(_('API method not found.'), $code = 404);
159             break;
160         }
161     }
162
163     /**
164      * Get notices
165      *
166      * @return array notices
167      */
168     function getMessages()
169     {
170         $message  = new Message();
171
172         if ($this->arg('sent')) {
173             $message->from_profile = $this->scoped->getID();
174         } else {
175             $message->to_profile = $this->scoped->getID();
176         }
177
178         if (!empty($this->max_id)) {
179             $message->whereAdd('id <= ' . $this->max_id);
180         }
181
182         if (!empty($this->since_id)) {
183             $message->whereAdd('id > ' . $this->since_id);
184         }
185
186         $message->orderBy('created DESC, id DESC');
187         $message->limit((($this->page - 1) * $this->count), $this->count);
188         $message->find();
189
190         $messages = array();
191
192         while ($message->fetch()) {
193             $messages[] = clone($message);
194         }
195
196         return $messages;
197     }
198
199     /**
200      * Is this action read only?
201      *
202      * @param array $args other arguments
203      *
204      * @return boolean true
205      */
206     function isReadOnly(array $args=array())
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     function lastModified()
217     {
218         if (!empty($this->messages)) {
219             return strtotime($this->messages[0]->created);
220         }
221
222         return null;
223     }
224
225     // BEGIN import from lib/apiaction.php
226
227     function showSingleXmlDirectMessage($message)
228     {
229         $this->initDocument('xml');
230         $dmsg = $this->directMessageArray($message);
231         $this->showXmlDirectMessage($dmsg, true);
232         $this->endDocument('xml');
233     }
234
235     function showSingleJsonDirectMessage($message)
236     {
237         $this->initDocument('json');
238         $dmsg = $this->directMessageArray($message);
239         $this->showJsonObjects($dmsg);
240         $this->endDocument('json');
241     }
242
243     function showXmlDirectMessage($dm, $namespaces=false)
244     {
245         $attrs = array();
246         if ($namespaces) {
247             $attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
248         }
249         $this->elementStart('direct_message', $attrs);
250         foreach($dm as $element => $value) {
251             switch ($element) {
252             case 'sender':
253             case 'recipient':
254                 $this->showTwitterXmlUser($value, $element);
255                 break;
256             case 'text':
257                 $this->element($element, null, common_xml_safe_str($value));
258                 break;
259             default:
260                 $this->element($element, null, $value);
261                 break;
262             }
263         }
264         $this->elementEnd('direct_message');
265     }
266
267     function directMessageArray($message)
268     {
269         $dmsg = array();
270
271         $from_profile = $message->getFrom();
272         $to_profile = $message->getTo();
273
274         $dmsg['id'] = intval($message->id);
275         $dmsg['sender_id'] = intval($from_profile->id);
276         $dmsg['text'] = trim($message->content);
277         $dmsg['recipient_id'] = intval($to_profile->id);
278         $dmsg['created_at'] = $this->dateTwitter($message->created);
279         $dmsg['sender_screen_name'] = $from_profile->nickname;
280         $dmsg['recipient_screen_name'] = $to_profile->nickname;
281         $dmsg['sender'] = $this->twitterUserArray($from_profile, false);
282         $dmsg['recipient'] = $this->twitterUserArray($to_profile, false);
283
284         return $dmsg;
285     }
286
287     function rssDirectMessageArray($message)
288     {
289         $entry = array();
290
291         $from = $message->getFrom();
292
293         $entry['title'] = sprintf('Message from %1$s to %2$s',
294             $from->nickname, $message->getTo()->nickname);
295
296         $entry['content'] = common_xml_safe_str($message->rendered);
297         $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
298         $entry['published'] = common_date_iso8601($message->created);
299
300         $taguribase = TagURI::base();
301
302         $entry['id'] = "tag:$taguribase:$entry[link]";
303         $entry['updated'] = $entry['published'];
304
305         $entry['author-name'] = $from->getBestName();
306         $entry['author-uri'] = $from->homepage;
307
308         $entry['avatar'] = $from->avatarUrl(AVATAR_STREAM_SIZE);
309         try {
310             $avatar = $from->getAvatar(AVATAR_STREAM_SIZE);
311             $entry['avatar-type'] = $avatar->mediatype;
312         } catch (Exception $e) {
313             $entry['avatar-type'] = 'image/png';
314         }
315
316         // RSS item specific
317
318         $entry['description'] = $entry['content'];
319         $entry['pubDate'] = common_date_rfc2822($message->created);
320         $entry['guid'] = $entry['link'];
321
322         return $entry;
323     }
324
325     // END import from lib/apiaction.php
326
327     /**
328      * Shows a list of direct messages as Twitter-style XML array
329      *
330      * @return void
331      */
332     function showXmlDirectMessages()
333     {
334         $this->initDocument('xml');
335         $this->elementStart('direct-messages', array('type' => 'array',
336                                                      'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
337
338         foreach ($this->messages as $m) {
339             $dm_array = $this->directMessageArray($m);
340             $this->showXmlDirectMessage($dm_array);
341         }
342
343         $this->elementEnd('direct-messages');
344         $this->endDocument('xml');
345     }
346
347     /**
348      * Shows a list of direct messages as a JSON encoded array
349      *
350      * @return void
351      */
352     function showJsonDirectMessages()
353     {
354         $this->initDocument('json');
355
356         $dmsgs = array();
357
358         foreach ($this->messages as $m) {
359             $dm_array = $this->directMessageArray($m);
360             array_push($dmsgs, $dm_array);
361         }
362
363         $this->showJsonObjects($dmsgs);
364         $this->endDocument('json');
365     }
366
367     /**
368      * Shows a list of direct messages as RSS items
369      *
370      * @return void
371      */
372     function showRssDirectMessages()
373     {
374         $this->initDocument('rss');
375
376         $this->element('title', null, $this->title);
377
378         $this->element('link', null, $this->link);
379         $this->element('description', null, $this->subtitle);
380         $this->element('language', null, 'en-us');
381
382         $this->element(
383             'atom:link',
384             array(
385                 'type' => 'application/rss+xml',
386                 'href' => $this->selfuri_base . '.rss',
387                 'rel' => self
388                 ),
389             null
390         );
391         $this->element('ttl', null, '40');
392
393         foreach ($this->messages as $m) {
394             $entry = $this->rssDirectMessageArray($m);
395             $this->showTwitterRssItem($entry);
396         }
397
398         $this->endTwitterRss();
399     }
400
401     /**
402      * Shows a list of direct messages as Atom entries
403      *
404      * @return void
405      */
406     function showAtomDirectMessages()
407     {
408         $this->initDocument('atom');
409
410         $this->element('title', null, $this->title);
411         $this->element('id', null, $this->id);
412
413         $selfuri = common_root_url() . 'api/direct_messages.atom';
414
415         $this->element(
416             'link', array(
417             'href' => $this->link,
418             'rel' => 'alternate',
419             'type' => 'text/html'),
420             null
421         );
422         $this->element(
423             'link', array(
424             'href' => $this->selfuri_base . '.atom', 'rel' => 'self',
425             'type' => 'application/atom+xml'),
426             null
427         );
428         $this->element('updated', null, common_date_iso8601('now'));
429         $this->element('subtitle', null, $this->subtitle);
430
431         foreach ($this->messages as $m) {
432             $entry = $this->rssDirectMessageArray($m);
433             $this->showTwitterAtomEntry($entry);
434         }
435
436         $this->endDocument('atom');
437     }
438
439     /**
440      * An entity tag for this notice
441      *
442      * Returns an Etag based on the action name, language, and
443      * timestamps of the notice
444      *
445      * @return string etag
446      */
447     function etag()
448     {
449         if (!empty($this->messages)) {
450
451             $last = count($this->messages) - 1;
452
453             return '"' . implode(
454                 ':',
455                 array($this->arg('action'),
456                       common_user_cache_hash($this->auth_user),
457                       common_language(),
458                       strtotime($this->messages[0]->created),
459                       strtotime($this->messages[$last]->created)
460                 )
461             )
462             . '"';
463         }
464
465         return null;
466     }
467 }