]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/FollowRequests.php
Merge pull request #8222 from annando/ap-gnusocial
[friendica.git] / src / Module / Api / Mastodon / FollowRequests.php
1 <?php
2
3 namespace Friendica\Module\Api\Mastodon;
4
5 use Friendica\Core\System;
6 use Friendica\DI;
7 use Friendica\Module\BaseApi;
8 use Friendica\Network\HTTPException;
9
10 /**
11  * @see https://docs.joinmastodon.org/methods/accounts/follow_requests
12  */
13 class FollowRequests extends BaseApi
14 {
15         public static function init(array $parameters = [])
16         {
17                 parent::init($parameters);
18
19                 if (!self::login()) {
20                         throw new HTTPException\UnauthorizedException();
21                 }
22         }
23
24         /**
25          * @param array $parameters
26          * @throws HTTPException\BadRequestException
27          * @throws HTTPException\ForbiddenException
28          * @throws HTTPException\InternalServerErrorException
29          * @throws HTTPException\NotFoundException
30          * @throws HTTPException\UnauthorizedException
31          * @throws \ImagickException
32          *
33          * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#accept-follow
34          * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#reject-follow
35          */
36         public static function post(array $parameters = [])
37         {
38                 parent::post($parameters);
39
40                 $introduction = DI::intro()->selectFirst(['id' => $parameters['id'], 'uid' => self::$current_user_id]);
41
42                 $contactId = $introduction->{'contact-id'};
43
44                 switch ($parameters['action']) {
45                         case 'authorize':
46                                 $introduction->confirm();
47
48                                 $relationship = DI::mstdnRelationship()->createFromContactId($contactId);
49                                 break;
50                         case 'ignore':
51                                 $introduction->ignore();
52
53                                 $relationship = DI::mstdnRelationship()->createDefaultFromContactId($contactId);
54                                 break;
55                         case 'reject':
56                                 $introduction->discard();
57
58                                 $relationship = DI::mstdnRelationship()->createDefaultFromContactId($contactId);
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                 $introductions = DI::intro()->selectByBoundaries(
82                         ['`uid` = ? AND NOT `ignore`', self::$current_user_id],
83                         ['order' => ['id' => 'DESC']],
84                         $since_id,
85                         $max_id,
86                         $limit
87                 );
88
89                 $return = [];
90
91                 foreach ($introductions as $key => $introduction) {
92                         try {
93                                 $return[] = DI::mstdnFollowRequest()->createFromIntroduction($introduction);
94                         } catch (HTTPException\InternalServerErrorException $exception) {
95                                 DI::intro()->delete($introduction);
96                                 unset($introductions[$key]);
97                         }
98                 }
99
100                 $base_query = [];
101                 if (isset($_GET['limit'])) {
102                         $base_query['limit'] = $limit;
103                 }
104
105                 $links = [];
106                 if ($introductions->getTotalCount() > $limit) {
107                         $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $introductions[count($introductions) - 1]->id]) . '>; rel="next"';
108                 }
109
110                 if (count($introductions)) {
111                         $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['since_id' => $introductions[0]->id]) . '>; rel="prev"';
112                 }
113
114                 header('Link: ' . implode(', ', $links));
115
116                 System::jsonExit($return);
117         }
118 }