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