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

91 lines
2.7 KiB
PHP
Raw Normal View History

2021-05-08 13:46:24 +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\Accounts;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Module\BaseApi;
/**
* @see https://docs.joinmastodon.org/methods/accounts/
*/
class Following extends BaseApi
{
/**
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
protected function rawContent(array $request = [])
2021-05-08 13:46:24 +02:00
{
self::checkAllowedScope(self::SCOPE_READ);
2021-05-08 13:46:24 +02:00
$uid = self::getCurrentUserID();
if (empty($this->parameters['id'])) {
2021-05-12 16:00:15 +02:00
DI::mstdnError()->UnprocessableEntity();
2021-05-08 13:46:24 +02:00
}
$id = $this->parameters['id'];
2021-05-08 13:46:24 +02:00
if (!DBA::exists('contact', ['id' => $id, 'uid' => 0])) {
DI::mstdnError()->RecordNotFound();
}
$request = self::getRequest([
'max_id' => 0, // Return results older than this id
'since_id' => 0, // Return results newer than this id
'min_id' => 0, // Return results immediately newer than id
2021-05-19 08:18:42 +02:00
'limit' => 40, // Maximum number of results to return. Defaults to 40.
], $request);
2021-05-08 13:46:24 +02:00
$params = ['order' => ['cid' => true], 'limit' => $request['limit']];
2021-05-08 13:46:24 +02:00
$condition = ['relation-cid' => $id, 'follows' => true];
2021-05-08 13:46:24 +02:00
if (!empty($request['max_id'])) {
$condition = DBA::mergeConditions($condition, ["`cid` < ?", $request['max_id']]);
2021-05-08 13:46:24 +02:00
}
if (!empty($request['since_id'])) {
$condition = DBA::mergeConditions($condition, ["`cid` > ?", $request['since_id']]);
2021-05-08 13:46:24 +02:00
}
if (!empty($request['min_id'])) {
$condition = DBA::mergeConditions($condition, ["`cid` > ?", $request['min_id']]);
2021-05-08 14:28:04 +02:00
2021-05-08 13:46:24 +02:00
$params['order'] = ['cid'];
}
$followers = DBA::select('contact-relation', ['cid'], $condition, $params);
2021-05-08 13:46:24 +02:00
while ($follower = DBA::fetch($followers)) {
2021-06-16 17:02:33 +02:00
self::setBoundaries($follower['cid']);
$accounts[] = DI::mstdnAccount()->createFromContactId($follower['cid'], $uid);
2021-05-08 13:46:24 +02:00
}
DBA::close($followers);
if (!empty($request['min_id'])) {
2021-05-09 11:35:51 +02:00
array_reverse($accounts);
}
2021-06-16 17:02:33 +02:00
self::setLinkHeader();
2021-05-08 13:46:24 +02:00
System::jsonExit($accounts);
}
}