]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapistatuses.php
Twitter now puts out an error msg when the status param is empty.
[quix0rs-gnu-social.git] / actions / twitapistatuses.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !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 home_timeline($args, $apidata)
140     {
141         call_user_func(array($this, 'friends_timeline'), $args, $apidata);
142     }
143
144     function user_timeline($args, $apidata)
145     {
146         parent::handle($args);
147
148         $this->auth_user = $apidata['user'];
149         $user = $this->get_user($apidata['api_arg'], $apidata);
150
151         if (empty($user)) {
152             $this->clientError('Not Found', 404, $apidata['content-type']);
153             return;
154         }
155
156         $profile = $user->getProfile();
157
158         $sitename   = common_config('site', 'name');
159         $title      = sprintf(_("%s timeline"), $user->nickname);
160         $taguribase = common_config('integration', 'taguri');
161         $id         = "tag:$taguribase:UserTimeline:".$user->id;
162         $link       = common_local_url('showstream',
163             array('nickname' => $user->nickname));
164         $subtitle   = sprintf(_('Updates from %1$s on %2$s!'),
165             $user->nickname, $sitename);
166
167         # FriendFeed's SUP protocol
168         # Also added RSS and Atom feeds
169
170         $suplink = common_local_url('sup', null, null, $user->id);
171         header('X-SUP-ID: '.$suplink);
172
173         $page     = (int)$this->arg('page', 1);
174         $count    = (int)$this->arg('count', 20);
175         $max_id   = (int)$this->arg('max_id', 0);
176         $since_id = (int)$this->arg('since_id', 0);
177         $since    = $this->arg('since');
178
179         $notice = $user->getNotices(($page-1)*$count,
180             $count, $since_id, $max_id, $since);
181
182         switch($apidata['content-type']) {
183          case 'xml':
184             $this->show_xml_timeline($notice);
185             break;
186          case 'rss':
187             $this->show_rss_timeline($notice, $title, $link,
188                 $subtitle, $suplink);
189             break;
190          case 'atom':
191             if (isset($apidata['api_arg'])) {
192                 $selfuri = common_root_url() .
193                     'api/statuses/user_timeline/' .
194                         $apidata['api_arg'] . '.atom';
195             } else {
196                 $selfuri = common_root_url() .
197                  'api/statuses/user_timeline.atom';
198             }
199             $this->show_atom_timeline($notice, $title, $id, $link,
200                 $subtitle, $suplink, $selfuri);
201             break;
202          case 'json':
203             $this->show_json_timeline($notice);
204             break;
205          default:
206             $this->clientError(_('API method not found!'), $code = 404);
207         }
208
209     }
210
211     function update($args, $apidata)
212     {
213         parent::handle($args);
214
215         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
216             $this->clientError(_('API method not found!'), $code = 404);
217             return;
218         }
219
220         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
221             $this->clientError(_('This method requires a POST.'),
222                 400, $apidata['content-type']);
223             return;
224         }
225
226         $user = $apidata['user'];  // Always the auth user
227
228         $status = $this->trimmed('status');
229         $source = $this->trimmed('source');
230         $in_reply_to_status_id =
231             intval($this->trimmed('in_reply_to_status_id'));
232         $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
233
234         if (empty($source) || in_array($source, $reserved_sources)) {
235             $source = 'api';
236         }
237
238         if (empty($status)) {
239             $this->clientError(_('Client must provide a \'status\' parameter with a value.'),
240                 $code = 403, $apidata['content-type']);
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, 500, $apidata['content-type']);
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         // 'id' is an undocumented parameter in Twitter's API. Several
379         // clients make use of it, so we support it too.
380
381         // show.json?id=12345 takes precedence over /show/12345.json
382
383         $this->auth_user = $apidata['user'];
384         $notice_id       = $this->trimmed('id');
385
386         if (empty($notice_id)) {
387             $notice_id   = $apidata['api_arg'];
388         }
389
390         $notice          = Notice::staticGet((int)$notice_id);
391
392         if ($notice) {
393             if ($apidata['content-type'] == 'xml') {
394                 $this->show_single_xml_status($notice);
395             } elseif ($apidata['content-type'] == 'json') {
396                 $this->show_single_json_status($notice);
397             }
398         } else {
399             // XXX: Twitter just sets a 404 header and doens't bother
400             // to return an err msg
401             $this->clientError(_('No status with that ID found.'),
402                 404, $apidata['content-type']);
403         }
404     }
405
406     function destroy($args, $apidata)
407     {
408         parent::handle($args);
409
410         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
411             $this->clientError(_('API method not found!'), $code = 404);
412             return;
413         }
414
415         // Check for RESTfulness
416         if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
417             // XXX: Twitter just prints the err msg, no XML / JSON.
418             $this->clientError(_('This method requires a POST or DELETE.'),
419                 400, $apidata['content-type']);
420             return;
421         }
422
423         $user      = $apidata['user']; // Always the auth user
424         $notice_id = $apidata['api_arg'];
425         $notice    = Notice::staticGet($notice_id);
426
427         if (empty($notice)) {
428             $this->clientError(_('No status found with that ID.'),
429                 404, $apidata['content-type']);
430             return;
431         }
432
433         if ($user->id == $notice->profile_id) {
434             $replies = new Reply;
435             $replies->get('notice_id', $notice_id);
436             $replies->delete();
437             $notice->delete();
438
439             if ($apidata['content-type'] == 'xml') {
440                 $this->show_single_xml_status($notice);
441             } elseif ($apidata['content-type'] == 'json') {
442                 $this->show_single_json_status($notice);
443             }
444         } else {
445             $this->clientError(_('You may not delete another user\'s status.'),
446                 403, $apidata['content-type']);
447         }
448
449     }
450
451     function friends($args, $apidata)
452     {
453         parent::handle($args);
454         $includeStatuses= !(array_key_exists('lite', $args) and $args['lite']);
455         return $this->subscriptions($apidata, 'subscribed', 'subscriber', false, $includeStatuses);
456     }
457
458     function friendsIDs($args, $apidata)
459     {
460         parent::handle($args);
461         return $this->subscriptions($apidata, 'subscribed', 'subscriber', true);
462     }
463
464     function followers($args, $apidata)
465     {
466         parent::handle($args);
467         $includeStatuses= !(array_key_exists('lite', $args) and $args['lite']);
468         return $this->subscriptions($apidata, 'subscriber', 'subscribed', false, $includeStatuses);
469     }
470
471     function followersIDs($args, $apidata)
472     {
473         parent::handle($args);
474         return $this->subscriptions($apidata, 'subscriber', 'subscribed', true);
475     }
476
477     function subscriptions($apidata, $other_attr, $user_attr, $onlyIDs=false, $includeStatuses=true)
478     {
479         $this->auth_user = $apidata['user'];
480         $user = $this->get_user($apidata['api_arg'], $apidata);
481
482         if (empty($user)) {
483             $this->clientError('Not Found', 404, $apidata['content-type']);
484             return;
485         }
486
487         $profile = $user->getProfile();
488
489         $sub = new Subscription();
490         $sub->$user_attr = $profile->id;
491
492         $sub->orderBy('created DESC');
493
494         // Normally, page 100 friends at a time
495
496         if (!$onlyIDs) {
497             $page  = $this->arg('page', 1);
498             $count = $this->arg('count', 100);
499             $sub->limit(($page-1)*$count, $count);
500         } else {
501
502             // If we're just looking at IDs, return
503             // ALL of them, unless the user specifies a page,
504             // in which case, return 500 per page.
505
506             $page = $this->arg('page');
507             if (!empty($page)) {
508                 if ($page < 1) {
509                     $page = 1;
510                 }
511                 $count = 500;
512                 $sub->limit(($page-1)*$count, $count);
513             }
514         }
515
516         $others = array();
517
518         if ($sub->find()) {
519             while ($sub->fetch()) {
520                 $others[] = Profile::staticGet($sub->$other_attr);
521             }
522         } else {
523             // user has no followers
524         }
525
526         $type = $apidata['content-type'];
527
528         $this->init_document($type);
529
530         if ($onlyIDs) {
531             $this->showIDs($others, $type);
532         } else {
533             $this->show_profiles($others, $type, $includeStatuses);
534         }
535
536         $this->end_document($type);
537     }
538
539     function show_profiles($profiles, $type, $includeStatuses)
540     {
541         switch ($type) {
542         case 'xml':
543             $this->elementStart('users', array('type' => 'array'));
544             foreach ($profiles as $profile) {
545                 $this->show_profile($profile,$type,null,$includeStatuses);
546             }
547             $this->elementEnd('users');
548             break;
549         case 'json':
550             $arrays = array();
551             foreach ($profiles as $profile) {
552                 $arrays[] = $this->twitter_user_array($profile, $includeStatuses);
553             }
554             print json_encode($arrays);
555             break;
556         default:
557             $this->clientError(_('unsupported file type'));
558         }
559     }
560
561     function showIDs($profiles, $type)
562     {
563         switch ($type) {
564         case 'xml':
565             $this->elementStart('ids');
566             foreach ($profiles as $profile) {
567                 $this->element('id', null, $profile->id);
568             }
569             $this->elementEnd('ids');
570             break;
571         case 'json':
572             $ids = array();
573             foreach ($profiles as $profile) {
574                 $ids[] = (int)$profile->id;
575             }
576             print json_encode($ids);
577             break;
578         default:
579             $this->clientError(_('unsupported file type'));
580         }
581     }
582
583     function featured($args, $apidata)
584     {
585         parent::handle($args);
586         $this->serverError(_('API method under construction.'), $code=501);
587     }
588
589     function supported($cmd)
590     {
591         $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand',
592             'FavCommand', 'OnCommand', 'OffCommand');
593
594         if (in_array(get_class($cmd), $cmdlist)) {
595             return true;
596         }
597
598         return false;
599     }
600
601 }