diff --git a/src/Module/Api/Mastodon/Directory.php b/src/Module/Api/Mastodon/Directory.php new file mode 100644 index 000000000..7d18637e2 --- /dev/null +++ b/src/Module/Api/Mastodon/Directory.php @@ -0,0 +1,72 @@ +. + * + */ + +namespace Friendica\Module\Api\Mastodon; + +use Friendica\Core\Logger; +use Friendica\Core\Protocol; +use Friendica\Core\System; +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Module\BaseApi; +use Friendica\Network\HTTPException; + +/** + * @see https://docs.joinmastodon.org/methods/instance/directory/ + */ +class Directory extends BaseApi +{ + /** + * @param array $parameters + * @throws HTTPException\InternalServerErrorException + * @throws \ImagickException + * @see https://docs.joinmastodon.org/methods/instance/directory/ + */ + public static function rawContent(array $parameters = []) + { + $offset = (int)!isset($_REQUEST['offset']) ? 0 : $_REQUEST['offset']; + $limit = (int)!isset($_REQUEST['limit']) ? 40 : $_REQUEST['limit']; + $order = !isset($_REQUEST['order']) ? 'active' : $_REQUEST['order']; + $local = (bool)!isset($_REQUEST['local']) ? false : ($_REQUEST['local'] == 'true'); + + Logger::info('directory', ['offset' => $offset, 'limit' => $limit, 'order' => $order, 'local' => $local]); + + if ($local) { + $table = 'owner-view'; + $condition = ['net-publish' => true]; + } else { + $table = 'contact'; + $condition = ['uid' => 0, 'hidden' => false, 'network' => Protocol::FEDERATED]; + } + + $params = ['limit' => [$offset, $limit], + 'order' => [($order == 'active') ? 'last-item' : 'created' => true]]; + + $accounts = []; + $contacts = DBA::select($table, ['id'], $condition, $params); + while ($contact = DBA::fetch($contacts)) { + $accounts[] = DI::mstdnAccount()->createFromContactId($contact['id']); + } + DBA::close($contacts); + + System::jsonExit($accounts); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 4227db240..8387366b3 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -49,6 +49,7 @@ return [ '/api' => [ '/v1' => [ '/custom_emojis' => [Module\Api\Mastodon\CustomEmojis::class, [R::GET ]], + '/directory' => [Module\Api\Mastodon\Directory::class, [R::GET ]], '/follow_requests' => [Module\Api\Mastodon\FollowRequests::class, [R::GET ]], '/follow_requests/{id:\d+}/{action}' => [Module\Api\Mastodon\FollowRequests::class, [ R::POST]], '/instance' => [Module\Api\Mastodon\Instance::class, [R::GET ]],