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 = $this->trimmed('id');
31 $other = $this->get_user($id);
34 $this->client_error(_('No such user'));
38 $user = $apidata['user'];
40 if ($user->isSubscribed($other)) {
41 $this->client_error(_('Already subscribed.'));
45 $sub = new Subscription();
46 $sub->subscriber = $user->id;
47 $sub->subscribed = $other->id;
49 $result = $sub->insert();
52 $this->server_error(_('Could not subscribe'));
56 mail_subscribe_notify($other, $user);
58 $this->show_profile($other);
64 //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.
66 //URL: http://twitter.com/friendships/destroy/id.format
72 //* 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
74 function destroy($args, $apidata) {
75 parent::handle($args);
76 $id = $this->trimmed('id');
78 # We can't subscribe to a remote person, but we can unsub
80 $other = $this->get_profile($id);
81 $user = $apidata['user'];
83 $sub = new Subscription();
84 $sub->subscriber = $user->id;
85 $sub->subscribed = $other->id;
87 if ($sub->fetch(TRUE)) {
91 $this->show_profile($other);
95 // Tests if a friendship exists between two users.
98 // URL: http://twitter.com/friendships/exists.format
100 // Formats: xml, json, none
104 // * user_a. Required. The ID or screen_name of the first user to test friendship for.
105 // * user_b. Required. The ID or screen_name of the second user to test friendship for.
106 // * Ex: http://twitter.com/friendships/exists.xml?user_a=alice&user_b=bob
108 function exists($args, $apidata) {
109 parent::handle($args);
110 $user_a_id = $this->trimmed('user_a');
111 $user_b_id = $this->trimmed('user_b');
112 $user_a = $this->get_profile($user_a_id);
113 $user_b = $this->get_profile($user_b_id);
115 if (!$user_a || !$user_b) {
116 $this->client_error(_('No such user'));
120 if ($user_a->isSubscribed($user_b)) {
126 switch ($apidata['content-type']) {
129 common_element('friends', NULL, $result);
133 print json_encode($result);
143 function get_profile($id) {
144 if (is_numeric($id)) {
145 return Profile::staticGet($id);
147 $user = User::staticGet('nickname', $id);
149 return $user->getProfile();
156 function get_user($id) {
157 if (is_numeric($id)) {
158 return User::staticGet($id);
160 return User::staticGet('nickname', $id);