3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !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 if ($user->id == $other->id) {
103 $this->clientError(_("You cannot unfollow yourself!"),
104 403, $apidata['content-type']);
108 $sub = new Subscription();
109 $sub->subscriber = $user->id;
110 $sub->subscribed = $other->id;
112 if ($sub->find(true)) {
113 $sub->query('BEGIN');
115 $sub->query('COMMIT');
117 $this->clientError(_('You are not friends with the specified user.'),
118 403, $apidata['content-type']);
122 $type = $apidata['content-type'];
123 $this->init_document($type);
124 $this->show_profile($other, $type);
125 $this->end_document($type);
129 function exists($args, $apidata)
131 parent::handle($args);
133 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
134 $this->clientError(_('API method not found!'), $code = 404);
138 $user_a_id = $this->trimmed('user_a');
139 $user_b_id = $this->trimmed('user_b');
141 $user_a = $this->get_user($user_a_id);
142 $user_b = $this->get_user($user_b_id);
144 if (empty($user_a) || empty($user_b)) {
145 $this->clientError(_('Two user ids or screen_names must be supplied.'),
146 400, $apidata['content-type']);
150 $result = $user_a->isSubscribed($user_b);
152 switch ($apidata['content-type']) {
154 $this->init_document('xml');
155 $this->element('friends', null, $result);
156 $this->end_document('xml');
159 $this->init_document('json');
160 print json_encode($result);
161 $this->end_document('json');
169 function show($args, $apidata)
171 parent::handle($args);
173 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
174 $this->clientError(_('API method not found!'), $code = 404);
178 $source_id = (int)$this->trimmed('source_id');
179 $source_screen_name = $this->trimmed('source_screen_name');
181 // If the source is not specified for an unauthenticated request,
182 // the method will return an HTTP 403.
184 if (empty($source_id) && empty($source_screen_name)) {
185 if (empty($apidata['user'])) {
186 $this->clientError(_('Could not determine source user.'),
194 if (!empty($source_id)) {
195 $source = User::staticGet($source_id);
196 } elseif (!empty($source_screen_name)) {
197 $source = User::staticGet('nickname', $source_screen_name);
199 $source = $apidata['user'];
202 // If a source or target is specified but does not exist,
203 // the method will return an HTTP 404.
205 if (empty($source)) {
206 $this->clientError(_('Could not determine source user.'),
211 $target_id = (int)$this->trimmed('target_id');
212 $target_screen_name = $this->trimmed('target_screen_name');
216 if (!empty($target_id)) {
217 $target = User::staticGet($target_id);
218 } elseif (!empty($target_screen_name)) {
219 $target = User::staticGet('nickname', $target_screen_name);
221 $this->clientError(_('Target user not specified.'),
226 if (empty($target)) {
227 $this->clientError(_('Could not find target user.'),
232 $result = $this->twitter_relationship_array($source, $target);
234 switch ($apidata['content-type']) {
236 $this->init_document('xml');
237 $this->show_twitter_xml_relationship($result[relationship]);
238 $this->end_document('xml');
241 $this->init_document('json');
242 print json_encode($result);
243 $this->end_document('json');