3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Api\Mastodon;
24 use Friendica\Core\System;
26 use Friendica\Module\BaseApi;
27 use Friendica\Network\HTTPException;
30 * @see https://docs.joinmastodon.org/methods/accounts/follow_requests
32 class FollowRequests extends BaseApi
34 public static function init(array $parameters = [])
36 parent::init($parameters);
39 throw new HTTPException\UnauthorizedException();
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
52 * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#accept-follow
53 * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#reject-follow
55 public static function post(array $parameters = [])
57 parent::post($parameters);
59 $introduction = DI::intro()->selectFirst(['id' => $parameters['id'], 'uid' => self::$current_user_id]);
61 $contactId = $introduction->{'contact-id'};
63 switch ($parameters['action']) {
65 $introduction->confirm();
67 $relationship = DI::mstdnRelationship()->createFromContactId($contactId);
70 $introduction->ignore();
72 $relationship = DI::mstdnRelationship()->createDefaultFromContactId($contactId);
75 $introduction->discard();
77 $relationship = DI::mstdnRelationship()->createDefaultFromContactId($contactId);
80 throw new HTTPException\BadRequestException('Unexpected action parameter, expecting "authorize", "ignore" or "reject"');
83 System::jsonExit($relationship);
87 * @param array $parameters
88 * @throws HTTPException\InternalServerErrorException
89 * @throws \ImagickException
90 * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#pending-follows
92 public static function rawContent(array $parameters = [])
94 $min_id = $_GET['min_id'] ?? null;
95 $max_id = $_GET['max_id'] ?? null;
96 $limit = intval($_GET['limit'] ?? 40);
98 $baseUrl = DI::baseUrl();
100 $introductions = DI::intro()->selectByBoundaries(
101 ['`uid` = ? AND NOT `ignore`', self::$current_user_id],
102 ['order' => ['id' => 'DESC']],
110 foreach ($introductions as $key => $introduction) {
112 $return[] = DI::mstdnFollowRequest()->createFromIntroduction($introduction);
113 } catch (HTTPException\InternalServerErrorException $exception) {
114 DI::intro()->delete($introduction);
115 unset($introductions[$key]);
120 if (isset($_GET['limit'])) {
121 $base_query['limit'] = $limit;
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"';
129 if (count($introductions)) {
130 $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['min_id' => $introductions[0]->id]) . '>; rel="prev"';
133 header('Link: ' . implode(', ', $links));
135 System::jsonExit($return);