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 $siteserver = common_config('site', 'server');
33 $title = sprintf(_("%s public timeline"), $sitename);
34 $id = "tag:$siteserver:Statuses";
35 $link = common_root_url();
36 $subtitle = sprintf(_("%s updates from everyone!"), $sitename);
38 // Number of public statuses to return by default -- Twitter sends 20
39 $MAX_PUBSTATUSES = 20;
41 // FIXME: To really live up to the spec we need to build a list
42 // of notices by users who have custom avatars, so fix this SQL -- Zach
44 $page = $this->arg('page');
45 $since_id = $this->arg('since_id');
46 $before_id = $this->arg('before_id');
48 // NOTE: page, since_id, and before_id are extensions to Twitter API -- TB
59 $since = strtotime($this->arg('since'));
61 $notice = Notice::publicStream((($page-1)*$MAX_PUBSTATUSES), $MAX_PUBSTATUSES, $since_id, $before_id, $since);
65 switch($apidata['content-type']) {
67 $this->show_xml_timeline($notice);
70 $this->show_rss_timeline($notice, $title, $link, $subtitle);
73 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
76 $this->show_json_timeline($notice);
79 $this->clientError(_('API method not found!'), $code = 404);
84 $this->serverError(_('Couldn\'t find any statuses.'), $code = 503);
89 function friends_timeline($args, $apidata)
91 parent::handle($args);
93 $since = $this->arg('since');
94 $since_id = $this->arg('since_id');
95 $count = $this->arg('count');
96 $page = $this->arg('page');
97 $before_id = $this->arg('before_id');
111 // NOTE: before_id is an extension to Twitter API -- TB
116 $since = strtotime($this->arg('since'));
118 $user = $this->get_user($apidata['api_arg'], $apidata);
119 $this->auth_user = $user;
122 $this->clientError(_('No such user!'), 404, $apidata['content-type']);
126 $profile = $user->getProfile();
128 $sitename = common_config('site', 'name');
129 $siteserver = common_config('site', 'server');
131 $title = sprintf(_("%s and friends"), $user->nickname);
132 $id = "tag:$siteserver:friends:" . $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 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
149 $this->show_json_timeline($notice);
152 $this->clientError(_('API method not found!'), $code = 404);
157 function user_timeline($args, $apidata)
159 parent::handle($args);
161 $this->auth_user = $apidata['user'];
162 $user = $this->get_user($apidata['api_arg'], $apidata);
165 $this->clientError('Not Found', 404, $apidata['content-type']);
169 $profile = $user->getProfile();
172 $this->serverError(_('User has no profile.'));
176 $count = $this->arg('count');
177 $since = $this->arg('since');
178 $since_id = $this->arg('since_id');
179 $page = $this->arg('page');
180 $before_id = $this->arg('before_id');
194 // NOTE: before_id is an extensions to Twitter API -- TB
199 $since = strtotime($this->arg('since'));
201 $sitename = common_config('site', 'name');
202 $siteserver = common_config('site', 'server');
204 $title = sprintf(_("%s timeline"), $user->nickname);
205 $id = "tag:$siteserver:user:".$user->id;
206 $link = common_local_url('showstream', array('nickname' => $user->nickname));
207 $subtitle = sprintf(_('Updates from %1$s on %2$s!'), $user->nickname, $sitename);
209 # FriendFeed's SUP protocol
210 # Also added RSS and Atom feeds
212 $suplink = common_local_url('sup', null, null, $user->id);
213 header('X-SUP-ID: '.$suplink);
217 $notice = $user->getNotices((($page-1)*20), $count, $since_id, $before_id, $since);
219 switch($apidata['content-type']) {
221 $this->show_xml_timeline($notice);
224 $this->show_rss_timeline($notice, $title, $link, $subtitle, $suplink);
227 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle, $suplink);
230 $this->show_json_timeline($notice);
233 $this->clientError(_('API method not found!'), $code = 404);
238 function update($args, $apidata)
241 parent::handle($args);
243 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
244 $this->clientError(_('API method not found!'), $code = 404);
248 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
249 $this->clientError(_('This method requires a POST.'), 400, $apidata['content-type']);
253 $this->auth_user = $apidata['user'];
254 $user = $this->auth_user;
255 $status = $this->trimmed('status');
256 $source = $this->trimmed('source');
257 $in_reply_to_status_id = intval($this->trimmed('in_reply_to_status_id'));
258 $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
259 if (!$source || in_array($source, $reserved_sources)) {
265 // XXX: Note: In this case, Twitter simply returns '200 OK'
266 // No error is given, but the status is not posted to the
267 // user's timeline. Seems bad. Shouldn't we throw an
273 $status_shortened = common_shorten_links($status);
275 if (mb_strlen($status_shortened) > 140) {
277 // XXX: Twitter truncates anything over 140, flags the status
278 // as "truncated." Sending this error may screw up some clients
279 // that assume Twitter will truncate for them. Should we just
280 // truncate too? -- Zach
281 $this->clientError(_('That\'s too long. Max notice size is 140 chars.'), $code = 406, $apidata['content-type']);
287 // Check for commands
288 $inter = new CommandInterpreter();
289 $cmd = $inter->handle_command($user, $status_shortened);
293 if ($this->supported($cmd)) {
294 $cmd->execute(new Channel());
297 // cmd not supported? Twitter just returns your latest status.
298 // And, it returns your last status whether the cmd was successful
300 $n = $user->getCurrentNotice();
301 $apidata['api_arg'] = $n->id;
306 if ($in_reply_to_status_id) {
308 // check whether notice actually exists
309 $reply = Notice::staticGet($in_reply_to_status_id);
312 $reply_to = $in_reply_to_status_id;
314 $this->clientError(_('Not found'), $code = 404, $apidata['content-type']);
319 $notice = Notice::saveNew($user->id, html_entity_decode($status, ENT_NOQUOTES, 'UTF-8'),
320 $source, 1, $reply_to);
322 if (is_string($notice)) {
323 $this->serverError($notice);
327 common_broadcast_notice($notice);
328 $apidata['api_arg'] = $notice->id;
331 $this->show($args, $apidata);
334 function replies($args, $apidata)
337 parent::handle($args);
339 $since = $this->arg('since');
340 $count = $this->arg('count');
341 $page = $this->arg('page');
342 $since_id = $this->arg('since_id');
343 $before_id = $this->arg('before_id');
345 $this->auth_user = $apidata['user'];
346 $user = $this->auth_user;
347 $profile = $user->getProfile();
349 $sitename = common_config('site', 'name');
350 $siteserver = common_config('site', 'server');
352 $title = sprintf(_('%1$s / Updates replying to %2$s'), $sitename, $user->nickname);
353 $id = "tag:$siteserver:replies:".$user->id;
354 $link = common_local_url('replies', array('nickname' => $user->nickname));
355 $subtitle = sprintf(_('%1$s updates that reply to updates from %2$s / %3$s.'), $sitename, $user->nickname, $profile->getBestName());
369 // NOTE: before_id is an extension to Twitter API -- TB
374 $since = strtotime($this->arg('since'));
376 $notice = $user->getReplies((($page-1)*20), $count, $since_id, $before_id, $since);
379 while ($notice->fetch()) {
380 $notices[] = clone($notice);
383 switch($apidata['content-type']) {
385 $this->show_xml_timeline($notices);
388 $this->show_rss_timeline($notices, $title, $link, $subtitle);
391 $this->show_atom_timeline($notices, $title, $id, $link, $subtitle);
394 $this->show_json_timeline($notices);
397 $this->clientError(_('API method not found!'), $code = 404);
402 function show($args, $apidata)
404 parent::handle($args);
406 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
407 $this->clientError(_('API method not found!'), $code = 404);
411 $this->auth_user = $apidata['user'];
412 $notice_id = $apidata['api_arg'];
413 $notice = Notice::staticGet($notice_id);
416 if ($apidata['content-type'] == 'xml') {
417 $this->show_single_xml_status($notice);
418 } elseif ($apidata['content-type'] == 'json') {
419 $this->show_single_json_status($notice);
422 // XXX: Twitter just sets a 404 header and doens't bother to return an err msg
423 $this->clientError(_('No status with that ID found.'), 404, $apidata['content-type']);
428 function destroy($args, $apidata)
431 parent::handle($args);
433 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
434 $this->clientError(_('API method not found!'), $code = 404);
438 // Check for RESTfulness
439 if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
440 // XXX: Twitter just prints the err msg, no XML / JSON.
441 $this->clientError(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
445 $this->auth_user = $apidata['user'];
446 $user = $this->auth_user;
447 $notice_id = $apidata['api_arg'];
448 $notice = Notice::staticGet($notice_id);
451 $this->clientError(_('No status found with that ID.'), 404, $apidata['content-type']);
455 if ($user->id == $notice->profile_id) {
456 $replies = new Reply;
457 $replies->get('notice_id', $notice_id);
461 if ($apidata['content-type'] == 'xml') {
462 $this->show_single_xml_status($notice);
463 } elseif ($apidata['content-type'] == 'json') {
464 $this->show_single_json_status($notice);
467 $this->clientError(_('You may not delete another user\'s status.'), 403, $apidata['content-type']);
472 function friends($args, $apidata)
474 parent::handle($args);
475 return $this->subscriptions($apidata, 'subscribed', 'subscriber');
478 function friendsIDs($args, $apidata)
480 parent::handle($args);
481 return $this->subscriptions($apidata, 'subscribed', 'subscriber', true);
484 function followers($args, $apidata)
486 parent::handle($args);
487 return $this->subscriptions($apidata, 'subscriber', 'subscribed');
490 function followersIDs($args, $apidata)
492 parent::handle($args);
493 return $this->subscriptions($apidata, 'subscriber', 'subscribed', true);
496 function subscriptions($apidata, $other_attr, $user_attr, $onlyIDs=false)
499 $this->auth_user = $apidata['user'];
500 $user = $this->get_user($apidata['api_arg'], $apidata);
503 $this->clientError('Not Found', 404, $apidata['content-type']);
507 $page = $this->trimmed('page');
509 if (!$page || !is_numeric($page)) {
513 $profile = $user->getProfile();
516 $this->serverError(_('User has no profile.'));
520 $sub = new Subscription();
521 $sub->$user_attr = $profile->id;
523 $since = strtotime($this->trimmed('since'));
526 $d = date('Y-m-d H:i:s', $since);
527 $sub->whereAdd("created > '$d'");
530 $sub->orderBy('created DESC');
533 $sub->limit(($page-1)*100, 100);
539 while ($sub->fetch()) {
540 $others[] = Profile::staticGet($sub->$other_attr);
543 // user has no followers
546 $type = $apidata['content-type'];
548 $this->init_document($type);
551 $this->showIDs($others, $type);
553 $this->show_profiles($others, $type);
556 $this->end_document($type);
559 function show_profiles($profiles, $type)
563 $this->elementStart('users', array('type' => 'array'));
564 foreach ($profiles as $profile) {
565 $this->show_profile($profile);
567 $this->elementEnd('users');
571 foreach ($profiles as $profile) {
572 $arrays[] = $this->twitter_user_array($profile, true);
574 print json_encode($arrays);
577 $this->clientError(_('unsupported file type'));
581 function showIDs($profiles, $type)
585 $this->elementStart('ids');
586 foreach ($profiles as $profile) {
587 $this->element('id', null, $profile->id);
589 $this->elementEnd('ids');
593 foreach ($profiles as $profile) {
594 $ids[] = (int)$profile->id;
596 print json_encode($ids);
599 $this->clientError(_('unsupported file type'));
603 function featured($args, $apidata)
605 parent::handle($args);
606 $this->serverError(_('API method under construction.'), $code=501);
609 function supported($cmd)
612 $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand', 'FavCommand', 'OnCommand', 'OffCommand');
614 if (in_array(get_class($cmd), $cmdlist)) {