[PLUGINS][Oomox] getEntity added.

[CORE][Cache] exists added.
This commit is contained in:
Eliseu Amaro 2021-11-26 17:11:28 +00:00 committed by Diogo Peralta Cordeiro
parent 7b9d388a44
commit 7145dba8af
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
3 changed files with 26 additions and 6 deletions

View File

@ -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]);

View File

@ -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;

View File

@ -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