]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapistatuses.php
f9b804bd16d423ff25d7dbcffe165351f9e1f566
[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 20 most recent statuses from non-protected users who have set a custom
32          *  user icon. Does not require authentication.
33          *      
34          *      URL: http://identi.ca/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                 $notice = DB_DataObject::factory('notice');
42
43                 # FIXME: bad performance
44                 $notice->whereAdd('EXISTS (SELECT user.id from user where user.id = notice.profile_id)');
45                 $notice->orderBy('created DESC, notice.id DESC');
46                 $notice->limit(20);
47                 $cnt = $notice->find();
48
49                 if ($apidata['content-type'] == 'xml') {
50                         
51                         header('Content-Type: application/xml; charset=utf-8');         
52                         
53                         common_start_xml();
54
55                         // XXX: To really live up to the spec we need to build a list
56                         // of notices by users who have custom avatars -- Zach
57                         if ($cnt > 0) {
58                                 common_element_start('statuses', array('type' => 'array'));
59                                 for ($i = 0; $i < 20; $i++) {
60                                         if ($notice->fetch()) {
61                                                 $twitter_status = $this->twitter_status_array($notice);                                         
62                                                 $this->render_twitter_xml_status($twitter_status);
63                                         } else {
64                                                 // shouldn't happen!
65                                                 break;
66                                         }
67                                 }
68                                 common_element_end('statuses');
69                         }
70                 
71                         common_end_xml();
72                 } elseif ($apidata['content-type'] == 'rss') {
73                         
74                         header("Content-Type: application/rss+xml; charset=utf-8");
75
76                         $this->init_twitter_rss();
77                         
78                         $sitename = common_config('site', 'name');
79                         $siteserver = common_config('site', 'server'); 
80                         
81                         common_element_start('channel');
82                         common_element('title', NULL, "$sitename public timeline");
83                         common_element('link', NULL, "http://$siteserver");
84                         common_element('description', NULL, "$sitename updates from everyone!");
85                         common_element('language', NULL, 'en-us');
86                         common_element('ttl', NULL, '40');
87                         
88                         if ($cnt > 0) {
89                                 for ($i = 0; $i < 20; $i++) {
90                                         if ($notice->fetch()) {
91                                                 $entry = $this->twitter_rss_entry_array($notice);                                               
92                                                 $this->show_twitter_rss_item($entry);
93                                         } else {
94                                                 // shouldn't happen!
95                                                 break;
96                                         }
97                                 }
98                         }
99                         common_element_end('channel');
100                                                 
101                         $this->end_twitter_rss();
102
103                 } elseif ($apidata['content-type'] == 'atom') {
104
105                         header('Content-Type: application/atom+xml; charset=utf-8');
106
107                         $this->init_twitter_atom();
108                         
109                         $sitename = common_config('site', 'name');
110                         $siteserver = common_config('site', 'server');
111
112                         common_element('title', NULL, "$sitename public timeline");
113                         common_element('id', NULL, "tag:$siteserver:Statuses");
114                         common_element('link', array('href' => "http://$siteserver", 'rel' => 'alternate', 'type' => 'text/html'), NULL);
115                         common_element('subtitle', NULL, "$sitename updates from everyone!");
116                         
117                         if ($cnt > 0) {
118                                 for ($i = 0; $i < 20; $i++) {
119                                         if ($notice->fetch()) {
120                                                 $entry = $this->twitter_rss_entry_array($notice);                                               
121                                                 $this->show_twitter_atom_entry($entry);
122                                         } else {
123                                                 // shouldn't happen!
124                                                 break;
125                                         }
126                                 }
127                         }
128
129                         $this->end_twitter_atom();
130
131                 } elseif ($apidata['content-type'] == 'json') {
132
133                         header('Content-Type: application/json; charset=utf-8');
134
135                         $statuses = array();
136                         
137                         if ($cnt > 0) {
138                                 for ($i = 0; $i < 20; $i++) {
139                                         if ($notice->fetch()) {
140                                                 $twitter_status = $this->twitter_status_array($notice);
141                                                 array_push($statuses, $twitter_status);                                         
142                                         } else {
143                                                 // shouldn't happen!
144                                                 break;
145                                         }
146                                 }                               
147                         }
148                         $this->render_twitter_json_statuses($statuses);                 
149                 }
150
151                 exit();
152         }       
153         
154
155                 
156         /*
157         Returns the 20 most recent statuses posted by the authenticating user and that user's friends. 
158         This is the equivalent of /home on the Web. 
159         
160         URL: http://identi.ca/api/statuses/friends_timeline.format
161         
162         Parameters:
163
164             * since.  Optional.  Narrows the returned results to just those statuses created after the specified 
165                         HTTP-formatted date.  The same behavior is available by setting an If-Modified-Since header in 
166                         your HTTP request.  
167                         Ex: http://identi.ca/api/statuses/friends_timeline.rss?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
168             * since_id.  Optional.  Returns only statuses with an ID greater than (that is, more recent than) 
169                         the specified ID.  Ex: http://identi.ca/api/statuses/friends_timeline.xml?since_id=12345
170             * count.  Optional.  Specifies the number of statuses to retrieve. May not be greater than 200.
171                         Ex: http://identi.ca/api/statuses/friends_timeline.xml?count=5 
172             * page. Optional. Ex: http://identi.ca/api/statuses/friends_timeline.rss?page=3
173         
174         Formats: xml, json, rss, atom
175         */
176         function friends_timeline($args, $apidata) {
177                 parent::handle($args);
178
179                 $since = $this->arg('since');
180                 $since_id = $this->arg('since_id');
181                 $count = $this->arg('count');
182                 $page = $this->arg('page');
183
184                 print "Friends Timeline! requested content-type: " . $apidata['content-type'] . "\n";
185                 print "since: $since, since_id: $since_id, count: $count, page: $page\n";
186                 
187                 exit();
188                 
189         }
190         
191         /*
192                 Returns the 20 most recent statuses posted from the authenticating user. It's also possible to
193         request another user's timeline via the id parameter below. This is the equivalent of the Web
194         /archive page for your own user, or the profile page for a third party.
195
196                 URL: http://identi.ca/api/statuses/user_timeline.format
197
198                 Formats: xml, json, rss, atom
199
200                 Parameters:
201
202                     * id. Optional. Specifies the ID or screen name of the user for whom to return the
203             friends_timeline. Ex: http://identi.ca/api/statuses/user_timeline/12345.xml or
204             http://identi.ca/api/statuses/user_timeline/bob.json. 
205                         * count. Optional. Specifies the number of
206             statuses to retrieve. May not be greater than 200. Ex:
207             http://identi.ca/api/statuses/user_timeline.xml?count=5 
208                         * since. Optional. Narrows the returned
209             results to just those statuses created after the specified HTTP-formatted date. The same
210             behavior is available by setting an If-Modified-Since header in your HTTP request. Ex:
211             http://identi.ca/api/statuses/user_timeline.rss?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT 
212                         * since_id. Optional. Returns only statuses with an ID greater than (that is, more recent than)
213             the specified ID. Ex: http://identi.ca/api/statuses/user_timeline.xml?since_id=12345 * page.
214             Optional. Ex: http://identi.ca/api/statuses/friends_timeline.rss?page=3
215         */
216         function user_timeline($args, $apidata) {
217                 parent::handle($args);
218                 
219                 $id = $this->arg('id');
220                 $count = $this->arg('count');
221                 $since = $this->arg('since');
222                 $since_id = $this->arg('since_id');
223                 
224                 print "User Timeline! requested content-type: " . $apidata['content-type'] . "\n";
225                 print "id: $id since: $since, since_id: $since_id, count: $count\n";
226                 
227                 exit(); 
228         }
229         
230         /*
231                 Returns a single status, specified by the id parameter below. The status's author will be returned inline.
232                 
233                  URL: http://identi.ca/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://identi.ca/api/statuses/show/123.xml
241         */
242         function show($args, $apidata) {
243                 parent::handle($args);
244
245                 $id = $this->arg('id');
246                 
247                 print "show requested content-type: " . $apidata['content-type'] . "\n";
248                 print "id: $id\n";
249                 
250                 exit();
251                 
252         }
253         
254         /*
255                 Updates the authenticating user's status.  Requires the status parameter specified below.  Request must be a POST.
256
257                 URL: http://identi.ca/api/statuses/update.format
258
259                 Formats: xml, json.  Returns the posted status in requested format when successful.
260
261                 Parameters:
262
263                     * status. Required. The text of your status update. Be sure to URL encode as necessary. Must not be more than 160
264             characters and should not be more than 140 characters to ensure optimal display.
265
266         */
267         function update($args, $apidata) {
268                 parent::handle($args);
269                 common_server_error("API method under construction.", $code=501);
270         }
271         
272         /*
273                 Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
274                 URL: http://identi.ca/api/statuses/replies.format
275                 
276                 Formats: xml, json, rss, atom
277
278                 Parameters:
279
280                 * page. Optional. Retrieves the 20 next most recent replies. Ex: http://identi.ca/api/statuses/replies.xml?page=3 
281                 * since. Optional. Narrows the returned results to just those replies created after the specified HTTP-formatted date. The
282         same behavior is available by setting an If-Modified-Since header in your HTTP request. Ex:
283         http://identi.ca/api/statuses/replies.xml?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
284                 * since_id. Optional. Returns only statuses with an ID greater than (that is, more recent than) the specified
285                 ID. Ex: http://identi.ca/api/statuses/replies.xml?since_id=12345
286         */
287         function replies($args, $apidata) {
288                 parent::handle($args);
289                 common_server_error("API method under construction.", $code=501);
290         }
291         
292         
293         /*
294                 Destroys the status specified by the required ID parameter. The authenticating user must be
295         the author of the specified status.
296                 
297                  URL: http://identi.ca/api/statuses/destroy/id.format
298                 
299                  Formats: xml, json
300                 
301                  Parameters:
302                 
303                  * id. Required. The ID of the status to destroy. Ex:
304                 http://identi.ca/api/statuses/destroy/12345.json or
305                 http://identi.ca/api/statuses/destroy/23456.xml
306         
307         */
308         function destroy($args, $apidata) {
309                 parent::handle($args);
310                 common_server_error("API method under construction.", $code=501);
311         }
312         
313         # User Methods
314         
315         /*
316                 Returns up to 100 of the authenticating user's friends who have most recently updated, each with current status inline.
317         It's also possible to request another user's recent friends list via the id parameter below.
318                 
319                  URL: http://identi.ca/api/statuses/friends.format
320                 
321                  Formats: xml, json
322                 
323                  Parameters:
324                 
325                  * id. Optional. The ID or screen name of the user for whom to request a list of friends. Ex:
326                 http://identi.ca/api/statuses/friends/12345.json 
327                         or 
328                         http://identi.ca/api/statuses/friends/bob.xml
329                  * page. Optional. Retrieves the next 100 friends. Ex: http://identi.ca/api/statuses/friends.xml?page=2
330                  * lite. Optional. Prevents the inline inclusion of current status. Must be set to a value of true. Ex:
331                 http://identi.ca/api/statuses/friends.xml?lite=true
332                  * since. Optional. Narrows the returned results to just those friendships created after the specified
333                         HTTP-formatted date. The same behavior is available by setting an If-Modified-Since header in your HTTP
334                         request. Ex: http://identi.ca/api/statuses/friends.xml?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
335         */
336         function friends($args, $apidata) {
337                 parent::handle($args);
338                 common_server_error("API method under construction.", $code=501);
339         }
340         
341         /*
342                 Returns the authenticating user's followers, each with current status inline. They are ordered by the
343                 order in which they joined Twitter (this is going to be changed).
344                 
345                 URL: http://identi.ca/api/statuses/followers.format
346                 Formats: xml, json
347
348                 Parameters: 
349
350                     * id. Optional. The ID or screen name of the user for whom to request a list of followers. Ex:
351                 http://identi.ca/api/statuses/followers/12345.json 
352                                 or 
353                                 http://identi.ca/api/statuses/followers/bob.xml
354                     * page. Optional. Retrieves the next 100 followers. Ex: http://identi.ca/api/statuses/followers.xml?page=2   
355                     * lite. Optional. Prevents the inline inclusion of current status. Must be set to a value of true.
356                                 Ex: http://identi.ca/api/statuses/followers.xml?lite=true
357         */
358         function followers($args, $apidata) {
359                 parent::handle($args);
360                 common_server_error("API method under construction.", $code=501);
361         }
362         
363         /*
364         Returns a list of the users currently featured on the site with their current statuses inline. 
365         URL: http://identi.ca/api/statuses/featured.format 
366
367         Formats: xml, json
368         */
369         function featured($args, $apidata) {
370                 parent::handle($args);
371                 common_server_error("API method under construction.", $code=501);
372         }
373         
374 }
375
376