]> git.mxchange.org Git - friendica.git/commitdiff
Refactor Mastodon API InstanceV2 so objects are simple and BI in API layer
authorHank Grabowski <hankgrabowski@gmail.com>
Mon, 20 Feb 2023 18:22:29 +0000 (13:22 -0500)
committerHank Grabowski <hankgrabowski@gmail.com>
Mon, 20 Feb 2023 18:22:29 +0000 (13:22 -0500)
src/Module/Api/Mastodon/InstanceV2.php
src/Object/Api/Mastodon/InstanceV2.php
src/Object/Api/Mastodon/InstanceV2/Configuration.php
src/Object/Api/Mastodon/InstanceV2/Contact.php
src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php
src/Object/Api/Mastodon/InstanceV2/MediaAttachmentsConfig.php
src/Object/Api/Mastodon/InstanceV2/Registrations.php
src/Object/Api/Mastodon/InstanceV2/StatusesConfig.php
src/Object/Api/Mastodon/InstanceV2/Thumbnail.php
src/Object/Api/Mastodon/InstanceV2/Usage.php
src/Object/Api/Mastodon/InstanceV2/UserStats.php

index 3177570c9416dbc08666ef352419d4a329781adb..c1dd8314f0b43a3552dfae1de33149d999f3282d 100644 (file)
 
 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));
        }
 }
index 20ea45ba3c88a1041e8214a7bb4578a2d65a5559..ec32b944f0a2509f31e3881e457998064acafa25 100644 (file)
 
 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;
        }
 }
index e30561356bf6f4265fd6ba29178764684069b981..a44c2e060ff012db6fa99b64bf47c14165c9ddde 100644 (file)
@@ -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;
        }
 }
index 007c0495bc3b781b1db7727169d0761d93767eb9..f48b027cf01961332626677407c0780a6589381c 100644 (file)
@@ -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;
        }
 }
index b5ff02ea5d37ae979ff00077cdbccd0231b257c8..d3a23721051eadb0f183d7f3fa955b1efd34cef2 100644 (file)
@@ -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;
        }
 }
index 598736c330993486e911e585b95abe60a42b07c7..085bdfd0e28729505fc7aed88a6af012540f33b5 100644 (file)
@@ -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;
        }
 }
index c1e7769e83786d1e197626513bf134471a925187..b4a90545685432e406097ea8d3fbcd8659820be2 100644 (file)
@@ -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;
        }
 }
index fb74b1d5bd149b77ed31af5907e486fb85f8e0af..aa2af4f04736346aa813d659745300d79b07bafa 100644 (file)
@@ -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;
        }
 }
index 18cb1bf2a78d392dba7f03fd3f2f4dcef6ce8cac..3d4989e3678be7df0021a79d936b0c6e37e3bbcf 100644 (file)
@@ -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;
        }
 }
index cb4934833719050ed1d9cdae59ca154c21c3845a..69e26b8ab7895a1ecf909f3e317d78cd2b42f53d 100644 (file)
@@ -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;
        }
 }
index c82cf563eeebae46fb3cdf1d64256c0047775c3d..8f398e0d5a1343c54c069bf8d5dc0029b9dfe727 100644 (file)
@@ -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;
        }
 }