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']);
39 $user = $apidata['user'];
41 if ($user->isSubscribed($other)) {
42 $this->client_error("Could not follow user: $other->nickname is already on your list.", 403, $apidata['content-type']);
47 $sub = new Subscription();
51 $sub->subscriber = $user->id;
52 $sub->subscribed = $other->id;
53 $sub->created = DB_DataObject_Cast::dateTime(); # current time
55 $result = $sub->insert();
58 $this->client_error("Could not follow user: $other->nickname.", 400, $apidata['content-type']);
63 $sub->query('COMMIT');
65 mail_subscribe_notify($other, $user);
67 $type = $apidata['content-type'];
68 $this->init_document($type);
69 $this->show_profile($other);
70 $this->end_document($type);
76 //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.
78 //URL: http://twitter.com/friendships/destroy/id.format
84 //* 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
86 function destroy($args, $apidata) {
87 parent::handle($args);
88 $id = $apidata['api_arg'];
90 # We can't subscribe to a remote person, but we can unsub
92 $other = $this->get_profile($id);
93 $user = $apidata['user'];
95 $sub = new Subscription();
96 $sub->subscriber = $user->id;
97 $sub->subscribed = $other->id;
99 if ($sub->find(TRUE)) {
100 $sub->query('BEGIN');
102 $sub->query('COMMIT');
104 $this->client_error(_('You are not friends with the specified user.'), 403, $apidata['content-type']);
108 $type = $apidata['content-type'];
109 $this->init_document($type);
110 $this->show_profile($other);
111 $this->end_document($type);
115 // Tests if a friendship exists between two users.
118 // URL: http://twitter.com/friendships/exists.format
120 // Formats: xml, json, none
124 // * user_a. Required. The ID or screen_name of the first user to test friendship for.
125 // * user_b. Required. The ID or screen_name of the second user to test friendship for.
126 // * Ex: http://twitter.com/friendships/exists.xml?user_a=alice&user_b=bob
128 function exists($args, $apidata) {
129 parent::handle($args);
132 $user_a_id = $this->trimmed('user_a');
133 $user_b_id = $this->trimmed('user_b');
135 $user_a = $this->get_profile($user_a_id);
136 $user_b = $this->get_profile($user_b_id);
138 if($user_a) { print "got user a profile";}
139 if($user_b) { print "got user b profile";}
142 if (!$user_a || !$user_b) {
143 $this->client_error(_('Two user ids or screen_names must be supplied.'), 400, $apidata['content-type']);
147 if ($user_a->isSubscribed($user_b)) {
153 switch ($apidata['content-type']) {
156 common_element('friends', NULL, $result);
160 print json_encode($result);
170 function get_profile($id) {
171 if (is_numeric($id)) {
172 return Profile::staticGet($id);
174 $user = User::staticGet('nickname', $id);
176 return $user->getProfile();
183 function get_user($id) {
184 if (is_numeric($id)) {
185 return User::staticGet($id);
187 return User::staticGet('nickname', $id);