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 require_once(INSTALLDIR.'/lib/twitterapi.php');
24 class TwitapifriendshipsAction extends TwitterapiAction {
26 function create($args, $apidata) {
27 parent::handle($args);
29 $id = $apidata['api_arg'];
31 $other = $this->get_user($id);
34 $this->client_error(_('Could not follow user: User not found.'), 403, $apidata['content-type']);
38 $user = $apidata['user'];
40 if ($user->isSubscribed($other)) {
41 $this->client_error("Could not follow user: $other->nickname is already on your list.", 403, $apidata['content-type']);
45 $sub = new Subscription();
49 $sub->subscriber = $user->id;
50 $sub->subscribed = $other->id;
51 $sub->created = DB_DataObject_Cast::dateTime(); # current time
53 $result = $sub->insert();
56 $this->client_error("Could not follow user: $other->nickname.", 400, $apidata['content-type']);
60 $sub->query('COMMIT');
62 mail_subscribe_notify($other, $user);
64 $type = $apidata['content-type'];
65 $this->init_document($type);
66 $this->show_profile($other, $type);
67 $this->end_document($type);
73 //Discontinues friendship with the user specified in the ID parameter as the authenticating user. Returns the un-friended user in the requested format when successful. Returns a string describing the failure condition when unsuccessful.
75 //URL: http://twitter.com/friendships/destroy/id.format
81 //* id. Required. The ID or screen name of the user with whom to discontinue friendship. Ex: http://twitter.com/friendships/destroy/12345.json or http://twitter.com/friendships/destroy/bob.xml
83 function destroy($args, $apidata) {
84 parent::handle($args);
85 $id = $apidata['api_arg'];
87 # We can't subscribe to a remote person, but we can unsub
89 $other = $this->get_profile($id);
90 $user = $apidata['user'];
92 $sub = new Subscription();
93 $sub->subscriber = $user->id;
94 $sub->subscribed = $other->id;
96 if ($sub->find(TRUE)) {
99 $sub->query('COMMIT');
101 $this->client_error(_('You are not friends with the specified user.'), 403, $apidata['content-type']);
105 $type = $apidata['content-type'];
106 $this->init_document($type);
107 $this->show_profile($other, $type);
108 $this->end_document($type);
112 // Tests if a friendship exists between two users.
115 // URL: http://twitter.com/friendships/exists.format
117 // Formats: xml, json, none
121 // * user_a. Required. The ID or screen_name of the first user to test friendship for.
122 // * user_b. Required. The ID or screen_name of the second user to test friendship for.
123 // * Ex: http://twitter.com/friendships/exists.xml?user_a=alice&user_b=bob
125 function exists($args, $apidata) {
126 parent::handle($args);
129 $user_a_id = $this->trimmed('user_a');
130 $user_b_id = $this->trimmed('user_b');
132 $user_a = $this->get_user($user_a_id);
133 $user_b = $this->get_user($user_b_id);
135 if (!$user_a || !$user_b) {
136 $this->client_error(_('Two user ids or screen_names must be supplied.'), 400, $apidata['content-type']);
140 if ($user_a->isSubscribed($user_b)) {
146 switch ($apidata['content-type']) {
148 $this->init_document('xml');
149 common_element('friends', NULL, $result);
150 $this->end_document('xml');
153 $this->init_document('json');
154 print json_encode($result);
155 $this->end_document('json');
158 print $result; // Really? --Zach
165 function get_profile($id) {
166 if (is_numeric($id)) {
167 return Profile::staticGet($id);
169 $user = User::staticGet('nickname', $id);
171 return $user->getProfile();
178 function get_user($id) {
179 if (is_numeric($id)) {
180 return User::staticGet($id);
182 return User::staticGet('nickname', $id);