]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/FollowRequests.php
Some more API functions moved
[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\Model\Contact;
27 use Friendica\Module\BaseApi;
28 use Friendica\Network\HTTPException;
29
30 /**
31  * @see https://docs.joinmastodon.org/methods/accounts/follow_requests
32  */
33 class FollowRequests extends BaseApi
34 {
35         /**
36          * @param array $parameters
37          * @throws HTTPException\BadRequestException
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()->selectOneById($parameters['id'], $uid);
52
53                 $contactId = $introduction->cid;
54
55                 switch ($parameters['action']) {
56                         case 'authorize':
57                                 Contact\Introduction::confirm($introduction);
58                                 $relationship = DI::mstdnRelationship()->createFromContactId($contactId, $uid);
59
60                                 DI::intro()->delete($introduction);
61                                 break;
62                         case 'ignore':
63                                 $introduction->ignore();
64                                 $relationship = DI::mstdnRelationship()->createFromContactId($contactId, $uid);
65
66                                 DI::intro()->save($introduction);
67                                 break;
68                         case 'reject':
69                                 Contact\Introduction::discard($introduction);
70                                 $relationship = DI::mstdnRelationship()->createFromContactId($contactId, $uid);
71
72                                 DI::intro()->delete($introduction);
73                                 break;
74                         default:
75                                 throw new HTTPException\BadRequestException('Unexpected action parameter, expecting "authorize", "ignore" or "reject"');
76                 }
77
78                 System::jsonExit($relationship);
79         }
80
81         /**
82          * @param array $parameters
83          * @throws HTTPException\InternalServerErrorException
84          * @throws \ImagickException
85          * @see https://docs.joinmastodon.org/methods/accounts/follow_requests/
86          */
87         public static function rawContent(array $parameters = [])
88         {
89                 self::checkAllowedScope(self::SCOPE_READ);
90                 $uid = self::getCurrentUserID();
91
92                 $request = self::getRequest([
93                         'min_id' => 0,
94                         'max_id' => 0,
95                         'limit'  => 40, // Maximum number of results to return. Defaults to 40. Paginate using the HTTP Link header.
96                 ]);
97
98                 $introductions = DI::intro()->selectForUser($uid, $request['min_id'], $request['max_id'], $request['limit']);
99
100                 $return = [];
101
102                 foreach ($introductions as $key => $introduction) {
103                         try {
104                                 self::setBoundaries($introduction->id);
105                                 $return[] = DI::mstdnFollowRequest()->createFromIntroduction($introduction);
106                         } catch (HTTPException\InternalServerErrorException $exception) {
107                                 DI::intro()->delete($introduction);
108                                 unset($introductions[$key]);
109                         }
110                 }
111
112                 self::setLinkHeader();
113                 System::jsonExit($return);
114         }
115 }