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 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
30 $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
34 $id = $apidata['api_arg'];
36 $other = $this->get_user($id);
39 $this->client_error(_('Could not follow user: User not found.'), 403, $apidata['content-type']);
43 $user = $apidata['user'];
45 if ($user->isSubscribed($other)) {
46 $errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname);
47 $this->client_error($errmsg, 403, $apidata['content-type']);
51 $sub = new Subscription();
55 $sub->subscriber = $user->id;
56 $sub->subscribed = $other->id;
57 $sub->created = DB_DataObject_Cast::dateTime(); # current time
59 $result = $sub->insert();
62 $errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname);
63 $this->client_error($errmsg, 400, $apidata['content-type']);
67 $sub->query('COMMIT');
69 mail_subscribe_notify($other, $user);
71 $type = $apidata['content-type'];
72 $this->init_document($type);
73 $this->show_profile($other, $type);
74 $this->end_document($type);
78 function destroy($args, $apidata) {
79 parent::handle($args);
81 if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
82 $this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
86 $id = $apidata['api_arg'];
88 # We can't subscribe to a remote person, but we can unsub
90 $other = $this->get_profile($id);
91 $user = $apidata['user'];
93 $sub = new Subscription();
94 $sub->subscriber = $user->id;
95 $sub->subscribed = $other->id;
97 if ($sub->find(TRUE)) {
100 $sub->query('COMMIT');
102 $this->client_error(_('You are not friends with the specified user.'), 403, $apidata['content-type']);
106 $type = $apidata['content-type'];
107 $this->init_document($type);
108 $this->show_profile($other, $type);
109 $this->end_document($type);
113 function exists($args, $apidata) {
114 parent::handle($args);
116 if (!in_array($apidata['content-type'], array('xml', 'json'))) {
117 common_user_error(_('API method not found!'), $code = 404);
121 $user_a_id = $this->trimmed('user_a');
122 $user_b_id = $this->trimmed('user_b');
124 $user_a = $this->get_user($user_a_id);
125 $user_b = $this->get_user($user_b_id);
127 if (!$user_a || !$user_b) {
128 $this->client_error(_('Two user ids or screen_names must be supplied.'), 400, $apidata['content-type']);
132 if ($user_a->isSubscribed($user_b)) {
138 switch ($apidata['content-type']) {
140 $this->init_document('xml');
141 common_element('friends', NULL, $result);
142 $this->end_document('xml');
145 $this->init_document('json');
146 print json_encode($result);
147 $this->end_document('json');