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);
81 common_debug("auth user = " . $this->auth_user->nickname);
84 $this->clientError(_('No such user!'), 404,
85 $apidata['content-type']);
89 $profile = $user->getProfile();
90 $sitename = common_config('site', 'name');
91 $title = sprintf(_("%s and friends"), $user->nickname);
92 $taguribase = common_config('integration', 'taguri');
93 $id = "tag:$taguribase:FriendsTimeline:" . $user->id;
94 $link = common_local_url('all',
95 array('nickname' => $user->nickname));
96 $subtitle = sprintf(_('Updates from %1$s and friends on %2$s!'),
97 $user->nickname, $sitename);
99 $page = (int)$this->arg('page', 1);
100 $count = (int)$this->arg('count', 20);
101 $max_id = (int)$this->arg('max_id', 0);
102 $since_id = (int)$this->arg('since_id', 0);
103 $since = $this->arg('since');
105 if (!empty($this->auth_user) && $this->auth_user->id == $user->id) {
106 $notice = $user->noticeInbox(($page-1)*$count,
107 $count, $since_id, $max_id, $since);
109 $notice = $user->noticesWithFriends(($page-1)*$count,
110 $count, $since_id, $max_id, $since);
113 switch($apidata['content-type']) {
115 $this->show_xml_timeline($notice);
118 $this->show_rss_timeline($notice, $title, $link, $subtitle);
121 if (isset($apidata['api_arg'])) {
122 $selfuri = common_root_url() .
123 'api/statuses/friends_timeline/' .
124 $apidata['api_arg'] . '.atom';
126 $selfuri = common_root_url() .
127 'api/statuses/friends_timeline.atom';
129 $this->show_atom_timeline($notice, $title, $id, $link,
130 $subtitle, null, $selfuri);
133 $this->show_json_timeline($notice);
136 $this->clientError(_('API method not found!'), $code = 404);
141 function user_timeline($args, $apidata)
143 parent::handle($args);
145 $this->auth_user = $apidata['user'];
146 $user = $this->get_user($apidata['api_arg'], $apidata);
149 $this->clientError('Not Found', 404, $apidata['content-type']);
153 $profile = $user->getProfile();
155 $sitename = common_config('site', 'name');
156 $title = sprintf(_("%s timeline"), $user->nickname);
157 $taguribase = common_config('integration', 'taguri');
158 $id = "tag:$taguribase:UserTimeline:".$user->id;
159 $link = common_local_url('showstream',
160 array('nickname' => $user->nickname));
161 $subtitle = sprintf(_('Updates from %1$s on %2$s!'),
162 $user->nickname, $sitename);
164 # FriendFeed's SUP protocol
165 # Also added RSS and Atom feeds
167 $suplink = common_local_url('sup', null, null, $user->id);
168 header('X-SUP-ID: '.$suplink);
170 $page = (int)$this->arg('page', 1);
171 $count = (int)$this->arg('count', 20);
172 $max_id = (int)$this->arg('max_id', 0);
173 $since_id = (int)$this->arg('since_id', 0);
174 $since = $this->arg('since');
176 $notice = $user->getNotices(($page-1)*$count,
177 $count, $since_id, $max_id, $since);
179 switch($apidata['content-type']) {
181 $this->show_xml_timeline($notice);
184 $this->show_rss_timeline($notice, $title, $link,
185 $subtitle, $suplink);
188 if (isset($apidata['api_arg'])) {
189 $selfuri = common_root_url() .
190 'api/statuses/user_timeline/' .
191 $apidata['api_arg'] . '.atom';
193 $selfuri = common_root_url() .
194 'api/statuses/user_timeline.atom';
196 $this->show_atom_timeline($notice, $title, $id, $link,
197 $subtitle, $suplink, $selfuri);
200 $this->show_json_timeline($notice);
203 $this->clientError(_('API method not found!'), $code = 404);
208 function update($args, $apidata)
210 parent::handle($args);
212 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
213 $this->clientError(_('API method not found!'), $code = 404);
217 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
218 $this->clientError(_('This method requires a POST.'),
219 400, $apidata['content-type']);
223 $user = $apidata['user']; // Always the auth user
225 $status = $this->trimmed('status');
226 $source = $this->trimmed('source');
227 $in_reply_to_status_id =
228 intval($this->trimmed('in_reply_to_status_id'));
229 $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
231 if (empty($source) || in_array($source, $reserved_sources)) {
235 if (empty($status)) {
237 // XXX: Note: In this case, Twitter simply returns '200 OK'
238 // No error is given, but the status is not posted to the
239 // user's timeline. Seems bad. Shouldn't we throw an
245 $status_shortened = common_shorten_links($status);
247 if (mb_strlen($status_shortened) > 140) {
249 // XXX: Twitter truncates anything over 140, flags the status
250 // as "truncated." Sending this error may screw up some clients
251 // that assume Twitter will truncate for them. Should we just
252 // truncate too? -- Zach
253 $this->clientError(_('That\'s too long. Max notice size is 140 chars.'),
254 $code = 406, $apidata['content-type']);
259 // Check for commands
260 $inter = new CommandInterpreter();
261 $cmd = $inter->handle_command($user, $status_shortened);
265 if ($this->supported($cmd)) {
266 $cmd->execute(new Channel());
269 // cmd not supported? Twitter just returns your latest status.
270 // And, it returns your last status whether the cmd was successful
272 $n = $user->getCurrentNotice();
273 $apidata['api_arg'] = $n->id;
278 if ($in_reply_to_status_id) {
280 // check whether notice actually exists
281 $reply = Notice::staticGet($in_reply_to_status_id);
284 $reply_to = $in_reply_to_status_id;
286 $this->clientError(_('Not found'), $code = 404,
287 $apidata['content-type']);
292 $notice = Notice::saveNew($user->id,
293 html_entity_decode($status, ENT_NOQUOTES, 'UTF-8'),
294 $source, 1, $reply_to);
296 if (is_string($notice)) {
297 $this->serverError($notice);
301 common_broadcast_notice($notice);
302 $apidata['api_arg'] = $notice->id;
305 $this->show($args, $apidata);
308 function mentions($args, $apidata)
310 parent::handle($args);
312 $user = $this->get_user($apidata['api_arg'], $apidata);
313 $this->auth_user = $apidata['user'];
316 $this->clientError(_('No such user!'), 404,
317 $apidata['content-type']);
321 $profile = $user->getProfile();
323 $sitename = common_config('site', 'name');
324 $title = sprintf(_('%1$s / Updates mentioning %2$s'),
325 $sitename, $user->nickname);
326 $taguribase = common_config('integration', 'taguri');
327 $id = "tag:$taguribase:Mentions:".$user->id;
328 $link = common_local_url('replies',
329 array('nickname' => $user->nickname));
330 $subtitle = sprintf(_('%1$s updates that reply to updates from %2$s / %3$s.'),
331 $sitename, $user->nickname, $profile->getBestName());
333 $page = (int)$this->arg('page', 1);
334 $count = (int)$this->arg('count', 20);
335 $max_id = (int)$this->arg('max_id', 0);
336 $since_id = (int)$this->arg('since_id', 0);
337 $since = $this->arg('since');
339 $notice = $user->getReplies(($page-1)*$count,
340 $count, $since_id, $max_id, $since);
342 switch($apidata['content-type']) {
344 $this->show_xml_timeline($notice);
347 $this->show_rss_timeline($notice, $title, $link, $subtitle);
350 $selfuri = common_root_url() .
351 ltrim($_SERVER['QUERY_STRING'], 'p=');
352 $this->show_atom_timeline($notice, $title, $id, $link, $subtitle,
356 $this->show_json_timeline($notice);
359 $this->clientError(_('API method not found!'), $code = 404);
364 function replies($args, $apidata)
366 call_user_func(array($this, 'mentions'), $args, $apidata);
369 function show($args, $apidata)
371 parent::handle($args);
373 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
374 $this->clientError(_('API method not found!'), $code = 404);
378 $this->auth_user = $apidata['user'];
379 $notice_id = $apidata['api_arg'];
380 $notice = Notice::staticGet($notice_id);
383 if ($apidata['content-type'] == 'xml') {
384 $this->show_single_xml_status($notice);
385 } elseif ($apidata['content-type'] == 'json') {
386 $this->show_single_json_status($notice);
389 // XXX: Twitter just sets a 404 header and doens't bother
390 // to return an err msg
391 $this->clientError(_('No status with that ID found.'),
392 404, $apidata['content-type']);
397 function destroy($args, $apidata)
399 parent::handle($args);
401 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
402 $this->clientError(_('API method not found!'), $code = 404);
406 // Check for RESTfulness
407 if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
408 // XXX: Twitter just prints the err msg, no XML / JSON.
409 $this->clientError(_('This method requires a POST or DELETE.'),
410 400, $apidata['content-type']);
414 $user = $apidata['user']; // Always the auth user
415 $notice_id = $apidata['api_arg'];
416 $notice = Notice::staticGet($notice_id);
418 if (empty($notice)) {
419 $this->clientError(_('No status found with that ID.'),
420 404, $apidata['content-type']);
424 if ($user->id == $notice->profile_id) {
425 $replies = new Reply;
426 $replies->get('notice_id', $notice_id);
430 if ($apidata['content-type'] == 'xml') {
431 $this->show_single_xml_status($notice);
432 } elseif ($apidata['content-type'] == 'json') {
433 $this->show_single_json_status($notice);
436 $this->clientError(_('You may not delete another user\'s status.'),
437 403, $apidata['content-type']);
442 function friends($args, $apidata)
444 parent::handle($args);
445 return $this->subscriptions($apidata, 'subscribed', 'subscriber');
448 function friendsIDs($args, $apidata)
450 parent::handle($args);
451 return $this->subscriptions($apidata, 'subscribed', 'subscriber', true);
454 function followers($args, $apidata)
456 parent::handle($args);
457 return $this->subscriptions($apidata, 'subscriber', 'subscribed');
460 function followersIDs($args, $apidata)
462 parent::handle($args);
463 return $this->subscriptions($apidata, 'subscriber', 'subscribed', true);
466 function subscriptions($apidata, $other_attr, $user_attr, $onlyIDs=false)
468 $this->auth_user = $apidata['user'];
469 $user = $this->get_user($apidata['api_arg'], $apidata);
472 $this->clientError('Not Found', 404, $apidata['content-type']);
476 $profile = $user->getProfile();
478 $sub = new Subscription();
479 $sub->$user_attr = $profile->id;
481 $sub->orderBy('created DESC');
483 // Normally, page 100 friends at a time
486 $page = $this->arg('page', 1);
487 $count = $this->arg('count', 100);
488 $sub->limit(($page-1)*$count, $count);
491 // If we're just looking at IDs, return
492 // ALL of them, unless the user specifies a page,
493 // in which case, return 500 per page.
495 $page = $this->arg('page');
501 $sub->limit(($page-1)*$count, $count);
508 while ($sub->fetch()) {
509 $others[] = Profile::staticGet($sub->$other_attr);
512 // user has no followers
515 $type = $apidata['content-type'];
517 $this->init_document($type);
520 $this->showIDs($others, $type);
522 $this->show_profiles($others, $type);
525 $this->end_document($type);
528 function show_profiles($profiles, $type)
532 $this->elementStart('users', array('type' => 'array'));
533 foreach ($profiles as $profile) {
534 $this->show_profile($profile);
536 $this->elementEnd('users');
540 foreach ($profiles as $profile) {
541 $arrays[] = $this->twitter_user_array($profile, true);
543 print json_encode($arrays);
546 $this->clientError(_('unsupported file type'));
550 function showIDs($profiles, $type)
554 $this->elementStart('ids');
555 foreach ($profiles as $profile) {
556 $this->element('id', null, $profile->id);
558 $this->elementEnd('ids');
562 foreach ($profiles as $profile) {
563 $ids[] = (int)$profile->id;
565 print json_encode($ids);
568 $this->clientError(_('unsupported file type'));
572 function featured($args, $apidata)
574 parent::handle($args);
575 $this->serverError(_('API method under construction.'), $code=501);
578 function supported($cmd)
580 $cmdlist = array('MessageCommand', 'SubCommand', 'UnsubCommand',
581 'FavCommand', 'OnCommand', 'OffCommand');
583 if (in_array(get_class($cmd), $cmdlist)) {