]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Lists/Accounts.php
135701904ec9b0efcfe29286a384e00760fb6862
[friendica.git] / src / Module / Api / Mastodon / Lists / Accounts.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\Lists;
23
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Circle;
28 use Friendica\Module\BaseApi;
29
30 /**
31  * @see https://docs.joinmastodon.org/methods/timelines/lists/#accounts-in-a-list
32  *
33  * Currently the output will be unordered since we use public contact ids in the api and not user contact ids.
34  */
35 class Accounts extends BaseApi
36 {
37         protected function delete(array $request = [])
38         {
39                 $this->checkAllowedScope(self::SCOPE_WRITE);
40
41                 $request = $this->getRequest([
42                         'account_ids' => [], // Array of account IDs to remove from the list
43                 ], $request);
44
45                 if (empty($request['account_ids']) || empty($this->parameters['id'])) {
46                         DI::mstdnError()->UnprocessableEntity();
47                 }
48
49                 return Circle::removeMembers($this->parameters['id'], $request['account_ids']);
50         }
51
52         protected function post(array $request = [])
53         {
54                 $this->checkAllowedScope(self::SCOPE_WRITE);
55
56                 $request = $this->getRequest([
57                         'account_ids' =>  [], // Array of account IDs to add to the list
58                 ], $request);
59
60                 if (empty($request['account_ids']) || empty($this->parameters['id'])) {
61                         DI::mstdnError()->UnprocessableEntity();
62                 }
63
64                 Circle::addMembers($this->parameters['id'], $request['account_ids']);
65         }
66
67         /**
68          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
69          */
70         protected function rawContent(array $request = [])
71         {
72                 $this->checkAllowedScope(self::SCOPE_READ);
73                 $uid = self::getCurrentUserID();
74
75                 if (empty($this->parameters['id'])) {
76                         DI::mstdnError()->UnprocessableEntity();
77                 }
78
79                 $id = $this->parameters['id'];
80                 if (!DBA::exists('group', ['id' => $id, 'uid' => $uid])) {
81                         DI::mstdnError()->RecordNotFound();
82                 }
83
84                 $request = $this->getRequest([
85                         'max_id'   => 0,  // Return results older than this id
86                         'since_id' => 0,  // Return results newer than this id
87                         'min_id'   => 0,  // Return results immediately newer than id
88                         'limit'    => 40, // Maximum number of results. Defaults to 40. Max 40. Set to 0 in order to get all accounts without pagination.
89                 ], $request);
90
91                 $params = ['order' => ['contact-id' => true]];
92
93                 if ($request['limit'] != 0) {
94                         $params['limit'] = min($request['limit'], 40);
95                 }
96
97                 $condition = ['gid' => $id];
98
99                 if (!empty($request['max_id'])) {
100                         $condition = DBA::mergeConditions($condition, ["`contact-id` < ?", $request['max_id']]);
101                 }
102
103                 if (!empty($request['since_id'])) {
104                         $condition = DBA::mergeConditions($condition, ["`contact-id` > ?", $request['since_id']]);
105                 }
106
107                 if (!empty($request['min_id'])) {
108                         $condition = DBA::mergeConditions($condition, ["`contact-id` > ?", $request['min_id']]);
109
110                         $params['order'] = ['contact-id'];
111                 }
112
113                 $accounts = [];
114
115                 $members = DBA::select('group_member', ['contact-id'], $condition, $params);
116                 while ($member = DBA::fetch($members)) {
117                         self::setBoundaries($member['contact-id']);
118                         try {
119                                 $accounts[] = DI::mstdnAccount()->createFromContactId($member['contact-id'], $uid);
120                         } catch (\Exception $exception) {
121                         }
122                 }
123                 DBA::close($members);
124
125                 if (!empty($request['min_id'])) {
126                         $accounts = array_reverse($accounts);
127                 }
128
129                 self::setLinkHeader();
130                 $this->jsonExit($accounts);
131         }
132 }