From 9296a55dfb5f075b13f1998d0ed8c782acf32b09 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 11 Dec 2019 06:51:59 +0000 Subject: [PATCH] Added entities --- src/Api/Mastodon/Instance.php | 83 ++++++++++++++++++++++++++++ src/Api/Mastodon/Stats.php | 38 +++++++++++++ src/Module/Api/Mastodon/Instance.php | 58 +------------------ 3 files changed, 123 insertions(+), 56 deletions(-) create mode 100644 src/Api/Mastodon/Instance.php create mode 100644 src/Api/Mastodon/Stats.php diff --git a/src/Api/Mastodon/Instance.php b/src/Api/Mastodon/Instance.php new file mode 100644 index 0000000000..fef7394cc6 --- /dev/null +++ b/src/Api/Mastodon/Instance.php @@ -0,0 +1,83 @@ +uri = $app->getBaseURL(); + $instance->title = Config::get('config', 'sitename'); + $instance->description = Config::get('config', 'info'); + $instance->email = Config::get('config', 'admin_email'); + $instance->version = FRIENDICA_VERSION; + $instance->urls = []; // Not supported + $instance->stats = Stats::get(); + $instance->thumbnail = $app->getBaseURL() . (Config::get('system', 'shortcut_icon') ?? 'images/friendica-32.png'); + $instance->languages = [Config::get('system', 'language')]; + $instance->max_toot_chars = (int)Config::get('config', 'api_import_size', Config::get('config', 'max_import_size')); + $instance->registrations = ($register_policy != Register::CLOSED); + $instance->approval_required = ($register_policy == Register::APPROVE); + $instance->contact_account = []; + + 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]); + $instance->contact_account = Account::createFromContact($adminContact); + } + } + + return $instance; + } +} diff --git a/src/Api/Mastodon/Stats.php b/src/Api/Mastodon/Stats.php new file mode 100644 index 0000000000..895a58e060 --- /dev/null +++ b/src/Api/Mastodon/Stats.php @@ -0,0 +1,38 @@ +user_count = intval(Config::get('nodeinfo', 'total_users')); + $stats->status_count = Config::get('nodeinfo', 'local_posts') + Config::get('nodeinfo', 'local_comments'); + $stats->domain_count = DBA::count('gserver', ["`network` in (?, ?) AND `last_contact` >= `last_failure`", Protocol::DFRN, Protocol::ACTIVITYPUB]); + } + return $stats; + } +} diff --git a/src/Module/Api/Mastodon/Instance.php b/src/Module/Api/Mastodon/Instance.php index 321dbf7caf..6a2b3abb63 100644 --- a/src/Module/Api/Mastodon/Instance.php +++ b/src/Module/Api/Mastodon/Instance.php @@ -2,75 +2,21 @@ namespace Friendica\Module\Api\Mastodon; -use Friendica\Api\Mastodon\Account; -use Friendica\Core\Config; -use Friendica\Core\Protocol; +use Friendica\Api\Mastodon\Instance as InstanceEntity; 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'), - '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')], - 'max_toot_chars' => (int)Config::get('config', 'api_import_size', Config::get('config', 'max_import_size')), - 'registrations' => ($register_policy != Register::CLOSED), - 'approval_required' => ($register_policy == Register::APPROVE), - 'contact_account' => [] - ]; - - if (!$return['registrations']) { - unset($return['approval_required']); - } - - 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 - ]; - } - - 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'] = Account::createFromContact($adminContact); - } - } - - System::jsonExit($return); + System::jsonExit(InstanceEntity::get(self::getApp())); } } -- 2.39.5