API: Central way to fetch the system rules

This commit is contained in:
Michael 2022-12-03 20:18:19 +00:00
parent 8cb7d4a9bd
commit b6f7d31036
4 changed files with 33 additions and 18 deletions

View File

@ -21,6 +21,8 @@
namespace Friendica\Core; namespace Friendica\Core;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\HTML;
use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\DI; use Friendica\DI;
use Friendica\Module\Response; use Friendica\Module\Response;
@ -659,4 +661,30 @@ class System
// Reaching this point means that the operating system is configured badly. // Reaching this point means that the operating system is configured badly.
return ""; 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;
}
} }

View File

@ -59,6 +59,6 @@ class Instance extends BaseApi
*/ */
protected function rawContent(array $request = []) 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()));
} }
} }

View File

@ -38,21 +38,6 @@ class Rules extends BaseApi
*/ */
protected function rawContent(array $request = []) protected function rawContent(array $request = [])
{ {
$rules = []; System::jsonExit(System::getRules());
$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);
} }
} }

View File

@ -75,11 +75,12 @@ class Instance extends BaseDataTransferObject
* @param IManageConfigValues $config * @param IManageConfigValues $config
* @param BaseURL $baseUrl * @param BaseURL $baseUrl
* @param Database $database * @param Database $database
* @param array $rules
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws HTTPException\NotFoundException * @throws HTTPException\NotFoundException
* @throws \ImagickException * @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')); $register_policy = intval($config->get('config', 'register_policy'));
@ -97,6 +98,7 @@ class Instance extends BaseDataTransferObject
$this->approval_required = ($register_policy == Register::APPROVE); $this->approval_required = ($register_policy == Register::APPROVE);
$this->invites_enabled = false; $this->invites_enabled = false;
$this->contact_account = []; $this->contact_account = [];
$this->rules = $rules;
$administrator = User::getFirstAdmin(['nickname']); $administrator = User::getFirstAdmin(['nickname']);
if ($administrator) { if ($administrator) {