3 namespace Friendica\Module\Api\Mastodon;
5 use Friendica\Api\Mastodon;
6 use Friendica\Core\System;
7 use Friendica\Database\DBA;
8 use Friendica\Model\APContact;
10 use Friendica\Model\Contact;
11 use Friendica\Model\Introduction;
12 use Friendica\Module\Base\Api;
13 use Friendica\Network\HTTPException;
16 * @see https://docs.joinmastodon.org/api/rest/follow-requests/
18 class FollowRequests extends Api
20 public static function init(array $parameters = [])
22 parent::init($parameters);
25 throw new HTTPException\UnauthorizedException();
30 * @param array $parameters
31 * @throws HTTPException\BadRequestException
32 * @throws HTTPException\ForbiddenException
33 * @throws HTTPException\NotFoundException
34 * @throws HTTPException\UnauthorizedException
35 * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#accept-follow
36 * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#reject-follow
38 public static function post(array $parameters = [])
40 parent::post($parameters);
42 $Intro = DI::intro()->fetch(['id' => $parameters['id'], 'uid' => self::$current_user_id]);
44 $contactId = $Intro->{'contact-id'};
46 $relationship = new Mastodon\Relationship();
47 $relationship->id = $contactId;
49 switch ($parameters['action']) {
52 $relationship = Mastodon\Relationship::createFromContact(Contact::getById($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 if (isset($since_id) && isset($max_id)) {
82 $condition = ['`uid` = ? AND NOT `ignore` AND `id` > ? AND `id` < ?', self::$current_user_id, $since_id, $max_id];
83 } elseif (isset($since_id)) {
84 $condition = ['`uid` = ? AND NOT `ignore` AND `id` > ?', self::$current_user_id, $since_id];
85 } elseif (isset($max_id)) {
86 $condition = ['`uid` = ? AND NOT `ignore` AND `id` < ?', self::$current_user_id, $max_id];
88 $condition = ['`uid` = ? AND NOT `ignore`', self::$current_user_id];
91 $count = DBA::count('intro', $condition);
93 $intros = DBA::selectToArray(
97 ['order' => ['id' => 'DESC'], 'limit' => $limit]
101 foreach ($intros as $intro) {
102 $cdata = Contact::getPublicAndUserContacID($intro['contact-id'], $intro['uid']);
103 if (empty($cdata['public'])) {
107 $publicContact = Contact::getById($cdata['public']);
108 $userContact = Contact::getById($cdata['user']);
109 $apcontact = APContact::getByURL($publicContact['url'], false);
110 $account = Mastodon\Account::create($baseUrl, $publicContact, $apcontact, $userContact);
112 // Not ideal, the same "account" can have multiple ids depending on the context
113 $account->id = $intro['id'];
115 $return[] = $account;
119 if (isset($_GET['limit'])) {
120 $base_query['limit'] = $limit;
124 if ($count > $limit) {
125 $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $intros[count($intros) - 1]['id']]) . '>; rel="next"';
127 $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['since_id' => $intros[0]['id']]) . '>; rel="prev"';
129 header('Link: ' . implode(', ', $links));
131 System::jsonExit($return);