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