]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapifriendships.php
remove spurious readme from Orbited
[quix0rs-gnu-social.git] / actions / twitapifriendships.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) {
21     exit(1);
22 }
23
24 require_once(INSTALLDIR.'/lib/twitterapi.php');
25
26 class TwitapifriendshipsAction extends TwitterapiAction
27 {
28
29     function create($args, $apidata)
30     {
31         parent::handle($args);
32
33         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
34             $this->clientError(_('This method requires a POST.'),
35                 400, $apidata['content-type']);
36             return;
37         }
38
39         $id    = $apidata['api_arg'];
40         $other = $this->get_user($id);
41
42         if (empty($other)) {
43             $this->clientError(_('Could not follow user: User not found.'),
44                 403, $apidata['content-type']);
45             return;
46         }
47
48         $user = $apidata['user'];
49
50         if ($user->isSubscribed($other)) {
51             $errmsg = sprintf(_('Could not follow user: %s is already on your list.'),
52                 $other->nickname);
53             $this->clientError($errmsg, 403, $apidata['content-type']);
54             return;
55         }
56
57         $sub = new Subscription();
58
59         $sub->query('BEGIN');
60
61         $sub->subscriber = $user->id;
62         $sub->subscribed = $other->id;
63         $sub->created = DB_DataObject_Cast::dateTime(); # current time
64
65         $result = $sub->insert();
66
67         if (empty($result)) {
68             $errmsg = sprintf(_('Could not follow user: %s is already on your list.'),
69                 $other->nickname);
70             $this->clientError($errmsg, 400, $apidata['content-type']);
71             return;
72         }
73
74         $sub->query('COMMIT');
75
76         mail_subscribe_notify($other, $user);
77
78         $type = $apidata['content-type'];
79         $this->init_document($type);
80         $this->show_profile($other, $type);
81         $this->end_document($type);
82
83     }
84
85     function destroy($args, $apidata)
86     {
87         parent::handle($args);
88
89         if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
90             $this->clientError(_('This method requires a POST or DELETE.'),
91                 400, $apidata['content-type']);
92             return;
93         }
94
95         $id = $apidata['api_arg'];
96
97         # We can't subscribe to a remote person, but we can unsub
98
99         $other = $this->get_profile($id);
100         $user = $apidata['user']; // Alwyas the auth user
101
102         if ($user->id == $other->id) {
103             $this->clientError(_("You cannot unfollow yourself!"),
104                                403, $apidata['content-type']);
105             return;
106         }
107
108         $sub = new Subscription();
109         $sub->subscriber = $user->id;
110         $sub->subscribed = $other->id;
111
112         if ($sub->find(true)) {
113             $sub->query('BEGIN');
114             $sub->delete();
115             $sub->query('COMMIT');
116         } else {
117             $this->clientError(_('You are not friends with the specified user.'),
118                 403, $apidata['content-type']);
119             return;
120         }
121
122         $type = $apidata['content-type'];
123         $this->init_document($type);
124         $this->show_profile($other, $type);
125         $this->end_document($type);
126
127     }
128
129     function exists($args, $apidata)
130     {
131         parent::handle($args);
132
133         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
134             $this->clientError(_('API method not found!'), $code = 404);
135             return;
136         }
137
138         $user_a_id = $this->trimmed('user_a');
139         $user_b_id = $this->trimmed('user_b');
140
141         $user_a = $this->get_user($user_a_id);
142         $user_b = $this->get_user($user_b_id);
143
144         if (empty($user_a) || empty($user_b)) {
145             $this->clientError(_('Two user ids or screen_names must be supplied.'),
146                 400, $apidata['content-type']);
147             return;
148         }
149
150         $result = $user_a->isSubscribed($user_b);
151
152         switch ($apidata['content-type']) {
153          case 'xml':
154             $this->init_document('xml');
155             $this->element('friends', null, $result);
156             $this->end_document('xml');
157             break;
158          case 'json':
159             $this->init_document('json');
160             print json_encode($result);
161             $this->end_document('json');
162             break;
163          default:
164             break;
165         }
166
167     }
168
169     function show($args, $apidata)
170     {
171         parent::handle($args);
172
173         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
174             $this->clientError(_('API method not found!'), $code = 404);
175             return;
176         }
177
178         $source_id          = (int)$this->trimmed('source_id');
179         $source_screen_name = $this->trimmed('source_screen_name');
180
181         // If the source is not specified for an unauthenticated request,
182         // the method will return an HTTP 403.
183
184         if (empty($source_id) && empty($source_screen_name)) {
185             if (empty($apidata['user'])) {
186                 $this->clientError(_('Could not determine source user.'),
187                         $code = 403);
188                 return;
189             }
190         }
191
192         $source = null;
193
194         if (!empty($source_id)) {
195             $source = User::staticGet($source_id);
196         } elseif (!empty($source_screen_name)) {
197             $source = User::staticGet('nickname', $source_screen_name);
198         } else {
199             $source = $apidata['user'];
200         }
201
202         // If a source or target is specified but does not exist,
203         // the method will return an HTTP 404.
204
205         if (empty($source)) {
206             $this->clientError(_('Could not determine source user.'),
207                 $code = 404);
208             return;
209         }
210
211         $target_id          = (int)$this->trimmed('target_id');
212         $target_screen_name = $this->trimmed('target_screen_name');
213
214         $target = null;
215
216         if (!empty($target_id)) {
217             $target = User::staticGet($target_id);
218         } elseif (!empty($target_screen_name)) {
219             $target = User::staticGet('nickname', $target_screen_name);
220         } else {
221             $this->clientError(_('Target user not specified.'),
222                 $code = 403);
223             return;
224         }
225
226         if (empty($target)) {
227             $this->clientError(_('Could not find target user.'),
228                 $code = 404);
229             return;
230         }
231
232         $result = $this->twitter_relationship_array($source, $target);
233
234         switch ($apidata['content-type']) {
235         case 'xml':
236             $this->init_document('xml');
237             $this->show_twitter_xml_relationship($result[relationship]);
238             $this->end_document('xml');
239             break;
240         case 'json':
241             $this->init_document('json');
242             print json_encode($result);
243             $this->end_document('json');
244             break;
245         default:
246             break;
247         }
248     }
249
250 }