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(_('No such user'));
39 $user = $apidata['user'];
41 if ($user->isSubscribed($other)) {
42 $this->client_error(_('Already subscribed.'));
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->server_error(_('Could not subscribe'));
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->fetch(TRUE)) {
100 $sub->query('BEGIN');
102 $sub->query('COMMIT');
104 $this->client_error(_('Not subscribed'));
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);
130 $user_a_id = $this->trimmed('user_a');
131 $user_b_id = $this->trimmed('user_b');
132 $user_a = $this->get_profile($user_a_id);
133 $user_b = $this->get_profile($user_b_id);
135 if (!$user_a || !$user_b) {
136 $this->client_error(_('No such user'));
140 if ($user_a->isSubscribed($user_b)) {
146 switch ($apidata['content-type']) {
149 common_element('friends', NULL, $result);
153 print json_encode($result);
163 function get_profile($id) {
164 if (is_numeric($id)) {
165 return Profile::staticGet($id);
167 $user = User::staticGet('nickname', $id);
169 return $user->getProfile();
176 function get_user($id) {
177 if (is_numeric($id)) {
178 return User::staticGet($id);
180 return User::staticGet('nickname', $id);