From b6f7d310367e0332417aa5342bcc1e07f4e1f3e2 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 3 Dec 2022 20:18:19 +0000 Subject: [PATCH] API: Central way to fetch the system rules --- src/Core/System.php | 28 ++++++++++++++++++++++ src/Module/Api/Mastodon/Instance.php | 2 +- src/Module/Api/Mastodon/Instance/Rules.php | 17 +------------ src/Object/Api/Mastodon/Instance.php | 4 +++- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/Core/System.php b/src/Core/System.php index e0c23e55b9..0c08fe1681 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -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; + } } diff --git a/src/Module/Api/Mastodon/Instance.php b/src/Module/Api/Mastodon/Instance.php index e5e0a95796..4747ecf51b 100644 --- a/src/Module/Api/Mastodon/Instance.php +++ b/src/Module/Api/Mastodon/Instance.php @@ -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())); } } diff --git a/src/Module/Api/Mastodon/Instance/Rules.php b/src/Module/Api/Mastodon/Instance/Rules.php index 4e25024f88..2b6732f269 100644 --- a/src/Module/Api/Mastodon/Instance/Rules.php +++ b/src/Module/Api/Mastodon/Instance/Rules.php @@ -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()); } } diff --git a/src/Object/Api/Mastodon/Instance.php b/src/Object/Api/Mastodon/Instance.php index b659dc3063..13b777db84 100644 --- a/src/Object/Api/Mastodon/Instance.php +++ b/src/Object/Api/Mastodon/Instance.php @@ -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) {