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