]> git.mxchange.org Git - friendica.git/commitdiff
Remove DI dependency in Object\Api\Mastodon\Instance
authorHypolite Petovan <hypolite@mrpetovan.com>
Sun, 26 Nov 2023 14:43:26 +0000 (09:43 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Sun, 26 Nov 2023 14:48:46 +0000 (09:48 -0500)
- Address part of https://github.com/friendica/friendica/issues/12011#issuecomment-1826792949

src/Module/Api/Mastodon/Instance.php
src/Object/Api/Mastodon/Instance.php

index 075d4a9ddef650c1b593ebc98cb8b2f108689322..3784030c89cfc30065e46d73ed7a94bb93df8a9b 100644 (file)
@@ -26,6 +26,8 @@ use Friendica\Core\Config\Capability\IManageConfigValues;
 use Friendica\Core\L10n;
 use Friendica\Core\System;
 use Friendica\Database\Database;
+use Friendica\Factory\Api\Mastodon\Account as AccountFactory;
+use Friendica\Model\User;
 use Friendica\Module\Api\ApiResponse;
 use Friendica\Module\BaseApi;
 use Friendica\Object\Api\Mastodon\Instance as InstanceEntity;
@@ -46,12 +48,16 @@ class Instance extends BaseApi
        /** @var IManageConfigValues */
        private $config;
 
-       public function __construct(\Friendica\Factory\Api\Mastodon\Error $errorFactory, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, Database $database, IManageConfigValues $config, array $server, array $parameters = [])
+       /** @var AccountFactory */
+       private $accountFactory;
+
+       public function __construct(AccountFactory $accountFactory, \Friendica\Factory\Api\Mastodon\Error $errorFactory, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, Database $database, IManageConfigValues $config, array $server, array $parameters = [])
        {
                parent::__construct($errorFactory, $app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
 
                $this->database = $database;
                $this->config = $config;
+               $this->accountFactory = $accountFactory;
        }
 
        /**
@@ -62,7 +68,13 @@ class Instance extends BaseApi
         */
        protected function rawContent(array $request = [])
        {
-               $this->jsonExit(new InstanceEntity($this->config, $this->baseUrl, $this->database, System::getRules(), $this->buildConfigurationInfo()));
+               $administrator = User::getFirstAdmin(['nickname']);
+               if ($administrator) {
+                       $adminContact = $this->database->selectFirst('contact', ['uri-id'], ['nick' => $administrator['nickname'], 'self' => true]);
+                       $contact_account = $this->accountFactory->createFromUriId($adminContact['uri-id']);
+               }
+
+               $this->jsonExit(new InstanceEntity($this->config, $this->baseUrl, $this->database, $this->buildConfigurationInfo(), $contact_account ?? null, System::getRules()));
        }
 
        private function buildConfigurationInfo(): InstanceV2Entity\Configuration
index 18707356ef9f0208803d552f4d9ac1304e18996a..6efb49c14a5455d4aa13c2358e88098f0ef1bfc4 100644 (file)
@@ -27,10 +27,8 @@ use Friendica\BaseDataTransferObject;
 use Friendica\Contact\Header;
 use Friendica\Core\Config\Capability\IManageConfigValues;
 use Friendica\Database\Database;
-use Friendica\DI;
 use Friendica\Model\User;
 use Friendica\Module\Register;
-use Friendica\Network\HTTPException;
 use Friendica\Object\Api\Mastodon\InstanceV2\Configuration;
 
 /**
@@ -68,23 +66,14 @@ class Instance extends BaseDataTransferObject
        protected $approval_required;
        /** @var bool */
        protected $invites_enabled;
-       /** @var Account|null */
        /** @var Configuration  */
        protected $configuration;
+       /** @var Account|null */
        protected $contact_account = null;
        /** @var array */
        protected $rules = [];
 
-       /**
-        * @param IManageConfigValues $config
-        * @param BaseURL             $baseUrl
-        * @param Database            $database
-        * @param array               $rules
-        * @throws HTTPException\InternalServerErrorException
-        * @throws HTTPException\NotFoundException
-        * @throws \ImagickException
-        */
-       public function __construct(IManageConfigValues $config, BaseURL $baseUrl, Database $database, array $rules = [], Configuration $configuration)
+       public function __construct(IManageConfigValues $config, BaseURL $baseUrl, Database $database, Configuration $configuration, ?Account $contact_account, array $rules)
        {
                $register_policy = intval($config->get('config', 'register_policy'));
 
@@ -102,13 +91,7 @@ class Instance extends BaseDataTransferObject
                $this->approval_required = ($register_policy == Register::APPROVE);
                $this->invites_enabled   = false;
                $this->configuration     = $configuration;
-               $this->contact_account   = [];
+               $this->contact_account   = $contact_account ?? [];
                $this->rules             = $rules;
-
-               $administrator = User::getFirstAdmin(['nickname']);
-               if ($administrator) {
-                       $adminContact = $database->selectFirst('contact', ['uri-id'], ['nick' => $administrator['nickname'], 'self' => true]);
-                       $this->contact_account = DI::mstdnAccount()->createFromUriId($adminContact['uri-id']);
-               }
        }
 }