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 twitter_rss_entry_array($notice) {
70 $profile = $notice->getProfile();
72 $server = common_config('site', 'server');
75 $entry['content'] = $profile->nickname . ': ' . $notice->content;
76 $entry['title'] = $entry['content'];
77 $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));;
78 $entry['published'] = common_date_iso8601($notice->created);
79 $entry['id'] = "tag:$server,$entry[published]:$entry[link]";
80 $entry['updated'] = $entry['published'];
83 $entry['description'] = $entry['content'];
84 $entry['pubDate'] = common_date_rfc2822($notice->created);
85 $entry['guid'] = $entry['link'];
90 function show_twitter_xml_status($twitter_status) {
91 common_element_start('status');
92 common_element('created_at', NULL, $twitter_status['created_at']);
93 common_element('id', NULL, $twitter_status['id']);
94 common_element('text', NULL, $twitter_status['text']);
95 common_element('source', NULL, $twitter_status['source']);
96 common_element('truncated', NULL, $twitter_status['truncated']);
97 common_element('in_reply_to_status_id', NULL, $twitter_status['in_reply_to_status_id']);
98 common_element('in_reply_to_user_id', NULL, $twitter_status['in_reply_to_user_id']);
99 common_element('favorited', Null, $twitter_status['favorited']);
101 $this->show_twitter_xml_user($twitter_status['user']);
103 common_element_end('status');
106 function show_twitter_xml_user($twitter_user) {
107 common_element_start('user');
108 common_element('id', NULL, $twitter_user['id']);
109 common_element('name', NULL, $twitter_user['name']);
110 common_element('screen_name', NULL, $twitter_user['screen_name']);
111 common_element('location', NULL, $twitter_user['location']);
112 common_element('description', NULL, $twitter_user['description']);
113 common_element('profile_image_url', NULL, $twitter_user['profile_image_url']);
114 common_element('url', NULL, $twitter_user['url']);
115 common_element('protected', NULL, $twitter_user['protected']);
116 common_element('followers_count', NULL, $twitter_user['followers_count']);
117 common_element_end('user');
120 function show_twitter_rss_item($entry) {
121 common_element_start('item');
122 common_element('title', NULL, $entry['title']);
123 common_element('description', NULL, $entry['description']);
124 common_element('pubDate', NULL, $entry['pubDate']);
125 common_element('guid', NULL, $entry['guid']);
126 common_element('link', NULL, $entry['link']);
127 common_element_end('item');
130 function show_twitter_atom_entry($entry) {
131 common_element_start('entry');
132 common_element('title', NULL, $entry['title']);
133 common_element('content', array('type' => 'html'), $entry['title']);
134 common_element('id', NULL, $entry['id']);
135 common_element('published', NULL, $entry['published']);
136 common_element('updated', NULL, $entry['updated']);
137 common_element('link', array('href' => $entry['link'], 'rel' => 'alternate', 'type' => 'text/html'), NULL);
138 common_element_end('entry');
141 function show_twitter_json_statuses($twitter_statuses) {
142 print(json_encode($twitter_statuses));
147 // Anyone know what date format this is?
148 // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
149 function date_twitter($dt) {
151 return date("D M d G:i:s O Y", $t);
154 function replier_by_reply($reply_id) {
156 $notice = Notice::staticGet($reply_id);
159 common_debug("TwitterapiAction::replier_by_reply: Got a bad notice_id: $reply_id");
162 $profile = $notice->getProfile();
165 common_debug("TwitterapiAction::replier_by_reply: Got a bad profile_id: $profile_id");
169 return intval($profile->id);
172 // XXX: Candidate for a general utility method somewhere?
173 function count_subscriptions($profile) {
176 $sub = new Subscription();
177 $sub->subscribed = $profile->id;
180 while ($sub->fetch()) {
182 $other = Remote_profile::staticGet('id', $sub->subscriber);
184 $other = User::staticGet('id', $sub->subscriber);
187 common_debug('Got a bad subscription: '.print_r($sub,TRUE));
201 function init_twitter_rss() {
203 common_element_start('rss', array('version' => '2.0'));
206 function end_twitter_rss() {
207 common_element_end('rss');
211 function init_twitter_atom() {
213 common_element_start('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US'));
216 function end_twitter_atom() {
218 common_element_end('feed');