]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/FollowRequests.php
Merge branch 'stable' into develop
[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::checkAllowedScope(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::checkAllowedScope(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                 $introductions = DI::intro()->selectByBoundaries(
96                         ['`uid` = ? AND NOT `ignore`', $uid],
97                         ['order' => ['id' => 'DESC']],
98                         $request['min_id'],
99                         $request['max_id'],
100                         $request['limit']
101                 );
102
103                 $return = [];
104
105                 foreach ($introductions as $key => $introduction) {
106                         try {
107                                 self::setBoundaries($introduction->id);
108                                 $return[] = DI::mstdnFollowRequest()->createFromIntroduction($introduction);
109                         } catch (HTTPException\InternalServerErrorException $exception) {
110                                 DI::intro()->delete($introduction);
111                                 unset($introductions[$key]);
112                         }
113                 }
114
115                 self::setLinkHeader();
116                 System::jsonExit($return);
117         }
118 }