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
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
43 * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#accept-follow
44 * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#reject-follow
46 public static function post(array $parameters = [])
48 self::checkAllowedScope(self::SCOPE_FOLLOW);
49 $uid = self::getCurrentUserID();
51 $introduction = DI::intro()->selectFirst(['id' => $parameters['id'], 'uid' => $uid]);
53 $contactId = $introduction->{'contact-id'};
55 switch ($parameters['action']) {
57 $introduction->confirm();
59 $relationship = DI::mstdnRelationship()->createFromContactId($contactId, $uid);
62 $introduction->ignore();
64 $relationship = DI::mstdnRelationship()->createFromContactId($contactId, $uid);
67 $introduction->discard();
69 $relationship = DI::mstdnRelationship()->createFromContactId($contactId, $uid);
72 throw new HTTPException\BadRequestException('Unexpected action parameter, expecting "authorize", "ignore" or "reject"');
75 System::jsonExit($relationship);
79 * @param array $parameters
80 * @throws HTTPException\InternalServerErrorException
81 * @throws \ImagickException
82 * @see https://docs.joinmastodon.org/methods/accounts/follow_requests/
84 public static function rawContent(array $parameters = [])
86 self::checkAllowedScope(self::SCOPE_READ);
87 $uid = self::getCurrentUserID();
89 $request = self::getRequest([
92 'limit' => 40, // Maximum number of results to return. Defaults to 40. Paginate using the HTTP Link header.
95 $baseUrl = DI::baseUrl();
97 $introductions = DI::intro()->selectByBoundaries(
98 ['`uid` = ? AND NOT `ignore`', $uid],
99 ['order' => ['id' => 'DESC']],
107 foreach ($introductions as $key => $introduction) {
109 $return[] = DI::mstdnFollowRequest()->createFromIntroduction($introduction);
110 } catch (HTTPException\InternalServerErrorException $exception) {
111 DI::intro()->delete($introduction);
112 unset($introductions[$key]);
117 if (isset($_GET['limit'])) {
118 $base_query['limit'] = $request['limit'];
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"';
126 if (count($introductions)) {
127 $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['min_id' => $introductions[0]->id]) . '>; rel="prev"';
130 header('Link: ' . implode(', ', $links));
132 System::jsonExit($return);