]> git.mxchange.org Git - friendica.git/commitdiff
API: Central way to fetch the system rules
authorMichael <heluecht@pirati.ca>
Sat, 3 Dec 2022 20:18:19 +0000 (20:18 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 3 Dec 2022 20:18:19 +0000 (20:18 +0000)
src/Core/System.php
src/Module/Api/Mastodon/Instance.php
src/Module/Api/Mastodon/Instance/Rules.php
src/Object/Api/Mastodon/Instance.php

index e0c23e55b9e4a9c1475cceee40bfbdb0abefddcd..0c08fe16818431c50b23f5f89278694f735ea6ae 100644 (file)
@@ -21,6 +21,8 @@
 
 namespace Friendica\Core;
 
+use Friendica\Content\Text\BBCode;
+use Friendica\Content\Text\HTML;
 use Friendica\Core\Config\Capability\IManageConfigValues;
 use Friendica\DI;
 use Friendica\Module\Response;
@@ -659,4 +661,30 @@ class System
                // Reaching this point means that the operating system is configured badly.
                return "";
        }
+
+       /**
+        * Fetch the system rules
+        * @todo We should have got a better way to store and fetch the rules
+        *
+        * @return array
+        */
+       public static function getRules(): array
+       {
+               $rules = [];
+               $id    = 0;
+
+               if (DI::config()->get('system', 'tosdisplay')) {
+                       $html = BBCode::convert(DI::config()->get('system', 'tostext'), false, BBCode::EXTERNAL);
+
+                       $msg = HTML::toPlaintext($html, 0, true);
+                       foreach (explode("\n", $msg) as $line) {
+                               $line = trim($line);
+                               if ($line) {
+                                       $rules[] = ['id' => (string)++$id, 'text' => $line];
+                               }
+                       }
+               }
+
+               return $rules;
+       }
 }
index e5e0a957969990f3a5c3802c935b4011db727494..4747ecf51b1e03096e92f8ff979e12fc73e86790 100644 (file)
@@ -59,6 +59,6 @@ class Instance extends BaseApi
         */
        protected function rawContent(array $request = [])
        {
-               System::jsonExit(new InstanceEntity($this->config, $this->baseUrl, $this->database));
+               System::jsonExit(new InstanceEntity($this->config, $this->baseUrl, $this->database, System::getRules()));
        }
 }
index 4e25024f88d4d555d3adc6ca39681ac54cb1191c..2b6732f2694bf004df62092e124466b21da15dd7 100644 (file)
@@ -38,21 +38,6 @@ class Rules extends BaseApi
         */
        protected function rawContent(array $request = [])
        {
-               $rules = [];
-               $id    = 0;
-
-               if (DI::config()->get('system', 'tosdisplay')) {
-                       $html = BBCode::convert(DI::config()->get('system', 'tostext'), false, BBCode::EXTERNAL);
-
-                       $msg = HTML::toPlaintext($html, 0, true);
-                       foreach (explode("\n", $msg) as $line) {
-                               $line = trim($line);
-                               if ($line) {
-                                       $rules[] = ['id' => (string)++$id, 'text' => $line];
-                               }
-                       }
-               }
-
-               System::jsonExit($rules);
+               System::jsonExit(System::getRules());
        }
 }
index b659dc30636c5e30182a46d5bcd1c047716183d5..13b777db846d22d6a19959bfef5690840473dbb2 100644 (file)
@@ -75,11 +75,12 @@ class Instance extends BaseDataTransferObject
         * @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)
+       public function __construct(IManageConfigValues $config, BaseURL $baseUrl, Database $database, array $rules = [])
        {
                $register_policy = intval($config->get('config', 'register_policy'));
 
@@ -97,6 +98,7 @@ class Instance extends BaseDataTransferObject
                $this->approval_required = ($register_policy == Register::APPROVE);
                $this->invites_enabled   = false;
                $this->contact_account   = [];
+               $this->rules             = $rules;
 
                $administrator = User::getFirstAdmin(['nickname']);
                if ($administrator) {