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