]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/FollowRequests.php
Implement correct behavior for min_id in boundary pagination
[friendica.git] / src / Module / Api / Mastodon / FollowRequests.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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         public static function init(array $parameters = [])
35         {
36                 parent::init($parameters);
37
38                 if (!self::login()) {
39                         throw new HTTPException\UnauthorizedException();
40                 }
41         }
42
43         /**
44          * @param array $parameters
45          * @throws HTTPException\BadRequestException
46          * @throws HTTPException\ForbiddenException
47          * @throws HTTPException\InternalServerErrorException
48          * @throws HTTPException\NotFoundException
49          * @throws HTTPException\UnauthorizedException
50          * @throws \ImagickException
51          *
52          * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#accept-follow
53          * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#reject-follow
54          */
55         public static function post(array $parameters = [])
56         {
57                 parent::post($parameters);
58
59                 $introduction = DI::intro()->selectFirst(['id' => $parameters['id'], 'uid' => self::$current_user_id]);
60
61                 $contactId = $introduction->{'contact-id'};
62
63                 switch ($parameters['action']) {
64                         case 'authorize':
65                                 $introduction->confirm();
66
67                                 $relationship = DI::mstdnRelationship()->createFromContactId($contactId);
68                                 break;
69                         case 'ignore':
70                                 $introduction->ignore();
71
72                                 $relationship = DI::mstdnRelationship()->createDefaultFromContactId($contactId);
73                                 break;
74                         case 'reject':
75                                 $introduction->discard();
76
77                                 $relationship = DI::mstdnRelationship()->createDefaultFromContactId($contactId);
78                                 break;
79                         default:
80                                 throw new HTTPException\BadRequestException('Unexpected action parameter, expecting "authorize", "ignore" or "reject"');
81                 }
82
83                 System::jsonExit($relationship);
84         }
85
86         /**
87          * @param array $parameters
88          * @throws HTTPException\InternalServerErrorException
89          * @throws \ImagickException
90          * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#pending-follows
91          */
92         public static function rawContent(array $parameters = [])
93         {
94                 $min_id = $_GET['min_id'] ?? null;
95                 $max_id = $_GET['max_id'] ?? null;
96                 $limit = intval($_GET['limit'] ?? 40);
97
98                 $baseUrl = DI::baseUrl();
99
100                 $introductions = DI::intro()->selectByBoundaries(
101                         ['`uid` = ? AND NOT `ignore`', self::$current_user_id],
102                         ['order' => ['id' => 'DESC']],
103                         $min_id,
104                         $max_id,
105                         $limit
106                 );
107
108                 $return = [];
109
110                 foreach ($introductions as $key => $introduction) {
111                         try {
112                                 $return[] = DI::mstdnFollowRequest()->createFromIntroduction($introduction);
113                         } catch (HTTPException\InternalServerErrorException $exception) {
114                                 DI::intro()->delete($introduction);
115                                 unset($introductions[$key]);
116                         }
117                 }
118
119                 $base_query = [];
120                 if (isset($_GET['limit'])) {
121                         $base_query['limit'] = $limit;
122                 }
123
124                 $links = [];
125                 if ($introductions->getTotalCount() > $limit) {
126                         $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $introductions[count($introductions) - 1]->id]) . '>; rel="next"';
127                 }
128
129                 if (count($introductions)) {
130                         $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['min_id' => $introductions[0]->id]) . '>; rel="prev"';
131                 }
132
133                 header('Link: ' . implode(', ', $links));
134
135                 System::jsonExit($return);
136         }
137 }