]> git.mxchange.org Git - friendica.git/commitdiff
API: Added endpoints /instance and /instance/peers
authorMichael <heluecht@pirati.ca>
Tue, 10 Dec 2019 21:27:09 +0000 (21:27 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 10 Dec 2019 21:27:09 +0000 (21:27 +0000)
doc/API-Mastodon.md
src/Module/Api/Mastodon/Instance.php [new file with mode: 0644]
src/Module/Api/Mastodon/Instance/Peers.php [new file with mode: 0644]
static/routes.config.php

index d885e6ab3c7e7cd655de593e13a7e0785249e2a0..a711e367fb8db0480bed4fdeeb0a3ad78f24206f 100644 (file)
@@ -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 (file)
index 0000000..f6f2657
--- /dev/null
@@ -0,0 +1,74 @@
+<?php
+
+namespace Friendica\Module\Api\Mastodon;
+
+use Friendica\Core\Config;
+use Friendica\Core\Protocol;
+use Friendica\Core\System;
+use Friendica\Database\DBA;
+use Friendica\Model\User;
+use Friendica\Module\Base\Api;
+use Friendica\Module\Register;
+use Friendica\Network\HTTPException;
+use Friendica\Util\Network;
+
+/**
+ * @see https://docs.joinmastodon.org/api/rest/instances/
+ */
+class Instance extends Api
+{
+       public static function init(array $parameters = [])
+       {
+               parent::init($parameters);
+       }
+
+       /**
+        * @param array $parameters
+        * @throws HTTPException\InternalServerErrorException
+        */
+       public static function rawContent(array $parameters = [])
+       {
+               $app = self::getApp();
+
+                $register_policy = intval(Config::get('config', 'register_policy'));
+
+               $return = [
+                       'uri' => $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 (file)
index 0000000..4dc4f48
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+
+namespace Friendica\Module\Api\Mastodon\Instance;
+
+use Friendica\Core\Protocol;
+use Friendica\Core\System;
+use Friendica\Database\DBA;
+use Friendica\Module\Base\Api;
+use Friendica\Network\HTTPException;
+use Friendica\Util\Network;
+
+/**
+ * Undocumented API endpoint that is implemented by both Mastodon and Pleroma
+ */
+class Peers extends Api
+{
+       public static function init(array $parameters = [])
+       {
+               parent::init($parameters);
+       }
+
+       /**
+        * @param array $parameters
+        * @throws HTTPException\InternalServerErrorException
+        */
+       public static function rawContent(array $parameters = [])
+       {
+               $return = [];
+
+               // We only select for Friendica and ActivityPub servers, since it is expected to only deliver AP compatible systems here.
+               $instances = DBA::select('gserver', ['url'], ["`network` in (?, ?) AND `last_contact` >= `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);
+       }
+}
index d8113c5c53ec4d0f64af3316d365c6ea353c1b92..824354690d5972675d846f328fd67cf77f705dbb 100644 (file)
@@ -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]],
                ],
        ],