3 namespace Friendica\Module\Api\Mastodon;
5 use Friendica\Core\System;
7 use Friendica\Module\BaseApi;
8 use Friendica\Network\HTTPException;
11 * @see https://docs.joinmastodon.org/methods/accounts/follow_requests
13 class FollowRequests extends BaseApi
15 public static function init(array $parameters = [])
17 parent::init($parameters);
20 throw new HTTPException\UnauthorizedException();
25 * @param array $parameters
26 * @throws HTTPException\BadRequestException
27 * @throws HTTPException\ForbiddenException
28 * @throws HTTPException\InternalServerErrorException
29 * @throws HTTPException\NotFoundException
30 * @throws HTTPException\UnauthorizedException
31 * @throws \ImagickException
33 * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#accept-follow
34 * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#reject-follow
36 public static function post(array $parameters = [])
38 parent::post($parameters);
40 $introduction = DI::intro()->selectFirst(['id' => $parameters['id'], 'uid' => self::$current_user_id]);
42 $contactId = $introduction->{'contact-id'};
44 switch ($parameters['action']) {
46 $introduction->confirm();
48 $relationship = DI::mstdnRelationship()->createFromContactId($contactId);
51 $introduction->ignore();
53 $relationship = DI::mstdnRelationship()->createDefaultFromContactId($contactId);
56 $introduction->discard();
58 $relationship = DI::mstdnRelationship()->createDefaultFromContactId($contactId);
61 throw new HTTPException\BadRequestException('Unexpected action parameter, expecting "authorize", "ignore" or "reject"');
64 System::jsonExit($relationship);
68 * @param array $parameters
69 * @throws HTTPException\InternalServerErrorException
70 * @throws \ImagickException
71 * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#pending-follows
73 public static function rawContent(array $parameters = [])
75 $since_id = $_GET['since_id'] ?? null;
76 $max_id = $_GET['max_id'] ?? null;
77 $limit = intval($_GET['limit'] ?? 40);
79 $baseUrl = DI::baseUrl();
81 $introductions = DI::intro()->selectByBoundaries(
82 ['`uid` = ? AND NOT `ignore`', self::$current_user_id],
83 ['order' => ['id' => 'DESC']],
91 foreach ($introductions as $key => $introduction) {
93 $return[] = DI::mstdnFollowRequest()->createFromIntroduction($introduction);
94 } catch (HTTPException\InternalServerErrorException $exception) {
95 DI::intro()->delete($introduction);
96 unset($introductions[$key]);
101 if (isset($_GET['limit'])) {
102 $base_query['limit'] = $limit;
106 if ($introductions->getTotalCount() > $limit) {
107 $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $introductions[count($introductions) - 1]->id]) . '>; rel="next"';
109 $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['since_id' => $introductions[0]->id]) . '>; rel="prev"';
111 header('Link: ' . implode(', ', $links));
113 System::jsonExit($return);