]> git.mxchange.org Git - friendica.git/commitdiff
Add constructors to a couple of Mastodon API value objects
authorHypolite Petovan <hypolite@mrpetovan.com>
Sun, 20 Feb 2022 21:17:03 +0000 (16:17 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Sun, 20 Feb 2022 21:17:03 +0000 (16:17 -0500)
src/Module/Api/Mastodon/Instance.php
src/Object/Api/Mastodon/Instance.php
src/Object/Api/Mastodon/Stats.php

index 4115e0e6b630924dd486b6ffb4046f23f8abeb74..6d3db9018472ff927bd3d18e1169adcfdaf96e53 100644 (file)
@@ -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()));
        }
 }
index 70184c3131fe3c9dad3df912792787bd05fde649..4337c3d1f35702d3b0aaa573a45953481277ff08 100644 (file)
 
 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;
        }
 }
index 5061c5729ab993fdfb9f9180f53d5a60f026e054..40a55c6d7d380291c3f05f27e3c37f59fa5172b9 100644 (file)
@@ -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;
        }
 }