]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapistatuses.php
98ca299f6ddc54a35b99985d3209d04d3234bb74
[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 notice, specified by the id parameter below. 
231          * The status's author will be returned inline.
232          *      
233          * URL: http://server/api/statuses/show/id.format
234          *      
235          * Formats: xml, json
236          *      
237      * Parameters:
238      *          
239      * id. Required. The numerical ID of the status you're trying to retrieve. 
240          * Ex: http://server/api/statuses/show/123.xml
241          *
242          */     
243         function show($args, $apidata) {
244                 parent::handle($args);
245
246                 $id = $apidata['api_arg'];
247                 $notice = Notice::staticGet($id);
248
249                 if ($notice) {
250
251                         if ($apidata['content-type'] == 'xml') { 
252                                 header('Content-Type: application/xml; charset=utf-8');         
253                         
254                                 common_start_xml();
255                                 $twitter_status = $this->twitter_status_array($notice);                                         
256                                 $this->show_twitter_xml_status($twitter_status);
257                                 common_end_xml();
258                         
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);
263                         }
264                 } else {
265                         header('HTTP/1.1 404 Not Found');
266                 }
267                 
268                 exit();
269         }
270         
271         /*
272                 Updates the authenticating user's status.  Requires the status parameter specified below.  Request must be a POST.
273
274                 URL: http://server/api/statuses/update.format
275
276                 Formats: xml, json.  Returns the posted status in requested format when successful.
277
278                 Parameters:
279
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.
282
283         */
284         function update($args, $apidata) {
285                 parent::handle($args);
286                 common_server_error("API method under construction.", $code=501);
287         }
288         
289         /*
290                 Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
291                 URL: http://server/api/statuses/replies.format
292                 
293                 Formats: xml, json, rss, atom
294
295                 Parameters:
296
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
303         */
304         function replies($args, $apidata) {
305                 parent::handle($args);
306                 common_server_error("API method under construction.", $code=501);
307         }
308         
309         
310         /*
311                 Destroys the status specified by the required ID parameter. The authenticating user must be
312         the author of the specified status.
313                 
314                  URL: http://server/api/statuses/destroy/id.format
315                 
316                  Formats: xml, json
317                 
318                  Parameters:
319                 
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
323         
324         */
325         function destroy($args, $apidata) {
326                 parent::handle($args);
327                 common_server_error("API method under construction.", $code=501);
328         }
329         
330         # User Methods
331         
332         /*
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.
335                 
336                  URL: http://server/api/statuses/friends.format
337                 
338                  Formats: xml, json
339                 
340                  Parameters:
341                 
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 
344                         or 
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
352         */
353         function friends($args, $apidata) {
354                 parent::handle($args);
355                 common_server_error("API method under construction.", $code=501);
356         }
357         
358         /*
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).
361                 
362                 URL: http://server/api/statuses/followers.format
363                 Formats: xml, json
364
365                 Parameters: 
366
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 
369                                 or 
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
374         */
375         function followers($args, $apidata) {
376                 parent::handle($args);
377                 common_server_error("API method under construction.", $code=501);
378         }
379         
380         /*
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 
383
384         Formats: xml, json
385         */
386         function featured($args, $apidata) {
387                 parent::handle($args);
388                 common_server_error("API method under construction.", $code=501);
389         }
390         
391 }
392
393