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) {
27 parent::handle($args);
29 $sitename = common_config('site', 'name');
30 $siteserver = common_config('site', 'server');
31 $title = sprintf(_("%s public timeline"), $sitename);
32 $id = "tag:$siteserver:Statuses";
33 $link = common_root_url();
34 $subtitle = sprintf(_("%s updates from everyone!"), $sitename);
36 // Number of public statuses to return by default -- Twitter sends 20
37 $MAX_PUBSTATUSES = 20;
39 // FIXME: To really live up to the spec we need to build a list
40 // of notices by users who have custom avatars, so fix this SQL -- Zach
42 $page = $this->arg('page');
43 $since_id = $this->arg('since_id');
44 $before_id = $this->arg('before_id');
46 // NOTE: page, since_id, and before_id are extensions to Twitter API -- TB
57 $notice = Notice::publicStream((($page-1)*$MAX_PUBSTATUSES), $MAX_PUBSTATUSES, $since_id, $before_id);
61 switch($apidata['content-type']) {
63 $this->show_xml_timeline($notice);
66 $this->show_rss_timeline($notice, $title, $link, $subtitle);
69 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
72 $this->show_json_timeline($notice);
75 common_user_error(_('API method not found!'), $code = 404);
80 common_server_error(_('Couldn\'t find any statuses.'), $code = 503);
85 function friends_timeline($args, $apidata) {
86 parent::handle($args);
88 $since = $this->arg('since');
89 $since_id = $this->arg('since_id');
90 $count = $this->arg('count');
91 $page = $this->arg('page');
92 $before_id = $this->arg('before_id');
106 // NOTE: before_id is an extension to Twitter API -- TB
111 $user = $this->get_user($id, $apidata);
112 $this->auth_user = $user;
114 $profile = $user->getProfile();
116 $sitename = common_config('site', 'name');
117 $siteserver = common_config('site', 'server');
119 $title = sprintf(_("%s and friends"), $user->nickname);
120 $id = "tag:$siteserver:friends:" . $user->id;
121 $link = common_local_url('all', array('nickname' => $user->nickname));
122 $subtitle = sprintf(_('Updates from %1$s and friends on %2$s!'), $user->nickname, $sitename);
124 $notice = $user->noticesWithFriends(($page-1)*20, $count, $since_id, $before_id);
126 switch($apidata['content-type']) {
128 $this->show_xml_timeline($notice);
131 $this->show_rss_timeline($notice, $title, $link, $subtitle);
134 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
137 $this->show_json_timeline($notice);
140 common_user_error(_('API method not found!'), $code = 404);
145 function user_timeline($args, $apidata) {
146 parent::handle($args);
148 $this->auth_user = $apidata['user'];
149 $user = $this->get_user($apidata['api_arg'], $apidata);
152 $this->client_error('Not Found', 404, $apidata['content-type']);
156 $profile = $user->getProfile();
159 common_server_error(_('User has no profile.'));
163 $count = $this->arg('count');
164 $since = $this->arg('since');
165 $since_id = $this->arg('since_id');
166 $page = $this->arg('page');
167 $before_id = $this->arg('before_id');
181 // NOTE: before_id is an extensions to Twitter API -- TB
186 $sitename = common_config('site', 'name');
187 $siteserver = common_config('site', 'server');
189 $title = sprintf(_("%s timeline"), $user->nickname);
190 $id = "tag:$siteserver:user:".$user->id;
191 $link = common_local_url('showstream', array('nickname' => $user->nickname));
192 $subtitle = sprintf(_('Updates from %1$s on %2$s!'), $user->nickname, $sitename);
194 # FriendFeed's SUP protocol
195 # Also added RSS and Atom feeds
197 $suplink = common_local_url('sup', NULL, $user->id);
198 header('X-SUP-ID: '.$suplink);
202 $notice = $user->getNotices((($page-1)*20), $count, $since_id, $before_id);
204 switch($apidata['content-type']) {
206 $this->show_xml_timeline($notice);
209 $this->show_rss_timeline($notice, $title, $link, $subtitle, $suplink);
212 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle, $suplink);
215 $this->show_json_timeline($notice);
218 common_user_error(_('API method not found!'), $code = 404);
223 function update($args, $apidata) {
225 parent::handle($args);
227 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
228 common_user_error(_('API method not found!'), $code = 404);
232 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
233 $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
237 $this->auth_user = $apidata['user'];
238 $user = $this->auth_user;
239 $status = $this->trimmed('status');
240 $source = $this->trimmed('source');
241 $in_reply_to_status_id = intval($this->trimmed('in_reply_to_status_id'));
249 // XXX: Note: In this case, Twitter simply returns '200 OK'
250 // No error is given, but the status is not posted to the
251 // user's timeline. Seems bad. Shouldn't we throw an
255 } else if (mb_strlen($status) > 140) {
257 $status = common_shorten_links($status);
259 if (mb_strlen($status) > 140) {
261 // XXX: Twitter truncates anything over 140, flags the status
262 // as "truncated." Sending this error may screw up some clients
263 // that assume Twitter will truncate for them. Should we just
264 // truncate too? -- Zach
265 $this->client_error(_('That\'s too long. Max notice size is 140 chars.'), $code = 406, $apidata['content-type']);
271 // Check for commands
272 $inter = new CommandInterpreter();
273 $cmd = $inter->handle_command($user, $status);
277 if ($this->supported($cmd)) {
278 $cmd->execute(new Channel());
281 // cmd not supported? Twitter just returns your latest status.
282 // And, it returns your last status whether the cmd was successful
284 $n = $user->getCurrentNotice();
285 $apidata['api_arg'] = $n->id;
290 if ($in_reply_to_status_id) {
292 // check whether notice actually exists
293 $reply = Notice::staticGet($in_reply_to_status_id);
296 $reply_to = $in_reply_to_status_id;
298 $this->client_error(_('Not found'), $code = 404, $apidata['content-type']);
303 $notice = Notice::saveNew($user->id, html_entity_decode($status, ENT_NOQUOTES, 'UTF-8'),
304 $source, 1, $reply_to);
306 if (is_string($notice)) {
307 $this->server_error($notice);
311 common_broadcast_notice($notice);
312 $apidata['api_arg'] = $notice->id;
315 $this->show($args, $apidata);
318 function replies($args, $apidata) {
320 parent::handle($args);
322 $since = $this->arg('since');
323 $count = $this->arg('count');
324 $page = $this->arg('page');
325 $since_id = $this->arg('since_id');
326 $before_id = $this->arg('before_id');
328 $this->auth_user = $apidata['user'];
329 $user = $this->auth_user;
330 $profile = $user->getProfile();
332 $sitename = common_config('site', 'name');
333 $siteserver = common_config('site', 'server');
335 $title = sprintf(_('%1$s / Updates replying to %2$s'), $sitename, $user->nickname);
336 $id = "tag:$siteserver:replies:".$user->id;
337 $link = common_local_url('replies', array('nickname' => $user->nickname));
338 $subtitle = sprintf(_('%1$s updates that reply to updates from %2$s / %3$s.'), $sitename, $user->nickname, $profile->getBestName());
352 // NOTE: before_id is an extension to Twitter API -- TB
356 $notice = $user->getReplies((($page-1)*20), $count, $since_id, $before_id);
359 while ($notice->fetch()) {
360 $notices[] = clone($notice);
363 switch($apidata['content-type']) {
365 $this->show_xml_timeline($notices);
368 $this->show_rss_timeline($notices, $title, $link, $subtitle);
371 $this->show_atom_timeline($notices, $title, $id, $link, $subtitle);
374 $this->show_json_timeline($notices);
377 common_user_error(_('API method not found!'), $code = 404);
382 function show($args, $apidata) {
383 parent::handle($args);
385 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
386 common_user_error(_('API method not found!'), $code = 404);
390 $this->auth_user = $apidata['user'];
391 $notice_id = $apidata['api_arg'];
392 $notice = Notice::staticGet($notice_id);
395 if ($apidata['content-type'] == 'xml') {
396 $this->show_single_xml_status($notice);
397 } elseif ($apidata['content-type'] == 'json') {
398 $this->show_single_json_status($notice);
401 // XXX: Twitter just sets a 404 header and doens't bother to return an err msg
402 $this->client_error(_('No status with that ID found.'), 404, $apidata['content-type']);
407 function destroy($args, $apidata) {
409 parent::handle($args);
411 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
412 common_user_error(_('API method not found!'), $code = 404);
416 // Check for RESTfulness
417 if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
418 // XXX: Twitter just prints the err msg, no XML / JSON.
419 $this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
423 $this->auth_user = $apidata['user'];
424 $user = $this->auth_user;
425 $notice_id = $apidata['api_arg'];
426 $notice = Notice::staticGet($notice_id);
429 $this->client_error(_('No status found with that ID.'), 404, $apidata['content-type']);
433 if ($user->id == $notice->profile_id) {
434 $replies = new Reply;
435 $replies->get('notice_id', $notice_id);
436 common_dequeue_notice($notice);
440 if ($apidata['content-type'] == 'xml') {
441 $this->show_single_xml_status($notice);
442 } elseif ($apidata['content-type'] == 'json') {
443 $this->show_single_json_status($notice);
446 $this->client_error(_('You may not delete another user\'s status.'), 403, $apidata['content-type']);
451 function friends($args, $apidata) {
452 parent::handle($args);
453 return $this->subscriptions($apidata, 'subscribed', 'subscriber');
456 function followers($args, $apidata) {
457 parent::handle($args);
459 return $this->subscriptions($apidata, 'subscriber', 'subscribed');
462 function subscriptions($apidata, $other_attr, $user_attr) {
466 $this->auth_user = $apidate['user'];
467 $user = $this->get_user($apidata['api_arg'], $apidata);
470 $this->client_error('Not Found', 404, $apidata['content-type']);
474 $page = $this->trimmed('page');
476 if (!$page || !is_numeric($page)) {
480 $profile = $user->getProfile();
483 common_server_error(_('User has no profile.'));
487 $sub = new Subscription();
488 $sub->$user_attr = $profile->id;
489 $sub->orderBy('created DESC');
490 $sub->limit(($page-1)*100, 100);
495 while ($sub->fetch()) {
496 $others[] = Profile::staticGet($sub->$other_attr);
499 // user has no followers
502 $type = $apidata['content-type'];
504 $this->init_document($type);
505 $this->show_profiles($others, $type);
506 $this->end_document($type);
509 function show_profiles($profiles, $type) {
512 common_element_start('users', array('type' => 'array'));
513 foreach ($profiles as $profile) {
514 $this->show_profile($profile);
516 common_element_end('users');
520 foreach ($profiles as $profile) {
521 $arrays[] = $this->twitter_user_array($profile, true);
523 print json_encode($arrays);
526 $this->client_error(_('unsupported file type'));
530 function featured($args, $apidata) {
531 parent::handle($args);
532 common_server_error(_('API method under construction.'), $code=501);
535 function supported($cmd) {
537 $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand', 'FavCommand', 'OnCommand', 'OffCommand');
539 if (in_array(get_class($cmd), $cmdlist)) {