From 5e67fda1cc94e5d933f4b2e720ebeda4a893d5b7 Mon Sep 17 00:00:00 2001 From: Alexander Piskun Date: Sat, 2 Dec 2023 17:52:52 +0300 Subject: [PATCH] remove fileActions during uninstall, corrected cache cleaning during enable/disable app process. --- lib/Db/ExFilesActionsMenuMapper.php | 12 ++++++++++++ lib/Service/AppAPIService.php | 6 ++++++ lib/Service/ExFilesActionsMenuService.php | 22 ++++++++++++++++++---- lib/Service/TopMenuService.php | 13 ++++++++----- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/lib/Db/ExFilesActionsMenuMapper.php b/lib/Db/ExFilesActionsMenuMapper.php index 779dbca2..b9831784 100644 --- a/lib/Db/ExFilesActionsMenuMapper.php +++ b/lib/Db/ExFilesActionsMenuMapper.php @@ -84,4 +84,16 @@ public function findByName(string $name): ExFilesActionsMenu { ); return $this->findEntity($qb); } + + /** + * @throws Exception + */ + public function removeAllByAppId(string $appId): int { + $qb = $this->db->getQueryBuilder(); + $qb->delete($this->tableName) + ->where( + $qb->expr()->eq('appid', $qb->createNamedParameter($appId, IQueryBuilder::PARAM_STR)) + ); + return $qb->executeStatement(); + } } diff --git a/lib/Service/AppAPIService.php b/lib/Service/AppAPIService.php index 8199a46b..91030d09 100644 --- a/lib/Service/AppAPIService.php +++ b/lib/Service/AppAPIService.php @@ -56,6 +56,7 @@ public function __construct( private readonly ExAppInitialStateService $initialStateService, private readonly ExAppScriptsService $scriptsService, private readonly ExAppStylesService $stylesService, + private readonly ExFilesActionsMenuService $filesActionsMenuService, private readonly ISecureRandom $random, private readonly IUserSession $userSession, private readonly ISession $session, @@ -143,6 +144,7 @@ public function unregisterExApp(string $appId): ?ExApp { $this->exAppScopesService->removeExAppScopes($exApp); $this->exAppUsersService->removeExAppUsers($exApp); $this->talkBotsService->unregisterExAppTalkBots($exApp); // TODO: Think about internal Events for clean and flexible unregister ExApp callbacks + $this->filesActionsMenuService->unregisterExAppFileActions($appId); $this->topMenuService->unregisterExAppMenuEntries($appId); $this->initialStateService->deleteExAppInitialStates($appId); $this->scriptsService->deleteExAppScripts($appId); @@ -186,6 +188,8 @@ public function enableExApp(ExApp $exApp): bool { $cacheKey = '/exApp_' . $exApp->getAppid(); $exApp->setEnabled(1); $this->cache->set($cacheKey, $exApp, self::CACHE_TTL); + $this->filesActionsMenuService->resetCacheEnabled(); + $this->topMenuService->resetCacheEnabled(); $exAppEnabled = $this->requestToExApp($exApp, '/enabled?enabled=1', null, 'PUT'); if ($exAppEnabled instanceof IResponse) { @@ -240,6 +244,8 @@ public function disableExApp(ExApp $exApp): bool { $cacheKey = '/exApp_' . $exApp->getAppid(); $exApp->setEnabled(0); $this->cache->set($cacheKey, $exApp, self::CACHE_TTL); + $this->topMenuService->resetCacheEnabled(); + $this->filesActionsMenuService->resetCacheEnabled(); return true; } catch (Exception $e) { $this->logger->error(sprintf('Error while disabling ExApp: %s', $e->getMessage())); diff --git a/lib/Service/ExFilesActionsMenuService.php b/lib/Service/ExFilesActionsMenuService.php index 7789f487..68b7a4ec 100644 --- a/lib/Service/ExFilesActionsMenuService.php +++ b/lib/Service/ExFilesActionsMenuService.php @@ -69,9 +69,8 @@ public function registerFileActionMenu(string $appId, array $params): ?ExFilesAc $newFileActionMenu->setId($fileActionMenu->getId()); } $fileActionMenu = $this->mapper->insertOrUpdate($newFileActionMenu); - $cacheKey = '/ex_files_actions_menu_' . $appId . '_' . $params['name']; - $this->cache->remove('/ex_files_actions_menus'); - $this->cache->set($cacheKey, $fileActionMenu); + $this->cache->set('/ex_files_actions_menu_' . $appId . '_' . $params['name'], $fileActionMenu); + $this->resetCacheEnabled(); } catch (Exception $e) { $this->logger->error(sprintf('Failed to register ExApp %s FileActionMenu %s. Error: %s', $appId, $params['name'], $e->getMessage()), ['exception' => $e]); return null; @@ -87,7 +86,7 @@ public function unregisterFileActionMenu(string $appId, string $fileActionMenuNa } $this->mapper->delete($fileActionMenu); $this->cache->remove('/ex_files_actions_menu_' . $appId . '_' . $fileActionMenuName); - $this->cache->remove('/ex_files_actions_menus'); + $this->resetCacheEnabled(); return $fileActionMenu; } catch (Exception $e) { $this->logger->error(sprintf('Failed to unregister ExApp %s FileActionMenu %s. Error: %s', $appId, $fileActionMenuName, $e->getMessage()), ['exception' => $e]); @@ -206,4 +205,19 @@ public function loadFileActionIcon(string $appId, string $exFileActionName): ?ar } return null; } + + public function unregisterExAppFileActions(string $appId): int { + try { + $result = $this->mapper->removeAllByAppId($appId); + } catch (Exception) { + $result = -1; + } + $this->cache->clear('/ex_files_actions_menu_' . $appId); + $this->resetCacheEnabled(); + return $result; + } + + public function resetCacheEnabled(): void { + $this->cache->remove('/ex_files_actions_menus'); + } } diff --git a/lib/Service/TopMenuService.php b/lib/Service/TopMenuService.php index 632162f6..b9186d56 100644 --- a/lib/Service/TopMenuService.php +++ b/lib/Service/TopMenuService.php @@ -91,9 +91,8 @@ public function registerExAppMenuEntry(string $appId, string $name, string $disp $newMenuEntry->setId($menuEntry->getId()); } $menuEntry = $this->mapper->insertOrUpdate($newMenuEntry); - $cacheKey = '/ex_top_menu_' . $appId . '_' . $name; - $this->cache->remove('/ex_top_menus'); - $this->cache->set($cacheKey, $menuEntry); + $this->cache->set('/ex_top_menu_' . $appId . '_' . $name, $menuEntry); + $this->resetCacheEnabled(); } catch (Exception $e) { $this->logger->error( sprintf('Failed to register ExApp %s TopMenu %s. Error: %s', $appId, $name, $e->getMessage()), ['exception' => $e] @@ -109,7 +108,7 @@ public function unregisterExAppMenuEntry(string $appId, string $name): bool { return false; } $this->cache->remove('/ex_top_menu_' . $appId . '_' . $name); - $this->cache->remove('/ex_top_menus'); + $this->resetCacheEnabled(); $this->initialStateService->deleteExAppInitialStatesByTypeName($appId, 'top_menu', $name); $this->scriptsService->deleteExAppScriptsByTypeName($appId, 'top_menu', $name); $this->stylesService->deleteExAppStylesByTypeName($appId, 'top_menu', $name); @@ -123,7 +122,7 @@ public function unregisterExAppMenuEntries(string $appId): int { $result = -1; } $this->cache->clear('/ex_top_menu_' . $appId); - $this->cache->remove('/ex_top_menus'); + $this->resetCacheEnabled(); return $result; } @@ -160,4 +159,8 @@ public function getExAppMenuEntries(): array { return []; } } + + public function resetCacheEnabled(): void { + $this->cache->remove('/ex_top_menus'); + } }