]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapifriendships.php
Merge branch '0.8.x' into group-rss-empty
[quix0rs-gnu-social.git] / actions / twitapifriendships.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, Control Yourself, 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 }