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