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