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 /* XXX: Please don't freak out about all the ugly comments in this file.
25 * They are mostly in here for reference while I work on the
26 * API. I'll fix things up to make them look better later. -- Zach
28 class TwitapistatusesAction extends TwitterapiAction {
31 * Returns the MAX_PUBSTATUSES most recent statuses from non-protected users who
32 * have set a custom avatar. Does not require authentication.
34 * URL: http://server/api/statuses/public_timeline.format
36 * Formats: xml, json, rss, atom
38 function public_timeline($args, $apidata) {
39 parent::handle($args);
41 // Number of public statuses to return by default -- Twitter sends 20
42 $MAX_PUBSTATUSES = 20;
44 $notice = DB_DataObject::factory('notice');
46 // FIXME: To really live up to the spec we need to build a list
47 // of notices by users who have custom avatars, so fix this SQL -- Zach
49 # FIXME: bad performance
50 $notice->whereAdd('EXISTS (SELECT user.id from user where user.id = notice.profile_id)');
51 $notice->orderBy('created DESC, notice.id DESC');
52 $notice->limit($MAX_PUBSTATUSES);
53 $cnt = $notice->find();
57 switch($apidata['content-type']) {
59 $this->show_xml_public_timeline($notice);
62 $this->show_rss_public_timeline($notice);
65 $this->show_atom_public_timeline($notice);
68 $this->show_json_public_timeline($notice);
71 common_user_error("API method not found!", $code = 404);
76 common_server_error('Couldn\'t find any statuses.', $code = 503);
82 function show_xml_public_timeline($notice) {
84 header('Content-Type: application/xml; charset=utf-8');
86 common_element_start('statuses', array('type' => 'array'));
88 while ($notice->fetch()) {
89 $twitter_status = $this->twitter_status_array($notice);
90 $this->show_twitter_xml_status($twitter_status);
93 common_element_end('statuses');
97 function show_rss_public_timeline($notice) {
99 header("Content-Type: application/rss+xml; charset=utf-8");
100 $this->init_twitter_rss();
101 $sitename = common_config('site', 'name');
102 $siteserver = common_config('site', 'server');
104 common_element_start('channel');
105 common_element('title', NULL, "$sitename public timeline");
106 common_element('link', NULL, "http://$siteserver");
107 common_element('description', NULL, "$sitename updates from everyone!");
108 common_element('language', NULL, 'en-us');
109 common_element('ttl', NULL, '40');
111 while ($notice->fetch()) {
112 $entry = $this->twitter_rss_entry_array($notice);
113 $this->show_twitter_rss_item($entry);
116 common_element_end('channel');
117 $this->end_twitter_rss();
120 function show_atom_public_timeline($notice) {
122 header('Content-Type: application/atom+xml; charset=utf-8');
124 $this->init_twitter_atom();
125 $sitename = common_config('site', 'name');
126 $siteserver = common_config('site', 'server');
128 common_element('title', NULL, "$sitename public timeline");
129 common_element('id', NULL, "tag:$siteserver:Statuses");
130 common_element('link', array('href' => "http://$siteserver", 'rel' => 'alternate', 'type' => 'text/html'), NULL);
131 common_element('subtitle', NULL, "$sitename updates from everyone!");
133 while ($notice->fetch()) {
134 $entry = $this->twitter_rss_entry_array($notice);
135 $this->show_twitter_atom_entry($entry);
138 $this->end_twitter_atom();
141 function show_json_public_timeline($notice) {
143 header('Content-Type: application/json; charset=utf-8');
147 while ($notice->fetch()) {
148 $twitter_status = $this->twitter_status_array($notice);
149 array_push($statuses, $twitter_status);
152 $this->show_twitter_json_statuses($statuses);
156 Returns the 20 most recent statuses posted by the authenticating user and that user's friends.
157 This is the equivalent of /home on the Web.
159 URL: http://server/api/statuses/friends_timeline.format
163 * since. Optional. Narrows the returned results to just those statuses created after the specified
164 HTTP-formatted date. The same behavior is available by setting an If-Modified-Since header in
166 Ex: http://server/api/statuses/friends_timeline.rss?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
167 * since_id. Optional. Returns only statuses with an ID greater than (that is, more recent than)
168 the specified ID. Ex: http://server/api/statuses/friends_timeline.xml?since_id=12345
169 * count. Optional. Specifies the number of statuses to retrieve. May not be greater than 200.
170 Ex: http://server/api/statuses/friends_timeline.xml?count=5
171 * page. Optional. Ex: http://server/api/statuses/friends_timeline.rss?page=3
173 Formats: xml, json, rss, atom
175 function friends_timeline($args, $apidata) {
176 parent::handle($args);
178 $since = $this->arg('since');
179 $since_id = $this->arg('since_id');
180 $count = $this->arg('count');
181 $page = $this->arg('page');
183 print "Friends Timeline! requested content-type: " . $apidata['content-type'] . "\n";
184 print "since: $since, since_id: $since_id, count: $count, page: $page\n";
191 Returns the 20 most recent statuses posted from the authenticating user. It's also possible to
192 request another user's timeline via the id parameter below. This is the equivalent of the Web
193 /archive page for your own user, or the profile page for a third party.
195 URL: http://server/api/statuses/user_timeline.format
197 Formats: xml, json, rss, atom
201 * id. Optional. Specifies the ID or screen name of the user for whom to return the
202 friends_timeline. Ex: http://server/api/statuses/user_timeline/12345.xml or
203 http://server/api/statuses/user_timeline/bob.json.
204 * count. Optional. Specifies the number of
205 statuses to retrieve. May not be greater than 200. Ex:
206 http://server/api/statuses/user_timeline.xml?count=5
207 * since. Optional. Narrows the returned
208 results to just those statuses created after the specified HTTP-formatted date. The same
209 behavior is available by setting an If-Modified-Since header in your HTTP request. Ex:
210 http://server/api/statuses/user_timeline.rss?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
211 * since_id. Optional. Returns only statuses with an ID greater than (that is, more recent than)
212 the specified ID. Ex: http://server/api/statuses/user_timeline.xml?since_id=12345 * page.
213 Optional. Ex: http://server/api/statuses/friends_timeline.rss?page=3
215 function user_timeline($args, $apidata) {
216 parent::handle($args);
218 $id = $this->arg('id');
219 $count = $this->arg('count');
220 $since = $this->arg('since');
221 $since_id = $this->arg('since_id');
223 print "User Timeline! requested content-type: " . $apidata['content-type'] . "\n";
224 print "id: $id since: $since, since_id: $since_id, count: $count\n";
230 * Returns a single notice, specified by the id parameter below.
231 * The status's author will be returned inline.
233 * URL: http://server/api/statuses/show/id.format
239 * id. Required. The numerical ID of the status you're trying to retrieve.
240 * Ex: http://server/api/statuses/show/123.xml
243 function show($args, $apidata) {
244 parent::handle($args);
246 $id = $apidata['api_arg'];
247 $notice = Notice::staticGet($id);
251 if ($apidata['content-type'] == 'xml') {
252 header('Content-Type: application/xml; charset=utf-8');
255 $twitter_status = $this->twitter_status_array($notice);
256 $this->show_twitter_xml_status($twitter_status);
259 } elseif ($apidata['content-type'] == 'json') {
260 header('Content-Type: application/json; charset=utf-8');
261 $status = $this->twitter_status_array($notice);
262 $this->show_twitter_json_statuses($status);
265 header('HTTP/1.1 404 Not Found');
272 Updates the authenticating user's status. Requires the status parameter specified below. Request must be a POST.
274 URL: http://server/api/statuses/update.format
276 Formats: xml, json. Returns the posted status in requested format when successful.
280 * status. Required. The text of your status update. Be sure to URL encode as necessary. Must not be more than 160
281 characters and should not be more than 140 characters to ensure optimal display.
284 function update($args, $apidata) {
285 parent::handle($args);
286 common_server_error("API method under construction.", $code=501);
290 Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
291 URL: http://server/api/statuses/replies.format
293 Formats: xml, json, rss, atom
297 * page. Optional. Retrieves the 20 next most recent replies. Ex: http://server/api/statuses/replies.xml?page=3
298 * since. Optional. Narrows the returned results to just those replies created after the specified HTTP-formatted date. The
299 same behavior is available by setting an If-Modified-Since header in your HTTP request. Ex:
300 http://server/api/statuses/replies.xml?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
301 * since_id. Optional. Returns only statuses with an ID greater than (that is, more recent than) the specified
302 ID. Ex: http://server/api/statuses/replies.xml?since_id=12345
304 function replies($args, $apidata) {
305 parent::handle($args);
306 common_server_error("API method under construction.", $code=501);
311 Destroys the status specified by the required ID parameter. The authenticating user must be
312 the author of the specified status.
314 URL: http://server/api/statuses/destroy/id.format
320 * id. Required. The ID of the status to destroy. Ex:
321 http://server/api/statuses/destroy/12345.json or
322 http://server/api/statuses/destroy/23456.xml
325 function destroy($args, $apidata) {
326 parent::handle($args);
327 common_server_error("API method under construction.", $code=501);
333 Returns up to 100 of the authenticating user's friends who have most recently updated, each with current status inline.
334 It's also possible to request another user's recent friends list via the id parameter below.
336 URL: http://server/api/statuses/friends.format
342 * id. Optional. The ID or screen name of the user for whom to request a list of friends. Ex:
343 http://server/api/statuses/friends/12345.json
345 http://server/api/statuses/friends/bob.xml
346 * page. Optional. Retrieves the next 100 friends. Ex: http://server/api/statuses/friends.xml?page=2
347 * lite. Optional. Prevents the inline inclusion of current status. Must be set to a value of true. Ex:
348 http://server/api/statuses/friends.xml?lite=true
349 * since. Optional. Narrows the returned results to just those friendships created after the specified
350 HTTP-formatted date. The same behavior is available by setting an If-Modified-Since header in your HTTP
351 request. Ex: http://server/api/statuses/friends.xml?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
353 function friends($args, $apidata) {
354 parent::handle($args);
355 common_server_error("API method under construction.", $code=501);
359 Returns the authenticating user's followers, each with current status inline. They are ordered by the
360 order in which they joined Twitter (this is going to be changed).
362 URL: http://server/api/statuses/followers.format
367 * id. Optional. The ID or screen name of the user for whom to request a list of followers. Ex:
368 http://server/api/statuses/followers/12345.json
370 http://server/api/statuses/followers/bob.xml
371 * page. Optional. Retrieves the next 100 followers. Ex: http://server/api/statuses/followers.xml?page=2
372 * lite. Optional. Prevents the inline inclusion of current status. Must be set to a value of true.
373 Ex: http://server/api/statuses/followers.xml?lite=true
375 function followers($args, $apidata) {
376 parent::handle($args);
377 common_server_error("API method under construction.", $code=501);
381 Returns a list of the users currently featured on the site with their current statuses inline.
382 URL: http://server/api/statuses/featured.format
386 function featured($args, $apidata) {
387 parent::handle($args);
388 common_server_error("API method under construction.", $code=501);