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
27 function create($args, $apidata)
29 parent::handle($args);
31 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
32 $this->clientError(_('This method requires a POST.'), 400, $apidata['content-type']);
36 $id = $apidata['api_arg'];
38 $other = $this->get_user($id);
41 $this->clientError(_('Could not follow user: User not found.'), 403, $apidata['content-type']);
45 $user = $apidata['user'];
47 if ($user->isSubscribed($other)) {
48 $errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname);
49 $this->clientError($errmsg, 403, $apidata['content-type']);
53 $sub = new Subscription();
57 $sub->subscriber = $user->id;
58 $sub->subscribed = $other->id;
59 $sub->created = DB_DataObject_Cast::dateTime(); # current time
61 $result = $sub->insert();
64 $errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname);
65 $this->clientError($errmsg, 400, $apidata['content-type']);
69 $sub->query('COMMIT');
71 mail_subscribe_notify($other, $user);
73 $type = $apidata['content-type'];
74 $this->init_document($type);
75 $this->show_profile($other, $type);
76 $this->end_document($type);
80 function destroy($args, $apidata)
82 parent::handle($args);
84 if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
85 $this->clientError(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
89 $id = $apidata['api_arg'];
91 # We can't subscribe to a remote person, but we can unsub
93 $other = $this->get_profile($id);
94 $user = $apidata['user'];
96 $sub = new Subscription();
97 $sub->subscriber = $user->id;
98 $sub->subscribed = $other->id;
100 if ($sub->find(true)) {
101 $sub->query('BEGIN');
103 $sub->query('COMMIT');
105 $this->clientError(_('You are not friends with the specified user.'), 403, $apidata['content-type']);
109 $type = $apidata['content-type'];
110 $this->init_document($type);
111 $this->show_profile($other, $type);
112 $this->end_document($type);
116 function exists($args, $apidata)
118 parent::handle($args);
120 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
121 $this->clientError(_('API method not found!'), $code = 404);
125 $user_a_id = $this->trimmed('user_a');
126 $user_b_id = $this->trimmed('user_b');
128 $user_a = $this->get_user($user_a_id);
129 $user_b = $this->get_user($user_b_id);
131 if (!$user_a || !$user_b) {
132 $this->clientError(_('Two user ids or screen_names must be supplied.'), 400, $apidata['content-type']);
136 $result = $user_a->isSubscribed($user_b);
138 switch ($apidata['content-type']) {
140 $this->init_document('xml');
141 $this->element('friends', null, $result);
142 $this->end_document('xml');
145 $this->init_document('json');
146 print json_encode($result);
147 $this->end_document('json');