]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapistatuses.php
185129d5eee91f2634a8d613f29da8f80066ad70
[quix0rs-gnu-social.git] / actions / twitapistatuses.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, Control Yourself, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) {
21     exit(1);
22 }
23
24 require_once(INSTALLDIR.'/lib/twitterapi.php');
25
26 class TwitapistatusesAction extends TwitterapiAction
27 {
28
29     function public_timeline($args, $apidata)
30     {
31         // XXX: To really live up to the spec we need to build a list
32         // of notices by users who have custom avatars, so fix this SQL -- Zach
33
34         parent::handle($args);
35
36         $sitename   = common_config('site', 'name');
37         $title      = sprintf(_("%s public timeline"), $sitename);
38         $taguribase = common_config('integration', 'taguri');
39         $id         = "tag:$taguribase:PublicTimeline";
40         $link       = common_root_url();
41         $subtitle   = sprintf(_("%s updates from everyone!"), $sitename);
42
43         $page     = (int)$this->arg('page', 1);
44         $count    = (int)$this->arg('count', 20);
45         $max_id   = (int)$this->arg('max_id', 0);
46         $since_id = (int)$this->arg('since_id', 0);
47         $since    = $this->arg('since');
48
49         $notice = Notice::publicStream(($page-1)*$count, $count, $since_id,
50             $max_id, $since);
51
52         switch($apidata['content-type']) {
53         case 'xml':
54             $this->show_xml_timeline($notice);
55             break;
56         case 'rss':
57             $this->show_rss_timeline($notice, $title, $link, $subtitle);
58             break;
59         case 'atom':
60             $selfuri = common_root_url() . 'api/statuses/public_timeline.atom';
61             $this->show_atom_timeline($notice, $title, $id, $link,
62                 $subtitle, null, $selfuri);
63             break;
64         case 'json':
65             $this->show_json_timeline($notice);
66             break;
67         default:
68             $this->clientError(_('API method not found!'), $code = 404);
69             break;
70         }
71
72     }
73
74     function friends_timeline($args, $apidata)
75     {
76         parent::handle($args);
77
78         $this->auth_user = $apidata['user'];
79         $user = $this->get_user($apidata['api_arg'], $apidata);
80
81         if (empty($user)) {
82              $this->clientError(_('No such user!'), 404,
83              $apidata['content-type']);
84             return;
85         }
86
87         $profile    = $user->getProfile();
88         $sitename   = common_config('site', 'name');
89         $title      = sprintf(_("%s and friends"), $user->nickname);
90         $taguribase = common_config('integration', 'taguri');
91         $id         = "tag:$taguribase:FriendsTimeline:" . $user->id;
92         $link       = common_local_url('all',
93             array('nickname' => $user->nickname));
94         $subtitle   = sprintf(_('Updates from %1$s and friends on %2$s!'),
95             $user->nickname, $sitename);
96
97         $page     = (int)$this->arg('page', 1);
98         $count    = (int)$this->arg('count', 20);
99         $max_id   = (int)$this->arg('max_id', 0);
100         $since_id = (int)$this->arg('since_id', 0);
101         $since    = $this->arg('since');
102
103         if (!empty($this->auth_user) && $this->auth_user->id == $user->id) {
104             $notice = $user->noticeInbox(($page-1)*$count,
105                 $count, $since_id, $max_id, $since);
106         } else {
107             $notice = $user->noticesWithFriends(($page-1)*$count,
108                 $count, $since_id, $max_id, $since);
109         }
110
111         switch($apidata['content-type']) {
112         case 'xml':
113             $this->show_xml_timeline($notice);
114             break;
115         case 'rss':
116             $this->show_rss_timeline($notice, $title, $link, $subtitle);
117             break;
118         case 'atom':
119             if (isset($apidata['api_arg'])) {
120                 $selfuri = common_root_url() .
121                     'api/statuses/friends_timeline/' .
122                         $apidata['api_arg'] . '.atom';
123             } else {
124                 $selfuri = common_root_url() .
125                     'api/statuses/friends_timeline.atom';
126             }
127             $this->show_atom_timeline($notice, $title, $id, $link,
128                 $subtitle, null, $selfuri);
129             break;
130         case 'json':
131             $this->show_json_timeline($notice);
132             break;
133         default:
134             $this->clientError(_('API method not found!'), $code = 404);
135         }
136
137     }
138
139     function user_timeline($args, $apidata)
140     {
141         parent::handle($args);
142
143         $this->auth_user = $apidata['user'];
144         $user = $this->get_user($apidata['api_arg'], $apidata);
145
146         if (empty($user)) {
147             $this->clientError('Not Found', 404, $apidata['content-type']);
148             return;
149         }
150
151         $profile = $user->getProfile();
152
153         $sitename   = common_config('site', 'name');
154         $title      = sprintf(_("%s timeline"), $user->nickname);
155         $taguribase = common_config('integration', 'taguri');
156         $id         = "tag:$taguribase:UserTimeline:".$user->id;
157         $link       = common_local_url('showstream',
158             array('nickname' => $user->nickname));
159         $subtitle   = sprintf(_('Updates from %1$s on %2$s!'),
160             $user->nickname, $sitename);
161
162         # FriendFeed's SUP protocol
163         # Also added RSS and Atom feeds
164
165         $suplink = common_local_url('sup', null, null, $user->id);
166         header('X-SUP-ID: '.$suplink);
167
168         $page     = (int)$this->arg('page', 1);
169         $count    = (int)$this->arg('count', 20);
170         $max_id   = (int)$this->arg('max_id', 0);
171         $since_id = (int)$this->arg('since_id', 0);
172         $since    = $this->arg('since');
173
174         $notice = $user->getNotices(($page-1)*$count,
175             $count, $since_id, $max_id, $since);
176
177         switch($apidata['content-type']) {
178          case 'xml':
179             $this->show_xml_timeline($notice);
180             break;
181          case 'rss':
182             $this->show_rss_timeline($notice, $title, $link,
183                 $subtitle, $suplink);
184             break;
185          case 'atom':
186             if (isset($apidata['api_arg'])) {
187                 $selfuri = common_root_url() .
188                     'api/statuses/user_timeline/' .
189                         $apidata['api_arg'] . '.atom';
190             } else {
191                 $selfuri = common_root_url() .
192                  'api/statuses/user_timeline.atom';
193             }
194             $this->show_atom_timeline($notice, $title, $id, $link,
195                 $subtitle, $suplink, $selfuri);
196             break;
197          case 'json':
198             $this->show_json_timeline($notice);
199             break;
200          default:
201             $this->clientError(_('API method not found!'), $code = 404);
202         }
203
204     }
205
206     function update($args, $apidata)
207     {
208         parent::handle($args);
209
210         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
211             $this->clientError(_('API method not found!'), $code = 404);
212             return;
213         }
214
215         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
216             $this->clientError(_('This method requires a POST.'),
217                 400, $apidata['content-type']);
218             return;
219         }
220
221         $user = $apidata['user'];  // Always the auth user
222
223         $status = $this->trimmed('status');
224         $source = $this->trimmed('source');
225         $in_reply_to_status_id =
226             intval($this->trimmed('in_reply_to_status_id'));
227         $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
228
229         if (empty($source) || in_array($source, $reserved_sources)) {
230             $source = 'api';
231         }
232
233         if (empty($status)) {
234
235             // XXX: Note: In this case, Twitter simply returns '200 OK'
236             // No error is given, but the status is not posted to the
237             // user's timeline.     Seems bad.     Shouldn't we throw an
238             // errror? -- Zach
239             return;
240
241         } else {
242
243             $status_shortened = common_shorten_links($status);
244
245             if (mb_strlen($status_shortened) > 140) {
246
247                 // XXX: Twitter truncates anything over 140, flags the status
248                 // as "truncated." Sending this error may screw up some clients
249                 // that assume Twitter will truncate for them.    Should we just
250                 // truncate too? -- Zach
251                 $this->clientError(_('That\'s too long. Max notice size is 140 chars.'),
252                     $code = 406, $apidata['content-type']);
253                 return;
254             }
255         }
256
257         // Check for commands
258         $inter = new CommandInterpreter();
259         $cmd = $inter->handle_command($user, $status_shortened);
260
261         if ($cmd) {
262
263             if ($this->supported($cmd)) {
264                 $cmd->execute(new Channel());
265             }
266
267             // cmd not supported?  Twitter just returns your latest status.
268             // And, it returns your last status whether the cmd was successful
269             // or not!
270             $n = $user->getCurrentNotice();
271             $apidata['api_arg'] = $n->id;
272         } else {
273
274             $reply_to = null;
275
276             if ($in_reply_to_status_id) {
277
278                 // check whether notice actually exists
279                 $reply = Notice::staticGet($in_reply_to_status_id);
280
281                 if ($reply) {
282                     $reply_to = $in_reply_to_status_id;
283                 } else {
284                     $this->clientError(_('Not found'), $code = 404,
285                         $apidata['content-type']);
286                     return;
287                 }
288             }
289
290             $notice = Notice::saveNew($user->id,
291                 html_entity_decode($status, ENT_NOQUOTES, 'UTF-8'),
292                     $source, 1, $reply_to);
293
294             if (is_string($notice)) {
295                 $this->serverError($notice);
296                 return;
297             }
298
299             common_broadcast_notice($notice);
300             $apidata['api_arg'] = $notice->id;
301         }
302
303         $this->show($args, $apidata);
304     }
305
306     function mentions($args, $apidata)
307     {
308         parent::handle($args);
309
310         $user = $this->get_user($apidata['api_arg'], $apidata);
311         $this->auth_user = $apidata['user'];
312
313         if (empty($user)) {
314              $this->clientError(_('No such user!'), 404,
315                  $apidata['content-type']);
316             return;
317         }
318
319         $profile = $user->getProfile();
320
321         $sitename   = common_config('site', 'name');
322         $title      = sprintf(_('%1$s / Updates mentioning %2$s'),
323             $sitename, $user->nickname);
324         $taguribase = common_config('integration', 'taguri');
325         $id         = "tag:$taguribase:Mentions:".$user->id;
326         $link       = common_local_url('replies',
327             array('nickname' => $user->nickname));
328         $subtitle   = sprintf(_('%1$s updates that reply to updates from %2$s / %3$s.'),
329             $sitename, $user->nickname, $profile->getBestName());
330
331         $page     = (int)$this->arg('page', 1);
332         $count    = (int)$this->arg('count', 20);
333         $max_id   = (int)$this->arg('max_id', 0);
334         $since_id = (int)$this->arg('since_id', 0);
335         $since    = $this->arg('since');
336
337         $notice = $user->getReplies(($page-1)*$count,
338             $count, $since_id, $max_id, $since);
339
340         switch($apidata['content-type']) {
341         case 'xml':
342             $this->show_xml_timeline($notice);
343             break;
344         case 'rss':
345             $this->show_rss_timeline($notice, $title, $link, $subtitle);
346             break;
347         case 'atom':
348             $selfuri = common_root_url() .
349                 ltrim($_SERVER['QUERY_STRING'], 'p=');
350             $this->show_atom_timeline($notice, $title, $id, $link, $subtitle,
351                 null, $selfuri);
352             break;
353         case 'json':
354             $this->show_json_timeline($notice);
355             break;
356         default:
357             $this->clientError(_('API method not found!'), $code = 404);
358         }
359
360     }
361
362     function replies($args, $apidata)
363     {
364         call_user_func(array($this, 'mentions'), $args, $apidata);
365     }
366
367     function show($args, $apidata)
368     {
369         parent::handle($args);
370
371         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
372             $this->clientError(_('API method not found!'), $code = 404);
373             return;
374         }
375
376         // 'id' is an undocumented parameter in Twitter's API. Several
377         // clients make use of it, so we support it too.
378
379         // show.json?id=12345 takes precedence over /show/12345.json
380
381         $this->auth_user = $apidata['user'];
382         $notice_id       = $this->trimmed('id');
383
384         if (empty($notice_id)) {
385             $notice_id   = $apidata['api_arg'];
386         }
387
388         $notice          = Notice::staticGet((int)$notice_id);
389
390         if ($notice) {
391             if ($apidata['content-type'] == 'xml') {
392                 $this->show_single_xml_status($notice);
393             } elseif ($apidata['content-type'] == 'json') {
394                 $this->show_single_json_status($notice);
395             }
396         } else {
397             // XXX: Twitter just sets a 404 header and doens't bother
398             // to return an err msg
399             $this->clientError(_('No status with that ID found.'),
400                 404, $apidata['content-type']);
401         }
402     }
403
404     function destroy($args, $apidata)
405     {
406         parent::handle($args);
407
408         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
409             $this->clientError(_('API method not found!'), $code = 404);
410             return;
411         }
412
413         // Check for RESTfulness
414         if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
415             // XXX: Twitter just prints the err msg, no XML / JSON.
416             $this->clientError(_('This method requires a POST or DELETE.'),
417                 400, $apidata['content-type']);
418             return;
419         }
420
421         $user      = $apidata['user']; // Always the auth user
422         $notice_id = $apidata['api_arg'];
423         $notice    = Notice::staticGet($notice_id);
424
425         if (empty($notice)) {
426             $this->clientError(_('No status found with that ID.'),
427                 404, $apidata['content-type']);
428             return;
429         }
430
431         if ($user->id == $notice->profile_id) {
432             $replies = new Reply;
433             $replies->get('notice_id', $notice_id);
434             $replies->delete();
435             $notice->delete();
436
437             if ($apidata['content-type'] == 'xml') {
438                 $this->show_single_xml_status($notice);
439             } elseif ($apidata['content-type'] == 'json') {
440                 $this->show_single_json_status($notice);
441             }
442         } else {
443             $this->clientError(_('You may not delete another user\'s status.'),
444                 403, $apidata['content-type']);
445         }
446
447     }
448
449     function friends($args, $apidata)
450     {
451         parent::handle($args);
452         $includeStatuses=! (boolean) $args['lite'];
453         return $this->subscriptions($apidata, 'subscribed', 'subscriber', false, $includeStatuses);
454     }
455
456     function friendsIDs($args, $apidata)
457     {
458         parent::handle($args);
459         return $this->subscriptions($apidata, 'subscribed', 'subscriber', true);
460     }
461
462     function followers($args, $apidata)
463     {
464         parent::handle($args);
465         $includeStatuses=! (boolean) $args['lite'];
466         return $this->subscriptions($apidata, 'subscriber', 'subscribed', false, $includeStatuses);
467     }
468
469     function followersIDs($args, $apidata)
470     {
471         parent::handle($args);
472         return $this->subscriptions($apidata, 'subscriber', 'subscribed', true);
473     }
474
475     function subscriptions($apidata, $other_attr, $user_attr, $onlyIDs=false, $includeStatuses=true)
476     {
477         $this->auth_user = $apidata['user'];
478         $user = $this->get_user($apidata['api_arg'], $apidata);
479
480         if (empty($user)) {
481             $this->clientError('Not Found', 404, $apidata['content-type']);
482             return;
483         }
484
485         $profile = $user->getProfile();
486
487         $sub = new Subscription();
488         $sub->$user_attr = $profile->id;
489
490         $sub->orderBy('created DESC');
491
492         // Normally, page 100 friends at a time
493
494         if (!$onlyIDs) {
495             $page  = $this->arg('page', 1);
496             $count = $this->arg('count', 100);
497             $sub->limit(($page-1)*$count, $count);
498         } else {
499
500             // If we're just looking at IDs, return
501             // ALL of them, unless the user specifies a page,
502             // in which case, return 500 per page.
503
504             $page = $this->arg('page');
505             if (!empty($page)) {
506                 if ($page < 1) {
507                     $page = 1;
508                 }
509                 $count = 500;
510                 $sub->limit(($page-1)*$count, $count);
511             }
512         }
513
514         $others = array();
515
516         if ($sub->find()) {
517             while ($sub->fetch()) {
518                 $others[] = Profile::staticGet($sub->$other_attr);
519             }
520         } else {
521             // user has no followers
522         }
523
524         $type = $apidata['content-type'];
525
526         $this->init_document($type);
527
528         if ($onlyIDs) {
529             $this->showIDs($others, $type);
530         } else {
531             $this->show_profiles($others, $type, $includeStatuses);
532         }
533
534         $this->end_document($type);
535     }
536
537     function show_profiles($profiles, $type, $includeStatuses)
538     {
539         switch ($type) {
540         case 'xml':
541             $this->elementStart('users', array('type' => 'array'));
542             foreach ($profiles as $profile) {
543                 $this->show_profile($profile,$type,null,$includeStatuses);
544             }
545             $this->elementEnd('users');
546             break;
547         case 'json':
548             $arrays = array();
549             foreach ($profiles as $profile) {
550                 $arrays[] = $this->twitter_user_array($profile, $includeStatuses);
551             }
552             print json_encode($arrays);
553             break;
554         default:
555             $this->clientError(_('unsupported file type'));
556         }
557     }
558
559     function showIDs($profiles, $type)
560     {
561         switch ($type) {
562         case 'xml':
563             $this->elementStart('ids');
564             foreach ($profiles as $profile) {
565                 $this->element('id', null, $profile->id);
566             }
567             $this->elementEnd('ids');
568             break;
569         case 'json':
570             $ids = array();
571             foreach ($profiles as $profile) {
572                 $ids[] = (int)$profile->id;
573             }
574             print json_encode($ids);
575             break;
576         default:
577             $this->clientError(_('unsupported file type'));
578         }
579     }
580
581     function featured($args, $apidata)
582     {
583         parent::handle($args);
584         $this->serverError(_('API method under construction.'), $code=501);
585     }
586
587     function supported($cmd)
588     {
589         $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand',
590             'FavCommand', 'OnCommand', 'OffCommand');
591
592         if (in_array(get_class($cmd), $cmdlist)) {
593             return true;
594         }
595
596         return false;
597     }
598
599 }