From e0ea51818e44c86d40a028ebef6e8d70569c3d0b Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 20 Feb 2022 16:17:03 -0500 Subject: [PATCH] Add constructors to a couple of Mastodon API value objects --- src/Module/Api/Mastodon/Instance.php | 3 +- src/Object/Api/Mastodon/Instance.php | 58 ++++++++++++++-------------- src/Object/Api/Mastodon/Stats.php | 21 ++++------ 3 files changed, 39 insertions(+), 43 deletions(-) diff --git a/src/Module/Api/Mastodon/Instance.php b/src/Module/Api/Mastodon/Instance.php index 4115e0e6b..6d3db9018 100644 --- a/src/Module/Api/Mastodon/Instance.php +++ b/src/Module/Api/Mastodon/Instance.php @@ -22,6 +22,7 @@ namespace Friendica\Module\Api\Mastodon; use Friendica\Core\System; +use Friendica\DI; use Friendica\Module\BaseApi; use Friendica\Object\Api\Mastodon\Instance as InstanceEntity; @@ -35,6 +36,6 @@ class Instance extends BaseApi */ protected function rawContent(array $request = []) { - System::jsonExit(InstanceEntity::get()); + System::jsonExit(new InstanceEntity(DI::config(), $this->baseUrl, DI::dba())); } } diff --git a/src/Object/Api/Mastodon/Instance.php b/src/Object/Api/Mastodon/Instance.php index 70184c313..4337c3d1f 100644 --- a/src/Object/Api/Mastodon/Instance.php +++ b/src/Object/Api/Mastodon/Instance.php @@ -21,11 +21,15 @@ namespace Friendica\Object\Api\Mastodon; +use Friendica\App\BaseURL; use Friendica\BaseDataTransferObject; +use Friendica\Core\Config\Capability\IManageConfigValues; +use Friendica\Database\Database; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\User; use Friendica\Module\Register; +use Friendica\Network\HTTPException; /** * Class Instance @@ -68,43 +72,39 @@ class Instance extends BaseDataTransferObject protected $rules = []; /** - * Creates an instance record - * - * @return Instance - * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @param IManageConfigValues $config + * @param BaseURL $baseUrl + * @param Database $database + * @throws HTTPException\InternalServerErrorException + * @throws HTTPException\NotFoundException * @throws \ImagickException */ - public static function get() + public function __construct(IManageConfigValues $config, BaseURL $baseUrl, Database $database) { - $register_policy = intval(DI::config()->get('config', 'register_policy')); + $register_policy = intval($config->get('config', 'register_policy')); - $baseUrl = DI::baseUrl(); + $this->uri = $baseUrl->get(); + $this->title = $config->get('config', 'sitename'); + $this->short_description = $this->description = $config->get('config', 'info'); + $this->email = $config->get('config', 'admin_email'); + $this->version = FRIENDICA_VERSION; + $this->urls = null; // Not supported + $this->stats = new Stats($config, $database); + $this->thumbnail = $baseUrl->get() . ($config->get('system', 'shortcut_icon') ?? 'images/friendica-32.png'); + $this->languages = [$config->get('system', 'language')]; + $this->max_toot_chars = (int)$config->get('config', 'api_import_size', $config->get('config', 'max_import_size')); + $this->registrations = ($register_policy != Register::CLOSED); + $this->approval_required = ($register_policy == Register::APPROVE); + $this->invites_enabled = false; + $this->contact_account = []; - $instance = new Instance(); - $instance->uri = $baseUrl->get(); - $instance->title = DI::config()->get('config', 'sitename'); - $instance->short_description = $instance->description = DI::config()->get('config', 'info'); - $instance->email = DI::config()->get('config', 'admin_email'); - $instance->version = FRIENDICA_VERSION; - $instance->urls = null; // Not supported - $instance->stats = Stats::get(); - $instance->thumbnail = $baseUrl->get() . (DI::config()->get('system', 'shortcut_icon') ?? 'images/friendica-32.png'); - $instance->languages = [DI::config()->get('system', 'language')]; - $instance->max_toot_chars = (int)DI::config()->get('config', 'api_import_size', DI::config()->get('config', 'max_import_size')); - $instance->registrations = ($register_policy != Register::CLOSED); - $instance->approval_required = ($register_policy == Register::APPROVE); - $instance->invites_enabled = false; - $instance->contact_account = []; - - if (!empty(DI::config()->get('config', 'admin_email'))) { - $adminList = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email'))); + 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', ['id'], ['nick' => $administrator['nickname'], 'self' => true]); - $instance->contact_account = DI::mstdnAccount()->createFromContactId($adminContact['id']); + $adminContact = $database->selectFirst('contact', ['id'], ['nick' => $administrator['nickname'], 'self' => true]); + $this->contact_account = DI::mstdnAccount()->createFromContactId($adminContact['id']); } } - - return $instance; } } diff --git a/src/Object/Api/Mastodon/Stats.php b/src/Object/Api/Mastodon/Stats.php index 5061c5729..40a55c6d7 100644 --- a/src/Object/Api/Mastodon/Stats.php +++ b/src/Object/Api/Mastodon/Stats.php @@ -22,7 +22,9 @@ namespace Friendica\Object\Api\Mastodon; use Friendica\BaseDataTransferObject; +use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Protocol; +use Friendica\Database\Database; use Friendica\Database\DBA; use Friendica\DI; @@ -40,19 +42,12 @@ class Stats extends BaseDataTransferObject /** @var int */ protected $domain_count = 0; - /** - * Creates a stats record - * - * @return Stats - * @throws \Friendica\Network\HTTPException\InternalServerErrorException - */ - public static function get() { - $stats = new Stats(); - if (!empty(DI::config()->get('system', 'nodeinfo'))) { - $stats->user_count = intval(DI::config()->get('nodeinfo', 'total_users')); - $stats->status_count = DI::config()->get('nodeinfo', 'local_posts') + DI::config()->get('nodeinfo', 'local_comments'); - $stats->domain_count = DBA::count('gserver', ["`network` in (?, ?) AND NOT `failed`", Protocol::DFRN, Protocol::ACTIVITYPUB]); + public function __construct(IManageConfigValues $config, Database $database) + { + if (!empty($config->get('system', 'nodeinfo'))) { + $this->user_count = intval($config->get('nodeinfo', 'total_users')); + $this->status_count = $config->get('nodeinfo', 'local_posts') + $config->get('nodeinfo', 'local_comments'); + $this->domain_count = $database->count('gserver', ["`network` in (?, ?) AND NOT `failed`", Protocol::DFRN, Protocol::ACTIVITYPUB]); } - return $stats; } }