From a670d478f83772394520991fa851394d592bbfc3 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 16 Jul 2023 17:26:31 -0400 Subject: [PATCH] Add user settings page to manage remote server settings --- src/Module/BaseSettings.php | 7 ++ src/Module/Settings/Server/Index.php | 126 +++++++++++++++++++++++ static/routes.config.php | 1 + view/templates/settings/server/index.tpl | 39 +++++++ 4 files changed, 173 insertions(+) create mode 100644 src/Module/Settings/Server/Index.php create mode 100644 view/templates/settings/server/index.tpl diff --git a/src/Module/BaseSettings.php b/src/Module/BaseSettings.php index a1e88e9a1..4b26a0f26 100644 --- a/src/Module/BaseSettings.php +++ b/src/Module/BaseSettings.php @@ -151,6 +151,13 @@ class BaseSettings extends BaseModule 'accesskey' => 'b', ]; + $tabs[] = [ + 'label' => $this->t('Remote servers'), + 'url' => 'settings/server', + 'selected' => static::class == Settings\Server\Index::class ? 'active' : '', + 'accesskey' => 's', + ]; + $tabs[] = [ 'label' => $this->t('Export personal data'), 'url' => 'settings/userexport', diff --git a/src/Module/Settings/Server/Index.php b/src/Module/Settings/Server/Index.php new file mode 100644 index 000000000..f59e23a87 --- /dev/null +++ b/src/Module/Settings/Server/Index.php @@ -0,0 +1,126 @@ +. + * + */ + +namespace Friendica\Module\Settings\Server; + +use Friendica\App; +use Friendica\Content\Pager; +use Friendica\Core\L10n; +use Friendica\Core\Renderer; +use Friendica\Core\Session\Capability\IHandleUserSessions; +use Friendica\Module\BaseSettings; +use Friendica\Module\Response; +use Friendica\Navigation\SystemMessages; +use Friendica\Network\HTTPException\NotFoundException; +use Friendica\User\Settings\Entity\UserGServer; +use Friendica\User\Settings\Repository; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; + +class Index extends BaseSettings +{ + /** @var Repository\UserGServer */ + private $repository; + /** @var SystemMessages */ + private $systemMessages; + + public function __construct(SystemMessages $systemMessages, Repository\UserGServer $repository, IHandleUserSessions $session, App\Page $page, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) + { + parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->repository = $repository; + $this->systemMessages = $systemMessages; + } + + protected function post(array $request = []) + { + self::checkFormSecurityTokenRedirectOnError($this->args->getQueryString(), 'settings-server'); + + foreach ($request['delete'] ?? [] as $gsid => $delete) { + if ($delete) { + unset($request['ignored'][$gsid]); + + try { + $userGServer = $this->repository->selectOneByUserAndServer($this->session->getLocalUserId(), $gsid, false); + $this->repository->delete($userGServer); + } catch (NotFoundException $e) { + // Nothing to delete + } + } + } + + foreach ($request['ignored'] ?? [] as $gsid => $ignored) { + $userGServer = $this->repository->getOneByUserAndServer($this->session->getLocalUserId(), $gsid, false); + if ($userGServer->ignored != $ignored) { + $userGServer->toggleIgnored(); + $this->repository->save($userGServer); + } + } + + $this->systemMessages->addInfo($this->t('Settings saved')); + + $this->baseUrl->redirect($this->args->getQueryString()); + } + + protected function content(array $request = []): string + { + parent::content(); + + $pager = new Pager($this->l10n, $this->args->getQueryString(), 30); + + $total = $this->repository->countByUser($this->session->getLocalUserId()); + + $servers = $this->repository->selectByUserWithPagination($this->session->getLocalUserId(), $pager); + + $ignoredCheckboxes = array_map(function (UserGServer $server) { + return ['ignored[' . $server->gsid . ']', '', $server->ignored]; + }, $servers->getArrayCopy()); + + $deleteCheckboxes = array_map(function (UserGServer $server) { + return ['delete[' . $server->gsid . ']']; + }, $servers->getArrayCopy()); + + $tpl = Renderer::getMarkupTemplate('settings/server/index.tpl'); + return Renderer::replaceMacros($tpl, [ + '$l10n' => [ + 'title' => $this->t('Remote server settings'), + 'desc' => $this->t('Here you can find all the remote servers you have taken individual moderation actions against. For a list of servers your node has blocked, please check out the Information page.'), + 'siteName' => $this->t('Server Name'), + 'ignored' => $this->t('Ignored'), + 'ignored_title' => $this->t("You won't see any content from this server including reshares in your Network page, the community pages and individual conversations."), + 'delete' => $this->t('Delete'), + 'delete_title' => $this->t('Delete all your settings for the remote server'), + 'submit' => $this->t('Save changes'), + ], + + '$count' => $total, + + '$servers' => $servers, + + '$form_security_token' => self::getFormSecurityToken('settings-server'), + + '$ignoredCheckboxes' => $ignoredCheckboxes, + '$deleteCheckboxes' => $deleteCheckboxes, + + '$paginate' => $pager->renderFull($total), + ]); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index edfd69ea2..f16a19362 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -640,6 +640,7 @@ return [ '/settings' => [ '/server' => [ + '[/]' => [Module\Settings\Server\Index::class, [R::GET, R::POST]], '/{gsid:\d+}/{action}' => [Module\Settings\Server\Action::class, [ R::POST]], ], '[/]' => [Module\Settings\Account::class, [R::GET, R::POST]], diff --git a/view/templates/settings/server/index.tpl b/view/templates/settings/server/index.tpl new file mode 100644 index 000000000..112af6d07 --- /dev/null +++ b/view/templates/settings/server/index.tpl @@ -0,0 +1,39 @@ +
+

{{$l10n.title}} ({{$count}})

+ +

{{$l10n.desc nofilter}}

+ + {{$paginate nofilter}} + +
+ + +

+ + + + + + + + +{{foreach $servers as $index => $server}} + + + + + +{{/foreach}} + +
{{$l10n.siteName}}{{$l10n.ignored}} {{$l10n.delete}}
+ {{($server->gserver->siteName) ? $server->gserver->siteName : $server->gserver->url}} + + {{include file="field_checkbox.tpl" field=$ignoredCheckboxes[$index]}} + + {{include file="field_checkbox.tpl" field=$deleteCheckboxes[$index]}} +
+

+
+ + {{$paginate nofilter}} +