3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, Control Yourself, 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')) {
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 user_timeline($args, $apidata)
141 parent::handle($args);
143 $this->auth_user = $apidata['user'];
144 $user = $this->get_user($apidata['api_arg'], $apidata);
147 $this->clientError('Not Found', 404, $apidata['content-type']);
151 $profile = $user->getProfile();
153 $sitename = common_config('site', 'name');
154 $title = sprintf(_("%s timeline"), $user->nickname);
155 $taguribase = common_config('integration', 'taguri');
156 $id = "tag:$taguribase:UserTimeline:".$user->id;
157 $link = common_local_url('showstream',
158 array('nickname' => $user->nickname));
159 $subtitle = sprintf(_('Updates from %1$s on %2$s!'),
160 $user->nickname, $sitename);
162 # FriendFeed's SUP protocol
163 # Also added RSS and Atom feeds
165 $suplink = common_local_url('sup', null, null, $user->id);
166 header('X-SUP-ID: '.$suplink);
168 $page = (int)$this->arg('page', 1);
169 $count = (int)$this->arg('count', 20);
170 $max_id = (int)$this->arg('max_id', 0);
171 $since_id = (int)$this->arg('since_id', 0);
172 $since = $this->arg('since');
174 $notice = $user->getNotices(($page-1)*$count,
175 $count, $since_id, $max_id, $since);
177 switch($apidata['content-type']) {
179 $this->show_xml_timeline($notice);
182 $this->show_rss_timeline($notice, $title, $link,
183 $subtitle, $suplink);
186 if (isset($apidata['api_arg'])) {
187 $selfuri = common_root_url() .
188 'api/statuses/user_timeline/' .
189 $apidata['api_arg'] . '.atom';
191 $selfuri = common_root_url() .
192 'api/statuses/user_timeline.atom';
194 $this->show_atom_timeline($notice, $title, $id, $link,
195 $subtitle, $suplink, $selfuri);
198 $this->show_json_timeline($notice);
201 $this->clientError(_('API method not found!'), $code = 404);
206 function update($args, $apidata)
208 parent::handle($args);
210 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
211 $this->clientError(_('API method not found!'), $code = 404);
215 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
216 $this->clientError(_('This method requires a POST.'),
217 400, $apidata['content-type']);
221 $user = $apidata['user']; // Always the auth user
223 $status = $this->trimmed('status');
224 $source = $this->trimmed('source');
225 $in_reply_to_status_id =
226 intval($this->trimmed('in_reply_to_status_id'));
227 $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
229 if (empty($source) || in_array($source, $reserved_sources)) {
233 if (empty($status)) {
235 // XXX: Note: In this case, Twitter simply returns '200 OK'
236 // No error is given, but the status is not posted to the
237 // user's timeline. Seems bad. Shouldn't we throw an
243 $status_shortened = common_shorten_links($status);
245 if (Notice::contentTooLong($status_shortened)) {
247 // XXX: Twitter truncates anything over 140, flags the status
248 // as "truncated." Sending this error may screw up some clients
249 // that assume Twitter will truncate for them. Should we just
250 // truncate too? -- Zach
251 $this->clientError(sprintf(_('That\'s too long. Max notice size is %d chars.'),
252 Notice::maxContent()),
253 $code = 406, $apidata['content-type']);
258 // Check for commands
259 $inter = new CommandInterpreter();
260 $cmd = $inter->handle_command($user, $status_shortened);
264 if ($this->supported($cmd)) {
265 $cmd->execute(new Channel());
268 // cmd not supported? Twitter just returns your latest status.
269 // And, it returns your last status whether the cmd was successful
271 $n = $user->getCurrentNotice();
272 $apidata['api_arg'] = $n->id;
277 if ($in_reply_to_status_id) {
279 // check whether notice actually exists
280 $reply = Notice::staticGet($in_reply_to_status_id);
283 $reply_to = $in_reply_to_status_id;
285 $this->clientError(_('Not found'), $code = 404,
286 $apidata['content-type']);
291 $notice = Notice::saveNew($user->id,
292 html_entity_decode($status, ENT_NOQUOTES, 'UTF-8'),
293 $source, 1, $reply_to);
295 if (is_string($notice)) {
296 $this->serverError($notice);
300 common_broadcast_notice($notice);
301 $apidata['api_arg'] = $notice->id;
304 $this->show($args, $apidata);
307 function mentions($args, $apidata)
309 parent::handle($args);
311 $user = $this->get_user($apidata['api_arg'], $apidata);
312 $this->auth_user = $apidata['user'];
315 $this->clientError(_('No such user!'), 404,
316 $apidata['content-type']);
320 $profile = $user->getProfile();
322 $sitename = common_config('site', 'name');
323 $title = sprintf(_('%1$s / Updates mentioning %2$s'),
324 $sitename, $user->nickname);
325 $taguribase = common_config('integration', 'taguri');
326 $id = "tag:$taguribase:Mentions:".$user->id;
327 $link = common_local_url('replies',
328 array('nickname' => $user->nickname));
329 $subtitle = sprintf(_('%1$s updates that reply to updates from %2$s / %3$s.'),
330 $sitename, $user->nickname, $profile->getBestName());
332 $page = (int)$this->arg('page', 1);
333 $count = (int)$this->arg('count', 20);
334 $max_id = (int)$this->arg('max_id', 0);
335 $since_id = (int)$this->arg('since_id', 0);
336 $since = $this->arg('since');
338 $notice = $user->getReplies(($page-1)*$count,
339 $count, $since_id, $max_id, $since);
341 switch($apidata['content-type']) {
343 $this->show_xml_timeline($notice);
346 $this->show_rss_timeline($notice, $title, $link, $subtitle);
349 $selfuri = common_root_url() .
350 ltrim($_SERVER['QUERY_STRING'], 'p=');
351 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle,
355 $this->show_json_timeline($notice);
358 $this->clientError(_('API method not found!'), $code = 404);
363 function replies($args, $apidata)
365 call_user_func(array($this, 'mentions'), $args, $apidata);
368 function show($args, $apidata)
370 parent::handle($args);
372 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
373 $this->clientError(_('API method not found!'), $code = 404);
377 // 'id' is an undocumented parameter in Twitter's API. Several
378 // clients make use of it, so we support it too.
380 // show.json?id=12345 takes precedence over /show/12345.json
382 $this->auth_user = $apidata['user'];
383 $notice_id = $this->trimmed('id');
385 if (empty($notice_id)) {
386 $notice_id = $apidata['api_arg'];
389 $notice = Notice::staticGet((int)$notice_id);
392 if ($apidata['content-type'] == 'xml') {
393 $this->show_single_xml_status($notice);
394 } elseif ($apidata['content-type'] == 'json') {
395 $this->show_single_json_status($notice);
398 // XXX: Twitter just sets a 404 header and doens't bother
399 // to return an err msg
400 $deleted = Deleted_notice::staticGet($notice_id);
401 if (!empty($deleted)) {
402 $this->clientError(_('Status deleted.'),
403 410, $apidata['content-type']);
405 $this->clientError(_('No status with that ID found.'),
406 404, $apidata['content-type']);
411 function destroy($args, $apidata)
413 parent::handle($args);
415 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
416 $this->clientError(_('API method not found!'), $code = 404);
420 // Check for RESTfulness
421 if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
422 // XXX: Twitter just prints the err msg, no XML / JSON.
423 $this->clientError(_('This method requires a POST or DELETE.'),
424 400, $apidata['content-type']);
428 $user = $apidata['user']; // Always the auth user
429 $notice_id = $apidata['api_arg'];
430 $notice = Notice::staticGet($notice_id);
432 if (empty($notice)) {
433 $this->clientError(_('No status found with that ID.'),
434 404, $apidata['content-type']);
438 if ($user->id == $notice->profile_id) {
439 $replies = new Reply;
440 $replies->get('notice_id', $notice_id);
444 if ($apidata['content-type'] == 'xml') {
445 $this->show_single_xml_status($notice);
446 } elseif ($apidata['content-type'] == 'json') {
447 $this->show_single_json_status($notice);
450 $this->clientError(_('You may not delete another user\'s status.'),
451 403, $apidata['content-type']);
456 function friends($args, $apidata)
458 parent::handle($args);
459 $includeStatuses=! (boolean) $args['lite'];
460 return $this->subscriptions($apidata, 'subscribed', 'subscriber', false, $includeStatuses);
463 function friendsIDs($args, $apidata)
465 parent::handle($args);
466 return $this->subscriptions($apidata, 'subscribed', 'subscriber', true);
469 function followers($args, $apidata)
471 parent::handle($args);
472 $includeStatuses=! (boolean) $args['lite'];
473 return $this->subscriptions($apidata, 'subscriber', 'subscribed', false, $includeStatuses);
476 function followersIDs($args, $apidata)
478 parent::handle($args);
479 return $this->subscriptions($apidata, 'subscriber', 'subscribed', true);
482 function subscriptions($apidata, $other_attr, $user_attr, $onlyIDs=false, $includeStatuses=true)
484 $this->auth_user = $apidata['user'];
485 $user = $this->get_user($apidata['api_arg'], $apidata);
488 $this->clientError('Not Found', 404, $apidata['content-type']);
492 $profile = $user->getProfile();
494 $sub = new Subscription();
495 $sub->$user_attr = $profile->id;
497 $sub->orderBy('created DESC');
499 // Normally, page 100 friends at a time
502 $page = $this->arg('page', 1);
503 $count = $this->arg('count', 100);
504 $sub->limit(($page-1)*$count, $count);
507 // If we're just looking at IDs, return
508 // ALL of them, unless the user specifies a page,
509 // in which case, return 500 per page.
511 $page = $this->arg('page');
517 $sub->limit(($page-1)*$count, $count);
524 while ($sub->fetch()) {
525 $others[] = Profile::staticGet($sub->$other_attr);
528 // user has no followers
531 $type = $apidata['content-type'];
533 $this->init_document($type);
536 $this->showIDs($others, $type);
538 $this->show_profiles($others, $type, $includeStatuses);
541 $this->end_document($type);
544 function show_profiles($profiles, $type, $includeStatuses)
548 $this->elementStart('users', array('type' => 'array'));
549 foreach ($profiles as $profile) {
550 $this->show_profile($profile,$type,null,$includeStatuses);
552 $this->elementEnd('users');
556 foreach ($profiles as $profile) {
557 $arrays[] = $this->twitter_user_array($profile, $includeStatuses);
559 print json_encode($arrays);
562 $this->clientError(_('unsupported file type'));
566 function showIDs($profiles, $type)
570 $this->elementStart('ids');
571 foreach ($profiles as $profile) {
572 $this->element('id', null, $profile->id);
574 $this->elementEnd('ids');
578 foreach ($profiles as $profile) {
579 $ids[] = (int)$profile->id;
581 print json_encode($ids);
584 $this->clientError(_('unsupported file type'));
588 function featured($args, $apidata)
590 parent::handle($args);
591 $this->serverError(_('API method under construction.'), $code=501);
594 function supported($cmd)
596 $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand',
597 'FavCommand', 'OnCommand', 'OffCommand');
599 if (in_array(get_class($cmd), $cmdlist)) {