]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Lists/Accounts.php
Merge pull request #10251 from nupplaphil/feat/drone_phptest
[friendica.git] / src / Module / Api / Mastodon / Lists / Accounts.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Lists;
23
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Module\BaseApi;
28
29 /**
30  * @see https://docs.joinmastodon.org/methods/timelines/lists/
31  *
32  * Currently the output will be unordered since we use public contact ids in the api and not user contact ids.
33  */
34 class Accounts extends BaseApi
35 {
36         public static function delete(array $parameters = [])
37         {
38                 self::unsupported('delete');
39         }
40
41         public static function post(array $parameters = [])
42         {
43                 self::unsupported('post');
44         }
45
46         /**
47          * @param array $parameters
48          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
49          */
50         public static function rawContent(array $parameters = [])
51         {
52                 self::login(self::SCOPE_READ);
53                 $uid = self::getCurrentUserID();
54
55                 if (empty($parameters['id'])) {
56                         DI::mstdnError()->UnprocessableEntity();
57                 }
58
59                 $id = $parameters['id'];
60                 if (!DBA::exists('group', ['id' => $id, 'uid' => $uid])) {
61                         DI::mstdnError()->RecordNotFound();
62                 }
63
64                 // Return results older than this id
65                 $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id'];
66                 // Return results newer than this id
67                 $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id'];
68                 // Maximum number of results. Defaults to 40. Max 40.
69                 // Set to 0 in order to get all accounts without pagination.
70                 $limit = (int)!isset($_REQUEST['limit']) ? 40 : $_REQUEST['limit'];
71
72
73                 $params = ['order' => ['contact-id' => true]];
74
75                 if ($limit != 0) {
76                         $params['limit'] = $limit;
77
78                 }
79         
80                 $condition = ['gid' => $id];
81
82                 if (!empty($max_id)) {
83                         $condition = DBA::mergeConditions($condition, ["`contact-id` < ?", $max_id]);
84                 }
85
86                 if (!empty($since_id)) {
87                         $condition = DBA::mergeConditions($condition, ["`contact-id` > ?", $since_id]);
88                 }
89
90                 if (!empty($min_id)) {
91                         $condition = DBA::mergeConditions($condition, ["`contact-id` > ?", $min_id]);
92
93                         $params['order'] = ['contact-id'];
94                 }
95
96                 $accounts = [];
97
98                 $members = DBA::select('group_member', ['contact-id'], $condition, $params);
99                 while ($member = DBA::fetch($members)) {
100                         $accounts[] = DI::mstdnAccount()->createFromContactId($member['contact-id'], $uid);
101                 }
102                 DBA::close($members);
103
104                 if (!empty($min_id)) {
105                         array_reverse($accounts);
106                 }
107
108                 System::jsonExit($accounts);
109         }
110 }