3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 class TwitterapiAction extends Action {
24 function handle($args) {
25 parent::handle($args);
28 function twitter_user_array($profile) {
30 $twitter_user = array();
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);
39 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
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;
48 function twitter_status_array($notice) {
50 $twitter_status = array();
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.
61 $profile = $notice->getProfile();
62 $twitter_user = $this->twitter_user_array($profile);
63 $twitter_status['user'] = $twitter_user;
65 return $twitter_status;
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']);
79 $this->render_twitter_xml_user($twitter_status['user']);
81 common_element_end('status');
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');
99 function render_twitter_json_statuses($twitter_statuses) {
100 print(json_encode($twitter_statuses));
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) {
107 return date("D M d G:i:s O Y", $t);
110 function replier_by_reply($reply_id) {
112 $notice = Notice::staticGet($reply_id);
115 common_debug("TwitterapiAction::replier_by_reply: Got a bad notice_id: $reply_id");
118 $profile = $notice->getProfile();
121 common_debug("TwitterapiAction::replier_by_reply: Got a bad profile_id: $profile_id");
125 return intval($profile->id);
128 // XXX: Candidate for a general utility method somewhere?
129 function count_subscriptions($profile) {
132 $sub = new Subscription();
133 $sub->subscribed = $profile->id;
136 while ($sub->fetch()) {
138 $other = Remote_profile::staticGet('id', $sub->subscriber);
140 $other = User::staticGet('id', $sub->subscriber);
143 common_debug('Got a bad subscription: '.print_r($sub,TRUE));