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