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 {
26 function public_timeline($args, $apidata)
28 parent::handle($args);
30 $sitename = common_config('site', 'name');
31 $siteserver = common_config('site', 'server');
32 $title = sprintf(_("%s public timeline"), $sitename);
33 $id = "tag:$siteserver:Statuses";
34 $link = common_root_url();
35 $subtitle = sprintf(_("%s updates from everyone!"), $sitename);
37 // Number of public statuses to return by default -- Twitter sends 20
38 $MAX_PUBSTATUSES = 20;
40 // FIXME: To really live up to the spec we need to build a list
41 // of notices by users who have custom avatars, so fix this SQL -- Zach
43 $page = $this->arg('page');
44 $since_id = $this->arg('since_id');
45 $before_id = $this->arg('before_id');
47 // NOTE: page, since_id, and before_id are extensions to Twitter API -- TB
58 $since = strtotime($this->arg('since'));
60 $notice = Notice::publicStream((($page-1)*$MAX_PUBSTATUSES), $MAX_PUBSTATUSES, $since_id, $before_id, $since);
64 switch($apidata['content-type']) {
66 $this->show_xml_timeline($notice);
69 $this->show_rss_timeline($notice, $title, $link, $subtitle);
72 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
75 $this->show_json_timeline($notice);
78 common_user_error(_('API method not found!'), $code = 404);
83 common_server_error(_('Couldn\'t find any statuses.'), $code = 503);
88 function friends_timeline($args, $apidata)
90 parent::handle($args);
92 $since = $this->arg('since');
93 $since_id = $this->arg('since_id');
94 $count = $this->arg('count');
95 $page = $this->arg('page');
96 $before_id = $this->arg('before_id');
110 // NOTE: before_id is an extension to Twitter API -- TB
115 $since = strtotime($this->arg('since'));
117 $user = $this->get_user(null, $apidata);
118 $this->auth_user = $user;
120 $profile = $user->getProfile();
122 $sitename = common_config('site', 'name');
123 $siteserver = common_config('site', 'server');
125 $title = sprintf(_("%s and friends"), $user->nickname);
126 $id = "tag:$siteserver:friends:" . $user->id;
127 $link = common_local_url('all', array('nickname' => $user->nickname));
128 $subtitle = sprintf(_('Updates from %1$s and friends on %2$s!'), $user->nickname, $sitename);
130 $notice = $user->noticesWithFriends(($page-1)*20, $count, $since_id, $before_id, $since);
132 switch($apidata['content-type']) {
134 $this->show_xml_timeline($notice);
137 $this->show_rss_timeline($notice, $title, $link, $subtitle);
140 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
143 $this->show_json_timeline($notice);
146 common_user_error(_('API method not found!'), $code = 404);
151 function user_timeline($args, $apidata)
153 parent::handle($args);
155 $this->auth_user = $apidata['user'];
156 $user = $this->get_user($apidata['api_arg'], $apidata);
159 $this->client_error('Not Found', 404, $apidata['content-type']);
163 $profile = $user->getProfile();
166 common_server_error(_('User has no profile.'));
170 $count = $this->arg('count');
171 $since = $this->arg('since');
172 $since_id = $this->arg('since_id');
173 $page = $this->arg('page');
174 $before_id = $this->arg('before_id');
188 // NOTE: before_id is an extensions to Twitter API -- TB
193 $since = strtotime($this->arg('since'));
195 $sitename = common_config('site', 'name');
196 $siteserver = common_config('site', 'server');
198 $title = sprintf(_("%s timeline"), $user->nickname);
199 $id = "tag:$siteserver:user:".$user->id;
200 $link = common_local_url('showstream', array('nickname' => $user->nickname));
201 $subtitle = sprintf(_('Updates from %1$s on %2$s!'), $user->nickname, $sitename);
203 # FriendFeed's SUP protocol
204 # Also added RSS and Atom feeds
206 $suplink = common_local_url('sup', null, $user->id);
207 header('X-SUP-ID: '.$suplink);
211 $notice = $user->getNotices((($page-1)*20), $count, $since_id, $before_id, $since);
213 switch($apidata['content-type']) {
215 $this->show_xml_timeline($notice);
218 $this->show_rss_timeline($notice, $title, $link, $subtitle, $suplink);
221 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle, $suplink);
224 $this->show_json_timeline($notice);
227 common_user_error(_('API method not found!'), $code = 404);
232 function update($args, $apidata)
235 parent::handle($args);
237 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
238 common_user_error(_('API method not found!'), $code = 404);
242 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
243 $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
247 $this->auth_user = $apidata['user'];
248 $user = $this->auth_user;
249 $status = $this->trimmed('status');
250 $source = $this->trimmed('source');
251 $in_reply_to_status_id = intval($this->trimmed('in_reply_to_status_id'));
252 $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
253 if (!$source || in_array($source, $reserved_sources)) {
259 // XXX: Note: In this case, Twitter simply returns '200 OK'
260 // No error is given, but the status is not posted to the
261 // user's timeline. Seems bad. Shouldn't we throw an
267 $status_shortened = common_shorten_links($status);
269 if (mb_strlen($status_shortened) > 140) {
271 // XXX: Twitter truncates anything over 140, flags the status
272 // as "truncated." Sending this error may screw up some clients
273 // that assume Twitter will truncate for them. Should we just
274 // truncate too? -- Zach
275 $this->client_error(_('That\'s too long. Max notice size is 140 chars.'), $code = 406, $apidata['content-type']);
281 // Check for commands
282 $inter = new CommandInterpreter();
283 $cmd = $inter->handle_command($user, $status_shortened);
287 if ($this->supported($cmd)) {
288 $cmd->execute(new Channel());
291 // cmd not supported? Twitter just returns your latest status.
292 // And, it returns your last status whether the cmd was successful
294 $n = $user->getCurrentNotice();
295 $apidata['api_arg'] = $n->id;
300 if ($in_reply_to_status_id) {
302 // check whether notice actually exists
303 $reply = Notice::staticGet($in_reply_to_status_id);
306 $reply_to = $in_reply_to_status_id;
308 $this->client_error(_('Not found'), $code = 404, $apidata['content-type']);
313 $notice = Notice::saveNew($user->id, html_entity_decode($status, ENT_NOQUOTES, 'UTF-8'),
314 $source, 1, $reply_to);
316 if (is_string($notice)) {
317 $this->server_error($notice);
321 common_broadcast_notice($notice);
322 $apidata['api_arg'] = $notice->id;
325 $this->show($args, $apidata);
328 function replies($args, $apidata)
331 parent::handle($args);
333 $since = $this->arg('since');
334 $count = $this->arg('count');
335 $page = $this->arg('page');
336 $since_id = $this->arg('since_id');
337 $before_id = $this->arg('before_id');
339 $this->auth_user = $apidata['user'];
340 $user = $this->auth_user;
341 $profile = $user->getProfile();
343 $sitename = common_config('site', 'name');
344 $siteserver = common_config('site', 'server');
346 $title = sprintf(_('%1$s / Updates replying to %2$s'), $sitename, $user->nickname);
347 $id = "tag:$siteserver:replies:".$user->id;
348 $link = common_local_url('replies', array('nickname' => $user->nickname));
349 $subtitle = sprintf(_('%1$s updates that reply to updates from %2$s / %3$s.'), $sitename, $user->nickname, $profile->getBestName());
363 // NOTE: before_id is an extension to Twitter API -- TB
368 $since = strtotime($this->arg('since'));
370 $notice = $user->getReplies((($page-1)*20), $count, $since_id, $before_id, $since);
373 while ($notice->fetch()) {
374 $notices[] = clone($notice);
377 switch($apidata['content-type']) {
379 $this->show_xml_timeline($notices);
382 $this->show_rss_timeline($notices, $title, $link, $subtitle);
385 $this->show_atom_timeline($notices, $title, $id, $link, $subtitle);
388 $this->show_json_timeline($notices);
391 common_user_error(_('API method not found!'), $code = 404);
396 function show($args, $apidata)
398 parent::handle($args);
400 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
401 common_user_error(_('API method not found!'), $code = 404);
405 $this->auth_user = $apidata['user'];
406 $notice_id = $apidata['api_arg'];
407 $notice = Notice::staticGet($notice_id);
410 if ($apidata['content-type'] == 'xml') {
411 $this->show_single_xml_status($notice);
412 } elseif ($apidata['content-type'] == 'json') {
413 $this->show_single_json_status($notice);
416 // XXX: Twitter just sets a 404 header and doens't bother to return an err msg
417 $this->client_error(_('No status with that ID found.'), 404, $apidata['content-type']);
422 function destroy($args, $apidata)
425 parent::handle($args);
427 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
428 common_user_error(_('API method not found!'), $code = 404);
432 // Check for RESTfulness
433 if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
434 // XXX: Twitter just prints the err msg, no XML / JSON.
435 $this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
439 $this->auth_user = $apidata['user'];
440 $user = $this->auth_user;
441 $notice_id = $apidata['api_arg'];
442 $notice = Notice::staticGet($notice_id);
445 $this->client_error(_('No status found with that ID.'), 404, $apidata['content-type']);
449 if ($user->id == $notice->profile_id) {
450 $replies = new Reply;
451 $replies->get('notice_id', $notice_id);
452 common_dequeue_notice($notice);
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->client_error(_('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 followers($args, $apidata)
475 parent::handle($args);
477 return $this->subscriptions($apidata, 'subscriber', 'subscribed');
480 function subscriptions($apidata, $other_attr, $user_attr)
485 $this->auth_user = $apidate['user'];
486 $user = $this->get_user($apidata['api_arg'], $apidata);
489 $this->client_error('Not Found', 404, $apidata['content-type']);
493 $page = $this->trimmed('page');
495 if (!$page || !is_numeric($page)) {
499 $profile = $user->getProfile();
502 common_server_error(_('User has no profile.'));
506 $sub = new Subscription();
507 $sub->$user_attr = $profile->id;
509 $since = strtotime($this->trimmed('since'));
512 $d = date('Y-m-d H:i:s', $since);
513 $sub->whereAdd("created > '$d'");
516 $sub->orderBy('created DESC');
517 $sub->limit(($page-1)*100, 100);
522 while ($sub->fetch()) {
523 $others[] = Profile::staticGet($sub->$other_attr);
526 // user has no followers
529 $type = $apidata['content-type'];
531 $this->init_document($type);
532 $this->show_profiles($others, $type);
533 $this->end_document($type);
536 function show_profiles($profiles, $type)
540 common_element_start('users', array('type' => 'array'));
541 foreach ($profiles as $profile) {
542 $this->show_profile($profile);
544 common_element_end('users');
548 foreach ($profiles as $profile) {
549 $arrays[] = $this->twitter_user_array($profile, true);
551 print json_encode($arrays);
554 $this->client_error(_('unsupported file type'));
558 function featured($args, $apidata)
560 parent::handle($args);
561 common_server_error(_('API method under construction.'), $code=501);
564 function supported($cmd)
567 $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand', 'FavCommand', 'OnCommand', 'OffCommand');
569 if (in_array(get_class($cmd), $cmdlist)) {