]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapistatuses.php
Merge branch '0.8.x' of git@gitorious.org:+laconica-developers/laconica/dev into...
[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         $max_id = $this->arg('max_id');
49
50         if (!$page) {
51             $page = 1;
52         }
53         if (!$since_id) {
54             $since_id = 0;
55         }
56         if (!$max_id) {
57             $max_id = 0;
58         }
59
60         $since = strtotime($this->arg('since'));
61
62         $notice = Notice::publicStream((($page-1)*$MAX_PUBSTATUSES), $MAX_PUBSTATUSES, $since_id, $max_id, $since);
63
64         if ($notice) {
65
66             switch($apidata['content-type']) {
67                 case 'xml':
68                     $this->show_xml_timeline($notice);
69                     break;
70                 case 'rss':
71                     $this->show_rss_timeline($notice, $title, $link, $subtitle);
72                     break;
73                 case 'atom':
74                     $selfuri = common_root_url() . 'api/statuses/public_timeline.atom';
75                     $this->show_atom_timeline($notice, $title, $id, $link, $subtitle, null, $selfuri);
76                     break;
77                 case 'json':
78                     $this->show_json_timeline($notice);
79                     break;
80                 default:
81                     $this->clientError(_('API method not found!'), $code = 404);
82                     break;
83             }
84
85         } else {
86             $this->serverError(_('Couldn\'t find any statuses.'), $code = 503);
87         }
88
89     }
90
91     function friends_timeline($args, $apidata)
92     {
93         parent::handle($args);
94
95         $since = $this->arg('since');
96         $since_id = $this->arg('since_id');
97         $count = $this->arg('count');
98         $page = $this->arg('page');
99         $max_id = $this->arg('max_id');
100
101         if (!$page) {
102             $page = 1;
103         }
104
105         if (!$count) {
106             $count = 20;
107         }
108
109         if (!$since_id) {
110             $since_id = 0;
111         }
112
113         if (!$max_id) {
114             $max_id = 0;
115         }
116
117         $since = strtotime($this->arg('since'));
118         $user = $this->get_user($apidata['api_arg'], $apidata);
119         $this->auth_user = $user;
120
121         if (empty($user)) {
122              $this->clientError(_('No such user!'), 404, $apidata['content-type']);
123             return;
124         }
125
126         $profile = $user->getProfile();
127         $sitename = common_config('site', 'name');
128         $title = sprintf(_("%s and friends"), $user->nickname);
129         $taguribase = common_config('integration', 'taguri');
130         $id = "tag:$taguribase:FriendsTimeline:" . $user->id;
131         $link = common_local_url('all', array('nickname' => $user->nickname));
132         $subtitle = sprintf(_('Updates from %1$s and friends on %2$s!'), $user->nickname, $sitename);
133
134         $notice = $user->noticesWithFriends(($page-1)*20, $count, $since_id, $max_id, $since);
135
136         switch($apidata['content-type']) {
137          case 'xml':
138             $this->show_xml_timeline($notice);
139             break;
140          case 'rss':
141             $this->show_rss_timeline($notice, $title, $link, $subtitle);
142             break;
143          case 'atom':
144             if (isset($apidata['api_arg'])) {
145                 $selfuri = common_root_url() .
146                     'api/statuses/friends_timeline/' . $apidata['api_arg'] . '.atom';
147             } else {
148                 $selfuri = common_root_url() .
149                     'api/statuses/friends_timeline.atom';
150             }
151             $this->show_atom_timeline($notice, $title, $id, $link, $subtitle, null, $selfuri);
152             break;
153          case 'json':
154             $this->show_json_timeline($notice);
155             break;
156          default:
157             $this->clientError(_('API method not found!'), $code = 404);
158         }
159
160     }
161
162     function user_timeline($args, $apidata)
163     {
164         parent::handle($args);
165
166         $this->auth_user = $apidata['user'];
167         $user = $this->get_user($apidata['api_arg'], $apidata);
168
169         if (!$user) {
170             $this->clientError('Not Found', 404, $apidata['content-type']);
171             return;
172         }
173
174         $profile = $user->getProfile();
175
176         if (!$profile) {
177             $this->serverError(_('User has no profile.'));
178             return;
179         }
180
181         $count = $this->arg('count');
182         $since = $this->arg('since');
183         $since_id = $this->arg('since_id');
184         $page = $this->arg('page');
185         $max_id = $this->arg('max_id');
186
187         if (!$page) {
188             $page = 1;
189         }
190
191         if (!$count) {
192             $count = 20;
193         }
194
195         if (!$since_id) {
196             $since_id = 0;
197         }
198
199         if (!$max_id) {
200             $max_id = 0;
201         }
202
203         $since = strtotime($this->arg('since'));
204
205         $sitename = common_config('site', 'name');
206         $title = sprintf(_("%s timeline"), $user->nickname);
207         $taguribase = common_config('integration', 'taguri');
208         $id = "tag:$taguribase:UserTimeline:".$user->id;
209         $link = common_local_url('showstream', array('nickname' => $user->nickname));
210         $subtitle = sprintf(_('Updates from %1$s on %2$s!'), $user->nickname, $sitename);
211
212         # FriendFeed's SUP protocol
213         # Also added RSS and Atom feeds
214
215         $suplink = common_local_url('sup', null, null, $user->id);
216         header('X-SUP-ID: '.$suplink);
217
218         # XXX: since
219
220         $notice = $user->getNotices((($page-1)*20), $count, $since_id, $max_id, $since);
221
222         switch($apidata['content-type']) {
223          case 'xml':
224             $this->show_xml_timeline($notice);
225             break;
226          case 'rss':
227             $this->show_rss_timeline($notice, $title, $link, $subtitle, $suplink);
228             break;
229          case 'atom':
230             if (isset($apidata['api_arg'])) {
231                 $selfuri = common_root_url() .
232                     'api/statuses/user_timeline/' . $apidata['api_arg'] . '.atom';
233             } else {
234                 $selfuri = common_root_url() .
235                  'api/statuses/user_timeline.atom';
236             }
237             $this->show_atom_timeline($notice, $title, $id, $link, $subtitle, $suplink, $selfuri);
238             break;
239          case 'json':
240             $this->show_json_timeline($notice);
241             break;
242          default:
243             $this->clientError(_('API method not found!'), $code = 404);
244         }
245
246     }
247
248     function update($args, $apidata)
249     {
250
251         parent::handle($args);
252
253         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
254             $this->clientError(_('API method not found!'), $code = 404);
255             return;
256         }
257
258         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
259             $this->clientError(_('This method requires a POST.'), 400, $apidata['content-type']);
260             return;
261         }
262
263         $this->auth_user = $apidata['user'];
264         $user = $this->auth_user;
265         $status = $this->trimmed('status');
266         $source = $this->trimmed('source');
267         $in_reply_to_status_id = intval($this->trimmed('in_reply_to_status_id'));
268         $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
269         if (!$source || in_array($source, $reserved_sources)) {
270             $source = 'api';
271         }
272
273         if (!$status) {
274
275             // XXX: Note: In this case, Twitter simply returns '200 OK'
276             // No error is given, but the status is not posted to the
277             // user's timeline.     Seems bad.     Shouldn't we throw an
278             // errror? -- Zach
279             return;
280
281         } else {
282
283             $status_shortened = common_shorten_links($status);
284
285             if (mb_strlen($status_shortened) > 140) {
286
287                 // XXX: Twitter truncates anything over 140, flags the status
288                 // as "truncated." Sending this error may screw up some clients
289                 // that assume Twitter will truncate for them.    Should we just
290                 // truncate too? -- Zach
291                 $this->clientError(_('That\'s too long. Max notice size is 140 chars.'), $code = 406, $apidata['content-type']);
292                 return;
293
294             }
295         }
296
297         // Check for commands
298         $inter = new CommandInterpreter();
299         $cmd = $inter->handle_command($user, $status_shortened);
300
301         if ($cmd) {
302
303             if ($this->supported($cmd)) {
304                 $cmd->execute(new Channel());
305             }
306
307             // cmd not supported?  Twitter just returns your latest status.
308             // And, it returns your last status whether the cmd was successful
309             // or not!
310             $n = $user->getCurrentNotice();
311             $apidata['api_arg'] = $n->id;
312         } else {
313
314             $reply_to = null;
315
316             if ($in_reply_to_status_id) {
317
318                 // check whether notice actually exists
319                 $reply = Notice::staticGet($in_reply_to_status_id);
320
321                 if ($reply) {
322                     $reply_to = $in_reply_to_status_id;
323                 } else {
324                     $this->clientError(_('Not found'), $code = 404, $apidata['content-type']);
325                     return;
326                 }
327             }
328
329             $notice = Notice::saveNew($user->id, html_entity_decode($status, ENT_NOQUOTES, 'UTF-8'),
330                 $source, 1, $reply_to);
331
332             if (is_string($notice)) {
333                 $this->serverError($notice);
334                 return;
335             }
336
337             common_broadcast_notice($notice);
338             $apidata['api_arg'] = $notice->id;
339         }
340
341         $this->show($args, $apidata);
342     }
343
344     function mentions($args, $apidata)
345     {
346
347         parent::handle($args);
348
349         $since = $this->arg('since');
350         $count = $this->arg('count');
351         $page = $this->arg('page');
352         $since_id = $this->arg('since_id');
353         $max_id = $this->arg('max_id');
354
355         $user = $this->get_user($apidata['api_arg'], $apidata);
356         $this->auth_user = $apidata['user'];
357         $profile = $user->getProfile();
358
359         $sitename = common_config('site', 'name');
360         $title = sprintf(_('%1$s / Updates mentioning %2$s'),
361             $sitename, $user->nickname);
362         $taguribase = common_config('integration', 'taguri');
363         $id = "tag:$taguribase:Mentions:".$user->id;
364         $link = common_local_url('replies', array('nickname' => $user->nickname));
365         $subtitle = sprintf(_('%1$s updates that reply to updates from %2$s / %3$s.'),
366             $sitename, $user->nickname, $profile->getBestName());
367
368         if (!$page) {
369             $page = 1;
370         }
371
372         if (!$count) {
373             $count = 20;
374         }
375
376         if (!$since_id) {
377             $since_id = 0;
378         }
379
380         if (!$max_id) {
381             $max_id = 0;
382         }
383
384         $since = strtotime($this->arg('since'));
385
386         $notice = $user->getReplies((($page-1)*20),
387             $count, $since_id, $max_id, $since);
388         $notices = array();
389
390         while ($notice->fetch()) {
391             $notices[] = clone($notice);
392         }
393
394         switch($apidata['content-type']) {
395          case 'xml':
396             $this->show_xml_timeline($notices);
397             break;
398          case 'rss':
399             $this->show_rss_timeline($notices, $title, $link, $subtitle);
400             break;
401          case 'atom':
402             $selfuri = common_root_url() .
403                 ltrim($_SERVER['QUERY_STRING'], 'p=');
404             $this->show_atom_timeline($notices, $title, $id, $link, $subtitle,
405                 null, $selfuri);
406             break;
407          case 'json':
408             $this->show_json_timeline($notices);
409             break;
410          default:
411             $this->clientError(_('API method not found!'), $code = 404);
412         }
413
414     }
415
416     function replies($args, $apidata)
417     {
418         call_user_func(array($this, 'mentions'), $args, $apidata);
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 }