]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapifriendships.php
move opening brace of class declaration to next line
[quix0rs-gnu-social.git] / actions / twitapifriendships.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/twitterapi.php');
23
24 class TwitapifriendshipsAction extends TwitterapiAction
25 {
26
27     function create($args, $apidata)
28     {
29         parent::handle($args);
30
31         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
32             $this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
33             return;
34         }
35
36         $id = $apidata['api_arg'];
37
38         $other = $this->get_user($id);
39
40         if (!$other) {
41             $this->client_error(_('Could not follow user: User not found.'), 403, $apidata['content-type']);
42             return;
43         }
44
45         $user = $apidata['user'];
46
47         if ($user->isSubscribed($other)) {
48             $errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname);
49             $this->client_error($errmsg, 403, $apidata['content-type']);
50             return;
51         }
52
53         $sub = new Subscription();
54
55         $sub->query('BEGIN');
56
57         $sub->subscriber = $user->id;
58         $sub->subscribed = $other->id;
59         $sub->created = DB_DataObject_Cast::dateTime(); # current time
60
61         $result = $sub->insert();
62
63         if (!$result) {
64             $errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname);
65             $this->client_error($errmsg, 400, $apidata['content-type']);
66             return;
67         }
68
69         $sub->query('COMMIT');
70
71         mail_subscribe_notify($other, $user);
72
73         $type = $apidata['content-type'];
74         $this->init_document($type);
75         $this->show_profile($other, $type);
76         $this->end_document($type);
77
78     }
79
80     function destroy($args, $apidata)
81     {
82         parent::handle($args);
83
84         if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
85             $this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
86             return;
87         }
88
89         $id = $apidata['api_arg'];
90
91         # We can't subscribe to a remote person, but we can unsub
92
93         $other = $this->get_profile($id);
94         $user = $apidata['user'];
95
96         $sub = new Subscription();
97         $sub->subscriber = $user->id;
98         $sub->subscribed = $other->id;
99
100         if ($sub->find(true)) {
101             $sub->query('BEGIN');
102             $sub->delete();
103             $sub->query('COMMIT');
104         } else {
105             $this->client_error(_('You are not friends with the specified user.'), 403, $apidata['content-type']);
106             return;
107         }
108
109         $type = $apidata['content-type'];
110         $this->init_document($type);
111         $this->show_profile($other, $type);
112         $this->end_document($type);
113
114     }
115
116     function exists($args, $apidata)
117     {
118         parent::handle($args);
119
120         if (!in_array($apidata['content-type'], array('xml', 'json'))) {
121             common_user_error(_('API method not found!'), $code = 404);
122             return;
123         }
124
125         $user_a_id = $this->trimmed('user_a');
126         $user_b_id = $this->trimmed('user_b');
127
128         $user_a = $this->get_user($user_a_id);
129         $user_b = $this->get_user($user_b_id);
130
131         if (!$user_a || !$user_b) {
132             $this->client_error(_('Two user ids or screen_names must be supplied.'), 400, $apidata['content-type']);
133             return;
134         }
135
136         if ($user_a->isSubscribed($user_b)) {
137             $result = 'true';
138         } else {
139             $result = 'false';
140         }
141
142         switch ($apidata['content-type']) {
143          case 'xml':
144             $this->init_document('xml');
145             common_element('friends', null, $result);
146             $this->end_document('xml');
147             break;
148          case 'json':
149             $this->init_document('json');
150             print json_encode($result);
151             $this->end_document('json');
152             break;
153          default:
154             break;
155         }
156
157     }
158
159 }