]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapistatuses.php
3a06d146c7b674ae3c2cf955599bd0ad7757ca9f
[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 later to make them look better later. -- Zach 
27  */
28 class TwitapistatusesAction extends TwitterapiAction {
29         
30         function is_readonly() {
31                 return false;
32         }
33         
34         function public_timeline($args, $apidata) {
35                 parent::handle($args);
36
37                 $sitename = common_config('site', 'name');
38                 $siteserver = common_config('site', 'server'); 
39                 $title = sprintf(_("%s public timeline"), $sitename);
40                 $id = "tag:$siteserver:Statuses";
41                 $link = common_root_url();
42                 $subtitle = sprintf(_("%s updates from everyone!"), $sitemap);
43
44                 // Number of public statuses to return by default -- Twitter sends 20
45                 $MAX_PUBSTATUSES = 20;
46
47                 $notice = new Notice();
48
49                 // FIXME: To really live up to the spec we need to build a list
50                 // of notices by users who have custom avatars, so fix this SQL -- Zach
51
52                 # XXX: sub-optimal performance
53                 
54                 $notice->is_local = 1;
55                 $notice->orderBy('created DESC, notice.id DESC');
56                 $notice->limit($MAX_PUBSTATUSES);
57                 $cnt = $notice->find();
58                 
59                 if ($cnt > 0) {
60                         
61                         switch($apidata['content-type']) {
62                                 case 'xml': 
63                                         $this->show_xml_timeline($notice);
64                                         break;
65                                 case 'rss':
66                                         $this->show_rss_timeline($notice, $title, $id, $link, $subtitle);
67                                         break;
68                                 case 'atom': 
69                                         $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
70                                         break;
71                                 case 'json':
72                                         $this->show_json_timeline($notice);
73                                         break;
74                                 default:
75                                         common_user_error("API method not found!", $code = 404);
76                                         break;
77                         }
78                         
79                 } else {
80                         common_server_error('Couldn\'t find any statuses.', $code = 503);
81                 }
82  
83                 exit();
84         }       
85         
86         function show_xml_timeline($notice) {
87
88                 $this->init_document('xml');
89                 common_element_start('statuses', array('type' => 'array'));
90
91                 if (is_array($notice)) {
92                         foreach ($notice as $n) {
93                                 $twitter_status = $this->twitter_status_array($n);                                              
94                                 $this->show_twitter_xml_status($twitter_status);        
95                         }
96                 } else {
97                         while ($notice->fetch()) {
98                                 $twitter_status = $this->twitter_status_array($notice);                                         
99                                 $this->show_twitter_xml_status($twitter_status);
100                         }
101                 }
102                 
103                 common_element_end('statuses');
104                 $this->end_document('xml');
105         }       
106         
107         function show_rss_timeline($notice, $title, $id, $link, $subtitle) {
108                 
109                 $this->init_document('rss');
110                 
111                 common_element_start('channel');
112                 common_element('title', NULL, $title);
113                 common_element('link', NULL, $link);
114                 common_element('description', NULL, $subtitle);
115                 common_element('language', NULL, 'en-us');
116                 common_element('ttl', NULL, '40');
117         
118         
119                 if (is_array($notice)) {
120                         foreach ($notice as $n) {
121                                 $entry = $this->twitter_rss_entry_array($n);                                            
122                                 $this->show_twitter_rss_item($entry);
123                         } 
124                 } else {
125                         while ($notice->fetch()) {
126                                 $entry = $this->twitter_rss_entry_array($notice);                                               
127                                 $this->show_twitter_rss_item($entry);
128                         }
129                 }
130
131                 common_element_end('channel');                  
132                 $this->end_twitter_rss();               
133         }
134
135         function show_atom_timeline($notice, $title, $id, $link, $subtitle=NULL) {
136                 
137                 $this->init_document('atom');
138
139                 common_element('title', NULL, $title);
140                 common_element('id', NULL, $id);
141                 common_element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), NULL);
142                 common_element('subtitle', NULL, $subtitle);
143
144                 if (is_array($notice)) {
145                         foreach ($notice as $n) {
146                                 $entry = $this->twitter_rss_entry_array($n);                                            
147                                 $this->show_twitter_atom_entry($entry);
148                         } 
149                 } else {
150                         while ($notice->fetch()) {
151                                 $entry = $this->twitter_rss_entry_array($notice);                                               
152                                 $this->show_twitter_atom_entry($entry);
153                         }
154                 }
155                 
156                 $this->end_document('atom');
157                 
158         }
159
160         function show_json_timeline($notice) {
161                 
162                 $this->init_document('json');
163                 
164                 $statuses = array();
165                 
166                 if (is_array($notice)) {
167                         foreach ($notice as $n) {
168                                 $twitter_status = $this->twitter_status_array($n);
169                                 array_push($statuses, $twitter_status);
170                         } 
171                 } else {
172                         while ($notice->fetch()) {
173                                 $twitter_status = $this->twitter_status_array($notice);
174                                 array_push($statuses, $twitter_status);
175                         }
176                 }                       
177                 
178                 $this->show_twitter_json_statuses($statuses);                   
179                 
180                 $this->end_document('json');
181         }
182                 
183         /*
184         Returns the 20 most recent statuses posted by the authenticating user and that user's friends. 
185         This is the equivalent of /home on the Web. 
186         
187         URL: http://server/api/statuses/friends_timeline.format
188         
189         Parameters:
190
191             * since.  Optional.  Narrows the returned results to just those statuses created after the specified 
192                         HTTP-formatted date.  The same behavior is available by setting an If-Modified-Since header in 
193                         your HTTP request.  
194                         Ex: http://server/api/statuses/friends_timeline.rss?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
195             * since_id.  Optional.  Returns only statuses with an ID greater than (that is, more recent than) 
196                         the specified ID.  Ex: http://server/api/statuses/friends_timeline.xml?since_id=12345
197             * count.  Optional.  Specifies the number of statuses to retrieve. May not be greater than 200.
198                         Ex: http://server/api/statuses/friends_timeline.xml?count=5 
199             * page. Optional. Ex: http://server/api/statuses/friends_timeline.rss?page=3
200         
201         Formats: xml, json, rss, atom
202         */
203         function friends_timeline($args, $apidata) {
204                 parent::handle($args);
205
206                 $since = $this->arg('since');
207                 $since_id = $this->arg('since_id');
208                 $count = $this->arg('count');
209                 $page = $this->arg('page');
210                 
211                 if (!$page) {
212                         $page = 1;
213                 }
214
215                 if (!$count) {
216                         $count = 20;
217                 }
218
219                 $user = $this->get_user($id, $apidata);
220                 $profile = $user->getProfile();
221                 
222                 $sitename = common_config('site', 'name');
223                 $siteserver = common_config('site', 'server'); 
224                 
225                 $title = sprintf(_("%s and friends"), $user->nickname);
226                 $id = "tag:$siteserver:friends:".$user->id;
227                 $link = common_local_url('all', array('nickname' => $user->nickname));
228                 $subtitle = sprintf(_("Updates from %s and friends on %s!"), $user->nickname, $sitename);
229
230                 $notice = $user->noticesWithFriends(($page-1)*20, $count);
231                 
232                 switch($apidata['content-type']) {
233                  case 'xml': 
234                         $this->show_xml_timeline($notice);
235                         break;
236                  case 'rss':
237                         $this->show_rss_timeline($notice, $title, $id, $link, $subtitle);
238                         break;
239                  case 'atom': 
240                         $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
241                         break;
242                  case 'json':
243                         $this->show_json_timeline($notice);
244                         break;
245                  default:
246                         common_user_error("API method not found!", $code = 404);
247                 }
248                 
249                 exit();
250         }
251
252         /*
253                 Returns the 20 most recent statuses posted from the authenticating user. It's also possible to
254         request another user's timeline via the id parameter below. This is the equivalent of the Web
255         /archive page for your own user, or the profile page for a third party.
256
257                 URL: http://server/api/statuses/user_timeline.format
258
259                 Formats: xml, json, rss, atom
260
261                 Parameters:
262
263                     * id. Optional. Specifies the ID or screen name of the user for whom to return the
264             friends_timeline. Ex: http://server/api/statuses/user_timeline/12345.xml or
265             http://server/api/statuses/user_timeline/bob.json. 
266                         * count. Optional. Specifies the number of
267             statuses to retrieve. May not be greater than 200. Ex:
268             http://server/api/statuses/user_timeline.xml?count=5 
269                         * since. Optional. Narrows the returned
270             results to just those statuses created after the specified HTTP-formatted date. The same
271             behavior is available by setting an If-Modified-Since header in your HTTP request. Ex:
272             http://server/api/statuses/user_timeline.rss?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT 
273                         * since_id. Optional. Returns only statuses with an ID greater than (that is, more recent than)
274             the specified ID. Ex: http://server/api/statuses/user_timeline.xml?since_id=12345 * page.
275             Optional. Ex: http://server/api/statuses/friends_timeline.rss?page=3
276         */
277         function user_timeline($args, $apidata) {
278                 parent::handle($args);
279                 
280                 $user = null;
281                 
282                 // function was called with an argument /statuses/user_timeline/api_arg.format
283                 if (isset($apidata['api_arg'])) {
284                 
285                         if (is_numeric($apidata['api_arg'])) {
286                                 $user = User::staticGet($apidata['api_arg']);
287                         } else {
288                                 $nickname = common_canonical_nickname($apidata['api_arg']);
289                                 $user = User::staticGet('nickname', $nickname);
290                         } 
291                 } else {
292                         
293                         // if no user was specified, then we'll use the authenticated user
294                         $user = $apidata['user'];
295                 }
296
297                 if (!$user) {
298                         // Set the user to be the auth user if asked-for can't be found
299                         // honestly! This is what Twitter does, I swear --Zach
300                         $user = $apidata['user'];
301                 }
302
303                 $profile = $user->getProfile();
304
305                 if (!$profile) {
306                         common_server_error(_('User has no profile.'));
307                         exit();
308                 }
309                                 
310                 $count = $this->arg('count');
311                 $since = $this->arg('since');
312                 $since_id = $this->arg('since_id');
313                                 
314                 if (!$page) {
315                         $page = 1;
316                 }
317
318                 if (!$count) {
319                         $count = 20;
320                 }
321                                 
322                 $sitename = common_config('site', 'name');
323                 $siteserver = common_config('site', 'server'); 
324                 
325                 $title = sprintf(_("%s timeline"), $user->nickname);
326                 $id = "tag:$siteserver:user:".$user->id;
327                 $link = common_local_url('showstream', array('nickname' => $user->nickname));
328                 $subtitle = sprintf(_("Updates from %s on %s!"), $user->nickname, $sitename);
329
330                 $notice = new Notice();
331
332                 $notice->profile_id = $user->id;
333                 
334                 # XXX: since
335                 # XXX: since_id
336                 
337                 $notice->orderBy('created DESC, notice.id DESC');
338
339                 $notice->limit((($page-1)*20), $count);
340
341                 $cnt = $notice->find();
342                 
343                 switch($apidata['content-type']) {
344                  case 'xml': 
345                         $this->show_xml_timeline($notice);
346                         break;
347                  case 'rss':
348                         $this->show_rss_timeline($notice, $title, $id, $link, $subtitle);
349                         break;
350                  case 'atom': 
351                         $this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
352                         break;
353                  case 'json':
354                         $this->show_json_timeline($notice);
355                         break;
356                  default:
357                         common_user_error("API method not found!", $code = 404);
358                 }
359                 
360                 exit();
361         }
362                 
363         function update($args, $apidata) {
364                 parent::handle($args);
365                 
366                 $user = $apidata['user'];
367                                 
368                 $notice = DB_DataObject::factory('notice');             
369                 
370                 $notice->profile_id = $user->id; # user id *is* profile id
371                 $notice->created = DB_DataObject_Cast::dateTime();      
372                 $notice->content = $this->trimmed('status');
373
374                 if (!$notice->content) {
375                         
376                         // XXX: Note: In this case, Twitter simply returns '200 OK'
377                         // No error is given, but the status is not posted to the 
378                         // user's timeline.  Seems bad.  Shouldn't we throw an 
379                         // errror? -- Zach
380                         exit();
381                         
382                 } else if (strlen($notice->content) > 140) {
383
384                         // XXX: Twitter truncates anything over 140, flags the status 
385                     // as "truncated."  Sending this error may screw up some clients
386                     // that assume Twitter will truncate for them.  Should we just
387                     // truncate too? -- Zach
388                         header('HTTP/1.1 406 Not Acceptable');                  
389                         print "That's too long. Max notice size is 140 chars.\n";
390                         exit();
391                 }
392
393                 $notice->rendered = common_render_content($notice->content, $notice);
394
395                 $id = $notice->insert();
396
397                 if (!$id) {
398                         common_server_error('Could not update status!', 500);
399                         exit();
400                 }
401
402                 $orig = clone($notice);
403                 $notice->uri = common_notice_uri($notice);
404
405                 if (!$notice->update($orig)) {
406                         common_server_error('Could not save status!', 500);
407                         exit();
408                 }
409
410         common_save_replies($notice);
411                 common_broadcast_notice($notice);
412
413                 // FIXME: Bad Hack 
414                 // I should be able to just sent this notice off for display,
415                 // but $notice->created does not contain a string at this
416                 // point and I don't know how to convert it to one here. So
417                 // I'm forced to have DBObject pull the notice back out of the
418                 // DB before printing. --Zach
419                 $apidata['api_arg'] = $id;
420                 $this->show($args, $apidata);
421
422                 exit();
423         }
424         
425         /*
426                 Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
427                 URL: http://server/api/statuses/replies.format
428                 
429                 Formats: xml, json, rss, atom
430
431                 Parameters:
432
433                 * page. Optional. Retrieves the 20 next most recent replies. Ex: http://server/api/statuses/replies.xml?page=3 
434                 * since. Optional. Narrows the returned results to just those replies created after the specified HTTP-formatted date. The
435         same behavior is available by setting an If-Modified-Since header in your HTTP request. Ex:
436         http://server/api/statuses/replies.xml?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
437                 * since_id. Optional. Returns only statuses with an ID greater than (that is, more recent than) the specified
438                 ID. Ex: http://server/api/statuses/replies.xml?since_id=12345
439         */
440         function replies($args, $apidata) {
441
442                 parent::handle($args);
443
444                 $since = $this->arg('since');
445
446                 $count = $this->arg('count');
447                 $page = $this->arg('page');
448
449                 $user = $apidata['user'];
450                 $profile = $user->getProfile();
451
452                 $sitename = common_config('site', 'name');
453                 $siteserver = common_config('site', 'server'); 
454
455                 $title = sprintf(_("%s / Updates replying to %s"), $sitename, $user->nickname);
456                 $id = "tag:$siteserver:replies:".$user->id;
457                 $link = common_local_url('replies', array('nickname' => $user->nickname));
458                 $subtitle = "gar";
459                 $subtitle = sprintf(_("%s updates that reply to updates from %s / %s."), $sitename, $user->nickname, $profile->getBestName());
460
461                 if (!$page) {
462                         $page = 1;
463                 }
464
465                 if (!$count) {
466                         $count = 20;
467                 }
468
469                 $reply = new Reply();
470
471                 $reply->profile_id = $user->id;
472
473                 $reply->orderBy('modified DESC');
474
475                 $page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
476
477                 $reply->limit((($page-1)*20), $count);
478
479                 $cnt = $reply->find();
480
481                 $notices = array();
482         
483                 if ($cnt) {
484                         while ($reply->fetch()) {
485                                 $notice = new Notice();
486                                 $notice->id = $reply->notice_id;
487                                 $result = $notice->find(true);
488                                 if (!$result) {
489                                         continue;
490                                 }
491                                 $notices[] = clone($notice);
492                         }
493                 }
494
495                 switch($apidata['content-type']) {
496                  case 'xml': 
497                         $this->show_xml_timeline($notices);
498                         break;
499                  case 'rss':
500                         $this->show_rss_timeline($notices, $title, $id, $link, $subtitle);
501                         break;
502                  case 'atom': 
503                         $this->show_atom_timeline($notices, $title, $id, $link, $subtitle);
504                         break;
505                  case 'json':
506                         $this->show_json_timeline($notices);
507                         break;
508                  default:
509                         common_user_error("API method not found!", $code = 404);
510                 }
511
512
513                 exit();
514
515
516         }
517
518         
519         
520         /*
521                 Destroys the status specified by the required ID parameter. The authenticating user must be
522         the author of the specified status.
523                 
524                  URL: http://server/api/statuses/destroy/id.format
525                 
526                  Formats: xml, json
527                 
528                  Parameters:
529                 
530                  * id. Required. The ID of the status to destroy. Ex:
531                 http://server/api/statuses/destroy/12345.json or
532                 http://server/api/statuses/destroy/23456.xml
533         
534         */
535         function destroy($args, $apidata) {
536                 parent::handle($args);
537                 common_server_error("API method under construction.", $code=501);
538         }
539         
540         # User Methods
541         
542         /*
543                 Returns up to 100 of the authenticating user's friends who have most recently updated, each with current status inline.
544         It's also possible to request another user's recent friends list via the id parameter below.
545                 
546                  URL: http://server/api/statuses/friends.format
547                 
548                  Formats: xml, json
549                 
550                  Parameters:
551                 
552                  * id. Optional. The ID or screen name of the user for whom to request a list of friends. Ex:
553                 http://server/api/statuses/friends/12345.json 
554                         or 
555                         http://server/api/statuses/friends/bob.xml
556                  * page. Optional. Retrieves the next 100 friends. Ex: http://server/api/statuses/friends.xml?page=2
557                  * lite. Optional. Prevents the inline inclusion of current status. Must be set to a value of true. Ex:
558                 http://server/api/statuses/friends.xml?lite=true
559                  * since. Optional. Narrows the returned results to just those friendships created after the specified
560                         HTTP-formatted date. The same behavior is available by setting an If-Modified-Since header in your HTTP
561                         request. Ex: http://server/api/statuses/friends.xml?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
562         */
563         function friends($args, $apidata) {
564                 parent::handle($args);
565                 return $this->subscriptions($apidata, 'subscribed', 'subscriber');
566         }
567         
568         /*
569                 Returns the authenticating user's followers, each with current status inline. They are ordered by the
570                 order in which they joined Twitter (this is going to be changed).
571                 
572                 URL: http://server/api/statuses/followers.format
573                 Formats: xml, json
574
575                 Parameters: 
576
577                     * id. Optional. The ID or screen name of the user for whom to request a list of followers. Ex:
578                 http://server/api/statuses/followers/12345.json 
579                                 or 
580                                 http://server/api/statuses/followers/bob.xml
581                     * page. Optional. Retrieves the next 100 followers. Ex: http://server/api/statuses/followers.xml?page=2   
582                     * lite. Optional. Prevents the inline inclusion of current status. Must be set to a value of true.
583                                 Ex: http://server/api/statuses/followers.xml?lite=true
584         */
585         function followers($args, $apidata) {
586                 parent::handle($args);
587
588                 return $this->subscriptions($apidata, 'subscriber', 'subscribed');
589         }
590
591         function subscriptions($apidata, $other_attr, $user_attr) {
592                 
593                 $user = $this->get_subs_user($apidata);
594                 
595                 # XXX: id
596                 # XXX: lite
597                 
598                 $page = $this->trimmed('page');
599                 
600                 if (!$page || !is_numeric($page)) {
601                         $page = 1;
602                 }
603                 
604                 $profile = $user->getProfile();
605                 
606                 if (!$profile) {
607                         common_server_error(_('User has no profile.'));
608                         return;
609                 }
610                                 
611                 $sub = new Subscription();
612                 $sub->$user_attr = $profile->id;
613                 $sub->orderBy('created DESC');
614                 $sub->limit(($page-1)*100, 100);
615                 
616                 $others = array();
617
618                 if ($sub->find()) {
619                         while ($sub->fetch()) {
620                                 $others[] = Profile::staticGet($sub->$other_attr);
621                         }
622                 } else {
623                         // user has no followers
624                 }
625                 
626                 $type = $apidata['content-type'];
627                 
628                 $this->init_document($type);
629                 $this->show_profiles($others, $type);
630                 $this->end_document($type);
631                 exit();
632         }
633
634         function get_subs_user($apidata) {
635                 
636                 // function was called with an argument /statuses/user_timeline/api_arg.format
637                 if (isset($apidata['api_arg'])) {
638                 
639                         if (is_numeric($apidata['api_arg'])) {
640                                 $user = User::staticGet($apidata['api_arg']);
641                         } else {
642                                 $nickname = common_canonical_nickname($apidata['api_arg']);
643                                 $user = User::staticGet('nickname', $nickname);
644                         } 
645                 } else {
646                         
647                         // if no user was specified, then we'll use the authenticated user
648                         $user = $apidata['user'];
649                 }
650
651                 if (!$user) {
652                         // Set the user to be the auth user if asked-for can't be found
653                         // honestly! This is what Twitter does, I swear --Zach
654                         $user = $apidata['user'];
655                 }
656                 
657                 return $user;
658         }
659         
660         function show_profiles($profiles, $type) {
661                 switch ($type) {
662                  case 'xml':
663                         common_element_start('users', array('type' => 'array'));
664                         foreach ($profiles as $profile) {
665                                 $this->show_profile($profile);
666                         }
667                         common_element_end('users');
668                         break;
669                  case 'json':
670                         $arrays = array();
671                         foreach ($profiles as $profile) {
672                                 $arrays[] = $this->twitter_user_array($profile, true);
673                         }
674                         print json_encode($arrays);
675                         break;
676                  default:
677                         $this->client_error(_('unsupported file type'));
678                         exit();
679                 }
680         }
681         
682         /*
683         Returns a list of the users currently featured on the site with their current statuses inline. 
684         URL: http://server/api/statuses/featured.format 
685
686         Formats: xml, json
687         */
688         function featured($args, $apidata) {
689                 parent::handle($args);
690                 common_server_error("API method under construction.", $code=501);
691         }
692
693         function get_user($id, $apidata) {
694                 if (!$id) {
695                         return $apidata['user'];
696                 } else if (is_numeric($id)) {
697                         return User::staticGet($id);
698                 } else {
699                         return User::staticGet('nickname', $id);
700                 }
701         }
702 }
703
704