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 status, specified by the id parameter below. The status's author will be returned inline.
232 URL: http://server/api/statuses/show/id.format
238 * id. Required. The numerical ID of the status you're trying to retrieve.
239 Ex: http://server/api/statuses/show/123.xml
241 function show($args, $apidata) {
242 parent::handle($args);
244 $id = $this->arg('id');
246 print "show requested content-type: " . $apidata['content-type'] . "\n";
254 Updates the authenticating user's status. Requires the status parameter specified below. Request must be a POST.
256 URL: http://server/api/statuses/update.format
258 Formats: xml, json. Returns the posted status in requested format when successful.
262 * status. Required. The text of your status update. Be sure to URL encode as necessary. Must not be more than 160
263 characters and should not be more than 140 characters to ensure optimal display.
266 function update($args, $apidata) {
267 parent::handle($args);
268 common_server_error("API method under construction.", $code=501);
272 Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
273 URL: http://server/api/statuses/replies.format
275 Formats: xml, json, rss, atom
279 * page. Optional. Retrieves the 20 next most recent replies. Ex: http://server/api/statuses/replies.xml?page=3
280 * since. Optional. Narrows the returned results to just those replies created after the specified HTTP-formatted date. The
281 same behavior is available by setting an If-Modified-Since header in your HTTP request. Ex:
282 http://server/api/statuses/replies.xml?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
283 * since_id. Optional. Returns only statuses with an ID greater than (that is, more recent than) the specified
284 ID. Ex: http://server/api/statuses/replies.xml?since_id=12345
286 function replies($args, $apidata) {
287 parent::handle($args);
288 common_server_error("API method under construction.", $code=501);
293 Destroys the status specified by the required ID parameter. The authenticating user must be
294 the author of the specified status.
296 URL: http://server/api/statuses/destroy/id.format
302 * id. Required. The ID of the status to destroy. Ex:
303 http://server/api/statuses/destroy/12345.json or
304 http://server/api/statuses/destroy/23456.xml
307 function destroy($args, $apidata) {
308 parent::handle($args);
309 common_server_error("API method under construction.", $code=501);
315 Returns up to 100 of the authenticating user's friends who have most recently updated, each with current status inline.
316 It's also possible to request another user's recent friends list via the id parameter below.
318 URL: http://server/api/statuses/friends.format
324 * id. Optional. The ID or screen name of the user for whom to request a list of friends. Ex:
325 http://server/api/statuses/friends/12345.json
327 http://server/api/statuses/friends/bob.xml
328 * page. Optional. Retrieves the next 100 friends. Ex: http://server/api/statuses/friends.xml?page=2
329 * lite. Optional. Prevents the inline inclusion of current status. Must be set to a value of true. Ex:
330 http://server/api/statuses/friends.xml?lite=true
331 * since. Optional. Narrows the returned results to just those friendships created after the specified
332 HTTP-formatted date. The same behavior is available by setting an If-Modified-Since header in your HTTP
333 request. Ex: http://server/api/statuses/friends.xml?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
335 function friends($args, $apidata) {
336 parent::handle($args);
337 common_server_error("API method under construction.", $code=501);
341 Returns the authenticating user's followers, each with current status inline. They are ordered by the
342 order in which they joined Twitter (this is going to be changed).
344 URL: http://server/api/statuses/followers.format
349 * id. Optional. The ID or screen name of the user for whom to request a list of followers. Ex:
350 http://server/api/statuses/followers/12345.json
352 http://server/api/statuses/followers/bob.xml
353 * page. Optional. Retrieves the next 100 followers. Ex: http://server/api/statuses/followers.xml?page=2
354 * lite. Optional. Prevents the inline inclusion of current status. Must be set to a value of true.
355 Ex: http://server/api/statuses/followers.xml?lite=true
357 function followers($args, $apidata) {
358 parent::handle($args);
359 common_server_error("API method under construction.", $code=501);
363 Returns a list of the users currently featured on the site with their current statuses inline.
364 URL: http://server/api/statuses/featured.format
368 function featured($args, $apidata) {
369 parent::handle($args);
370 common_server_error("API method under construction.", $code=501);