]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapistatuses.php
remove string-checks from code using Notice::saveNew()
[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
240             // XXX: Note: In this case, Twitter simply returns '200 OK'
241             // No error is given, but the status is not posted to the
242             // user's timeline.     Seems bad.     Shouldn't we throw an
243             // errror? -- Zach
244             return;
245
246         } else {
247
248             $status_shortened = common_shorten_links($status);
249
250             if (Notice::contentTooLong($status_shortened)) {
251
252                 // XXX: Twitter truncates anything over 140, flags the status
253                 // as "truncated." Sending this error may screw up some clients
254                 // that assume Twitter will truncate for them.    Should we just
255                 // truncate too? -- Zach
256                 $this->clientError(sprintf(_('That\'s too long. Max notice size is %d chars.'),
257                                            Notice::maxContent()),
258                                    $code = 406, $apidata['content-type']);
259                 return;
260             }
261         }
262
263         // Check for commands
264         $inter = new CommandInterpreter();
265         $cmd = $inter->handle_command($user, $status_shortened);
266
267         if ($cmd) {
268
269             if ($this->supported($cmd)) {
270                 $cmd->execute(new Channel());
271             }
272
273             // cmd not supported?  Twitter just returns your latest status.
274             // And, it returns your last status whether the cmd was successful
275             // or not!
276             $n = $user->getCurrentNotice();
277             $apidata['api_arg'] = $n->id;
278         } else {
279
280             $reply_to = null;
281
282             if ($in_reply_to_status_id) {
283
284                 // check whether notice actually exists
285                 $reply = Notice::staticGet($in_reply_to_status_id);
286
287                 if ($reply) {
288                     $reply_to = $in_reply_to_status_id;
289                 } else {
290                     $this->clientError(_('Not found'), $code = 404,
291                         $apidata['content-type']);
292                     return;
293                 }
294             }
295
296             $notice = Notice::saveNew($user->id,
297                 html_entity_decode($status, ENT_NOQUOTES, 'UTF-8'),
298                     $source, 1, $reply_to);
299
300             common_broadcast_notice($notice);
301             $apidata['api_arg'] = $notice->id;
302         }
303
304         $this->show($args, $apidata);
305     }
306
307     function mentions($args, $apidata)
308     {
309         parent::handle($args);
310
311         $user = $this->get_user($apidata['api_arg'], $apidata);
312         $this->auth_user = $apidata['user'];
313
314         if (empty($user)) {
315              $this->clientError(_('No such user!'), 404,
316                  $apidata['content-type']);
317             return;
318         }
319
320         $profile = $user->getProfile();
321
322         $sitename   = common_config('site', 'name');
323         $title      = sprintf(_('%1$s / Updates mentioning %2$s'),
324             $sitename, $user->nickname);
325         $taguribase = common_config('integration', 'taguri');
326         $id         = "tag:$taguribase:Mentions:".$user->id;
327         $link       = common_local_url('replies',
328             array('nickname' => $user->nickname));
329         $subtitle   = sprintf(_('%1$s updates that reply to updates from %2$s / %3$s.'),
330             $sitename, $user->nickname, $profile->getBestName());
331
332         $page     = (int)$this->arg('page', 1);
333         $count    = (int)$this->arg('count', 20);
334         $max_id   = (int)$this->arg('max_id', 0);
335         $since_id = (int)$this->arg('since_id', 0);
336         $since    = $this->arg('since');
337
338         $notice = $user->getReplies(($page-1)*$count,
339             $count, $since_id, $max_id, $since);
340
341         switch($apidata['content-type']) {
342         case 'xml':
343             $this->show_xml_timeline($notice);
344             break;
345         case 'rss':
346             $this->show_rss_timeline($notice, $title, $link, $subtitle);
347             break;
348         case 'atom':
349             $selfuri = common_root_url() .
350                 ltrim($_SERVER['QUERY_STRING'], 'p=');
351             $this->show_atom_timeline($notice, $title, $id, $link, $subtitle,
352                 null, $selfuri);
353             break;
354         case 'json':
355             $this->show_json_timeline($notice);
356             break;
357         default:
358             $this->clientError(_('API method not found!'), $code = 404);
359         }
360
361     }
362
363     function replies($args, $apidata)
364     {
365         call_user_func(array($this, 'mentions'), $args, $apidata);
366     }
367
368     function show($args, $apidata)
369     {
370         parent::handle($args);
371
372         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
373             $this->clientError(_('API method not found!'), $code = 404);
374             return;
375         }
376
377         // 'id' is an undocumented parameter in Twitter's API. Several
378         // clients make use of it, so we support it too.
379
380         // show.json?id=12345 takes precedence over /show/12345.json
381
382         $this->auth_user = $apidata['user'];
383         $notice_id       = $this->trimmed('id');
384
385         if (empty($notice_id)) {
386             $notice_id   = $apidata['api_arg'];
387         }
388
389         $notice          = Notice::staticGet((int)$notice_id);
390
391         if ($notice) {
392             if ($apidata['content-type'] == 'xml') {
393                 $this->show_single_xml_status($notice);
394             } elseif ($apidata['content-type'] == 'json') {
395                 $this->show_single_json_status($notice);
396             }
397         } else {
398             // XXX: Twitter just sets a 404 header and doens't bother
399             // to return an err msg
400             $deleted = Deleted_notice::staticGet($notice_id);
401             if (!empty($deleted)) {
402                 $this->clientError(_('Status deleted.'),
403                                    410, $apidata['content-type']);
404             } else {
405                 $this->clientError(_('No status with that ID found.'),
406                                    404, $apidata['content-type']);
407             }
408         }
409     }
410
411     function destroy($args, $apidata)
412     {
413         parent::handle($args);
414
415         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
416             $this->clientError(_('API method not found!'), $code = 404);
417             return;
418         }
419
420         // Check for RESTfulness
421         if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
422             // XXX: Twitter just prints the err msg, no XML / JSON.
423             $this->clientError(_('This method requires a POST or DELETE.'),
424                 400, $apidata['content-type']);
425             return;
426         }
427
428         $user      = $apidata['user']; // Always the auth user
429         $notice_id = $apidata['api_arg'];
430         $notice    = Notice::staticGet($notice_id);
431
432         if (empty($notice)) {
433             $this->clientError(_('No status found with that ID.'),
434                 404, $apidata['content-type']);
435             return;
436         }
437
438         if ($user->id == $notice->profile_id) {
439             $replies = new Reply;
440             $replies->get('notice_id', $notice_id);
441             $replies->delete();
442             $notice->delete();
443
444             if ($apidata['content-type'] == 'xml') {
445                 $this->show_single_xml_status($notice);
446             } elseif ($apidata['content-type'] == 'json') {
447                 $this->show_single_json_status($notice);
448             }
449         } else {
450             $this->clientError(_('You may not delete another user\'s status.'),
451                 403, $apidata['content-type']);
452         }
453
454     }
455
456     function friends($args, $apidata)
457     {
458         parent::handle($args);
459         $includeStatuses=! (boolean) $args['lite'];
460         return $this->subscriptions($apidata, 'subscribed', 'subscriber', false, $includeStatuses);
461     }
462
463     function friendsIDs($args, $apidata)
464     {
465         parent::handle($args);
466         return $this->subscriptions($apidata, 'subscribed', 'subscriber', true);
467     }
468
469     function followers($args, $apidata)
470     {
471         parent::handle($args);
472         $includeStatuses=! (boolean) $args['lite'];
473         return $this->subscriptions($apidata, 'subscriber', 'subscribed', false, $includeStatuses);
474     }
475
476     function followersIDs($args, $apidata)
477     {
478         parent::handle($args);
479         return $this->subscriptions($apidata, 'subscriber', 'subscribed', true);
480     }
481
482     function subscriptions($apidata, $other_attr, $user_attr, $onlyIDs=false, $includeStatuses=true)
483     {
484         $this->auth_user = $apidata['user'];
485         $user = $this->get_user($apidata['api_arg'], $apidata);
486
487         if (empty($user)) {
488             $this->clientError('Not Found', 404, $apidata['content-type']);
489             return;
490         }
491
492         $profile = $user->getProfile();
493
494         $sub = new Subscription();
495         $sub->$user_attr = $profile->id;
496
497         $sub->orderBy('created DESC');
498
499         // Normally, page 100 friends at a time
500
501         if (!$onlyIDs) {
502             $page  = $this->arg('page', 1);
503             $count = $this->arg('count', 100);
504             $sub->limit(($page-1)*$count, $count);
505         } else {
506
507             // If we're just looking at IDs, return
508             // ALL of them, unless the user specifies a page,
509             // in which case, return 500 per page.
510
511             $page = $this->arg('page');
512             if (!empty($page)) {
513                 if ($page < 1) {
514                     $page = 1;
515                 }
516                 $count = 500;
517                 $sub->limit(($page-1)*$count, $count);
518             }
519         }
520
521         $others = array();
522
523         if ($sub->find()) {
524             while ($sub->fetch()) {
525                 $others[] = Profile::staticGet($sub->$other_attr);
526             }
527         } else {
528             // user has no followers
529         }
530
531         $type = $apidata['content-type'];
532
533         $this->init_document($type);
534
535         if ($onlyIDs) {
536             $this->showIDs($others, $type);
537         } else {
538             $this->show_profiles($others, $type, $includeStatuses);
539         }
540
541         $this->end_document($type);
542     }
543
544     function show_profiles($profiles, $type, $includeStatuses)
545     {
546         switch ($type) {
547         case 'xml':
548             $this->elementStart('users', array('type' => 'array'));
549             foreach ($profiles as $profile) {
550                 $this->show_profile($profile,$type,null,$includeStatuses);
551             }
552             $this->elementEnd('users');
553             break;
554         case 'json':
555             $arrays = array();
556             foreach ($profiles as $profile) {
557                 $arrays[] = $this->twitter_user_array($profile, $includeStatuses);
558             }
559             print json_encode($arrays);
560             break;
561         default:
562             $this->clientError(_('unsupported file type'));
563         }
564     }
565
566     function showIDs($profiles, $type)
567     {
568         switch ($type) {
569         case 'xml':
570             $this->elementStart('ids');
571             foreach ($profiles as $profile) {
572                 $this->element('id', null, $profile->id);
573             }
574             $this->elementEnd('ids');
575             break;
576         case 'json':
577             $ids = array();
578             foreach ($profiles as $profile) {
579                 $ids[] = (int)$profile->id;
580             }
581             print json_encode($ids);
582             break;
583         default:
584             $this->clientError(_('unsupported file type'));
585         }
586     }
587
588     function featured($args, $apidata)
589     {
590         parent::handle($args);
591         $this->serverError(_('API method under construction.'), $code=501);
592     }
593
594     function supported($cmd)
595     {
596         $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand',
597             'FavCommand', 'OnCommand', 'OffCommand');
598
599         if (in_array(get_class($cmd), $cmdlist)) {
600             return true;
601         }
602
603         return false;
604     }
605
606 }