From 7be6fbf6526d1d0ed321061a4194793293078a02 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 1 Sep 2020 19:29:39 +0000 Subject: [PATCH] API: Support for the "directory" endpoint --- src/Module/Api/Mastodon/Directory.php | 72 +++++++++++++++++++++++++++ static/routes.config.php | 1 + 2 files changed, 73 insertions(+) create mode 100644 src/Module/Api/Mastodon/Directory.php diff --git a/src/Module/Api/Mastodon/Directory.php b/src/Module/Api/Mastodon/Directory.php new file mode 100644 index 0000000000..7d18637e2a --- /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 4227db2406..8387366b3d 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 ]],