]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/FollowRequests.php
515dc451cdcb947d412d67931684155016d54197
[friendica.git] / src / Module / Api / Mastodon / FollowRequests.php
1 <?php
2
3 namespace Friendica\Module\Api\Mastodon;
4
5 use Friendica\Api\Mastodon\Account;
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;
12
13 /**
14  * @see https://docs.joinmastodon.org/api/rest/follow-requests/
15  */
16 class FollowRequests extends Api
17 {
18         public static function init(array $parameters = [])
19         {
20                 parent::init($parameters);
21
22                 self::login();
23         }
24
25         /**
26          * @param array $parameters
27          * @throws HTTPException\InternalServerErrorException
28          * @see https://docs.joinmastodon.org/api/rest/follow-requests/#get-api-v1-follow-requests
29          */
30         public static function rawContent(array $parameters = [])
31         {
32                 $since_id = $_GET['since_id'] ?? null;
33                 $max_id = $_GET['max_id'] ?? null;
34                 $limit = intval($_GET['limit'] ?? 40);
35
36                 if (isset($since_id) && isset($max_id)) {
37                         $condition = ['`uid` = ? AND NOT `self` AND `pending` AND `id` > ? AND `id` < ?', self::$current_user_id, $since_id, $max_id];
38                 } elseif (isset($since_id)) {
39                         $condition = ['`uid` = ? AND NOT `self` AND `pending` AND `id` > ?', self::$current_user_id, $since_id];
40                 } elseif (isset($max_id)) {
41                         $condition = ['`uid` = ? AND NOT `self` AND `pending` AND `id` < ?', self::$current_user_id, $max_id];
42                 } else {
43                         $condition = ['`uid` = ? AND NOT `self` AND `pending`', self::$current_user_id];
44                 }
45
46                 $count = DBA::count('contact', $condition);
47
48                 $contacts = Contact::selectToArray(
49                         [],
50                         $condition,
51                         ['order' => ['id' => 'DESC'], 'limit' => $limit]
52                 );
53
54                 $return = [];
55                 foreach ($contacts as $contact) {
56                         $account = Account::createFromContact($contact);
57
58                         $return[] = $account;
59                 }
60
61                 $base_query = [];
62                 if (isset($_GET['limit'])) {
63                         $base_query['limit'] = $limit;
64                 }
65
66                 /** @var BaseURL $BaseURL */
67                 $BaseURL = self::getClass(BaseURL::class);
68
69                 $links = [];
70                 if ($count > $limit) {
71                         $links[] = '<' . $BaseURL->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $contacts[count($contacts) - 1]['id']]) . '>; rel="next"';
72                 }
73                 $links[] = '<' . $BaseURL->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['since_id' => $contacts[0]['id']]) . '>; rel="prev"';
74
75                 header('Link: ' . implode(', ', $links));
76
77                 System::jsonExit($return);
78         }
79 }