]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/twitterapi.php
708630247cd0e46668acefcef48cc20c2c8e1a21
[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         function twitter_user_array($profile) {
29                 
30                 $twitter_user = array();
31
32                 $twitter_user['name'] = $profile->getBestName();                
33                 $twitter_user['followers_count'] = $this->count_subscriptions($profile);
34                 $twitter_user['screen_name'] = $profile->nickname;
35                 $twitter_user['description'] = ($profile->bio) ? $profile->bio : NULL;
36                 $twitter_user['location'] = ($profile->location) ? $profile->location : NULL;
37                 $twitter_user['id'] = intval($profile->id);
38                 
39                 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
40                 
41                 $twitter_user['profile_image_url'] = ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE);
42                 $twitter_user['protected'] = false; # not supported by Laconica yet
43                 $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : NULL;
44                 
45                 return $twitter_user;           
46         }
47
48         function twitter_status_array($notice) {
49                 
50                 $twitter_status = array();
51
52                 $twitter_status['text'] = $notice->content;             
53                 $twitter_status['truncated'] = false; # Not possible on Laconica
54                 $twitter_status['created_at'] = $this->date_twitter($notice->created);
55                 $twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ? intval($notice->reply_to) : NULL;
56                 $twitter_status['source'] = NULL; # XXX: twitterific, twitterfox, etc. Not supported yet.
57                 $twitter_status['id'] = intval($notice->id);
58                 $twitter_status['in_reply_to_user_id'] = ($notice->reply_to) ? $this->replier_by_reply($notice->reply_to) : NULL;
59                 $twitter_status['favorited'] = NULL; # XXX: Not implemented on Laconica yet.
60                 
61                 $profile = $notice->getProfile();
62                 $twitter_user = $this->twitter_user_array($profile);
63                 $twitter_status['user'] = $twitter_user;
64                                 
65                 return $twitter_status;
66         }
67         
68         function render_twitter_xml_status($twitter_status) {   
69                 common_element_start('status');
70                 common_element('created_at', NULL, $twitter_status['created_at']);
71                 common_element('id', NULL, $twitter_status['id']);
72                 common_element('text', NULL, $twitter_status['text']);
73                 common_element('source', NULL, $twitter_status['source']);  
74                 common_element('truncated', NULL, $twitter_status['truncated']); 
75                 common_element('in_reply_to_status_id', NULL, $twitter_status['in_reply_to_status_id']);
76                 common_element('in_reply_to_user_id', NULL, $twitter_status['in_reply_to_user_id']);
77                 common_element('favorited', Null, $twitter_status['favorited']);  
78
79                 $this->render_twitter_xml_user($twitter_status['user']);
80                 
81                 common_element_end('status');
82         }       
83         
84         function render_twitter_xml_user($twitter_user) {
85                 common_element_start('user');
86                 common_element('id', NULL, $twitter_user['id']);
87                 common_element('name', NULL, $twitter_user['name']);
88                 common_element('screen_name', NULL, $twitter_user['screen_name']);
89                 common_element('location', NULL, $twitter_user['location']);
90                 common_element('description', NULL, $twitter_user['description']);              
91                 common_element('profile_image_url', NULL, $twitter_user['profile_image_url']);
92                 common_element('url', NULL, $twitter_user['url']);
93                 common_element('protected', NULL, $twitter_user['protected']);
94                 common_element('followers_count', NULL, $twitter_user['followers_count']);
95                 common_element_end('user');
96         }
97         
98         
99         function render_twitter_json_statuses($twitter_statuses) {
100                 print(json_encode($twitter_statuses));
101         }
102                 
103         // Anyone know what date format this is?  It's not RFC 2822 
104         // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach 
105         function date_twitter($dt) {
106                 $t = strtotime($dt);
107                 return date("D M d G:i:s O Y", $t);
108         }
109
110         function replier_by_reply($reply_id) {  
111
112                 $notice = Notice::staticGet($reply_id);
113         
114                 if (!$notice) {
115                         common_debug("TwitterapiAction::replier_by_reply: Got a bad notice_id: $reply_id");
116                 }
117
118                 $profile = $notice->getProfile();
119                 
120                 if (!$profile) {
121                         common_debug("TwitterapiAction::replier_by_reply: Got a bad profile_id: $profile_id");
122                         return false;
123                 }
124                 
125                 return intval($profile->id);            
126         }
127
128         // XXX: Candidate for a general utility method somewhere?       
129         function count_subscriptions($profile) {
130                 
131                 $count = 0;
132                 $sub = new Subscription();
133                 $sub->subscribed = $profile->id;
134
135                 if ($sub->find()) {
136                         while ($sub->fetch()) {
137                                 if ($sub->token) {
138                                         $other = Remote_profile::staticGet('id', $sub->subscriber);
139                                 } else {
140                                         $other = User::staticGet('id', $sub->subscriber);
141                                 }
142                                 if (!$other) {
143                                         common_debug('Got a bad subscription: '.print_r($sub,TRUE));
144                                         continue;
145                                 }               
146                                 $count++;
147                         }
148                 }
149                 
150                 if ($count > 0) {
151                         return $count;
152                 }
153                 
154                 return NULL;
155         }
156         
157 }