hooks: clear all cache on logout (close #665)

Signed-off-by: Varun Patil <radialapps@gmail.com>
pull/672/head
Varun Patil 2023-05-26 11:05:44 -07:00
parent 1a6f17fb54
commit e7cd6d0e60
5 changed files with 108 additions and 0 deletions

View File

@ -24,16 +24,20 @@ declare(strict_types=1);
namespace OCA\Memories\AppInfo; namespace OCA\Memories\AppInfo;
use OCA\Memories\ClustersBackend; use OCA\Memories\ClustersBackend;
use OCA\Memories\Listeners\BeforeTemplateListener;
use OCA\Memories\Listeners\PostDeleteListener; use OCA\Memories\Listeners\PostDeleteListener;
use OCA\Memories\Listeners\PostLogoutListener;
use OCA\Memories\Listeners\PostWriteListener; use OCA\Memories\Listeners\PostWriteListener;
use OCA\Memories\Util; use OCA\Memories\Util;
use OCP\AppFramework\App; use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\Files\Events\Node\NodeDeletedEvent; use OCP\Files\Events\Node\NodeDeletedEvent;
use OCP\Files\Events\Node\NodeTouchedEvent; use OCP\Files\Events\Node\NodeTouchedEvent;
use OCP\Files\Events\Node\NodeWrittenEvent; use OCP\Files\Events\Node\NodeWrittenEvent;
use OCP\User\Events\UserLoggedOutEvent;
const AUTH_HEADER = 'HTTP_AUTHORIZATION'; const AUTH_HEADER = 'HTTP_AUTHORIZATION';
@ -77,6 +81,10 @@ class Application extends App implements IBootstrap
$context->registerEventListener(NodeTouchedEvent::class, PostWriteListener::class); $context->registerEventListener(NodeTouchedEvent::class, PostWriteListener::class);
$context->registerEventListener(NodeDeletedEvent::class, PostDeleteListener::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 // Register clusters backends
ClustersBackend\AlbumsBackend::register(); ClustersBackend\AlbumsBackend::register();
ClustersBackend\TagsBackend::register(); ClustersBackend\TagsBackend::register();

View File

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
/**
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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');
}
}
}

View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
/**
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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');
}
}

View File

@ -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);
}
}
})();

View File

@ -11,6 +11,7 @@ const isDev = buildMode === 'development';
webpackConfig.entry = { webpackConfig.entry = {
main: path.resolve(path.join('src', 'main')), main: path.resolve(path.join('src', 'main')),
admin: path.resolve(path.join('src', 'admin')), admin: path.resolve(path.join('src', 'admin')),
'hooks-clear-cache': path.resolve(path.join('src', 'hooks', 'clear-cache')),
}; };
// Enable TypeScript // Enable TypeScript