3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/twitterapi.php');
24 class TwitapistatusesAction extends TwitterapiAction
27 function public_timeline($args, $apidata)
29 parent::handle($args);
31 $sitename = common_config('site', 'name');
32 $title = sprintf(_("%s public timeline"), $sitename);
34 $taguribase = common_config('integration', 'taguri');
35 $id = "tag:$taguribase:PublicTimeline";
36 $link = common_root_url();
38 $subtitle = sprintf(_("%s updates from everyone!"), $sitename);
40 // Number of public statuses to return by default -- Twitter sends 20
41 $MAX_PUBSTATUSES = 20;
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
46 $page = $this->arg('page');
47 $since_id = $this->arg('since_id');
48 $before_id = $this->arg('before_id');
50 // NOTE: page, since_id, and before_id are extensions to Twitter API -- TB
61 $since = strtotime($this->arg('since'));
63 $notice = Notice::publicStream((($page-1)*$MAX_PUBSTATUSES), $MAX_PUBSTATUSES, $since_id, $before_id, $since);
67 switch($apidata['content-type']) {
69 $this->show_xml_timeline($notice);
72 $this->show_rss_timeline($notice, $title, $link, $subtitle);
75 $selfuri = common_root_url() . 'api/statuses/public_timeline.atom';
76 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle, null, $selfuri);
79 $this->show_json_timeline($notice);
82 $this->clientError(_('API method not found!'), $code = 404);
87 $this->serverError(_('Couldn\'t find any statuses.'), $code = 503);
92 function friends_timeline($args, $apidata)
94 parent::handle($args);
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');
114 // NOTE: before_id is an extension to Twitter API -- TB
119 $since = strtotime($this->arg('since'));
120 $user = $this->get_user($apidata['api_arg'], $apidata);
121 $this->auth_user = $user;
124 $this->clientError(_('No such user!'), 404, $apidata['content-type']);
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);
136 $notice = $user->noticesWithFriends(($page-1)*20, $count, $since_id, $before_id, $since);
138 switch($apidata['content-type']) {
140 $this->show_xml_timeline($notice);
143 $this->show_rss_timeline($notice, $title, $link, $subtitle);
146 if (isset($apidata['api_arg'])) {
147 $selfuri = common_root_url() .
148 'api/statuses/friends_timeline/' . $apidata['api_arg'] . '.atom';
150 $selfuri = common_root_url() .
151 'api/statuses/friends_timeline.atom';
153 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle, null, $selfuri);
156 $this->show_json_timeline($notice);
159 $this->clientError(_('API method not found!'), $code = 404);
164 function user_timeline($args, $apidata)
166 parent::handle($args);
168 $this->auth_user = $apidata['user'];
169 $user = $this->get_user($apidata['api_arg'], $apidata);
172 $this->clientError('Not Found', 404, $apidata['content-type']);
176 $profile = $user->getProfile();
179 $this->serverError(_('User has no profile.'));
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');
201 // NOTE: before_id is an extensions to Twitter API -- TB
206 $since = strtotime($this->arg('since'));
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);
215 # FriendFeed's SUP protocol
216 # Also added RSS and Atom feeds
218 $suplink = common_local_url('sup', null, null, $user->id);
219 header('X-SUP-ID: '.$suplink);
223 $notice = $user->getNotices((($page-1)*20), $count, $since_id, $before_id, $since);
225 switch($apidata['content-type']) {
227 $this->show_xml_timeline($notice);
230 $this->show_rss_timeline($notice, $title, $link, $subtitle, $suplink);
233 if (isset($apidata['api_arg'])) {
234 $selfuri = common_root_url() .
235 'api/statuses/user_timeline/' . $apidata['api_arg'] . '.atom';
237 $selfuri = common_root_url() .
238 'api/statuses/user_timeline.atom';
240 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle, $suplink, $selfuri);
243 $this->show_json_timeline($notice);
246 $this->clientError(_('API method not found!'), $code = 404);
251 function update($args, $apidata)
254 parent::handle($args);
256 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
257 $this->clientError(_('API method not found!'), $code = 404);
261 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
262 $this->clientError(_('This method requires a POST.'), 400, $apidata['content-type']);
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)) {
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
286 $status_shortened = common_shorten_links($status);
288 if (mb_strlen($status_shortened) > 140) {
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']);
300 // Check for commands
301 $inter = new CommandInterpreter();
302 $cmd = $inter->handle_command($user, $status_shortened);
306 if ($this->supported($cmd)) {
307 $cmd->execute(new Channel());
310 // cmd not supported? Twitter just returns your latest status.
311 // And, it returns your last status whether the cmd was successful
313 $n = $user->getCurrentNotice();
314 $apidata['api_arg'] = $n->id;
319 if ($in_reply_to_status_id) {
321 // check whether notice actually exists
322 $reply = Notice::staticGet($in_reply_to_status_id);
325 $reply_to = $in_reply_to_status_id;
327 $this->clientError(_('Not found'), $code = 404, $apidata['content-type']);
332 $notice = Notice::saveNew($user->id, html_entity_decode($status, ENT_NOQUOTES, 'UTF-8'),
333 $source, 1, $reply_to);
335 if (is_string($notice)) {
336 $this->serverError($notice);
340 common_broadcast_notice($notice);
341 $apidata['api_arg'] = $notice->id;
344 $this->show($args, $apidata);
347 function mentions($args, $apidata)
350 parent::handle($args);
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');
358 $user = $this->get_user($apidata['api_arg'], $apidata);
359 $this->auth_user = $apidata['user'];
360 $profile = $user->getProfile();
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());
383 // NOTE: before_id is an extension to Twitter API -- TB
388 $since = strtotime($this->arg('since'));
390 $notice = $user->getReplies((($page-1)*20),
391 $count, $since_id, $before_id, $since);
394 while ($notice->fetch()) {
395 $notices[] = clone($notice);
398 switch($apidata['content-type']) {
400 $this->show_xml_timeline($notices);
403 $this->show_rss_timeline($notices, $title, $link, $subtitle);
406 $selfuri = common_root_url() .
407 ltrim($_SERVER['QUERY_STRING'], 'p=');
408 $this->show_atom_timeline($notices, $title, $id, $link, $subtitle,
412 $this->show_json_timeline($notices);
415 $this->clientError(_('API method not found!'), $code = 404);
420 function replies($args, $apidata)
422 call_user_func(array($this, 'mentions'), $args, $apidata);
425 function show($args, $apidata)
427 parent::handle($args);
429 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
430 $this->clientError(_('API method not found!'), $code = 404);
434 $this->auth_user = $apidata['user'];
435 $notice_id = $apidata['api_arg'];
436 $notice = Notice::staticGet($notice_id);
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);
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']);
451 function destroy($args, $apidata)
454 parent::handle($args);
456 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
457 $this->clientError(_('API method not found!'), $code = 404);
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']);
468 $this->auth_user = $apidata['user'];
469 $user = $this->auth_user;
470 $notice_id = $apidata['api_arg'];
471 $notice = Notice::staticGet($notice_id);
474 $this->clientError(_('No status found with that ID.'), 404, $apidata['content-type']);
478 if ($user->id == $notice->profile_id) {
479 $replies = new Reply;
480 $replies->get('notice_id', $notice_id);
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);
490 $this->clientError(_('You may not delete another user\'s status.'), 403, $apidata['content-type']);
495 function friends($args, $apidata)
497 parent::handle($args);
498 return $this->subscriptions($apidata, 'subscribed', 'subscriber');
501 function friendsIDs($args, $apidata)
503 parent::handle($args);
504 return $this->subscriptions($apidata, 'subscribed', 'subscriber', true);
507 function followers($args, $apidata)
509 parent::handle($args);
510 return $this->subscriptions($apidata, 'subscriber', 'subscribed');
513 function followersIDs($args, $apidata)
515 parent::handle($args);
516 return $this->subscriptions($apidata, 'subscriber', 'subscribed', true);
519 function subscriptions($apidata, $other_attr, $user_attr, $onlyIDs=false)
522 $this->auth_user = $apidata['user'];
523 $user = $this->get_user($apidata['api_arg'], $apidata);
526 $this->clientError('Not Found', 404, $apidata['content-type']);
530 $page = $this->trimmed('page');
532 if (!$page || !is_numeric($page)) {
536 $profile = $user->getProfile();
539 $this->serverError(_('User has no profile.'));
543 $sub = new Subscription();
544 $sub->$user_attr = $profile->id;
546 $since = strtotime($this->trimmed('since'));
549 $d = date('Y-m-d H:i:s', $since);
550 $sub->whereAdd("created > '$d'");
553 $sub->orderBy('created DESC');
556 $sub->limit(($page-1)*100, 100);
562 while ($sub->fetch()) {
563 $others[] = Profile::staticGet($sub->$other_attr);
566 // user has no followers
569 $type = $apidata['content-type'];
571 $this->init_document($type);
574 $this->showIDs($others, $type);
576 $this->show_profiles($others, $type);
579 $this->end_document($type);
582 function show_profiles($profiles, $type)
586 $this->elementStart('users', array('type' => 'array'));
587 foreach ($profiles as $profile) {
588 $this->show_profile($profile);
590 $this->elementEnd('users');
594 foreach ($profiles as $profile) {
595 $arrays[] = $this->twitter_user_array($profile, true);
597 print json_encode($arrays);
600 $this->clientError(_('unsupported file type'));
604 function showIDs($profiles, $type)
608 $this->elementStart('ids');
609 foreach ($profiles as $profile) {
610 $this->element('id', null, $profile->id);
612 $this->elementEnd('ids');
616 foreach ($profiles as $profile) {
617 $ids[] = (int)$profile->id;
619 print json_encode($ids);
622 $this->clientError(_('unsupported file type'));
626 function featured($args, $apidata)
628 parent::handle($args);
629 $this->serverError(_('API method under construction.'), $code=501);
632 function supported($cmd)
635 $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand', 'FavCommand', 'OnCommand', 'OffCommand');
637 if (in_array(get_class($cmd), $cmdlist)) {