From 7145dba8af3e40f8406e140f3e820915245e2874 Mon Sep 17 00:00:00 2001 From: Eliseu Amaro Date: Fri, 26 Nov 2021 17:11:28 +0000 Subject: [PATCH] [PLUGINS][Oomox] getEntity added. [CORE][Cache] exists added. --- plugins/Oomox/Controller/Oomox.php | 13 ++++++++----- plugins/Oomox/Oomox.php | 14 +++++++++++++- src/Core/Cache.php | 5 +++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/plugins/Oomox/Controller/Oomox.php b/plugins/Oomox/Controller/Oomox.php index c3f6479005..42c1093fe8 100644 --- a/plugins/Oomox/Controller/Oomox.php +++ b/plugins/Oomox/Controller/Oomox.php @@ -140,14 +140,17 @@ class Oomox return ['_template' => 'oomox/oomoxSettings.html.twig', 'oomox' => $form->createView()]; } + /** + * @throws ClientException + * @throws \App\Util\Exception\NoLoggedInUser + * @throws ServerException + */ public function oomoxCSS() { $user = Common::ensureLoggedIn(); - $actor_id = $user->getId(); - try { - $oomox_table = Cache::get("oomox-css-{$actor_id}", fn() => DB::findOneBy('oomox', ['actor_id' => $actor_id])); - } catch (NotFoundException $e) { - throw new ClientException(_m('No custom colours defined.'),404, $e); + $oomox_table = \Plugin\Oomox\Oomox::getEntity($user); + if (is_null($oomox_table)) { + throw new ClientException(_m('No custom colors defined', 404)); } $content = Formatting::twigRenderFile('/oomox/root_override.css.twig', ['oomox' => $oomox_table]); diff --git a/plugins/Oomox/Oomox.php b/plugins/Oomox/Oomox.php index 393a017cf4..1cf361bdaf 100644 --- a/plugins/Oomox/Oomox.php +++ b/plugins/Oomox/Oomox.php @@ -31,12 +31,14 @@ use App\Core\Router\Router; use App\Entity\Actor; use App\Entity\LocalUser; use App\Util\Common; +use App\Util\Exception\ClientException; use App\Util\Exception\NotFoundException; use App\Util\Exception\RedirectException; use App\Util\Exception\ServerException; use App\Util\Formatting; use Plugin\Oomox\Controller as C; use Symfony\Component\HttpFoundation\Request; +use function App\Core\I18n\_m; /** * Profile Color plugin main class @@ -80,10 +82,20 @@ class Oomox extends Plugin public static function cacheKey(LocalUser $user) :string { return "oomox-css-{$user->getId()}"; } + + public static function getEntity(LocalUser $user): ?Entity\Oomox + { + try { + return Cache::get(self::cacheKey($user), fn() => DB::findOneBy('oomox', ['actor_id' => $user->getId()])); + } catch (NotFoundException $e) { + return null; + } + } + public function onEndShowStyles(array &$styles, string $route) { $user = Common::user(); - if (!is_null($user) && !is_null(Cache::get(self::cacheKey($user), fn() => null))) { + if (!is_null($user) && !is_null(Cache::get(self::cacheKey($user), fn () => self::getEntity($user)))) { $styles[] = Router::url('oomox_css'); } return Event::next; diff --git a/src/Core/Cache.php b/src/Core/Cache.php index cf94d1de8a..2f16e3a38d 100644 --- a/src/Core/Cache.php +++ b/src/Core/Cache.php @@ -151,6 +151,11 @@ abstract class Cache return self::$pools[$pool]->delete($key); } + public static function exists(string $key, string $pool = 'default'): bool + { + return self::$pools[$pool]->hasItem($key); + } + /** * Retrieve a list from the cache, with a different implementation * for redis and others, trimming to $max_count if given