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 /* XXX: Please don't freak out about all the ugly comments in this file.
25 * They are mostly in here for reference while I work on the
26 * API. I'll fix things up later to make them look better later. -- Zach
28 class TwitapistatusesAction extends TwitterapiAction {
30 function is_readonly() {
32 static $write_methods = array( 'update',
35 $cmdtext = explode('.', $this->arg('method'));
37 if (in_array($cmdtext[0], $write_methods)) {
44 function public_timeline($args, $apidata) {
45 parent::handle($args);
47 $sitename = common_config('site', 'name');
48 $siteserver = common_config('site', 'server');
49 $title = sprintf(_("%s public timeline"), $sitename);
50 $id = "tag:$siteserver:Statuses";
51 $link = common_root_url();
52 $subtitle = sprintf(_("%s updates from everyone!"), $sitename);
54 // Number of public statuses to return by default -- Twitter sends 20
55 $MAX_PUBSTATUSES = 20;
57 // FIXME: To really live up to the spec we need to build a list
58 // of notices by users who have custom avatars, so fix this SQL -- Zach
60 $page = $this->arg('page');
61 $since_id = $this->arg('since_id');
62 $before_id = $this->arg('before_id');
64 // NOTE: page, since_id, and before_id are extensions to Twitter API -- TB
75 $notice = Notice::publicStream((($page-1)*$MAX_PUBSTATUSES), $MAX_PUBSTATUSES, $since_id, $before_id);
79 switch($apidata['content-type']) {
81 $this->show_xml_timeline($notice);
84 $this->show_rss_timeline($notice, $title, $link, $subtitle);
87 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
90 $this->show_json_timeline($notice);
93 common_user_error(_('API method not found!'), $code = 404);
98 common_server_error(_('Couldn\'t find any statuses.'), $code = 503);
104 Returns the 20 most recent statuses posted by the authenticating user and that user's friends.
105 This is the equivalent of /home on the Web.
107 URL: http://server/api/statuses/friends_timeline.format
111 * since. Optional. Narrows the returned results to just those statuses created after the specified
112 HTTP-formatted date. The same behavior is available by setting an If-Modified-Since header in
114 Ex: http://server/api/statuses/friends_timeline.rss?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
115 * since_id. Optional. Returns only statuses with an ID greater than (that is, more recent than)
116 the specified ID. Ex: http://server/api/statuses/friends_timeline.xml?since_id=12345
117 * count. Optional. Specifies the number of statuses to retrieve. May not be greater than 200.
118 Ex: http://server/api/statuses/friends_timeline.xml?count=5
119 * page. Optional. Ex: http://server/api/statuses/friends_timeline.rss?page=3
121 Formats: xml, json, rss, atom
123 function friends_timeline($args, $apidata) {
124 parent::handle($args);
126 $since = $this->arg('since');
127 $since_id = $this->arg('since_id');
128 $count = $this->arg('count');
129 $page = $this->arg('page');
130 $before_id = $this->arg('before_id');
144 // NOTE: before_id is an extensions to Twitter API -- TB
149 $user = $this->get_user($id, $apidata);
150 $profile = $user->getProfile();
152 $sitename = common_config('site', 'name');
153 $siteserver = common_config('site', 'server');
155 $title = sprintf(_("%s and friends"), $user->nickname);
156 $id = "tag:$siteserver:friends:".$user->id;
157 $link = common_local_url('all', array('nickname' => $user->nickname));
158 $subtitle = sprintf(_('Updates from %1$s and friends on %2$s!'), $user->nickname, $sitename);
160 $notice = $user->noticesWithFriends(($page-1)*20, $count, $since_id, $before_id);
162 switch($apidata['content-type']) {
164 $this->show_xml_timeline($notice);
167 $this->show_rss_timeline($notice, $title, $id, $link, $subtitle);
170 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
173 $this->show_json_timeline($notice);
176 common_user_error(_('API method not found!'), $code = 404);
182 Returns the 20 most recent statuses posted from the authenticating user. It's also possible to
183 request another user's timeline via the id parameter below. This is the equivalent of the Web
184 /archive page for your own user, or the profile page for a third party.
186 URL: http://server/api/statuses/user_timeline.format
188 Formats: xml, json, rss, atom
192 * id. Optional. Specifies the ID or screen name of the user for whom to return the
193 friends_timeline. Ex: http://server/api/statuses/user_timeline/12345.xml or
194 http://server/api/statuses/user_timeline/bob.json.
195 * count. Optional. Specifies the number of
196 statuses to retrieve. May not be greater than 200. Ex:
197 http://server/api/statuses/user_timeline.xml?count=5
198 * since. Optional. Narrows the returned
199 results to just those statuses created after the specified HTTP-formatted date. The same
200 behavior is available by setting an If-Modified-Since header in your HTTP request. Ex:
201 http://server/api/statuses/user_timeline.rss?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
202 * since_id. Optional. Returns only statuses with an ID greater than (that is, more recent than)
203 the specified ID. Ex: http://server/api/statuses/user_timeline.xml?since_id=12345 * page.
204 Optional. Ex: http://server/api/statuses/friends_timeline.rss?page=3
206 function user_timeline($args, $apidata) {
207 parent::handle($args);
211 // function was called with an argument /statuses/user_timeline/api_arg.format
212 if (isset($apidata['api_arg'])) {
214 if (is_numeric($apidata['api_arg'])) {
215 $user = User::staticGet($apidata['api_arg']);
217 $nickname = common_canonical_nickname($apidata['api_arg']);
218 $user = User::staticGet('nickname', $nickname);
222 // if no user was specified, then we'll use the authenticated user
223 $user = $apidata['user'];
227 // Set the user to be the auth user if asked-for can't be found
228 // honestly! This is what Twitter does, I swear --Zach
229 $user = $apidata['user'];
232 $profile = $user->getProfile();
235 common_server_error(_('User has no profile.'));
239 $count = $this->arg('count');
240 $since = $this->arg('since');
241 $since_id = $this->arg('since_id');
242 $page = $this->arg('page');
243 $before_id = $this->arg('before_id');
257 // NOTE: before_id is an extensions to Twitter API -- TB
262 $sitename = common_config('site', 'name');
263 $siteserver = common_config('site', 'server');
265 $title = sprintf(_("%s timeline"), $user->nickname);
266 $id = "tag:$siteserver:user:".$user->id;
267 $link = common_local_url('showstream', array('nickname' => $user->nickname));
268 $subtitle = sprintf(_('Updates from %1$s on %2$s!'), $user->nickname, $sitename);
272 $notice = $user->getNotices((($page-1)*20), $count, $since_id, $before_id);
274 switch($apidata['content-type']) {
276 $this->show_xml_timeline($notice);
279 $this->show_rss_timeline($notice, $title, $id, $link, $subtitle);
282 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
285 $this->show_json_timeline($notice);
288 common_user_error(_('API method not found!'), $code = 404);
293 function update($args, $apidata) {
295 parent::handle($args);
297 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
298 common_user_error(_('API method not found!'), $code = 404);
302 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
303 $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
307 $user = $apidata['user'];
308 $status = $this->trimmed('status');
309 $source = $this->trimmed('source');
310 $in_reply_to_status_id = intval($this->trimmed('in_reply_to_status_id'));
318 // XXX: Note: In this case, Twitter simply returns '200 OK'
319 // No error is given, but the status is not posted to the
320 // user's timeline. Seems bad. Shouldn't we throw an
324 } else if (mb_strlen($status) > 140) {
326 // XXX: Twitter truncates anything over 140, flags the status
327 // as "truncated." Sending this error may screw up some clients
328 // that assume Twitter will truncate for them. Should we just
329 // truncate too? -- Zach
330 $this->client_error(_('That\'s too long. Max notice size is 140 chars.'), $code = 406, $apidata['content-type']);
334 // Check for commands
335 $inter = new CommandInterpreter();
336 $cmd = $inter->handle_command($user, $status);
340 if ($this->supported($cmd)) {
341 $cmd->execute(new Channel());
344 // cmd not supported? Twitter just returns your latest status.
345 // And, it returns your last status whether the cmd was successful
347 $n = $user->getCurrentNotice();
348 $apidata['api_arg'] = $n->id;
353 if ($in_reply_to_status_id) {
355 // check whether notice actually exists
356 $reply = Notice::staticGet($in_reply_to_status_id);
359 $reply_to = $in_reply_to_status_id;
361 $this->client_error(_('Not found'), $code = 404, $apidata['content-type']);
366 $notice = Notice::saveNew($user->id, $status, $source, 1, $reply_to);
368 if (is_string($notice)) {
369 $this->server_error($notice);
373 common_broadcast_notice($notice);
374 $apidata['api_arg'] = $notice->id;
377 $this->show($args, $apidata);
381 Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
382 URL: http://server/api/statuses/replies.format
384 Formats: xml, json, rss, atom
388 * page. Optional. Retrieves the 20 next most recent replies. Ex: http://server/api/statuses/replies.xml?page=3
389 * since. Optional. Narrows the returned results to just those replies created after the specified HTTP-formatted date. The
390 same behavior is available by setting an If-Modified-Since header in your HTTP request. Ex:
391 http://server/api/statuses/replies.xml?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
392 * since_id. Optional. Returns only statuses with an ID greater than (that is, more recent than) the specified
393 ID. Ex: http://server/api/statuses/replies.xml?since_id=12345
395 function replies($args, $apidata) {
397 parent::handle($args);
399 $since = $this->arg('since');
401 $count = $this->arg('count');
402 $page = $this->arg('page');
403 $since_id = $this->arg('since_id');
404 $before_id = $this->arg('before_id');
406 $user = $apidata['user'];
407 $profile = $user->getProfile();
409 $sitename = common_config('site', 'name');
410 $siteserver = common_config('site', 'server');
412 $title = sprintf(_('%1$s / Updates replying to %2$s'), $sitename, $user->nickname);
413 $id = "tag:$siteserver:replies:".$user->id;
414 $link = common_local_url('replies', array('nickname' => $user->nickname));
415 $subtitle = sprintf(_('%1$s updates that reply to updates from %2$s / %3$s.'), $sitename, $user->nickname, $profile->getBestName());
429 // NOTE: before_id is an extensions to Twitter API -- TB
433 $notice = $user->getReplies((($page-1)*20), $count, $since_id, $before_id);
436 while ($notice->fetch()) {
437 $notices[] = clone($notice);
440 switch($apidata['content-type']) {
442 $this->show_xml_timeline($notices);
445 $this->show_rss_timeline($notices, $title, $id, $link, $subtitle);
448 $this->show_atom_timeline($notices, $title, $id, $link, $subtitle);
451 $this->show_json_timeline($notices);
454 common_user_error(_('API method not found!'), $code = 404);
459 function show($args, $apidata) {
460 parent::handle($args);
462 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
463 common_user_error(_('API method not found!'), $code = 404);
467 $notice_id = $apidata['api_arg'];
468 $notice = Notice::staticGet($notice_id);
471 if ($apidata['content-type'] == 'xml') {
472 $this->show_single_xml_status($notice);
473 } elseif ($apidata['content-type'] == 'json') {
474 $this->show_single_json_status($notice);
477 // XXX: Twitter just sets a 404 header and doens't bother to return an err msg
478 $this->client_error(_('No status with that ID found.'), 404, $apidata['content-type']);
485 Destroys the status specified by the required ID parameter. The authenticating user must be
486 the author of the specified status.
488 URL: http://server/api/statuses/destroy/id.format
494 * id. Required. The ID of the status to destroy. Ex:
495 http://server/api/statuses/destroy/12345.json or
496 http://server/api/statuses/destroy/23456.xml
499 function destroy($args, $apidata) {
501 parent::handle($args);
503 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
504 common_user_error(_('API method not found!'), $code = 404);
508 // Check for RESTfulness
509 if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
510 // XXX: Twitter just prints the err msg, no XML / JSON.
511 $this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
515 $user = $apidata['user'];
516 $notice_id = $apidata['api_arg'];
517 $notice = Notice::staticGet($notice_id);
520 $this->client_error(_('No status found with that ID.'), 404, $apidata['content-type']);
524 if ($user->id == $notice->profile_id) {
525 $replies = new Reply;
526 $replies->get('notice_id', $notice_id);
527 common_dequeue_notice($notice);
531 if ($apidata['content-type'] == 'xml') {
532 $this->show_single_xml_status($notice);
533 } elseif ($apidata['content-type'] == 'json') {
534 $this->show_single_json_status($notice);
537 $this->client_error(_('You may not delete another user\'s status.'), 403, $apidata['content-type']);
545 Returns up to 100 of the authenticating user's friends who have most recently updated, each with current status inline.
546 It's also possible to request another user's recent friends list via the id parameter below.
548 URL: http://server/api/statuses/friends.format
554 * id. Optional. The ID or screen name of the user for whom to request a list of friends. Ex:
555 http://server/api/statuses/friends/12345.json
557 http://server/api/statuses/friends/bob.xml
558 * page. Optional. Retrieves the next 100 friends. Ex: http://server/api/statuses/friends.xml?page=2
559 * lite. Optional. Prevents the inline inclusion of current status. Must be set to a value of true. Ex:
560 http://server/api/statuses/friends.xml?lite=true
561 * since. Optional. Narrows the returned results to just those friendships created after the specified
562 HTTP-formatted date. The same behavior is available by setting an If-Modified-Since header in your HTTP
563 request. Ex: http://server/api/statuses/friends.xml?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
565 function friends($args, $apidata) {
566 parent::handle($args);
567 return $this->subscriptions($apidata, 'subscribed', 'subscriber');
571 Returns the authenticating user's followers, each with current status inline. They are ordered by the
572 order in which they joined Twitter (this is going to be changed).
574 URL: http://server/api/statuses/followers.format
579 * id. Optional. The ID or screen name of the user for whom to request a list of followers. Ex:
580 http://server/api/statuses/followers/12345.json
582 http://server/api/statuses/followers/bob.xml
583 * page. Optional. Retrieves the next 100 followers. Ex: http://server/api/statuses/followers.xml?page=2
584 * lite. Optional. Prevents the inline inclusion of current status. Must be set to a value of true.
585 Ex: http://server/api/statuses/followers.xml?lite=true
587 function followers($args, $apidata) {
588 parent::handle($args);
590 return $this->subscriptions($apidata, 'subscriber', 'subscribed');
593 function subscriptions($apidata, $other_attr, $user_attr) {
595 $user = $this->get_subs_user($apidata);
600 $page = $this->trimmed('page');
602 if (!$page || !is_numeric($page)) {
606 $profile = $user->getProfile();
609 common_server_error(_('User has no profile.'));
613 $sub = new Subscription();
614 $sub->$user_attr = $profile->id;
615 $sub->orderBy('created DESC');
616 $sub->limit(($page-1)*100, 100);
621 while ($sub->fetch()) {
622 $others[] = Profile::staticGet($sub->$other_attr);
625 // user has no followers
628 $type = $apidata['content-type'];
630 $this->init_document($type);
631 $this->show_profiles($others, $type);
632 $this->end_document($type);
635 function get_subs_user($apidata) {
637 // function was called with an argument /statuses/user_timeline/api_arg.format
638 if (isset($apidata['api_arg'])) {
640 if (is_numeric($apidata['api_arg'])) {
641 $user = User::staticGet($apidata['api_arg']);
643 $nickname = common_canonical_nickname($apidata['api_arg']);
644 $user = User::staticGet('nickname', $nickname);
648 // if no user was specified, then we'll use the authenticated user
649 $user = $apidata['user'];
653 // Set the user to be the auth user if asked-for can't be found
654 // honestly! This is what Twitter does, I swear --Zach
655 $user = $apidata['user'];
661 function show_profiles($profiles, $type) {
664 common_element_start('users', array('type' => 'array'));
665 foreach ($profiles as $profile) {
666 $this->show_profile($profile);
668 common_element_end('users');
672 foreach ($profiles as $profile) {
673 $arrays[] = $this->twitter_user_array($profile, true);
675 print json_encode($arrays);
678 $this->client_error(_('unsupported file type'));
683 Returns a list of the users currently featured on the site with their current statuses inline.
684 URL: http://server/api/statuses/featured.format
688 function featured($args, $apidata) {
689 parent::handle($args);
690 common_server_error(_('API method under construction.'), $code=501);
693 function get_user($id, $apidata) {
695 return $apidata['user'];
696 } else if (is_numeric($id)) {
697 return User::staticGet($id);
699 return User::staticGet('nickname', $id);
703 function supported($cmd) {
705 $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand', 'FavCommand');
707 if (in_array(get_class($cmd), $cmdlist)) {