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();
29 public static function post(array $parameters = [])
31 parent::post($parameters);
33 $Intro = DI::intro()->fetch(['id' => $parameters['id'], 'uid' => self::$current_user_id]);
35 $contactId = $Intro->{'contact-id'};
37 $relationship = new Mastodon\Relationship();
38 $relationship->id = $contactId;
40 switch ($parameters['action']) {
43 $relationship = Mastodon\Relationship::createFromContact(Contact::getById($contactId));
52 throw new HTTPException\BadRequestException('Unexpected action parameter, expecting "authorize", "ignore" or "reject"');
55 System::jsonExit($relationship);
59 * @param array $parameters
60 * @throws HTTPException\InternalServerErrorException
61 * @see https://docs.joinmastodon.org/api/rest/follow-requests/#get-api-v1-follow-requests
63 public static function rawContent(array $parameters = [])
65 $since_id = $_GET['since_id'] ?? null;
66 $max_id = $_GET['max_id'] ?? null;
67 $limit = intval($_GET['limit'] ?? 40);
69 if (isset($since_id) && isset($max_id)) {
70 $condition = ['`uid` = ? AND NOT `ignore` AND `id` > ? AND `id` < ?', self::$current_user_id, $since_id, $max_id];
71 } elseif (isset($since_id)) {
72 $condition = ['`uid` = ? AND NOT `ignore` AND `id` > ?', self::$current_user_id, $since_id];
73 } elseif (isset($max_id)) {
74 $condition = ['`uid` = ? AND NOT `ignore` AND `id` < ?', self::$current_user_id, $max_id];
76 $condition = ['`uid` = ? AND NOT `ignore`', self::$current_user_id];
79 $count = DBA::count('intro', $condition);
81 $intros = DBA::selectToArray(
85 ['order' => ['id' => 'DESC'], 'limit' => $limit]
89 foreach ($intros as $intro) {
90 $contact = Contact::getById($intro['contact-id']);
91 $apcontact = APContact::getByURL($contact['url'], false);
92 $account = Mastodon\Account::createFromContact($contact, $apcontact);
94 // Not ideal, the same "account" can have multiple ids depending on the context
95 $account->id = $intro['id'];
101 if (isset($_GET['limit'])) {
102 $base_query['limit'] = $limit;
105 $BaseURL = DI::baseUrl();
108 if ($count > $limit) {
109 $links[] = '<' . $BaseURL->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $intros[count($intros) - 1]['id']]) . '>; rel="next"';
111 $links[] = '<' . $BaseURL->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['since_id' => $intros[0]['id']]) . '>; rel="prev"';
113 header('Link: ' . implode(', ', $links));
115 System::jsonExit($return);