friendica/src/Module/Api/Mastodon/Lists/Accounts.php

110 lines
3.1 KiB
PHP
Raw Normal View History

2021-05-09 11:35:51 +02:00
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module\Api\Mastodon\Lists;
2021-07-30 08:19:02 +02:00
use Friendica\App\Router;
2021-05-09 11:35:51 +02:00
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Module\BaseApi;
/**
2021-05-19 08:18:42 +02:00
* @see https://docs.joinmastodon.org/methods/timelines/lists/#accounts-in-a-list
2021-05-09 11:35:51 +02:00
*
* Currently the output will be unordered since we use public contact ids in the api and not user contact ids.
*/
class Accounts extends BaseApi
{
public static function delete(array $parameters = [])
{
2021-07-30 08:19:02 +02:00
self::unsupported(Router::DELETE);
2021-05-09 11:35:51 +02:00
}
public static function post(array $parameters = [])
{
2021-07-30 08:19:02 +02:00
self::unsupported(Router::POST);
2021-05-09 11:35:51 +02:00
}
/**
* @param array $parameters
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function rawContent(array $parameters = [])
{
self::checkAllowedScope(self::SCOPE_READ);
2021-05-09 11:35:51 +02:00
$uid = self::getCurrentUserID();
if (empty($parameters['id'])) {
2021-05-12 16:00:15 +02:00
DI::mstdnError()->UnprocessableEntity();
2021-05-09 11:35:51 +02:00
}
$id = $parameters['id'];
if (!DBA::exists('group', ['id' => $id, 'uid' => $uid])) {
DI::mstdnError()->RecordNotFound();
}
$request = self::getRequest([
'max_id' => 0, // Return results older than this id
'since_id' => 0, // Return results newer than this id
'limit' => 40, // Maximum number of results. Defaults to 40. Max 40. Set to 0 in order to get all accounts without pagination.
]);
2021-05-09 11:35:51 +02:00
2021-05-12 01:39:08 +02:00
$params = ['order' => ['contact-id' => true]];
2021-05-09 11:35:51 +02:00
if ($request['limit'] != 0) {
$params['limit'] = min($request['limit'], 40);
2021-05-12 01:39:08 +02:00
}
2021-07-30 08:19:02 +02:00
2021-05-09 11:35:51 +02:00
$condition = ['gid' => $id];
if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`contact-id` < ?", $request['max_id']]);
2021-05-09 11:35:51 +02:00
}
if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`contact-id` > ?", $request['since_id']]);
2021-05-09 11:35:51 +02:00
}
if (!empty($min_id)) {
$condition = DBA::mergeConditions($condition, ["`contact-id` > ?", $min_id]);
$params['order'] = ['contact-id'];
}
2021-05-12 01:39:08 +02:00
$accounts = [];
2021-05-09 11:35:51 +02:00
$members = DBA::select('group_member', ['contact-id'], $condition, $params);
while ($member = DBA::fetch($members)) {
2021-06-16 17:02:33 +02:00
self::setBoundaries($member['contact-id']);
2021-05-09 11:35:51 +02:00
$accounts[] = DI::mstdnAccount()->createFromContactId($member['contact-id'], $uid);
}
DBA::close($members);
if (!empty($min_id)) {
array_reverse($accounts);
}
2021-06-16 17:02:33 +02:00
self::setLinkHeader();
2021-05-09 11:35:51 +02:00
System::jsonExit($accounts);
}
}