]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapistatuses.php
42c6313c8489426d4246a0414fbdbb9ec58c064b
[quix0rs-gnu-social.git] / actions / twitapistatuses.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/twitterapi.php');
23
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 
27  */
28 class TwitapistatusesAction extends TwitterapiAction {
29         
30         /*
31          *  Returns the MAX_PUBSTATUSES most recent statuses from non-protected users who 
32          *  have set a custom avatar. Does not require authentication.
33          *      
34          *      URL: http://server/api/statuses/public_timeline.format
35      *
36          *      Formats: xml, json, rss, atom
37          */
38         function public_timeline($args, $apidata) {
39                 parent::handle($args);
40
41                 // Number of public statuses to return by default -- Twitter sends 20
42                 $MAX_PUBSTATUSES = 20;
43
44                 $notice = DB_DataObject::factory('notice');
45
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
48
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();
54                 
55                 if ($cnt > 0) {
56                         
57                         switch($apidata['content-type']) {
58                                 case 'xml': 
59                                         $this->show_xml_public_timeline($notice);
60                                         break;
61                                 case 'rss':
62                                         $this->show_rss_public_timeline($notice);
63                                         break;
64                                 case 'atom': 
65                                         $this->show_atom_public_timeline($notice);
66                                         break;
67                                 case 'json':
68                                         $this->show_json_public_timeline($notice);
69                                         break;
70                                 default:
71                                         common_user_error("API method not found!", $code = 404);
72                                         break;
73                         }
74                         
75                 } else {
76                         common_server_error('Couldn\'t find any statuses.', $code = 503);
77                 }
78  
79                 exit();
80         }       
81         
82         function show_xml_public_timeline($notice) {
83                 
84                 header('Content-Type: application/xml; charset=utf-8');         
85                 common_start_xml();
86                 common_element_start('statuses', array('type' => 'array'));
87                 
88                 while ($notice->fetch()) {
89                         $twitter_status = $this->twitter_status_array($notice);                                         
90                         $this->show_twitter_xml_status($twitter_status);
91                 }
92                 
93                 common_element_end('statuses');
94                 common_end_xml();
95         }
96         
97         function show_rss_public_timeline($notice) {
98                 
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'); 
103                 
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');
110         
111                 while ($notice->fetch()) {
112                         $entry = $this->twitter_rss_entry_array($notice);                                               
113                         $this->show_twitter_rss_item($entry);
114                 }
115                 
116                 common_element_end('channel');                  
117                 $this->end_twitter_rss();
118         }
119
120         function show_atom_public_timeline($notice) {
121                 
122                 header('Content-Type: application/atom+xml; charset=utf-8');
123
124                 $this->init_twitter_atom();
125                 $sitename = common_config('site', 'name');
126                 $siteserver = common_config('site', 'server');
127
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!");
132
133                 while ($notice->fetch()) {
134                         $entry = $this->twitter_rss_entry_array($notice);                                               
135                         $this->show_twitter_atom_entry($entry);
136                 }
137                 
138                 $this->end_twitter_atom();
139         }
140
141         function show_json_public_timeline($notice) {
142                 
143                 header('Content-Type: application/json; charset=utf-8');
144                 
145                 $statuses = array();
146                 
147                 while ($notice->fetch()) {      
148                         $twitter_status = $this->twitter_status_array($notice);
149                         array_push($statuses, $twitter_status);                                         
150                 }                               
151                 
152                 $this->show_twitter_json_statuses($statuses);                   
153         }
154                 
155         /*
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. 
158         
159         URL: http://server/api/statuses/friends_timeline.format
160         
161         Parameters:
162
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 
165                         your HTTP request.  
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
172         
173         Formats: xml, json, rss, atom
174         */
175         function friends_timeline($args, $apidata) {
176                 parent::handle($args);
177
178                 $since = $this->arg('since');
179                 $since_id = $this->arg('since_id');
180                 $count = $this->arg('count');
181                 $page = $this->arg('page');
182
183                 print "Friends Timeline! requested content-type: " . $apidata['content-type'] . "\n";
184                 print "since: $since, since_id: $since_id, count: $count, page: $page\n";
185                 
186                 exit();
187                 
188         }
189         
190         /*
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.
194
195                 URL: http://server/api/statuses/user_timeline.format
196
197                 Formats: xml, json, rss, atom
198
199                 Parameters:
200
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
214         */
215         function user_timeline($args, $apidata) {
216                 parent::handle($args);
217                 
218                 $id = $this->arg('id');
219                 $count = $this->arg('count');
220                 $since = $this->arg('since');
221                 $since_id = $this->arg('since_id');
222                 
223                 print "User Timeline! requested content-type: " . $apidata['content-type'] . "\n";
224                 print "id: $id since: $since, since_id: $since_id, count: $count\n";
225                 
226                 exit(); 
227         }
228         
229         /*
230                 Returns a single status, specified by the id parameter below. The status's author will be returned inline.
231                 
232                  URL: http://server/api/statuses/show/id.format
233                 
234                  Formats: xml, json
235                 
236                  Parameters:
237                 
238                  * id. Required. The numerical ID of the status you're trying to retrieve. 
239                  Ex: http://server/api/statuses/show/123.xml
240         */
241         function show($args, $apidata) {
242                 parent::handle($args);
243
244                 $id = $this->arg('id');
245                 
246                 print "show requested content-type: " . $apidata['content-type'] . "\n";
247                 print "id: $id\n";
248                 
249                 exit();
250                 
251         }
252         
253         /*
254                 Updates the authenticating user's status.  Requires the status parameter specified below.  Request must be a POST.
255
256                 URL: http://server/api/statuses/update.format
257
258                 Formats: xml, json.  Returns the posted status in requested format when successful.
259
260                 Parameters:
261
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.
264
265         */
266         function update($args, $apidata) {
267                 parent::handle($args);
268                 common_server_error("API method under construction.", $code=501);
269         }
270         
271         /*
272                 Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
273                 URL: http://server/api/statuses/replies.format
274                 
275                 Formats: xml, json, rss, atom
276
277                 Parameters:
278
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
285         */
286         function replies($args, $apidata) {
287                 parent::handle($args);
288                 common_server_error("API method under construction.", $code=501);
289         }
290         
291         
292         /*
293                 Destroys the status specified by the required ID parameter. The authenticating user must be
294         the author of the specified status.
295                 
296                  URL: http://server/api/statuses/destroy/id.format
297                 
298                  Formats: xml, json
299                 
300                  Parameters:
301                 
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
305         
306         */
307         function destroy($args, $apidata) {
308                 parent::handle($args);
309                 common_server_error("API method under construction.", $code=501);
310         }
311         
312         # User Methods
313         
314         /*
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.
317                 
318                  URL: http://server/api/statuses/friends.format
319                 
320                  Formats: xml, json
321                 
322                  Parameters:
323                 
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 
326                         or 
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
334         */
335         function friends($args, $apidata) {
336                 parent::handle($args);
337                 common_server_error("API method under construction.", $code=501);
338         }
339         
340         /*
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).
343                 
344                 URL: http://server/api/statuses/followers.format
345                 Formats: xml, json
346
347                 Parameters: 
348
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 
351                                 or 
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
356         */
357         function followers($args, $apidata) {
358                 parent::handle($args);
359                 common_server_error("API method under construction.", $code=501);
360         }
361         
362         /*
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 
365
366         Formats: xml, json
367         */
368         function featured($args, $apidata) {
369                 parent::handle($args);
370                 common_server_error("API method under construction.", $code=501);
371         }
372         
373 }
374
375