From e7cd6d0e60b626548f2db27c173dab5a4d9a97d4 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Fri, 26 May 2023 11:05:44 -0700 Subject: [PATCH] hooks: clear all cache on logout (close #665) Signed-off-by: Varun Patil --- lib/AppInfo/Application.php | 8 +++++ lib/Listeners/BeforeTemplateListener.php | 45 ++++++++++++++++++++++++ lib/Listeners/PostLogoutListener.php | 43 ++++++++++++++++++++++ src/hooks/clear-cache.ts | 11 ++++++ webpack.js | 1 + 5 files changed, 108 insertions(+) create mode 100644 lib/Listeners/BeforeTemplateListener.php create mode 100644 lib/Listeners/PostLogoutListener.php create mode 100644 src/hooks/clear-cache.ts diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 91a46cbb..236eb071 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -24,16 +24,20 @@ declare(strict_types=1); namespace OCA\Memories\AppInfo; use OCA\Memories\ClustersBackend; +use OCA\Memories\Listeners\BeforeTemplateListener; use OCA\Memories\Listeners\PostDeleteListener; +use OCA\Memories\Listeners\PostLogoutListener; use OCA\Memories\Listeners\PostWriteListener; use OCA\Memories\Util; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; use OCP\Files\Events\Node\NodeDeletedEvent; use OCP\Files\Events\Node\NodeTouchedEvent; use OCP\Files\Events\Node\NodeWrittenEvent; +use OCP\User\Events\UserLoggedOutEvent; const AUTH_HEADER = 'HTTP_AUTHORIZATION'; @@ -77,6 +81,10 @@ class Application extends App implements IBootstrap $context->registerEventListener(NodeTouchedEvent::class, PostWriteListener::class); $context->registerEventListener(NodeDeletedEvent::class, PostDeleteListener::class); + // Register other global hooks + $context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateListener::class); + $context->registerEventListener(UserLoggedOutEvent::class, PostLogoutListener::class); + // Register clusters backends ClustersBackend\AlbumsBackend::register(); ClustersBackend\TagsBackend::register(); diff --git a/lib/Listeners/BeforeTemplateListener.php b/lib/Listeners/BeforeTemplateListener.php new file mode 100644 index 00000000..fd8f20b7 --- /dev/null +++ b/lib/Listeners/BeforeTemplateListener.php @@ -0,0 +1,45 @@ +. + */ + +namespace OCA\Memories\Listeners; + +use OCA\Memories\AppInfo\Application; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\ISession; + +class BeforeTemplateListener implements IEventListener +{ + private ISession $session; + + public function __construct(ISession $session) + { + $this->session = $session; + } + + public function handle(Event $event): void + { + if (null !== $this->session->get(PostLogoutListener::CLEAR_CACHE_KEY)) { + $this->session->remove(PostLogoutListener::CLEAR_CACHE_KEY); + \OCP\Util::addScript(Application::APPNAME, 'memories-hooks-clear-cache'); + } + } +} diff --git a/lib/Listeners/PostLogoutListener.php b/lib/Listeners/PostLogoutListener.php new file mode 100644 index 00000000..a4d1d131 --- /dev/null +++ b/lib/Listeners/PostLogoutListener.php @@ -0,0 +1,43 @@ +. + */ + +namespace OCA\Memories\Listeners; + +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\ISession; + +class PostLogoutListener implements IEventListener +{ + public const CLEAR_CACHE_KEY = 'memories_clear_cache'; + + private ISession $session; + + public function __construct(ISession $session) + { + $this->session = $session; + } + + public function handle(Event $event): void + { + $this->session->set(self::CLEAR_CACHE_KEY, '1'); + } +} diff --git a/src/hooks/clear-cache.ts b/src/hooks/clear-cache.ts new file mode 100644 index 00000000..9b5dd66f --- /dev/null +++ b/src/hooks/clear-cache.ts @@ -0,0 +1,11 @@ +// DO NOT import this file anywhere, it clears all caches on load. +// Imported as a hook from BeforeTemplateListener.php + +(async function () { + const keys = (await window.caches?.keys()) ?? []; + for (const key of keys) { + if (key.match(/^memories-/)) { + window.caches.delete(key); + } + } +})(); diff --git a/webpack.js b/webpack.js index 81b320b7..72e4e039 100644 --- a/webpack.js +++ b/webpack.js @@ -11,6 +11,7 @@ const isDev = buildMode === 'development'; webpackConfig.entry = { main: path.resolve(path.join('src', 'main')), admin: path.resolve(path.join('src', 'admin')), + 'hooks-clear-cache': path.resolve(path.join('src', 'hooks', 'clear-cache')), }; // Enable TypeScript