]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/FollowRequests.php
Merge pull request #10275 from very-ape/authenticate-hook
[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/
83          */
84         public static function rawContent(array $parameters = [])
85         {
86                 self::login(self::SCOPE_READ);
87                 $uid = self::getCurrentUserID();
88
89                 $request = self::getRequest([
90                         'min_id' => 0,
91                         'max_id' => 0,
92                         'limit'  => 40, // Maximum number of results to return. Defaults to 40. Paginate using the HTTP Link header.
93                 ]);
94
95                 $baseUrl = DI::baseUrl();
96
97                 $introductions = DI::intro()->selectByBoundaries(
98                         ['`uid` = ? AND NOT `ignore`', $uid],
99                         ['order' => ['id' => 'DESC']],
100                         $request['min_id'],
101                         $request['max_id'],
102                         $request['limit']
103                 );
104
105                 $return = [];
106
107                 foreach ($introductions as $key => $introduction) {
108                         try {
109                                 $return[] = DI::mstdnFollowRequest()->createFromIntroduction($introduction);
110                         } catch (HTTPException\InternalServerErrorException $exception) {
111                                 DI::intro()->delete($introduction);
112                                 unset($introductions[$key]);
113                         }
114                 }
115
116                 $base_query = [];
117                 if (isset($_GET['limit'])) {
118                         $base_query['limit'] = $request['limit'];
119                 }
120
121                 $links = [];
122                 if ($introductions->getTotalCount() > $request['limit']) {
123                         $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $introductions[count($introductions) - 1]->id]) . '>; rel="next"';
124                 }
125
126                 if (count($introductions)) {
127                         $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['min_id' => $introductions[0]->id]) . '>; rel="prev"';
128                 }
129
130                 header('Link: ' . implode(', ', $links));
131
132                 System::jsonExit($return);
133         }
134 }