]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapistatuses.php
Added Snapshot::check() to main function for index.php
[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 = $selfuri = common_root_url() .
148                     'api/statuses/friends_timeline/' . $apidata['api_arg'] . '.atom';
149             } else {
150                 $selfuri = $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 = $selfuri = common_root_url() .
235                     'api/statuses/user_timeline/' . $apidata['api_arg'] . '.atom';
236             } else {
237                 $selfuri = $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 replies($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 replying to %2$s'), $sitename, $user->nickname);
364         $taguribase = common_config('integration', 'taguri');
365         $id = "tag:$taguribase:Replies:".$user->id;
366         $link = common_local_url('replies', array('nickname' => $user->nickname));
367         $subtitle = sprintf(_('%1$s updates that reply to updates from %2$s / %3$s.'), $sitename, $user->nickname, $profile->getBestName());
368
369         if (!$page) {
370             $page = 1;
371         }
372
373         if (!$count) {
374             $count = 20;
375         }
376
377         if (!$since_id) {
378             $since_id = 0;
379         }
380
381         // NOTE: before_id is an extension to Twitter API -- TB
382         if (!$before_id) {
383             $before_id = 0;
384         }
385
386         $since = strtotime($this->arg('since'));
387
388         $notice = $user->getReplies((($page-1)*20), $count, $since_id, $before_id, $since);
389         $notices = array();
390
391         while ($notice->fetch()) {
392             $notices[] = clone($notice);
393         }
394
395         switch($apidata['content-type']) {
396          case 'xml':
397             $this->show_xml_timeline($notices);
398             break;
399          case 'rss':
400             $this->show_rss_timeline($notices, $title, $link, $subtitle);
401             break;
402          case 'atom':
403              if (isset($apidata['api_arg'])) {
404                  $selfuri = $selfuri = common_root_url() .
405                      'api/statuses/replies/' . $apidata['api_arg'] . '.atom';
406              } else {
407                  $selfuri = $selfuri = common_root_url() .
408                   'api/statuses/replies.atom';
409              }
410             $this->show_atom_timeline($notices, $title, $id, $link, $subtitle, null, $selfuri);
411             break;
412          case 'json':
413             $this->show_json_timeline($notices);
414             break;
415          default:
416             $this->clientError(_('API method not found!'), $code = 404);
417         }
418
419     }
420
421     function show($args, $apidata)
422     {
423         parent::handle($args);
424
425         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
426             $this->clientError(_('API method not found!'), $code = 404);
427             return;
428         }
429
430         $this->auth_user = $apidata['user'];
431         $notice_id = $apidata['api_arg'];
432         $notice = Notice::staticGet($notice_id);
433
434         if ($notice) {
435             if ($apidata['content-type'] == 'xml') {
436                 $this->show_single_xml_status($notice);
437             } elseif ($apidata['content-type'] == 'json') {
438                 $this->show_single_json_status($notice);
439             }
440         } else {
441             // XXX: Twitter just sets a 404 header and doens't bother to return an err msg
442             $this->clientError(_('No status with that ID found.'), 404, $apidata['content-type']);
443         }
444
445     }
446
447     function destroy($args, $apidata)
448     {
449
450         parent::handle($args);
451
452         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
453             $this->clientError(_('API method not found!'), $code = 404);
454             return;
455         }
456
457         // Check for RESTfulness
458         if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
459             // XXX: Twitter just prints the err msg, no XML / JSON.
460             $this->clientError(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
461             return;
462         }
463
464         $this->auth_user = $apidata['user'];
465         $user = $this->auth_user;
466         $notice_id = $apidata['api_arg'];
467         $notice = Notice::staticGet($notice_id);
468
469         if (!$notice) {
470             $this->clientError(_('No status found with that ID.'), 404, $apidata['content-type']);
471             return;
472         }
473
474         if ($user->id == $notice->profile_id) {
475             $replies = new Reply;
476             $replies->get('notice_id', $notice_id);
477             $replies->delete();
478             $notice->delete();
479
480             if ($apidata['content-type'] == 'xml') {
481                 $this->show_single_xml_status($notice);
482             } elseif ($apidata['content-type'] == 'json') {
483                 $this->show_single_json_status($notice);
484             }
485         } else {
486             $this->clientError(_('You may not delete another user\'s status.'), 403, $apidata['content-type']);
487         }
488
489     }
490
491     function friends($args, $apidata)
492     {
493         parent::handle($args);
494         return $this->subscriptions($apidata, 'subscribed', 'subscriber');
495     }
496
497     function friendsIDs($args, $apidata)
498     {
499         parent::handle($args);
500         return $this->subscriptions($apidata, 'subscribed', 'subscriber', true);
501     }
502
503     function followers($args, $apidata)
504     {
505         parent::handle($args);
506         return $this->subscriptions($apidata, 'subscriber', 'subscribed');
507     }
508
509     function followersIDs($args, $apidata)
510     {
511         parent::handle($args);
512         return $this->subscriptions($apidata, 'subscriber', 'subscribed', true);
513     }
514
515     function subscriptions($apidata, $other_attr, $user_attr, $onlyIDs=false)
516     {
517
518         $this->auth_user = $apidata['user'];
519         $user = $this->get_user($apidata['api_arg'], $apidata);
520
521         if (!$user) {
522             $this->clientError('Not Found', 404, $apidata['content-type']);
523             return;
524         }
525
526         $page = $this->trimmed('page');
527
528         if (!$page || !is_numeric($page)) {
529             $page = 1;
530         }
531
532         $profile = $user->getProfile();
533
534         if (!$profile) {
535             $this->serverError(_('User has no profile.'));
536             return;
537         }
538
539         $sub = new Subscription();
540         $sub->$user_attr = $profile->id;
541
542         $since = strtotime($this->trimmed('since'));
543
544         if ($since) {
545             $d = date('Y-m-d H:i:s', $since);
546             $sub->whereAdd("created > '$d'");
547         }
548
549         $sub->orderBy('created DESC');
550
551         if (!$onlyIDs) {
552             $sub->limit(($page-1)*100, 100);
553         }
554
555         $others = array();
556
557         if ($sub->find()) {
558             while ($sub->fetch()) {
559                 $others[] = Profile::staticGet($sub->$other_attr);
560             }
561         } else {
562             // user has no followers
563         }
564
565         $type = $apidata['content-type'];
566
567         $this->init_document($type);
568
569         if ($onlyIDs) {
570             $this->showIDs($others, $type);
571         } else {
572             $this->show_profiles($others, $type);
573         }
574
575         $this->end_document($type);
576     }
577
578     function show_profiles($profiles, $type)
579     {
580         switch ($type) {
581          case 'xml':
582             $this->elementStart('users', array('type' => 'array'));
583             foreach ($profiles as $profile) {
584                 $this->show_profile($profile);
585             }
586             $this->elementEnd('users');
587             break;
588          case 'json':
589             $arrays = array();
590             foreach ($profiles as $profile) {
591                 $arrays[] = $this->twitter_user_array($profile, true);
592             }
593             print json_encode($arrays);
594             break;
595          default:
596             $this->clientError(_('unsupported file type'));
597         }
598     }
599
600     function showIDs($profiles, $type)
601     {
602         switch ($type) {
603          case 'xml':
604             $this->elementStart('ids');
605             foreach ($profiles as $profile) {
606                 $this->element('id', null, $profile->id);
607             }
608             $this->elementEnd('ids');
609             break;
610          case 'json':
611             $ids = array();
612             foreach ($profiles as $profile) {
613                 $ids[] = (int)$profile->id;
614             }
615             print json_encode($ids);
616             break;
617          default:
618             $this->clientError(_('unsupported file type'));
619         }
620     }
621
622     function featured($args, $apidata)
623     {
624         parent::handle($args);
625         $this->serverError(_('API method under construction.'), $code=501);
626     }
627
628     function supported($cmd)
629     {
630
631         $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand', 'FavCommand', 'OnCommand', 'OffCommand');
632
633         if (in_array(get_class($cmd), $cmdlist)) {
634             return true;
635         }
636
637         return false;
638     }
639
640 }