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)
28 parent::handle($args);
30 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
31 $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
35 $id = $apidata['api_arg'];
37 $other = $this->get_user($id);
40 $this->client_error(_('Could not follow user: User not found.'), 403, $apidata['content-type']);
44 $user = $apidata['user'];
46 if ($user->isSubscribed($other)) {
47 $errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname);
48 $this->client_error($errmsg, 403, $apidata['content-type']);
52 $sub = new Subscription();
56 $sub->subscriber = $user->id;
57 $sub->subscribed = $other->id;
58 $sub->created = DB_DataObject_Cast::dateTime(); # current time
60 $result = $sub->insert();
63 $errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname);
64 $this->client_error($errmsg, 400, $apidata['content-type']);
68 $sub->query('COMMIT');
70 mail_subscribe_notify($other, $user);
72 $type = $apidata['content-type'];
73 $this->init_document($type);
74 $this->show_profile($other, $type);
75 $this->end_document($type);
79 function destroy($args, $apidata)
81 parent::handle($args);
83 if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
84 $this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
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, $type);
111 $this->end_document($type);
115 function exists($args, $apidata)
117 parent::handle($args);
119 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
120 common_user_error(_('API method not found!'), $code = 404);
124 $user_a_id = $this->trimmed('user_a');
125 $user_b_id = $this->trimmed('user_b');
127 $user_a = $this->get_user($user_a_id);
128 $user_b = $this->get_user($user_b_id);
130 if (!$user_a || !$user_b) {
131 $this->client_error(_('Two user ids or screen_names must be supplied.'), 400, $apidata['content-type']);
135 if ($user_a->isSubscribed($user_b)) {
141 switch ($apidata['content-type']) {
143 $this->init_document('xml');
144 common_element('friends', null, $result);
145 $this->end_document('xml');
148 $this->init_document('json');
149 print json_encode($result);
150 $this->end_document('json');