3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, Control Yourself, 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')) {
24 require_once(INSTALLDIR.'/lib/twitterapi.php');
26 class TwitapifriendshipsAction extends TwitterapiAction
29 function create($args, $apidata)
31 parent::handle($args);
33 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
34 $this->clientError(_('This method requires a POST.'),
35 400, $apidata['content-type']);
39 $id = $apidata['api_arg'];
40 $other = $this->get_user($id);
43 $this->clientError(_('Could not follow user: User not found.'),
44 403, $apidata['content-type']);
48 $user = $apidata['user'];
50 if ($user->isSubscribed($other)) {
51 $errmsg = sprintf(_('Could not follow user: %s is already on your list.'),
53 $this->clientError($errmsg, 403, $apidata['content-type']);
57 $sub = new Subscription();
61 $sub->subscriber = $user->id;
62 $sub->subscribed = $other->id;
63 $sub->created = DB_DataObject_Cast::dateTime(); # current time
65 $result = $sub->insert();
68 $errmsg = sprintf(_('Could not follow user: %s is already on your list.'),
70 $this->clientError($errmsg, 400, $apidata['content-type']);
74 $sub->query('COMMIT');
76 mail_subscribe_notify($other, $user);
78 $type = $apidata['content-type'];
79 $this->init_document($type);
80 $this->show_profile($other, $type);
81 $this->end_document($type);
85 function destroy($args, $apidata)
87 parent::handle($args);
89 if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
90 $this->clientError(_('This method requires a POST or DELETE.'),
91 400, $apidata['content-type']);
95 $id = $apidata['api_arg'];
97 # We can't subscribe to a remote person, but we can unsub
99 $other = $this->get_profile($id);
100 $user = $apidata['user']; // Alwyas the auth user
102 $sub = new Subscription();
103 $sub->subscriber = $user->id;
104 $sub->subscribed = $other->id;
106 if ($sub->find(true)) {
107 $sub->query('BEGIN');
109 $sub->query('COMMIT');
111 $this->clientError(_('You are not friends with the specified user.'),
112 403, $apidata['content-type']);
116 $type = $apidata['content-type'];
117 $this->init_document($type);
118 $this->show_profile($other, $type);
119 $this->end_document($type);
123 function exists($args, $apidata)
125 parent::handle($args);
127 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
128 $this->clientError(_('API method not found!'), $code = 404);
132 $user_a_id = $this->trimmed('user_a');
133 $user_b_id = $this->trimmed('user_b');
135 $user_a = $this->get_user($user_a_id);
136 $user_b = $this->get_user($user_b_id);
138 if (empty($user_a) || empty($user_b)) {
139 $this->clientError(_('Two user ids or screen_names must be supplied.'),
140 400, $apidata['content-type']);
144 $result = $user_a->isSubscribed($user_b);
146 switch ($apidata['content-type']) {
148 $this->init_document('xml');
149 $this->element('friends', null, $result);
150 $this->end_document('xml');
153 $this->init_document('json');
154 print json_encode($result);
155 $this->end_document('json');
163 function show($args, $apidata)
165 parent::handle($args);
167 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
168 $this->clientError(_('API method not found!'), $code = 404);
172 $source_id = (int)$this->trimmed('source_id');
173 $source_screen_name = $this->trimmed('source_screen_name');
175 // If the source is not specified for an unauthenticated request,
176 // the method will return an HTTP 403.
178 if (empty($source_id) && empty($source_screen_name)) {
179 if (empty($apidata['user'])) {
180 $this->clientError(_('Could not determine source user.'),
188 if (!empty($source_id)) {
189 $source = User::staticGet($source_id);
190 } elseif (!empty($source_screen_name)) {
191 $source = User::staticGet('nickname', $source_screen_name);
193 $source = $apidata['user'];
196 // If a source or target is specified but does not exist,
197 // the method will return an HTTP 404.
199 if (empty($source)) {
200 $this->clientError(_('Could not determine source user.'),
205 $target_id = (int)$this->trimmed('target_id');
206 $target_screen_name = $this->trimmed('target_screen_name');
210 if (!empty($target_id)) {
211 $target = User::staticGet($target_id);
212 } elseif (!empty($target_screen_name)) {
213 $target = User::staticGet('nickname', $target_screen_name);
215 $this->clientError(_('Target user not specified.'),
220 if (empty($target)) {
221 $this->clientError(_('Could not find target user.'),
226 $result = $this->twitter_relationship_array($source, $target);
228 switch ($apidata['content-type']) {
230 $this->init_document('xml');
231 $this->show_twitter_xml_relationship($result[relationship]);
232 $this->end_document('xml');
235 $this->init_document('json');
236 print json_encode($result);
237 $this->end_document('json');