3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !defined('LACONICA')) {
24 require_once(INSTALLDIR.'/lib/twitterapi.php');
26 class TwitapistatusesAction extends TwitterapiAction
29 function public_timeline($args, $apidata)
31 // XXX: To really live up to the spec we need to build a list
32 // of notices by users who have custom avatars, so fix this SQL -- Zach
34 parent::handle($args);
36 $sitename = common_config('site', 'name');
37 $title = sprintf(_("%s public timeline"), $sitename);
38 $taguribase = common_config('integration', 'taguri');
39 $id = "tag:$taguribase:PublicTimeline";
40 $link = common_root_url();
41 $subtitle = sprintf(_("%s updates from everyone!"), $sitename);
43 $page = (int)$this->arg('page', 1);
44 $count = (int)$this->arg('count', 20);
45 $max_id = (int)$this->arg('max_id', 0);
46 $since_id = (int)$this->arg('since_id', 0);
47 $since = $this->arg('since');
49 $notice = Notice::publicStream(($page-1)*$count, $count, $since_id,
52 switch($apidata['content-type']) {
54 $this->show_xml_timeline($notice);
57 $this->show_rss_timeline($notice, $title, $link, $subtitle);
60 $selfuri = common_root_url() . 'api/statuses/public_timeline.atom';
61 $this->show_atom_timeline($notice, $title, $id, $link,
62 $subtitle, null, $selfuri);
65 $this->show_json_timeline($notice);
68 $this->clientError(_('API method not found!'), $code = 404);
74 function friends_timeline($args, $apidata)
76 parent::handle($args);
78 $this->auth_user = $apidata['user'];
79 $user = $this->get_user($apidata['api_arg'], $apidata);
82 $this->clientError(_('No such user!'), 404,
83 $apidata['content-type']);
87 $profile = $user->getProfile();
88 $sitename = common_config('site', 'name');
89 $title = sprintf(_("%s and friends"), $user->nickname);
90 $taguribase = common_config('integration', 'taguri');
91 $id = "tag:$taguribase:FriendsTimeline:" . $user->id;
92 $link = common_local_url('all',
93 array('nickname' => $user->nickname));
94 $subtitle = sprintf(_('Updates from %1$s and friends on %2$s!'),
95 $user->nickname, $sitename);
97 $page = (int)$this->arg('page', 1);
98 $count = (int)$this->arg('count', 20);
99 $max_id = (int)$this->arg('max_id', 0);
100 $since_id = (int)$this->arg('since_id', 0);
101 $since = $this->arg('since');
103 if (!empty($this->auth_user) && $this->auth_user->id == $user->id) {
104 $notice = $user->noticeInbox(($page-1)*$count,
105 $count, $since_id, $max_id, $since);
107 $notice = $user->noticesWithFriends(($page-1)*$count,
108 $count, $since_id, $max_id, $since);
111 switch($apidata['content-type']) {
113 $this->show_xml_timeline($notice);
116 $this->show_rss_timeline($notice, $title, $link, $subtitle);
119 if (isset($apidata['api_arg'])) {
120 $selfuri = common_root_url() .
121 'api/statuses/friends_timeline/' .
122 $apidata['api_arg'] . '.atom';
124 $selfuri = common_root_url() .
125 'api/statuses/friends_timeline.atom';
127 $this->show_atom_timeline($notice, $title, $id, $link,
128 $subtitle, null, $selfuri);
131 $this->show_json_timeline($notice);
134 $this->clientError(_('API method not found!'), $code = 404);
139 function home_timeline($args, $apidata)
141 call_user_func(array($this, 'friends_timeline'), $args, $apidata);
144 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->clientError('Not Found', 404, $apidata['content-type']);
156 $profile = $user->getProfile();
158 $sitename = common_config('site', 'name');
159 $title = sprintf(_("%s timeline"), $user->nickname);
160 $taguribase = common_config('integration', 'taguri');
161 $id = "tag:$taguribase:UserTimeline:".$user->id;
162 $link = common_local_url('showstream',
163 array('nickname' => $user->nickname));
164 $subtitle = sprintf(_('Updates from %1$s on %2$s!'),
165 $user->nickname, $sitename);
167 # FriendFeed's SUP protocol
168 # Also added RSS and Atom feeds
170 $suplink = common_local_url('sup', null, null, $user->id);
171 header('X-SUP-ID: '.$suplink);
173 $page = (int)$this->arg('page', 1);
174 $count = (int)$this->arg('count', 20);
175 $max_id = (int)$this->arg('max_id', 0);
176 $since_id = (int)$this->arg('since_id', 0);
177 $since = $this->arg('since');
179 $notice = $user->getNotices(($page-1)*$count,
180 $count, $since_id, $max_id, $since);
182 switch($apidata['content-type']) {
184 $this->show_xml_timeline($notice);
187 $this->show_rss_timeline($notice, $title, $link,
188 $subtitle, $suplink);
191 if (isset($apidata['api_arg'])) {
192 $selfuri = common_root_url() .
193 'api/statuses/user_timeline/' .
194 $apidata['api_arg'] . '.atom';
196 $selfuri = common_root_url() .
197 'api/statuses/user_timeline.atom';
199 $this->show_atom_timeline($notice, $title, $id, $link,
200 $subtitle, $suplink, $selfuri);
203 $this->show_json_timeline($notice);
206 $this->clientError(_('API method not found!'), $code = 404);
211 function update($args, $apidata)
213 parent::handle($args);
215 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
216 $this->clientError(_('API method not found!'), $code = 404);
220 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
221 $this->clientError(_('This method requires a POST.'),
222 400, $apidata['content-type']);
226 $user = $apidata['user']; // Always the auth user
228 $status = $this->trimmed('status');
229 $source = $this->trimmed('source');
230 $in_reply_to_status_id =
231 intval($this->trimmed('in_reply_to_status_id'));
232 $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
234 if (empty($source) || in_array($source, $reserved_sources)) {
238 if (empty($status)) {
240 // XXX: Note: In this case, Twitter simply returns '200 OK'
241 // No error is given, but the status is not posted to the
242 // user's timeline. Seems bad. Shouldn't we throw an
248 $status_shortened = common_shorten_links($status);
250 if (mb_strlen($status_shortened) > 140) {
252 // XXX: Twitter truncates anything over 140, flags the status
253 // as "truncated." Sending this error may screw up some clients
254 // that assume Twitter will truncate for them. Should we just
255 // truncate too? -- Zach
256 $this->clientError(_('That\'s too long. Max notice size is 140 chars.'),
257 $code = 406, $apidata['content-type']);
262 // Check for commands
263 $inter = new CommandInterpreter();
264 $cmd = $inter->handle_command($user, $status_shortened);
268 if ($this->supported($cmd)) {
269 $cmd->execute(new Channel());
272 // cmd not supported? Twitter just returns your latest status.
273 // And, it returns your last status whether the cmd was successful
275 $n = $user->getCurrentNotice();
276 $apidata['api_arg'] = $n->id;
281 if ($in_reply_to_status_id) {
283 // check whether notice actually exists
284 $reply = Notice::staticGet($in_reply_to_status_id);
287 $reply_to = $in_reply_to_status_id;
289 $this->clientError(_('Not found'), $code = 404,
290 $apidata['content-type']);
295 $notice = Notice::saveNew($user->id,
296 html_entity_decode($status, ENT_NOQUOTES, 'UTF-8'),
297 $source, 1, $reply_to);
299 if (is_string($notice)) {
300 $this->serverError($notice, 500, $apidata['content-type']);
304 common_broadcast_notice($notice);
305 $apidata['api_arg'] = $notice->id;
308 $this->show($args, $apidata);
311 function mentions($args, $apidata)
313 parent::handle($args);
315 $user = $this->get_user($apidata['api_arg'], $apidata);
316 $this->auth_user = $apidata['user'];
319 $this->clientError(_('No such user!'), 404,
320 $apidata['content-type']);
324 $profile = $user->getProfile();
326 $sitename = common_config('site', 'name');
327 $title = sprintf(_('%1$s / Updates mentioning %2$s'),
328 $sitename, $user->nickname);
329 $taguribase = common_config('integration', 'taguri');
330 $id = "tag:$taguribase:Mentions:".$user->id;
331 $link = common_local_url('replies',
332 array('nickname' => $user->nickname));
333 $subtitle = sprintf(_('%1$s updates that reply to updates from %2$s / %3$s.'),
334 $sitename, $user->nickname, $profile->getBestName());
336 $page = (int)$this->arg('page', 1);
337 $count = (int)$this->arg('count', 20);
338 $max_id = (int)$this->arg('max_id', 0);
339 $since_id = (int)$this->arg('since_id', 0);
340 $since = $this->arg('since');
342 $notice = $user->getReplies(($page-1)*$count,
343 $count, $since_id, $max_id, $since);
345 switch($apidata['content-type']) {
347 $this->show_xml_timeline($notice);
350 $this->show_rss_timeline($notice, $title, $link, $subtitle);
353 $selfuri = common_root_url() .
354 ltrim($_SERVER['QUERY_STRING'], 'p=');
355 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle,
359 $this->show_json_timeline($notice);
362 $this->clientError(_('API method not found!'), $code = 404);
367 function replies($args, $apidata)
369 call_user_func(array($this, 'mentions'), $args, $apidata);
372 function show($args, $apidata)
374 parent::handle($args);
376 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
377 $this->clientError(_('API method not found!'), $code = 404);
381 // 'id' is an undocumented parameter in Twitter's API. Several
382 // clients make use of it, so we support it too.
384 // show.json?id=12345 takes precedence over /show/12345.json
386 $this->auth_user = $apidata['user'];
387 $notice_id = $this->trimmed('id');
389 if (empty($notice_id)) {
390 $notice_id = $apidata['api_arg'];
393 $notice = Notice::staticGet((int)$notice_id);
396 if ($apidata['content-type'] == 'xml') {
397 $this->show_single_xml_status($notice);
398 } elseif ($apidata['content-type'] == 'json') {
399 $this->show_single_json_status($notice);
402 // XXX: Twitter just sets a 404 header and doens't bother
403 // to return an err msg
404 $this->clientError(_('No status with that ID found.'),
405 404, $apidata['content-type']);
409 function destroy($args, $apidata)
411 parent::handle($args);
413 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
414 $this->clientError(_('API method not found!'), $code = 404);
418 // Check for RESTfulness
419 if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
420 // XXX: Twitter just prints the err msg, no XML / JSON.
421 $this->clientError(_('This method requires a POST or DELETE.'),
422 400, $apidata['content-type']);
426 $user = $apidata['user']; // Always the auth user
427 $notice_id = $apidata['api_arg'];
428 $notice = Notice::staticGet($notice_id);
430 if (empty($notice)) {
431 $this->clientError(_('No status found with that ID.'),
432 404, $apidata['content-type']);
436 if ($user->id == $notice->profile_id) {
437 $replies = new Reply;
438 $replies->get('notice_id', $notice_id);
442 if ($apidata['content-type'] == 'xml') {
443 $this->show_single_xml_status($notice);
444 } elseif ($apidata['content-type'] == 'json') {
445 $this->show_single_json_status($notice);
448 $this->clientError(_('You may not delete another user\'s status.'),
449 403, $apidata['content-type']);
454 function friends($args, $apidata)
456 parent::handle($args);
457 $includeStatuses=! (boolean) $args['lite'];
458 return $this->subscriptions($apidata, 'subscribed', 'subscriber', false, $includeStatuses);
461 function friendsIDs($args, $apidata)
463 parent::handle($args);
464 return $this->subscriptions($apidata, 'subscribed', 'subscriber', true);
467 function followers($args, $apidata)
469 parent::handle($args);
470 $includeStatuses=! (boolean) $args['lite'];
471 return $this->subscriptions($apidata, 'subscriber', 'subscribed', false, $includeStatuses);
474 function followersIDs($args, $apidata)
476 parent::handle($args);
477 return $this->subscriptions($apidata, 'subscriber', 'subscribed', true);
480 function subscriptions($apidata, $other_attr, $user_attr, $onlyIDs=false, $includeStatuses=true)
482 $this->auth_user = $apidata['user'];
483 $user = $this->get_user($apidata['api_arg'], $apidata);
486 $this->clientError('Not Found', 404, $apidata['content-type']);
490 $profile = $user->getProfile();
492 $sub = new Subscription();
493 $sub->$user_attr = $profile->id;
495 $sub->orderBy('created DESC');
497 // Normally, page 100 friends at a time
500 $page = $this->arg('page', 1);
501 $count = $this->arg('count', 100);
502 $sub->limit(($page-1)*$count, $count);
505 // If we're just looking at IDs, return
506 // ALL of them, unless the user specifies a page,
507 // in which case, return 500 per page.
509 $page = $this->arg('page');
515 $sub->limit(($page-1)*$count, $count);
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);
534 $this->showIDs($others, $type);
536 $this->show_profiles($others, $type, $includeStatuses);
539 $this->end_document($type);
542 function show_profiles($profiles, $type, $includeStatuses)
546 $this->elementStart('users', array('type' => 'array'));
547 foreach ($profiles as $profile) {
548 $this->show_profile($profile,$type,null,$includeStatuses);
550 $this->elementEnd('users');
554 foreach ($profiles as $profile) {
555 $arrays[] = $this->twitter_user_array($profile, $includeStatuses);
557 print json_encode($arrays);
560 $this->clientError(_('unsupported file type'));
564 function showIDs($profiles, $type)
568 $this->elementStart('ids');
569 foreach ($profiles as $profile) {
570 $this->element('id', null, $profile->id);
572 $this->elementEnd('ids');
576 foreach ($profiles as $profile) {
577 $ids[] = (int)$profile->id;
579 print json_encode($ids);
582 $this->clientError(_('unsupported file type'));
586 function featured($args, $apidata)
588 parent::handle($args);
589 $this->serverError(_('API method under construction.'), $code=501);
592 function supported($cmd)
594 $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand',
595 'FavCommand', 'OnCommand', 'OffCommand');
597 if (in_array(get_class($cmd), $cmdlist)) {