3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Api\Mastodon\Lists;
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
27 use Friendica\Model\Circle;
28 use Friendica\Module\BaseApi;
31 * @see https://docs.joinmastodon.org/methods/timelines/lists/#accounts-in-a-list
33 * Currently the output will be unordered since we use public contact ids in the api and not user contact ids.
35 class Accounts extends BaseApi
37 protected function delete(array $request = [])
39 self::checkAllowedScope(self::SCOPE_WRITE);
41 $request = $this->getRequest([
42 'account_ids' => [], // Array of account IDs to remove from the list
45 if (empty($request['account_ids']) || empty($this->parameters['id'])) {
46 DI::mstdnError()->UnprocessableEntity();
49 return Circle::removeMembers($this->parameters['id'], $request['account_ids']);
52 protected function post(array $request = [])
54 self::checkAllowedScope(self::SCOPE_WRITE);
56 $request = $this->getRequest([
57 'account_ids' => [], // Array of account IDs to add to the list
60 if (empty($request['account_ids']) || empty($this->parameters['id'])) {
61 DI::mstdnError()->UnprocessableEntity();
64 Circle::addMembers($this->parameters['id'], $request['account_ids']);
68 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
70 protected function rawContent(array $request = [])
72 self::checkAllowedScope(self::SCOPE_READ);
73 $uid = self::getCurrentUserID();
75 if (empty($this->parameters['id'])) {
76 DI::mstdnError()->UnprocessableEntity();
79 $id = $this->parameters['id'];
80 if (!DBA::exists('group', ['id' => $id, 'uid' => $uid])) {
81 DI::mstdnError()->RecordNotFound();
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.
91 $params = ['order' => ['contact-id' => true]];
93 if ($request['limit'] != 0) {
94 $params['limit'] = min($request['limit'], 40);
97 $condition = ['gid' => $id];
99 if (!empty($request['max_id'])) {
100 $condition = DBA::mergeConditions($condition, ["`contact-id` < ?", $request['max_id']]);
103 if (!empty($request['since_id'])) {
104 $condition = DBA::mergeConditions($condition, ["`contact-id` > ?", $request['since_id']]);
107 if (!empty($request['min_id'])) {
108 $condition = DBA::mergeConditions($condition, ["`contact-id` > ?", $request['min_id']]);
110 $params['order'] = ['contact-id'];
115 $members = DBA::select('group_member', ['contact-id'], $condition, $params);
116 while ($member = DBA::fetch($members)) {
117 self::setBoundaries($member['contact-id']);
119 $accounts[] = DI::mstdnAccount()->createFromContactId($member['contact-id'], $uid);
120 } catch (\Exception $exception) {
123 DBA::close($members);
125 if (!empty($request['min_id'])) {
126 $accounts = array_reverse($accounts);
129 self::setLinkHeader();
130 System::jsonExit($accounts);