]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/FollowRequests.php
Add Model\Introduction class to DI registry
[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\Core\System;
7 use Friendica\Database\DBA;
8 use Friendica\Model\APContact;
9 use Friendica\DI;
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                 $Intro = DI::intro()->fetch(['id' => $parameters['id'], 'uid' => self::$current_user_id]);
34
35                 $contactId = $Intro->{'contact-id'};
36
37                 $relationship = new Mastodon\Relationship();
38                 $relationship->id = $contactId;
39
40                 switch ($parameters['action']) {
41                         case 'authorize':
42                                 $Intro->confirm();
43                                 $relationship = Mastodon\Relationship::createFromContact(Contact::getById($contactId));
44                                 break;
45                         case 'ignore':
46                                 $Intro->ignore();
47                                 break;
48                         case 'reject':
49                                 $Intro->discard();
50                                 break;
51                         default:
52                                 throw new HTTPException\BadRequestException('Unexpected action parameter, expecting "authorize", "ignore" or "reject"');
53                 }
54
55                 System::jsonExit($relationship);
56         }
57
58         /**
59          * @param array $parameters
60          * @throws HTTPException\InternalServerErrorException
61          * @see https://docs.joinmastodon.org/api/rest/follow-requests/#get-api-v1-follow-requests
62          */
63         public static function rawContent(array $parameters = [])
64         {
65                 $since_id = $_GET['since_id'] ?? null;
66                 $max_id = $_GET['max_id'] ?? null;
67                 $limit = intval($_GET['limit'] ?? 40);
68
69                 if (isset($since_id) && isset($max_id)) {
70                         $condition = ['`uid` = ? AND NOT `ignore` AND `id` > ? AND `id` < ?', self::$current_user_id, $since_id, $max_id];
71                 } elseif (isset($since_id)) {
72                         $condition = ['`uid` = ? AND NOT `ignore` AND `id` > ?', self::$current_user_id, $since_id];
73                 } elseif (isset($max_id)) {
74                         $condition = ['`uid` = ? AND NOT `ignore` AND `id` < ?', self::$current_user_id, $max_id];
75                 } else {
76                         $condition = ['`uid` = ? AND NOT `ignore`', self::$current_user_id];
77                 }
78
79                 $count = DBA::count('intro', $condition);
80
81                 $intros = DBA::selectToArray(
82                         'intro',
83                         [],
84                         $condition,
85                         ['order' => ['id' => 'DESC'], 'limit' => $limit]
86                 );
87
88                 $return = [];
89                 foreach ($intros as $intro) {
90                         $contact = Contact::getById($intro['contact-id']);
91                         $apcontact = APContact::getByURL($contact['url'], false);
92                         $account = Mastodon\Account::createFromContact($contact, $apcontact);
93
94                         // Not ideal, the same "account" can have multiple ids depending on the context
95                         $account->id = $intro['id'];
96
97                         $return[] = $account;
98                 }
99
100                 $base_query = [];
101                 if (isset($_GET['limit'])) {
102                         $base_query['limit'] = $limit;
103                 }
104
105                 $BaseURL = DI::baseUrl();
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 }