From 1958bde4c0739f14c46c00195a9892329350535b Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 10 Dec 2019 21:27:09 +0000 Subject: [PATCH] API: Added endpoints /instance and /instance/peers --- doc/API-Mastodon.md | 2 + src/Module/Api/Mastodon/Instance.php | 74 ++++++++++++++++++++++ src/Module/Api/Mastodon/Instance/Peers.php | 41 ++++++++++++ static/routes.config.php | 2 + 4 files changed, 119 insertions(+) create mode 100644 src/Module/Api/Mastodon/Instance.php create mode 100644 src/Module/Api/Mastodon/Instance/Peers.php diff --git a/doc/API-Mastodon.md b/doc/API-Mastodon.md index d885e6ab3c..a711e367fb 100644 --- a/doc/API-Mastodon.md +++ b/doc/API-Mastodon.md @@ -16,6 +16,8 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/ap ## Implemented endpoints - [GET /api/v1/follow_requests](https://docs.joinmastodon.org/api/rest/follow-requests/#get-api-v1-follow-requests) +- [GET /api/v1/instance](https://docs.joinmastodon.org/api/rest/instances) +- GET /api/v1/instance/peers - undocumented, but implemented by Mastodon and Pleroma ## Non-implemented endpoints diff --git a/src/Module/Api/Mastodon/Instance.php b/src/Module/Api/Mastodon/Instance.php new file mode 100644 index 0000000000..f6f2657448 --- /dev/null +++ b/src/Module/Api/Mastodon/Instance.php @@ -0,0 +1,74 @@ + $app->getBaseURL(), + 'title' => Config::get('config', 'sitename'), + 'short_description' => '', // Not supported + 'description' => Config::get('config', 'info'), + 'email' => Config::get('config', 'admin_email'), + 'version' => FRIENDICA_VERSION, + 'urls' => [], // Not supported + 'stats' => [], + 'thumbnail' => $app->getBaseURL() . (Config::get('system', 'shortcut_icon') ?? 'images/friendica-32.png'), + 'languages' => [Config::get('system', 'language')], + 'registrations' => ($register_policy != Register::CLOSED), + 'approval_required' => ($register_policy == Register::APPROVE), + 'contact_account' => [] // Currently unsupported + ]; + + if (!empty(Config::get('system', 'nodeinfo'))) { + $count = DBA::count('gserver', ["`network` in (?, ?) AND `last_contact` >= `last_failure`", Protocol::DFRN, Protocol::ACTIVITYPUB]); + $return['stats'] = [ + 'user_count' => intval(Config::get('nodeinfo', 'total_users')), + 'status_count' => Config::get('nodeinfo', 'local_posts') + Config::get('nodeinfo', 'local_comments'), + 'domain_count' => $count + ]; + } + + /// @ToDo will be done, once that we have an API function for that + /* + if (!empty(Config::get('config', 'admin_email'))) { + $adminList = explode(',', str_replace(' ', '', Config::get('config', 'admin_email'))); + $administrator = User::getByEmail($adminList[0], ['nickname']); + if (!empty($administrator)) { + $adminContact = DBA::selectFirst('contact', [], ['nick' => $administrator['nickname'], 'self' => true]); + $return['contact_account'] = Api::getAccountArray($adminContact); + } + } + */ + + System::jsonExit($return); + } +} diff --git a/src/Module/Api/Mastodon/Instance/Peers.php b/src/Module/Api/Mastodon/Instance/Peers.php new file mode 100644 index 0000000000..4dc4f482e7 --- /dev/null +++ b/src/Module/Api/Mastodon/Instance/Peers.php @@ -0,0 +1,41 @@ += `last_failure`", Protocol::DFRN, Protocol::ACTIVITYPUB]); + while ($instance = DBA::fetch($instances)) { + $urldata = parse_url($instance['url']); + unset($urldata['scheme']); + $return[] = ltrim(Network::unparseURL($urldata), '/'); + } + DBA::close($instances); + + System::jsonExit($return); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index d8113c5c53..824354690d 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -30,6 +30,8 @@ return [ '/api' => [ '/v1' => [ '/follow_requests' => [Module\Api\Mastodon\FollowRequests::class, [R::GET ]], + '/instance' => [Module\Api\Mastodon\Instance::class, [R::GET]], + '/instance/peers' => [Module\Api\Mastodon\Instance\Peers::class, [R::GET]], ], ], -- 2.39.5