]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Accounts/Followers.php
Make BaseApi->checkAllowedScope into an object method
[friendica.git] / src / Module / Api / Mastodon / Accounts / Followers.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Api\Mastodon\Accounts;
23
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Contact;
28 use Friendica\Module\BaseApi;
29
30 /**
31  * @see https://docs.joinmastodon.org/methods/accounts/
32  */
33 class Followers extends BaseApi
34 {
35         /**
36          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
37          */
38         protected function rawContent(array $request = [])
39         {
40                 $this->checkAllowedScope(self::SCOPE_READ);
41                 $uid = self::getCurrentUserID();
42
43                 if (empty($this->parameters['id'])) {
44                         DI::mstdnError()->UnprocessableEntity();
45                 }
46
47                 $id = $this->parameters['id'];
48                 if (!DBA::exists('contact', ['id' => $id, 'uid' => 0])) {
49                         DI::mstdnError()->RecordNotFound();
50                 }
51
52                 $request = $this->getRequest([
53                         'max_id'   => 0,  // Return results older than this id
54                         'since_id' => 0,  // Return results newer than this id
55                         'min_id'   => 0,  // Return results immediately newer than id
56                         'limit'    => 40, // Maximum number of results to return. Defaults to 40.
57                 ], $request);
58
59                 if ($id == Contact::getPublicIdByUserId($uid)) {
60                         $params = ['order' => ['pid' => true], 'limit' => $request['limit']];
61
62                         $condition = ['uid' => $uid, 'self' => false, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]];
63
64                         if (!empty($request['max_id'])) {
65                                 $condition = DBA::mergeConditions($condition, ["`pid` < ?", $request['max_id']]);
66                         }
67
68                         if (!empty($request['since_id'])) {
69                                 $condition = DBA::mergeConditions($condition, ["`pid` > ?", $request['since_id']]);
70                         }
71
72                         if (!empty($request['min_id'])) {
73                                 $condition = DBA::mergeConditions($condition, ["`pid` > ?", $request['min_id']]);
74
75                                 $params['order'] = ['pid'];
76                         }
77
78                         $accounts = [];
79
80                         foreach (Contact::selectAccountToArray(['pid'], $condition, $params) as $follower) {
81                                 self::setBoundaries($follower['pid']);
82                                 $accounts[] = DI::mstdnAccount()->createFromContactId($follower['pid'], $uid);
83                         }
84                 } else {
85                         $params = ['order' => ['relation-cid' => true], 'limit' => $request['limit']];
86
87                         $condition = ['cid' => $id, 'follows' => true];
88
89                         if (!empty($request['max_id'])) {
90                                 $condition = DBA::mergeConditions($condition, ["`relation-cid` < ?", $request['max_id']]);
91                         }
92
93                         if (!empty($request['since_id'])) {
94                                 $condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $request['since_id']]);
95                         }
96
97                         if (!empty($request['min_id'])) {
98                                 $condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $request['min_id']]);
99
100                                 $params['order'] = ['relation-cid'];
101                         }
102
103                         $accounts = [];
104
105                         $followers = DBA::select('contact-relation', ['relation-cid'], $condition, $params);
106                         while ($follower = DBA::fetch($followers)) {
107                                 self::setBoundaries($follower['relation-cid']);
108                                 $accounts[] = DI::mstdnAccount()->createFromContactId($follower['relation-cid'], $uid);
109                         }
110                         DBA::close($followers);
111                 }
112
113                 if (!empty($request['min_id'])) {
114                         $accounts = array_reverse($accounts);
115                 }
116
117                 self::setLinkHeader();
118                 $this->jsonExit($accounts);
119         }
120 }