]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapifriendships.php
change Laconica and Control Yourself to StatusNet in PHP files
[quix0rs-gnu-social.git] / actions / twitapifriendships.php
1 <?php
2 /*
3  * StatusNet - a 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('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         $sub = new Subscription();
103         $sub->subscriber = $user->id;
104         $sub->subscribed = $other->id;
105
106         if ($sub->find(true)) {
107             $sub->query('BEGIN');
108             $sub->delete();
109             $sub->query('COMMIT');
110         } else {
111             $this->clientError(_('You are not friends with the specified user.'),
112                 403, $apidata['content-type']);
113             return;
114         }
115
116         $type = $apidata['content-type'];
117         $this->init_document($type);
118         $this->show_profile($other, $type);
119         $this->end_document($type);
120
121     }
122
123     function exists($args, $apidata)
124     {
125         parent::handle($args);
126
127         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
128             $this->clientError(_('API method not found!'), $code = 404);
129             return;
130         }
131
132         $user_a_id = $this->trimmed('user_a');
133         $user_b_id = $this->trimmed('user_b');
134
135         $user_a = $this->get_user($user_a_id);
136         $user_b = $this->get_user($user_b_id);
137
138         if (empty($user_a) || empty($user_b)) {
139             $this->clientError(_('Two user ids or screen_names must be supplied.'),
140                 400, $apidata['content-type']);
141             return;
142         }
143
144         $result = $user_a->isSubscribed($user_b);
145
146         switch ($apidata['content-type']) {
147          case 'xml':
148             $this->init_document('xml');
149             $this->element('friends', null, $result);
150             $this->end_document('xml');
151             break;
152          case 'json':
153             $this->init_document('json');
154             print json_encode($result);
155             $this->end_document('json');
156             break;
157          default:
158             break;
159         }
160
161     }
162
163     function show($args, $apidata)
164     {
165         parent::handle($args);
166
167         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
168             $this->clientError(_('API method not found!'), $code = 404);
169             return;
170         }
171
172         $source_id          = (int)$this->trimmed('source_id');
173         $source_screen_name = $this->trimmed('source_screen_name');
174
175         // If the source is not specified for an unauthenticated request,
176         // the method will return an HTTP 403.
177
178         if (empty($source_id) && empty($source_screen_name)) {
179             if (empty($apidata['user'])) {
180                 $this->clientError(_('Could not determine source user.'),
181                         $code = 403);
182                 return;
183             }
184         }
185
186         $source = null;
187
188         if (!empty($source_id)) {
189             $source = User::staticGet($source_id);
190         } elseif (!empty($source_screen_name)) {
191             $source = User::staticGet('nickname', $source_screen_name);
192         } else {
193             $source = $apidata['user'];
194         }
195
196         // If a source or target is specified but does not exist,
197         // the method will return an HTTP 404.
198
199         if (empty($source)) {
200             $this->clientError(_('Could not determine source user.'),
201                 $code = 404);
202             return;
203         }
204
205         $target_id          = (int)$this->trimmed('target_id');
206         $target_screen_name = $this->trimmed('target_screen_name');
207
208         $target = null;
209
210         if (!empty($target_id)) {
211             $target = User::staticGet($target_id);
212         } elseif (!empty($target_screen_name)) {
213             $target = User::staticGet('nickname', $target_screen_name);
214         } else {
215             $this->clientError(_('Target user not specified.'),
216                 $code = 403);
217             return;
218         }
219
220         if (empty($target)) {
221             $this->clientError(_('Could not find target user.'),
222                 $code = 404);
223             return;
224         }
225
226         $result = $this->twitter_relationship_array($source, $target);
227
228         switch ($apidata['content-type']) {
229         case 'xml':
230             $this->init_document('xml');
231             $this->show_twitter_xml_relationship($result[relationship]);
232             $this->end_document('xml');
233             break;
234         case 'json':
235             $this->init_document('json');
236             print json_encode($result);
237             $this->end_document('json');
238             break;
239         default:
240             break;
241         }
242     }
243
244 }