]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/FollowRequests.php
Switch Api\Mastodon\FollowRequests to list introductions instead of pending contacts
[friendica.git] / src / Module / Api / Mastodon / FollowRequests.php
1 <?php
2
3 namespace Friendica\Module\Api\Mastodon;
4
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;
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                 if (!self::login()) {
23                         throw new HTTPException\UnauthorizedException();
24                 }
25         }
26
27         /**
28          * @param array $parameters
29          * @throws HTTPException\InternalServerErrorException
30          * @see https://docs.joinmastodon.org/api/rest/follow-requests/#get-api-v1-follow-requests
31          */
32         public static function rawContent(array $parameters = [])
33         {
34                 $since_id = $_GET['since_id'] ?? null;
35                 $max_id = $_GET['max_id'] ?? null;
36                 $limit = intval($_GET['limit'] ?? 40);
37
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];
44                 } else {
45                         $condition = ['`uid` = ? AND NOT `ignore`', self::$current_user_id];
46                 }
47
48                 $count = DBA::count('intro', $condition);
49
50                 $intros = DBA::selectToArray(
51                         'intro',
52                         [],
53                         $condition,
54                         ['order' => ['id' => 'DESC'], 'limit' => $limit]
55                 );
56
57                 $return = [];
58                 foreach ($intros as $intro) {
59                         $account = Mastodon\Account::createFromContact(Contact::getById($intro['contact-id']));
60
61                         // Not ideal, the same "account" can have multiple ids depending on the context
62                         $account->id = $intro['id'];
63
64                         $return[] = $account;
65                 }
66
67                 $base_query = [];
68                 if (isset($_GET['limit'])) {
69                         $base_query['limit'] = $limit;
70                 }
71
72                 /** @var BaseURL $BaseURL */
73                 $BaseURL = self::getClass(BaseURL::class);
74
75                 $links = [];
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"';
78                 }
79                 $links[] = '<' . $BaseURL->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['since_id' => $intros[0]['id']]) . '>; rel="prev"';
80
81                 header('Link: ' . implode(', ', $links));
82
83                 System::jsonExit($return);
84         }
85 }