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(null, $apidata);
119 $this->auth_user = $user;
121 $profile = $user->getProfile();
123 $sitename = common_config('site', 'name');
124 $siteserver = common_config('site', 'server');
126 $title = sprintf(_("%s and friends"), $user->nickname);
127 $id = "tag:$siteserver:friends:" . $user->id;
128 $link = common_local_url('all', array('nickname' => $user->nickname));
129 $subtitle = sprintf(_('Updates from %1$s and friends on %2$s!'), $user->nickname, $sitename);
131 $notice = $user->noticesWithFriends(($page-1)*20, $count, $since_id, $before_id, $since);
133 switch($apidata['content-type']) {
135 $this->show_xml_timeline($notice);
138 $this->show_rss_timeline($notice, $title, $link, $subtitle);
141 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
144 $this->show_json_timeline($notice);
147 $this->clientError(_('API method not found!'), $code = 404);
152 function user_timeline($args, $apidata)
154 parent::handle($args);
156 $this->auth_user = $apidata['user'];
157 $user = $this->get_user($apidata['api_arg'], $apidata);
160 $this->clientError('Not Found', 404, $apidata['content-type']);
164 $profile = $user->getProfile();
167 $this->serverError(_('User has no profile.'));
171 $count = $this->arg('count');
172 $since = $this->arg('since');
173 $since_id = $this->arg('since_id');
174 $page = $this->arg('page');
175 $before_id = $this->arg('before_id');
189 // NOTE: before_id is an extensions to Twitter API -- TB
194 $since = strtotime($this->arg('since'));
196 $sitename = common_config('site', 'name');
197 $siteserver = common_config('site', 'server');
199 $title = sprintf(_("%s timeline"), $user->nickname);
200 $id = "tag:$siteserver:user:".$user->id;
201 $link = common_local_url('showstream', array('nickname' => $user->nickname));
202 $subtitle = sprintf(_('Updates from %1$s on %2$s!'), $user->nickname, $sitename);
204 # FriendFeed's SUP protocol
205 # Also added RSS and Atom feeds
207 $suplink = common_local_url('sup', null, null, $user->id);
208 header('X-SUP-ID: '.$suplink);
212 $notice = $user->getNotices((($page-1)*20), $count, $since_id, $before_id, $since);
214 switch($apidata['content-type']) {
216 $this->show_xml_timeline($notice);
219 $this->show_rss_timeline($notice, $title, $link, $subtitle, $suplink);
222 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle, $suplink);
225 $this->show_json_timeline($notice);
228 $this->clientError(_('API method not found!'), $code = 404);
233 function update($args, $apidata)
236 parent::handle($args);
238 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
239 $this->clientError(_('API method not found!'), $code = 404);
243 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
244 $this->clientError(_('This method requires a POST.'), 400, $apidata['content-type']);
248 $this->auth_user = $apidata['user'];
249 $user = $this->auth_user;
250 $status = $this->trimmed('status');
251 $source = $this->trimmed('source');
252 $in_reply_to_status_id = intval($this->trimmed('in_reply_to_status_id'));
253 $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
254 if (!$source || in_array($source, $reserved_sources)) {
260 // XXX: Note: In this case, Twitter simply returns '200 OK'
261 // No error is given, but the status is not posted to the
262 // user's timeline. Seems bad. Shouldn't we throw an
268 $status_shortened = common_shorten_links($status);
270 if (mb_strlen($status_shortened) > 140) {
272 // XXX: Twitter truncates anything over 140, flags the status
273 // as "truncated." Sending this error may screw up some clients
274 // that assume Twitter will truncate for them. Should we just
275 // truncate too? -- Zach
276 $this->clientError(_('That\'s too long. Max notice size is 140 chars.'), $code = 406, $apidata['content-type']);
282 // Check for commands
283 $inter = new CommandInterpreter();
284 $cmd = $inter->handle_command($user, $status_shortened);
288 if ($this->supported($cmd)) {
289 $cmd->execute(new Channel());
292 // cmd not supported? Twitter just returns your latest status.
293 // And, it returns your last status whether the cmd was successful
295 $n = $user->getCurrentNotice();
296 $apidata['api_arg'] = $n->id;
301 if ($in_reply_to_status_id) {
303 // check whether notice actually exists
304 $reply = Notice::staticGet($in_reply_to_status_id);
307 $reply_to = $in_reply_to_status_id;
309 $this->clientError(_('Not found'), $code = 404, $apidata['content-type']);
314 $notice = Notice::saveNew($user->id, html_entity_decode($status, ENT_NOQUOTES, 'UTF-8'),
315 $source, 1, $reply_to);
317 if (is_string($notice)) {
318 $this->serverError($notice);
322 common_broadcast_notice($notice);
323 $apidata['api_arg'] = $notice->id;
326 $this->show($args, $apidata);
329 function replies($args, $apidata)
332 parent::handle($args);
334 $since = $this->arg('since');
335 $count = $this->arg('count');
336 $page = $this->arg('page');
337 $since_id = $this->arg('since_id');
338 $before_id = $this->arg('before_id');
340 $this->auth_user = $apidata['user'];
341 $user = $this->auth_user;
342 $profile = $user->getProfile();
344 $sitename = common_config('site', 'name');
345 $siteserver = common_config('site', 'server');
347 $title = sprintf(_('%1$s / Updates replying to %2$s'), $sitename, $user->nickname);
348 $id = "tag:$siteserver:replies:".$user->id;
349 $link = common_local_url('replies', array('nickname' => $user->nickname));
350 $subtitle = sprintf(_('%1$s updates that reply to updates from %2$s / %3$s.'), $sitename, $user->nickname, $profile->getBestName());
364 // NOTE: before_id is an extension to Twitter API -- TB
369 $since = strtotime($this->arg('since'));
371 $notice = $user->getReplies((($page-1)*20), $count, $since_id, $before_id, $since);
374 while ($notice->fetch()) {
375 $notices[] = clone($notice);
378 switch($apidata['content-type']) {
380 $this->show_xml_timeline($notices);
383 $this->show_rss_timeline($notices, $title, $link, $subtitle);
386 $this->show_atom_timeline($notices, $title, $id, $link, $subtitle);
389 $this->show_json_timeline($notices);
392 $this->clientError(_('API method not found!'), $code = 404);
397 function show($args, $apidata)
399 parent::handle($args);
401 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
402 $this->clientError(_('API method not found!'), $code = 404);
406 $this->auth_user = $apidata['user'];
407 $notice_id = $apidata['api_arg'];
408 $notice = Notice::staticGet($notice_id);
411 if ($apidata['content-type'] == 'xml') {
412 $this->show_single_xml_status($notice);
413 } elseif ($apidata['content-type'] == 'json') {
414 $this->show_single_json_status($notice);
417 // XXX: Twitter just sets a 404 header and doens't bother to return an err msg
418 $this->clientError(_('No status with that ID found.'), 404, $apidata['content-type']);
423 function destroy($args, $apidata)
426 parent::handle($args);
428 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
429 $this->clientError(_('API method not found!'), $code = 404);
433 // Check for RESTfulness
434 if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
435 // XXX: Twitter just prints the err msg, no XML / JSON.
436 $this->clientError(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
440 $this->auth_user = $apidata['user'];
441 $user = $this->auth_user;
442 $notice_id = $apidata['api_arg'];
443 $notice = Notice::staticGet($notice_id);
446 $this->clientError(_('No status found with that ID.'), 404, $apidata['content-type']);
450 if ($user->id == $notice->profile_id) {
451 $replies = new Reply;
452 $replies->get('notice_id', $notice_id);
456 if ($apidata['content-type'] == 'xml') {
457 $this->show_single_xml_status($notice);
458 } elseif ($apidata['content-type'] == 'json') {
459 $this->show_single_json_status($notice);
462 $this->clientError(_('You may not delete another user\'s status.'), 403, $apidata['content-type']);
467 function friends($args, $apidata)
469 parent::handle($args);
470 return $this->subscriptions($apidata, 'subscribed', 'subscriber');
473 function friendsIDs($args, $apidata)
475 parent::handle($args);
476 return $this->subscriptions($apidata, 'subscribed', 'subscriber', true);
479 function followers($args, $apidata)
481 parent::handle($args);
482 return $this->subscriptions($apidata, 'subscriber', 'subscribed');
485 function followersIDs($args, $apidata)
487 parent::handle($args);
488 return $this->subscriptions($apidata, 'subscriber', 'subscribed', true);
491 function subscriptions($apidata, $other_attr, $user_attr, $onlyIDs=false)
494 $this->auth_user = $apidata['user'];
495 $user = $this->get_user($apidata['api_arg'], $apidata);
498 $this->clientError('Not Found', 404, $apidata['content-type']);
502 $page = $this->trimmed('page');
504 if (!$page || !is_numeric($page)) {
508 $profile = $user->getProfile();
511 $this->serverError(_('User has no profile.'));
515 $sub = new Subscription();
516 $sub->$user_attr = $profile->id;
518 $since = strtotime($this->trimmed('since'));
521 $d = date('Y-m-d H:i:s', $since);
522 $sub->whereAdd("created > '$d'");
525 $sub->orderBy('created DESC');
528 $sub->limit(($page-1)*100, 100);
534 while ($sub->fetch()) {
535 $others[] = Profile::staticGet($sub->$other_attr);
538 // user has no followers
541 $type = $apidata['content-type'];
543 $this->init_document($type);
546 $this->showIDs($others, $type);
548 $this->show_profiles($others, $type);
551 $this->end_document($type);
554 function show_profiles($profiles, $type)
558 $this->elementStart('users', array('type' => 'array'));
559 foreach ($profiles as $profile) {
560 $this->show_profile($profile);
562 $this->elementEnd('users');
566 foreach ($profiles as $profile) {
567 $arrays[] = $this->twitter_user_array($profile, true);
569 print json_encode($arrays);
572 $this->clientError(_('unsupported file type'));
576 function showIDs($profiles, $type)
580 $this->elementStart('ids');
581 foreach ($profiles as $profile) {
582 $this->element('id', null, $profile->id);
584 $this->elementEnd('ids');
588 foreach ($profiles as $profile) {
589 $ids[] = (int)$profile->id;
591 print json_encode($ids);
594 $this->clientError(_('unsupported file type'));
598 function featured($args, $apidata)
600 parent::handle($args);
601 $this->serverError(_('API method under construction.'), $code=501);
604 function supported($cmd)
607 $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand', 'FavCommand', 'OnCommand', 'OffCommand');
609 if (in_array(get_class($cmd), $cmdlist)) {