3 namespace Friendica\Module\Api\Mastodon;
5 use Friendica\Api\Mastodon;
6 use Friendica\App\BaseURL;
7 use Friendica\Core\System;
8 use Friendica\Database\DBA;
9 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();
29 public static function post(array $parameters = [])
31 parent::post($parameters);
33 /** @var Introduction $Intro */
34 $Intro = self::getClass(Introduction::class);
35 $Intro->fetch(['id' => $parameters['id'], 'uid' => self::$current_user_id]);
37 $contactId = $Intro->{'contact-id'};
39 $relationship = new Mastodon\Relationship();
40 $relationship->id = $contactId;
42 switch ($parameters['action']) {
45 $relationship = Mastodon\Relationship::createFromContact(Contact::getById($contactId));
54 throw new HTTPException\BadRequestException('Unexpected action parameter, expecting "authorize", "ignore" or "reject"');
57 System::jsonExit($relationship);
61 * @param array $parameters
62 * @throws HTTPException\InternalServerErrorException
63 * @see https://docs.joinmastodon.org/api/rest/follow-requests/#get-api-v1-follow-requests
65 public static function rawContent(array $parameters = [])
67 $since_id = $_GET['since_id'] ?? null;
68 $max_id = $_GET['max_id'] ?? null;
69 $limit = intval($_GET['limit'] ?? 40);
71 if (isset($since_id) && isset($max_id)) {
72 $condition = ['`uid` = ? AND NOT `ignore` AND `id` > ? AND `id` < ?', self::$current_user_id, $since_id, $max_id];
73 } elseif (isset($since_id)) {
74 $condition = ['`uid` = ? AND NOT `ignore` AND `id` > ?', self::$current_user_id, $since_id];
75 } elseif (isset($max_id)) {
76 $condition = ['`uid` = ? AND NOT `ignore` AND `id` < ?', self::$current_user_id, $max_id];
78 $condition = ['`uid` = ? AND NOT `ignore`', self::$current_user_id];
81 $count = DBA::count('intro', $condition);
83 $intros = DBA::selectToArray(
87 ['order' => ['id' => 'DESC'], 'limit' => $limit]
91 foreach ($intros as $intro) {
92 $contact = Contact::getById($intro['contact-id']);
93 $apcontact = APContact::getByURL($contact['url'], false);
94 $account = Mastodon\Account::createFromContact($contact, $apcontact);
96 // Not ideal, the same "account" can have multiple ids depending on the context
97 $account->id = $intro['id'];
103 if (isset($_GET['limit'])) {
104 $base_query['limit'] = $limit;
107 /** @var BaseURL $BaseURL */
108 $BaseURL = self::getClass(BaseURL::class);
111 if ($count > $limit) {
112 $links[] = '<' . $BaseURL->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $intros[count($intros) - 1]['id']]) . '>; rel="next"';
114 $links[] = '<' . $BaseURL->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['since_id' => $intros[0]['id']]) . '>; rel="prev"';
116 header('Link: ' . implode(', ', $links));
118 System::jsonExit($return);