From: Hank Grabowski Date: Mon, 20 Feb 2023 18:22:29 +0000 (-0500) Subject: Refactor Mastodon API InstanceV2 so objects are simple and BI in API layer X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=71a7c0a4de5800c33fc92f03c0a388c5a8fd3e75;p=friendica.git Refactor Mastodon API InstanceV2 so objects are simple and BI in API layer --- diff --git a/src/Module/Api/Mastodon/InstanceV2.php b/src/Module/Api/Mastodon/InstanceV2.php index 3177570c94..c1dd8314f0 100644 --- a/src/Module/Api/Mastodon/InstanceV2.php +++ b/src/Module/Api/Mastodon/InstanceV2.php @@ -21,14 +21,19 @@ namespace Friendica\Module\Api\Mastodon; +use Exception; use Friendica\App; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\L10n; use Friendica\Core\System; use Friendica\Database\Database; +use Friendica\DI; +use Friendica\Model\User; use Friendica\Module\Api\ApiResponse; use Friendica\Module\BaseApi; +use Friendica\Module\Register; use Friendica\Object\Api\Mastodon\InstanceV2 as InstanceEntity; +use Friendica\Util\Images; use Friendica\Util\Profiler; use Psr\Log\LoggerInterface; @@ -67,9 +72,97 @@ class InstanceV2 extends BaseApi * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\NotFoundException * @throws \ImagickException + * @throws Exception */ protected function rawContent(array $request = []) { - System::jsonExit(new InstanceEntity($this->config, $this->baseUrl, $this->database, System::getRules())); + $domain = $this->baseUrl->getHostname(); + $title = $this->config->get('config', 'sitename'); + $version = '2.8.0 (compatible; Friendica ' . App::VERSION . ')'; + $description = $this->config->get('config', 'info'); + $usage = $this->buildUsageInfo(); + $thumbnail = new InstanceEntity\Thumbnail($this->baseUrl->get() . 'images/friendica-banner.jpg'); + $languages = [$this->config->get('system', 'language')]; + $configuration = $this->buildConfigurationInfo(); + $registration = $this->buildRegistrationsInfo(); + $contact = $this->buildContactInfo(); + $friendica_extensions = $this->buildFriendicaExtensionInfo(); + $rules = System::getRules(); + System::jsonExit(new InstanceEntity( + $domain, + $title, + $version, + $description, + $usage, + $thumbnail, + $languages, + $configuration, + $registration, + $contact, + $friendica_extensions, + $rules + )); + } + + private function buildConfigurationInfo():InstanceEntity\Configuration + { + $statuses_config = new InstanceEntity\StatusesConfig((int)$this->config->get( + 'config', + 'api_import_size', + $this->config->get('config', 'max_import_size') + )); + + return new InstanceEntity\Configuration( + $statuses_config, + new InstanceEntity\MediaAttachmentsConfig(Images::supportedTypes()), + $this->config->get('system', 'maximagesize') + ); + } + + private function buildContactInfo():InstanceEntity\Contact + { + $email = implode(',', User::getAdminEmailList()); + $administrator = User::getFirstAdmin(); + $account = null; + + if ($administrator) { + $adminContact = $this->database->selectFirst( + 'contact', + ['uri-id'], + ['nick' => $administrator['nickname'], 'self' => true] + ); + $account = DI::mstdnAccount()->createFromUriId($adminContact['uri-id']); + } + + return new InstanceEntity\Contact($email, $account); + } + + private function buildFriendicaExtensionInfo():InstanceEntity\FriendicaExtensions + { + return new InstanceEntity\FriendicaExtensions( + App::VERSION, + App::CODENAME, + $this->config->get('system', 'build') + ); + } + + private function buildRegistrationsInfo():InstanceEntity\Registrations + { + $register_policy = intval($this->config->get('config', 'register_policy')); + $enabled = ($register_policy != Register::CLOSED); + $approval_required = ($register_policy == Register::APPROVE); + + return new InstanceEntity\Registrations($enabled, $approval_required); + } + + private function buildUsageInfo():InstanceEntity\Usage + { + if (!empty($this->config->get('system', 'nodeinfo'))) { + $active_monthly = intval(DI::keyValue()->get('nodeinfo_active_users_monthly')); + } else { + $active_monthly = 0; + } + + return new InstanceEntity\Usage(new InstanceEntity\UserStats($active_monthly)); } } diff --git a/src/Object/Api/Mastodon/InstanceV2.php b/src/Object/Api/Mastodon/InstanceV2.php index 20ea45ba3c..ec32b944f0 100644 --- a/src/Object/Api/Mastodon/InstanceV2.php +++ b/src/Object/Api/Mastodon/InstanceV2.php @@ -21,12 +21,7 @@ namespace Friendica\Object\Api\Mastodon; -use Friendica\App; -use Friendica\App\BaseURL; use Friendica\BaseDataTransferObject; -use Friendica\Core\Config\Capability\IManageConfigValues; -use Friendica\Database\Database; -use Friendica\Network\HTTPException; use Friendica\Object\Api\Mastodon\InstanceV2\Configuration; use Friendica\Object\Api\Mastodon\InstanceV2\Contact; use Friendica\Object\Api\Mastodon\InstanceV2\FriendicaExtensions; @@ -69,28 +64,45 @@ class InstanceV2 extends BaseDataTransferObject protected $friendica; /** - * @param IManageConfigValues $config - * @param BaseURL $baseUrl - * @param Database $database - * @param array $rules - * @throws HTTPException\InternalServerErrorException - * @throws HTTPException\NotFoundException - * @throws \ImagickException + * @param string $domain + * @param string $title + * @param $version + * @param string $description + * @param Usage $usage + * @param Thumbnail $thumbnail + * @param array $languages + * @param Configuration $configuration + * @param Registrations $registrations + * @param Contact $contact + * @param FriendicaExtensions $friendica_extensions + * @param array $rules */ - public function __construct(IManageConfigValues $config, BaseURL $baseUrl, Database $database, array $rules = []) - { - $this->domain = $baseUrl->getHostname(); - $this->title = $config->get('config', 'sitename'); - $this->version = '2.8.0 (compatible; Friendica ' . App::VERSION . ')'; - $this->source_url = null; //not supported yet - $this->description = $config->get('config', 'info'); - $this->usage = new Usage($config); - $this->thumbnail = new Thumbnail($baseUrl); - $this->languages = [$config->get('system', 'language')]; - $this->configuration = new Configuration(); - $this->registrations = new Registrations(); - $this->contact = new Contact($database); + public function __construct( + string $domain, + string $title, + string $version, + string $description, + Usage $usage, + Thumbnail $thumbnail, + array $languages, + Configuration $configuration, + Registrations $registrations, + Contact $contact, + FriendicaExtensions $friendica_extensions, + array $rules + ) { + $this->domain = $domain; + $this->title = $title; + $this->version = $version; + $this->source_url = null; //not supported yet + $this->description = $description; + $this->usage = $usage; + $this->thumbnail = $thumbnail; + $this->languages = $languages; + $this->configuration = $configuration; + $this->registrations = $registrations; + $this->contact = $contact; $this->rules = $rules; - $this->friendica = new FriendicaExtensions(); + $this->friendica = $friendica_extensions; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/Configuration.php b/src/Object/Api/Mastodon/InstanceV2/Configuration.php index e30561356b..a44c2e060f 100644 --- a/src/Object/Api/Mastodon/InstanceV2/Configuration.php +++ b/src/Object/Api/Mastodon/InstanceV2/Configuration.php @@ -22,7 +22,6 @@ namespace Friendica\Object\Api\Mastodon\InstanceV2; use Friendica\BaseDataTransferObject; -use Friendica\DI; /** * Class Configuration @@ -33,15 +32,23 @@ class Configuration extends BaseDataTransferObject { /** @var StatusesConfig */ protected $statuses; - /** @var MediaAttachmentsConfig */ + /** @var MediaAttachmentsConfig */ protected $media_attachments; /** @var int */ protected $image_size_limit; - public function __construct() - { - $this->statuses = new StatusesConfig(); - $this->media_attachments = new MediaAttachmentsConfig(); - $this->image_size_limit = DI::config()->get('system', 'maximagesize'); + /** + * @param StatusesConfig $statuses + * @param MediaAttachmentsConfig $media_attachments + * @param int $image_size_limit + */ + public function __construct( + StatusesConfig $statuses, + MediaAttachmentsConfig $media_attachments, + int $image_size_limit + ) { + $this->statuses = $statuses; + $this->media_attachments = $media_attachments; + $this->image_size_limit = $image_size_limit; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/Contact.php b/src/Object/Api/Mastodon/InstanceV2/Contact.php index 007c0495bc..f48b027cf0 100644 --- a/src/Object/Api/Mastodon/InstanceV2/Contact.php +++ b/src/Object/Api/Mastodon/InstanceV2/Contact.php @@ -22,9 +22,6 @@ namespace Friendica\Object\Api\Mastodon\InstanceV2; use Friendica\BaseDataTransferObject; -use Friendica\Database\Database; -use Friendica\DI; -use Friendica\Model\User; use Friendica\Object\Api\Mastodon\Account; /** @@ -36,21 +33,17 @@ class Contact extends BaseDataTransferObject { /** @var string */ protected $email; - /** @var Account */ + /** @var Account|null */ protected $account = null; - public function __construct(Database $database) + /** + * @param string $email + * @param Account $account + */ + public function __construct(string $email, Account $account) { - $this->email = implode(',', User::getAdminEmailList()); - $administrator = User::getFirstAdmin(); - if ($administrator) { - $adminContact = $database->selectFirst( - 'contact', - ['uri-id'], - ['nick' => $administrator['nickname'], 'self' => true] - ); - $this->account = DI::mstdnAccount()->createFromUriId($adminContact['uri-id']); - } + $this->email = $email; + $this->account = $account; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php b/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php index b5ff02ea5d..d3a2372105 100644 --- a/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php +++ b/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php @@ -21,9 +21,7 @@ namespace Friendica\Object\Api\Mastodon\InstanceV2; -use Friendica\App; use Friendica\BaseDataTransferObject; -use Friendica\DI; /** * Class FriendicaExtensions @@ -41,10 +39,15 @@ class FriendicaExtensions extends BaseDataTransferObject /** @var int */ protected $db_version; - public function __construct() + /** + * @param string $version + * @param string $codename + * @param int $db_version + */ + public function __construct(string $version, string $codename, int $db_version) { - $this->version = App::VERSION; - $this->codename = App::CODENAME; - $this->db_version = DI::config()->get('system', 'build'); + $this->version = $version; + $this->codename = $codename; + $this->db_version = $db_version; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/MediaAttachmentsConfig.php b/src/Object/Api/Mastodon/InstanceV2/MediaAttachmentsConfig.php index 598736c330..085bdfd0e2 100644 --- a/src/Object/Api/Mastodon/InstanceV2/MediaAttachmentsConfig.php +++ b/src/Object/Api/Mastodon/InstanceV2/MediaAttachmentsConfig.php @@ -22,7 +22,6 @@ namespace Friendica\Object\Api\Mastodon\InstanceV2; use Friendica\BaseDataTransferObject; -use Friendica\Util\Images; /** * Class MediaAttachmentsConfig @@ -33,8 +32,12 @@ class MediaAttachmentsConfig extends BaseDataTransferObject { /** @var string[] */ protected $supported_mime_types; - public function __construct() + + /** + * @param array $supported_mime_types + */ + public function __construct(array $supported_mime_types) { - $this->supported_mime_types = Images::supportedTypes(); + $this->supported_mime_types = $supported_mime_types; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/Registrations.php b/src/Object/Api/Mastodon/InstanceV2/Registrations.php index c1e7769e83..b4a9054568 100644 --- a/src/Object/Api/Mastodon/InstanceV2/Registrations.php +++ b/src/Object/Api/Mastodon/InstanceV2/Registrations.php @@ -22,8 +22,6 @@ namespace Friendica\Object\Api\Mastodon\InstanceV2; use Friendica\BaseDataTransferObject; -use Friendica\DI; -use Friendica\Module\Register; /** * Class Registrations @@ -37,11 +35,13 @@ class Registrations extends BaseDataTransferObject /** @var bool */ protected $approval_required; - public function __construct() + /** + * @param bool $enabled + * @param bool $approval_required + */ + public function __construct(bool $enabled, bool $approval_required) { - $config = DI::config(); - $register_policy = intval($config->get('config', 'register_policy')); - $this->enabled = ($register_policy != Register::CLOSED); - $this->approval_required = ($register_policy == Register::APPROVE); + $this->enabled = $enabled; + $this->approval_required = $approval_required; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/StatusesConfig.php b/src/Object/Api/Mastodon/InstanceV2/StatusesConfig.php index fb74b1d5bd..aa2af4f047 100644 --- a/src/Object/Api/Mastodon/InstanceV2/StatusesConfig.php +++ b/src/Object/Api/Mastodon/InstanceV2/StatusesConfig.php @@ -22,7 +22,6 @@ namespace Friendica\Object\Api\Mastodon\InstanceV2; use Friendica\BaseDataTransferObject; -use Friendica\DI; /** * Class StatusConfig @@ -31,16 +30,14 @@ use Friendica\DI; */ class StatusesConfig extends BaseDataTransferObject { - /** @var int */ + /** @var int */ protected $max_characters = 0; - public function __construct() + /** + * @param int $max_characters + */ + public function __construct(int $max_characters) { - $config = DI::config(); - $this->max_characters = (int)$config->get( - 'config', - 'api_import_size', - $config->get('config', 'max_import_size') - ); + $this->max_characters = $max_characters; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/Thumbnail.php b/src/Object/Api/Mastodon/InstanceV2/Thumbnail.php index 18cb1bf2a7..3d4989e367 100644 --- a/src/Object/Api/Mastodon/InstanceV2/Thumbnail.php +++ b/src/Object/Api/Mastodon/InstanceV2/Thumbnail.php @@ -21,7 +21,6 @@ namespace Friendica\Object\Api\Mastodon\InstanceV2; -use Friendica\App\BaseURL; use Friendica\BaseDataTransferObject; /** @@ -34,8 +33,11 @@ class Thumbnail extends BaseDataTransferObject /** @var string (URL) */ protected $url; - public function __construct(BaseURL $baseUrl) + /** + * @param string $url + */ + public function __construct(string $url) { - $this->url = $baseUrl->get() . 'images/friendica-banner.jpg'; + $this->url = $url; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/Usage.php b/src/Object/Api/Mastodon/InstanceV2/Usage.php index cb49348337..69e26b8ab7 100644 --- a/src/Object/Api/Mastodon/InstanceV2/Usage.php +++ b/src/Object/Api/Mastodon/InstanceV2/Usage.php @@ -33,8 +33,11 @@ class Usage extends BaseDataTransferObject /** @var UserStats */ protected $users; - public function __construct() + /** + * @param UserStats $users + */ + public function __construct(UserStats $users) { - $this->users = new UserStats(); + $this->users = $users; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/UserStats.php b/src/Object/Api/Mastodon/InstanceV2/UserStats.php index c82cf563ee..8f398e0d5a 100644 --- a/src/Object/Api/Mastodon/InstanceV2/UserStats.php +++ b/src/Object/Api/Mastodon/InstanceV2/UserStats.php @@ -22,7 +22,6 @@ namespace Friendica\Object\Api\Mastodon\InstanceV2; use Friendica\BaseDataTransferObject; -use Friendica\DI; /** * Class UserStats @@ -34,11 +33,11 @@ class UserStats extends BaseDataTransferObject /** @var int */ protected $active_monthly = 0; - public function __construct() + /** + * @param $active_monthly + */ + public function __construct($active_monthly) { - $config = DI::config(); - if (!empty($config->get('system', 'nodeinfo'))) { - $this->active_monthly = intval(DI::keyValue()->get('nodeinfo_active_users_monthly')); - } + $this->active_monthly = $active_monthly; } }