]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/twitterapi.php
Twitter-compatible API: public_timeline.xml more complete
[quix0rs-gnu-social.git] / lib / twitterapi.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 class TwitterapiAction extends Action {
23
24         function handle($args) {
25                 parent::handle($args);
26         }
27         
28         /*
29          * Spits out a Laconica Notice as a Twitter-XML "status" 
30          */
31         function render_xml_status($notice) {
32                 global $config;
33         
34                 common_element_start('status');
35                 common_element('created_at', NULL, $this->date_twitter($notice->created));
36                 common_element('id', NULL, $notice->id);
37                 common_element('text', NULL, $notice->content);
38                 common_element('source', NULL, '');  # XXX: twitterific, twitterfox, etc. Not supported yet.
39                 common_element('truncated', NULL, 'false'); # Not possible on Laconica
40                 common_element('in_reply_to_status_id', NULL, $notice->reply_to);
41                 common_element('in_reply_to_user_id', NULL, ($notice->reply_to) ? $this->replier_by_reply($notice->reply_to) : '');
42                 common_element('favorited', Null, '');  # XXX: Not implemented on Laconica yet.
43
44                 $profile = $notice->getProfile();               
45                 $this->render_xml_user($profile);
46                 
47                 common_element_end('status');
48         }       
49         
50         /*
51          * Spits out a Laconica Profile as a Twitter-XML "user"
52          */
53         function render_xml_user($profile) {
54                 common_element_start('user');
55                 common_element('id', NULL, $profile->id);
56                 common_element('name', NULL, $profile->getBestName());
57                 common_element('screen_name', NULL, $profile->nickname);
58                 common_element('location', NULL, $profile->location);
59                 common_element('description', NULL, $profile->bio);
60                 
61                 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
62                 
63                 common_element('profile_image_url', NULL, ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE));
64                 common_element('url', NULL, $profile->homepage);
65                 common_element('protected', NULL, 'false'); # not supported by Laconica yet
66                 common_element('followers_count', NULL, $this->count_subscriptions($profile));
67                 common_element_end('user');
68         }
69         
70         // Anyone know what date format this is?  It's not RFC 2822 
71         // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach 
72         function date_twitter($dt) {
73                 $t = strtotime($dt);
74                 return date("D M d G:i:s O Y", $t);
75         }
76
77         function replier_by_reply($reply_id) {  
78
79                 $notice = Notice::staticGet($reply_id);
80         
81                 if (!$notice) {
82                         common_debug("Got a bad notice_id: $reply_id");
83                 }
84
85                 $profile = $notice->getProfile();
86                 
87                 if (!$profile) {
88                         common_debug("Got a bad profile_id: $profile_id");
89                         return false;
90                 }
91                 
92                 return $profile->id;            
93         }
94
95         // XXX: Candidate for a general utility method somewhere?       
96         function count_subscriptions($profile) {
97                 
98                 $count = 0;
99                 $sub = new Subscription();
100                 $sub->subscribed = $profile->id;
101
102                 if ($sub->find()) {
103                         while ($sub->fetch()) {
104                                 if ($sub->token) {
105                                         $other = Remote_profile::staticGet('id', $sub->subscriber);
106                                 } else {
107                                         $other = User::staticGet('id', $sub->subscriber);
108                                 }
109                                 if (!$other) {
110                                         common_debug('Got a bad subscription: '.print_r($sub,TRUE));
111                                         continue;
112                                 }               
113                                 $count++;
114                         }
115                 }
116                 return $count;
117         }
118         
119 }