3 namespace Friendica\Module\Api\Mastodon;
5 use Friendica\Api\Entity\Mastodon;
6 use Friendica\Api\Entity\Mastodon\Relationship;
7 use Friendica\Core\System;
9 use Friendica\Model\Contact;
10 use Friendica\Module\Base\Api;
11 use Friendica\Network\HTTPException;
14 * @see https://docs.joinmastodon.org/methods/accounts/follow_requests
16 class FollowRequests extends Api
18 public static function init(array $parameters = [])
20 parent::init($parameters);
23 throw new HTTPException\UnauthorizedException();
28 * @param array $parameters
29 * @throws HTTPException\BadRequestException
30 * @throws HTTPException\ForbiddenException
31 * @throws HTTPException\InternalServerErrorException
32 * @throws HTTPException\NotFoundException
33 * @throws HTTPException\UnauthorizedException
34 * @throws \ImagickException
36 * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#accept-follow
37 * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#reject-follow
39 public static function post(array $parameters = [])
41 parent::post($parameters);
43 $introduction = DI::intro()->selectFirst(['id' => $parameters['id'], 'uid' => self::$current_user_id]);
45 $contactId = $introduction->{'contact-id'};
47 switch ($parameters['action']) {
49 $introduction->confirm();
51 $relationship = DI::mstdnRelationship()->createFromContactId($contactId);
54 $introduction->ignore();
56 $relationship = DI::mstdnRelationship()->createDefaultFromContactId($contactId);
59 $introduction->discard();
61 $relationship = DI::mstdnRelationship()->createDefaultFromContactId($contactId);
64 throw new HTTPException\BadRequestException('Unexpected action parameter, expecting "authorize", "ignore" or "reject"');
67 System::jsonExit($relationship);
71 * @param array $parameters
72 * @throws HTTPException\InternalServerErrorException
73 * @throws \ImagickException
74 * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#pending-follows
76 public static function rawContent(array $parameters = [])
78 $since_id = $_GET['since_id'] ?? null;
79 $max_id = $_GET['max_id'] ?? null;
80 $limit = intval($_GET['limit'] ?? 40);
82 $baseUrl = DI::baseUrl();
84 $introductions = DI::intro()->selectByBoundaries(
85 ['`uid` = ? AND NOT `ignore`', self::$current_user_id],
86 ['order' => ['id' => 'DESC']],
94 foreach ($introductions as $key => $introduction) {
96 $return[] = DI::mstdnFollowRequest()->createFromIntroduction($introduction);
97 } catch (HTTPException\InternalServerErrorException $exception) {
98 DI::intro()->delete($introduction);
99 unset($introductions[$key]);
104 if (isset($_GET['limit'])) {
105 $base_query['limit'] = $limit;
109 if ($introductions->getTotalCount() > $limit) {
110 $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $introductions[count($introductions) - 1]->id]) . '>; rel="next"';
112 $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['since_id' => $introductions[0]->id]) . '>; rel="prev"';
114 header('Link: ' . implode(', ', $links));
116 System::jsonExit($return);