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