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\Contact;
10 use Friendica\Module\Base\Api;
11 use Friendica\Network\HTTPException;
14 * @see https://docs.joinmastodon.org/api/rest/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\InternalServerErrorException
30 * @see https://docs.joinmastodon.org/api/rest/follow-requests/#get-api-v1-follow-requests
32 public static function rawContent(array $parameters = [])
34 $since_id = $_GET['since_id'] ?? null;
35 $max_id = $_GET['max_id'] ?? null;
36 $limit = intval($_GET['limit'] ?? 40);
38 if (isset($since_id) && isset($max_id)) {
39 $condition = ['`uid` = ? AND NOT `ignore` AND `id` > ? AND `id` < ?', self::$current_user_id, $since_id, $max_id];
40 } elseif (isset($since_id)) {
41 $condition = ['`uid` = ? AND NOT `ignore` AND `id` > ?', self::$current_user_id, $since_id];
42 } elseif (isset($max_id)) {
43 $condition = ['`uid` = ? AND NOT `ignore` AND `id` < ?', self::$current_user_id, $max_id];
45 $condition = ['`uid` = ? AND NOT `ignore`', self::$current_user_id];
48 $count = DBA::count('intro', $condition);
50 $intros = DBA::selectToArray(
54 ['order' => ['id' => 'DESC'], 'limit' => $limit]
58 foreach ($intros as $intro) {
59 $account = Mastodon\Account::createFromContact(Contact::getById($intro['contact-id']));
61 // Not ideal, the same "account" can have multiple ids depending on the context
62 $account->id = $intro['id'];
68 if (isset($_GET['limit'])) {
69 $base_query['limit'] = $limit;
72 /** @var BaseURL $BaseURL */
73 $BaseURL = self::getClass(BaseURL::class);
76 if ($count > $limit) {
77 $links[] = '<' . $BaseURL->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $intros[count($intros) - 1]['id']]) . '>; rel="next"';
79 $links[] = '<' . $BaseURL->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['since_id' => $intros[0]['id']]) . '>; rel="prev"';
81 header('Link: ' . implode(', ', $links));
83 System::jsonExit($return);