]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/FollowRequests.php
Merge pull request #8010 from MrPetovan/task/7968-mastodon-api-account-fields
[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         /**
30          * @param array $parameters
31          * @throws HTTPException\BadRequestException
32          * @throws HTTPException\ForbiddenException
33          * @throws HTTPException\NotFoundException
34          * @throws HTTPException\UnauthorizedException
35          * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#accept-follow
36          * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#reject-follow
37          */
38         public static function post(array $parameters = [])
39         {
40                 parent::post($parameters);
41
42                 $Intro = DI::intro()->fetch(['id' => $parameters['id'], 'uid' => self::$current_user_id]);
43
44                 $contactId = $Intro->{'contact-id'};
45
46                 $relationship = new Mastodon\Relationship();
47                 $relationship->id = $contactId;
48
49                 switch ($parameters['action']) {
50                         case 'authorize':
51                                 $Intro->confirm();
52                                 $relationship = Mastodon\Relationship::createFromContact(Contact::getById($contactId));
53                                 break;
54                         case 'ignore':
55                                 $Intro->ignore();
56                                 break;
57                         case 'reject':
58                                 $Intro->discard();
59                                 break;
60                         default:
61                                 throw new HTTPException\BadRequestException('Unexpected action parameter, expecting "authorize", "ignore" or "reject"');
62                 }
63
64                 System::jsonExit($relationship);
65         }
66
67         /**
68          * @param array $parameters
69          * @throws HTTPException\InternalServerErrorException
70          * @throws \ImagickException
71          * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#pending-follows
72          */
73         public static function rawContent(array $parameters = [])
74         {
75                 $since_id = $_GET['since_id'] ?? null;
76                 $max_id = $_GET['max_id'] ?? null;
77                 $limit = intval($_GET['limit'] ?? 40);
78
79                 $baseUrl = DI::baseUrl();
80
81                 if (isset($since_id) && isset($max_id)) {
82                         $condition = ['`uid` = ? AND NOT `ignore` AND `id` > ? AND `id` < ?', self::$current_user_id, $since_id, $max_id];
83                 } elseif (isset($since_id)) {
84                         $condition = ['`uid` = ? AND NOT `ignore` AND `id` > ?', self::$current_user_id, $since_id];
85                 } elseif (isset($max_id)) {
86                         $condition = ['`uid` = ? AND NOT `ignore` AND `id` < ?', self::$current_user_id, $max_id];
87                 } else {
88                         $condition = ['`uid` = ? AND NOT `ignore`', self::$current_user_id];
89                 }
90
91                 $count = DBA::count('intro', $condition);
92
93                 $intros = DBA::selectToArray(
94                         'intro',
95                         [],
96                         $condition,
97                         ['order' => ['id' => 'DESC'], 'limit' => $limit]
98                 );
99
100                 $return = [];
101                 foreach ($intros as $intro) {
102                         $cdata = Contact::getPublicAndUserContacID($intro['contact-id'], $intro['uid']);
103                         if (empty($cdata['public'])) {
104                                 continue;
105                         }
106
107                         $publicContact = Contact::getById($cdata['public']);
108                         $userContact = Contact::getById($cdata['user']);
109                         $apcontact = APContact::getByURL($publicContact['url'], false);
110                         $account = Mastodon\Account::create($baseUrl, $publicContact, $apcontact, $userContact);
111
112                         // Not ideal, the same "account" can have multiple ids depending on the context
113                         $account->id = $intro['id'];
114
115                         $return[] = $account;
116                 }
117
118                 $base_query = [];
119                 if (isset($_GET['limit'])) {
120                         $base_query['limit'] = $limit;
121                 }
122
123                 $links = [];
124                 if ($count > $limit) {
125                         $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $intros[count($intros) - 1]['id']]) . '>; rel="next"';
126                 }
127                 $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['since_id' => $intros[0]['id']]) . '>; rel="prev"';
128
129                 header('Link: ' . implode(', ', $links));
130
131                 System::jsonExit($return);
132         }
133 }