]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/FollowRequests.php
Added scope check
[friendica.git] / src / Module / Api / Mastodon / FollowRequests.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Api\Mastodon;
23
24 use Friendica\Core\System;
25 use Friendica\DI;
26 use Friendica\Module\BaseApi;
27 use Friendica\Network\HTTPException;
28
29 /**
30  * @see https://docs.joinmastodon.org/methods/accounts/follow_requests
31  */
32 class FollowRequests extends BaseApi
33 {
34         /**
35          * @param array $parameters
36          * @throws HTTPException\BadRequestException
37          * @throws HTTPException\ForbiddenException
38          * @throws HTTPException\InternalServerErrorException
39          * @throws HTTPException\NotFoundException
40          * @throws HTTPException\UnauthorizedException
41          * @throws \ImagickException
42          *
43          * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#accept-follow
44          * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#reject-follow
45          */
46         public static function post(array $parameters = [])
47         {
48                 self::login(self::SCOPE_FOLLOW);
49                 $uid = self::getCurrentUserID();
50
51                 $introduction = DI::intro()->selectFirst(['id' => $parameters['id'], 'uid' => $uid]);
52
53                 $contactId = $introduction->{'contact-id'};
54
55                 switch ($parameters['action']) {
56                         case 'authorize':
57                                 $introduction->confirm();
58
59                                 $relationship = DI::mstdnRelationship()->createFromContactId($contactId, $uid);
60                                 break;
61                         case 'ignore':
62                                 $introduction->ignore();
63
64                                 $relationship = DI::mstdnRelationship()->createFromContactId($contactId, $uid);
65                                 break;
66                         case 'reject':
67                                 $introduction->discard();
68
69                                 $relationship = DI::mstdnRelationship()->createFromContactId($contactId, $uid);
70                                 break;
71                         default:
72                                 throw new HTTPException\BadRequestException('Unexpected action parameter, expecting "authorize", "ignore" or "reject"');
73                 }
74
75                 System::jsonExit($relationship);
76         }
77
78         /**
79          * @param array $parameters
80          * @throws HTTPException\InternalServerErrorException
81          * @throws \ImagickException
82          * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#pending-follows
83          */
84         public static function rawContent(array $parameters = [])
85         {
86                 self::login(self::SCOPE_READ);
87                 $uid = self::getCurrentUserID();
88
89                 $min_id = $_GET['min_id'] ?? null;
90                 $max_id = $_GET['max_id'] ?? null;
91                 $limit = intval($_GET['limit'] ?? 40);
92
93                 $baseUrl = DI::baseUrl();
94
95                 $introductions = DI::intro()->selectByBoundaries(
96                         ['`uid` = ? AND NOT `ignore`', $uid],
97                         ['order' => ['id' => 'DESC']],
98                         $min_id,
99                         $max_id,
100                         $limit
101                 );
102
103                 $return = [];
104
105                 foreach ($introductions as $key => $introduction) {
106                         try {
107                                 $return[] = DI::mstdnFollowRequest()->createFromIntroduction($introduction);
108                         } catch (HTTPException\InternalServerErrorException $exception) {
109                                 DI::intro()->delete($introduction);
110                                 unset($introductions[$key]);
111                         }
112                 }
113
114                 $base_query = [];
115                 if (isset($_GET['limit'])) {
116                         $base_query['limit'] = $limit;
117                 }
118
119                 $links = [];
120                 if ($introductions->getTotalCount() > $limit) {
121                         $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $introductions[count($introductions) - 1]->id]) . '>; rel="next"';
122                 }
123
124                 if (count($introductions)) {
125                         $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['min_id' => $introductions[0]->id]) . '>; rel="prev"';
126                 }
127
128                 header('Link: ' . implode(', ', $links));
129
130                 System::jsonExit($return);
131         }
132 }