]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/FollowRequests.php
Add POST follow request Mastodon API endpoint
[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\Model\Introduction;
11 use Friendica\Module\Base\Api;
12 use Friendica\Network\HTTPException;
13
14 /**
15  * @see https://docs.joinmastodon.org/api/rest/follow-requests/
16  */
17 class FollowRequests extends Api
18 {
19         public static function init(array $parameters = [])
20         {
21                 parent::init($parameters);
22
23                 if (!self::login()) {
24                         throw new HTTPException\UnauthorizedException();
25                 }
26         }
27
28         public static function post(array $parameters = [])
29         {
30                 parent::post($parameters);
31
32                 /** @var Introduction $Intro */
33                 $Intro = self::getClass(Introduction::class);
34                 $Intro->fetch(['id' => $parameters['id'], 'uid' => self::$current_user_id]);
35
36                 $contactId = $Intro->{'contact-id'};
37
38                 $relationship = new Mastodon\Relationship();
39                 $relationship->id = $contactId;
40
41                 switch ($parameters['action']) {
42                         case 'authorize':
43                                 $Intro->confirm();
44                                 $relationship = Mastodon\Relationship::createFromContact(Contact::getById($contactId));
45                                 break;
46                         case 'ignore':
47                                 $Intro->ignore();
48                                 break;
49                         case 'reject':
50                                 $Intro->discard();
51                                 break;
52                         default:
53                                 throw new HTTPException\BadRequestException('Unexpected action parameter, expecting "authorize", "ignore" or "reject"');
54                 }
55
56                 System::jsonExit($relationship);
57         }
58
59         /**
60          * @param array $parameters
61          * @throws HTTPException\InternalServerErrorException
62          * @see https://docs.joinmastodon.org/api/rest/follow-requests/#get-api-v1-follow-requests
63          */
64         public static function rawContent(array $parameters = [])
65         {
66                 $since_id = $_GET['since_id'] ?? null;
67                 $max_id = $_GET['max_id'] ?? null;
68                 $limit = intval($_GET['limit'] ?? 40);
69
70                 if (isset($since_id) && isset($max_id)) {
71                         $condition = ['`uid` = ? AND NOT `ignore` AND `id` > ? AND `id` < ?', self::$current_user_id, $since_id, $max_id];
72                 } elseif (isset($since_id)) {
73                         $condition = ['`uid` = ? AND NOT `ignore` AND `id` > ?', self::$current_user_id, $since_id];
74                 } elseif (isset($max_id)) {
75                         $condition = ['`uid` = ? AND NOT `ignore` AND `id` < ?', self::$current_user_id, $max_id];
76                 } else {
77                         $condition = ['`uid` = ? AND NOT `ignore`', self::$current_user_id];
78                 }
79
80                 $count = DBA::count('intro', $condition);
81
82                 $intros = DBA::selectToArray(
83                         'intro',
84                         [],
85                         $condition,
86                         ['order' => ['id' => 'DESC'], 'limit' => $limit]
87                 );
88
89                 $return = [];
90                 foreach ($intros as $intro) {
91                         $account = Mastodon\Account::createFromContact(Contact::getById($intro['contact-id']));
92
93                         // Not ideal, the same "account" can have multiple ids depending on the context
94                         $account->id = $intro['id'];
95
96                         $return[] = $account;
97                 }
98
99                 $base_query = [];
100                 if (isset($_GET['limit'])) {
101                         $base_query['limit'] = $limit;
102                 }
103
104                 /** @var BaseURL $BaseURL */
105                 $BaseURL = self::getClass(BaseURL::class);
106
107                 $links = [];
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"';
110                 }
111                 $links[] = '<' . $BaseURL->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['since_id' => $intros[0]['id']]) . '>; rel="prev"';
112
113                 header('Link: ' . implode(', ', $links));
114
115                 System::jsonExit($return);
116         }
117 }