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 $title = sprintf(_("%s public timeline"), $sitename);
34 $taguribase = common_config('integration', 'taguri');
35 $id = "tag:$taguribase:PublicTimeline";
36 $link = common_root_url();
38 $subtitle = sprintf(_("%s updates from everyone!"), $sitename);
40 // Number of public statuses to return by default -- Twitter sends 20
41 $MAX_PUBSTATUSES = 20;
43 // FIXME: To really live up to the spec we need to build a list
44 // of notices by users who have custom avatars, so fix this SQL -- Zach
46 $page = $this->arg('page');
47 $since_id = $this->arg('since_id');
48 $max_id = $this->arg('max_id');
60 $since = strtotime($this->arg('since'));
62 $notice = Notice::publicStream((($page-1)*$MAX_PUBSTATUSES), $MAX_PUBSTATUSES, $since_id, $max_id, $since);
66 switch($apidata['content-type']) {
68 $this->show_xml_timeline($notice);
71 $this->show_rss_timeline($notice, $title, $link, $subtitle);
74 $selfuri = common_root_url() . 'api/statuses/public_timeline.atom';
75 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle, null, $selfuri);
78 $this->show_json_timeline($notice);
81 $this->clientError(_('API method not found!'), $code = 404);
86 $this->serverError(_('Couldn\'t find any statuses.'), $code = 503);
91 function friends_timeline($args, $apidata)
93 parent::handle($args);
95 $since = $this->arg('since');
96 $since_id = $this->arg('since_id');
97 $count = $this->arg('count');
98 $page = $this->arg('page');
99 $max_id = $this->arg('max_id');
117 $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();
127 $sitename = common_config('site', 'name');
128 $title = sprintf(_("%s and friends"), $user->nickname);
129 $taguribase = common_config('integration', 'taguri');
130 $id = "tag:$taguribase:FriendsTimeline:" . $user->id;
131 $link = common_local_url('all', array('nickname' => $user->nickname));
132 $subtitle = sprintf(_('Updates from %1$s and friends on %2$s!'), $user->nickname, $sitename);
134 $notice = $user->noticesWithFriends(($page-1)*20, $count, $since_id, $max_id, $since);
136 switch($apidata['content-type']) {
138 $this->show_xml_timeline($notice);
141 $this->show_rss_timeline($notice, $title, $link, $subtitle);
144 if (isset($apidata['api_arg'])) {
145 $selfuri = common_root_url() .
146 'api/statuses/friends_timeline/' . $apidata['api_arg'] . '.atom';
148 $selfuri = common_root_url() .
149 'api/statuses/friends_timeline.atom';
151 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle, null, $selfuri);
154 $this->show_json_timeline($notice);
157 $this->clientError(_('API method not found!'), $code = 404);
162 function user_timeline($args, $apidata)
164 parent::handle($args);
166 $this->auth_user = $apidata['user'];
167 $user = $this->get_user($apidata['api_arg'], $apidata);
170 $this->clientError('Not Found', 404, $apidata['content-type']);
174 $profile = $user->getProfile();
177 $this->serverError(_('User has no profile.'));
181 $count = $this->arg('count');
182 $since = $this->arg('since');
183 $since_id = $this->arg('since_id');
184 $page = $this->arg('page');
185 $max_id = $this->arg('max_id');
203 $since = strtotime($this->arg('since'));
205 $sitename = common_config('site', 'name');
206 $title = sprintf(_("%s timeline"), $user->nickname);
207 $taguribase = common_config('integration', 'taguri');
208 $id = "tag:$taguribase:UserTimeline:".$user->id;
209 $link = common_local_url('showstream', array('nickname' => $user->nickname));
210 $subtitle = sprintf(_('Updates from %1$s on %2$s!'), $user->nickname, $sitename);
212 # FriendFeed's SUP protocol
213 # Also added RSS and Atom feeds
215 $suplink = common_local_url('sup', null, null, $user->id);
216 header('X-SUP-ID: '.$suplink);
220 $notice = $user->getNotices((($page-1)*20), $count, $since_id, $max_id, $since);
222 switch($apidata['content-type']) {
224 $this->show_xml_timeline($notice);
227 $this->show_rss_timeline($notice, $title, $link, $subtitle, $suplink);
230 if (isset($apidata['api_arg'])) {
231 $selfuri = common_root_url() .
232 'api/statuses/user_timeline/' . $apidata['api_arg'] . '.atom';
234 $selfuri = common_root_url() .
235 'api/statuses/user_timeline.atom';
237 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle, $suplink, $selfuri);
240 $this->show_json_timeline($notice);
243 $this->clientError(_('API method not found!'), $code = 404);
248 function update($args, $apidata)
251 parent::handle($args);
253 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
254 $this->clientError(_('API method not found!'), $code = 404);
258 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
259 $this->clientError(_('This method requires a POST.'), 400, $apidata['content-type']);
263 $this->auth_user = $apidata['user'];
264 $user = $this->auth_user;
265 $status = $this->trimmed('status');
266 $source = $this->trimmed('source');
267 $in_reply_to_status_id = intval($this->trimmed('in_reply_to_status_id'));
268 $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
269 if (!$source || in_array($source, $reserved_sources)) {
275 // XXX: Note: In this case, Twitter simply returns '200 OK'
276 // No error is given, but the status is not posted to the
277 // user's timeline. Seems bad. Shouldn't we throw an
283 $status_shortened = common_shorten_links($status);
285 if (mb_strlen($status_shortened) > 140) {
287 // XXX: Twitter truncates anything over 140, flags the status
288 // as "truncated." Sending this error may screw up some clients
289 // that assume Twitter will truncate for them. Should we just
290 // truncate too? -- Zach
291 $this->clientError(_('That\'s too long. Max notice size is 140 chars.'), $code = 406, $apidata['content-type']);
297 // Check for commands
298 $inter = new CommandInterpreter();
299 $cmd = $inter->handle_command($user, $status_shortened);
303 if ($this->supported($cmd)) {
304 $cmd->execute(new Channel());
307 // cmd not supported? Twitter just returns your latest status.
308 // And, it returns your last status whether the cmd was successful
310 $n = $user->getCurrentNotice();
311 $apidata['api_arg'] = $n->id;
316 if ($in_reply_to_status_id) {
318 // check whether notice actually exists
319 $reply = Notice::staticGet($in_reply_to_status_id);
322 $reply_to = $in_reply_to_status_id;
324 $this->clientError(_('Not found'), $code = 404, $apidata['content-type']);
329 $notice = Notice::saveNew($user->id, html_entity_decode($status, ENT_NOQUOTES, 'UTF-8'),
330 $source, 1, $reply_to);
332 if (is_string($notice)) {
333 $this->serverError($notice);
337 common_broadcast_notice($notice);
338 $apidata['api_arg'] = $notice->id;
341 $this->show($args, $apidata);
344 function mentions($args, $apidata)
347 parent::handle($args);
349 $since = $this->arg('since');
350 $count = $this->arg('count');
351 $page = $this->arg('page');
352 $since_id = $this->arg('since_id');
353 $max_id = $this->arg('max_id');
355 $user = $this->get_user($apidata['api_arg'], $apidata);
356 $this->auth_user = $apidata['user'];
357 $profile = $user->getProfile();
359 $sitename = common_config('site', 'name');
360 $title = sprintf(_('%1$s / Updates mentioning %2$s'),
361 $sitename, $user->nickname);
362 $taguribase = common_config('integration', 'taguri');
363 $id = "tag:$taguribase:Mentions:".$user->id;
364 $link = common_local_url('replies', array('nickname' => $user->nickname));
365 $subtitle = sprintf(_('%1$s updates that reply to updates from %2$s / %3$s.'),
366 $sitename, $user->nickname, $profile->getBestName());
384 $since = strtotime($this->arg('since'));
386 $notice = $user->getReplies((($page-1)*20),
387 $count, $since_id, $max_id, $since);
390 while ($notice->fetch()) {
391 $notices[] = clone($notice);
394 switch($apidata['content-type']) {
396 $this->show_xml_timeline($notices);
399 $this->show_rss_timeline($notices, $title, $link, $subtitle);
402 $selfuri = common_root_url() .
403 ltrim($_SERVER['QUERY_STRING'], 'p=');
404 $this->show_atom_timeline($notices, $title, $id, $link, $subtitle,
408 $this->show_json_timeline($notices);
411 $this->clientError(_('API method not found!'), $code = 404);
416 function replies($args, $apidata)
418 call_user_func(array($this, 'mentions'), $args, $apidata);
421 function show($args, $apidata)
423 parent::handle($args);
425 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
426 $this->clientError(_('API method not found!'), $code = 404);
430 $this->auth_user = $apidata['user'];
431 $notice_id = $apidata['api_arg'];
432 $notice = Notice::staticGet($notice_id);
435 if ($apidata['content-type'] == 'xml') {
436 $this->show_single_xml_status($notice);
437 } elseif ($apidata['content-type'] == 'json') {
438 $this->show_single_json_status($notice);
441 // XXX: Twitter just sets a 404 header and doens't bother to return an err msg
442 $this->clientError(_('No status with that ID found.'), 404, $apidata['content-type']);
447 function destroy($args, $apidata)
450 parent::handle($args);
452 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
453 $this->clientError(_('API method not found!'), $code = 404);
457 // Check for RESTfulness
458 if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
459 // XXX: Twitter just prints the err msg, no XML / JSON.
460 $this->clientError(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
464 $this->auth_user = $apidata['user'];
465 $user = $this->auth_user;
466 $notice_id = $apidata['api_arg'];
467 $notice = Notice::staticGet($notice_id);
470 $this->clientError(_('No status found with that ID.'), 404, $apidata['content-type']);
474 if ($user->id == $notice->profile_id) {
475 $replies = new Reply;
476 $replies->get('notice_id', $notice_id);
480 if ($apidata['content-type'] == 'xml') {
481 $this->show_single_xml_status($notice);
482 } elseif ($apidata['content-type'] == 'json') {
483 $this->show_single_json_status($notice);
486 $this->clientError(_('You may not delete another user\'s status.'), 403, $apidata['content-type']);
491 function friends($args, $apidata)
493 parent::handle($args);
494 return $this->subscriptions($apidata, 'subscribed', 'subscriber');
497 function friendsIDs($args, $apidata)
499 parent::handle($args);
500 return $this->subscriptions($apidata, 'subscribed', 'subscriber', true);
503 function followers($args, $apidata)
505 parent::handle($args);
506 return $this->subscriptions($apidata, 'subscriber', 'subscribed');
509 function followersIDs($args, $apidata)
511 parent::handle($args);
512 return $this->subscriptions($apidata, 'subscriber', 'subscribed', true);
515 function subscriptions($apidata, $other_attr, $user_attr, $onlyIDs=false)
518 $this->auth_user = $apidata['user'];
519 $user = $this->get_user($apidata['api_arg'], $apidata);
522 $this->clientError('Not Found', 404, $apidata['content-type']);
526 $page = $this->trimmed('page');
528 if (!$page || !is_numeric($page)) {
532 $profile = $user->getProfile();
535 $this->serverError(_('User has no profile.'));
539 $sub = new Subscription();
540 $sub->$user_attr = $profile->id;
542 $since = strtotime($this->trimmed('since'));
545 $d = date('Y-m-d H:i:s', $since);
546 $sub->whereAdd("created > '$d'");
549 $sub->orderBy('created DESC');
552 $sub->limit(($page-1)*100, 100);
558 while ($sub->fetch()) {
559 $others[] = Profile::staticGet($sub->$other_attr);
562 // user has no followers
565 $type = $apidata['content-type'];
567 $this->init_document($type);
570 $this->showIDs($others, $type);
572 $this->show_profiles($others, $type);
575 $this->end_document($type);
578 function show_profiles($profiles, $type)
582 $this->elementStart('users', array('type' => 'array'));
583 foreach ($profiles as $profile) {
584 $this->show_profile($profile);
586 $this->elementEnd('users');
590 foreach ($profiles as $profile) {
591 $arrays[] = $this->twitter_user_array($profile, true);
593 print json_encode($arrays);
596 $this->clientError(_('unsupported file type'));
600 function showIDs($profiles, $type)
604 $this->elementStart('ids');
605 foreach ($profiles as $profile) {
606 $this->element('id', null, $profile->id);
608 $this->elementEnd('ids');
612 foreach ($profiles as $profile) {
613 $ids[] = (int)$profile->id;
615 print json_encode($ids);
618 $this->clientError(_('unsupported file type'));
622 function featured($args, $apidata)
624 parent::handle($args);
625 $this->serverError(_('API method under construction.'), $code=501);
628 function supported($cmd)
631 $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand', 'FavCommand', 'OnCommand', 'OffCommand');
633 if (in_array(get_class($cmd), $cmdlist)) {