From b654e8b236966f205109a26fa703f24284550a19 Mon Sep 17 00:00:00 2001 From: Jamie V Date: Tue, 5 Mar 2024 16:07:31 -0800 Subject: [PATCH] Create Root folders for older persistence stores (#153) --- .../missingUserFolderInterceptor.js | 30 ------------- .../oldPersistenceFolderInterceptor.js | 45 +++++++++++++++++++ src/persistence/plugin.js | 13 +++--- 3 files changed, 50 insertions(+), 38 deletions(-) delete mode 100644 src/persistence/missingUserFolderInterceptor.js create mode 100644 src/persistence/oldPersistenceFolderInterceptor.js diff --git a/src/persistence/missingUserFolderInterceptor.js b/src/persistence/missingUserFolderInterceptor.js deleted file mode 100644 index ab041b2..0000000 --- a/src/persistence/missingUserFolderInterceptor.js +++ /dev/null @@ -1,30 +0,0 @@ -import { - createModelFromNamespaceDefinition, - interpolateUsername -} from './utils'; - -export default async function missingUserFolderInterceptor(openmct, usersNamespace, ROOT_IDENTIFIERS) { - const userTemplate = usersNamespace.childTemplate.key.split('$')[0]; - const userKeyCheck = new RegExp(`^${userTemplate}([^:]+)$`); - - openmct.objects.addGetInterceptor({ - appliesTo: (identifier, domainObject) => { - const isMissing = !domainObject; - const isNotRoot = !ROOT_IDENTIFIERS.find(rootId => openmct.objects.areIdsEqual(rootId, identifier)); - const isUserFolderIdentifier = userKeyCheck.test(identifier.namespace); - - - return isMissing && isNotRoot && isUserFolderIdentifier; - }, - invoke: (identifier, object) => { - const userId = identifier.namespace.match(userKeyCheck)[1]; - const userNamespaceDefinition = interpolateUsername(usersNamespace.childTemplate, userId); - userNamespaceDefinition.location = usersNamespace.id; - const model = createModelFromNamespaceDefinition(userId, userNamespaceDefinition); - - openmct.objects.save(model); - - return model; - } - }); -} diff --git a/src/persistence/oldPersistenceFolderInterceptor.js b/src/persistence/oldPersistenceFolderInterceptor.js new file mode 100644 index 0000000..8bbce7f --- /dev/null +++ b/src/persistence/oldPersistenceFolderInterceptor.js @@ -0,0 +1,45 @@ +import { + createModelFromNamespaceDefinition, + interpolateUsername +} from './utils'; + +export default async function oldPersistenceFolderInterceptor(openmct, namespaces, ROOT_IDENTIFIERS) { + let usersNamespace = namespaces.find((namespace) => namespace.containsNamespaces); + usersNamespace = structuredClone(usersNamespace); + + const userTemplate = usersNamespace.childTemplate.key.split('$')[0]; + const userKeyCheck = new RegExp(`^${userTemplate}([^:]+)$`); + + openmct.objects.addGetInterceptor({ + appliesTo: (identifier, domainObject) => { + const isMissing = !domainObject; + const isNotUserRoot = identifier.key !== 'container'; + const isUserFolderIdentifier = userKeyCheck.test(identifier.namespace); + const nonUserRoots = ROOT_IDENTIFIERS.filter(rootId => rootId.key !== 'container'); + const isRootFolder = nonUserRoots.find(rootId => openmct.objects.areIdsEqual(rootId, identifier)) !== undefined; + + // we will create domain objects for empty older persistence user or + // root (ex: shared) folders, not for the user root or other folders + return isMissing && isNotUserRoot && (isUserFolderIdentifier || isRootFolder); + }, + invoke: (identifier, object) => { + const isUserNamespace = identifier.key === 'container'; + + const userId = identifier.namespace.match(userKeyCheck)[1]; + let namespaceDefinition; + + if (isUserNamespace) { + namespaceDefinition = interpolateUsername(usersNamespace.childTemplate, userId); + namespaceDefinition.location = usersNamespace.id; + } else { + namespaceDefinition = namespaces.find(namespace => namespace.key === identifier.namespace); + } + + const model = createModelFromNamespaceDefinition(userId, namespaceDefinition); + + openmct.objects.save(model); + + return model; + } + }); +} diff --git a/src/persistence/plugin.js b/src/persistence/plugin.js index 630a397..5fca50f 100644 --- a/src/persistence/plugin.js +++ b/src/persistence/plugin.js @@ -1,7 +1,7 @@ import { createIdentifierFromNamespaceDefinition, createNamespace } from './utils'; import existingNamespaceUpdateInterceptor from './existingNamespaceUpdateInterceptor'; import MCWSPersistenceProvider from './MCWSPersistenceProvider'; -import missingUserFolderInterceptor from './missingUserFolderInterceptor'; +import oldPersistenceFolderInterceptor from './oldPersistenceFolderInterceptor'; export default function MCWSPersistenceProviderPlugin(configNamespaces) { return async function install(openmct) { @@ -11,8 +11,6 @@ export default function MCWSPersistenceProviderPlugin(configNamespaces) { }); openmct.objects.addRoot(() => rootsPromise); const namespaces = configNamespaces.map(createNamespace); - let usersNamespace = namespaces.find((namespace) => namespace.containsNamespaces); - usersNamespace = structuredClone(usersNamespace); const mcwsPersistenceProvider = new MCWSPersistenceProvider(openmct, namespaces); // install the provider for each persistence space, @@ -25,11 +23,10 @@ export default function MCWSPersistenceProviderPlugin(configNamespaces) { const ROOT_IDENTIFIERS = rootNamespaces.map(createIdentifierFromNamespaceDefinition); // user namespaces are not required - if (usersNamespace) { - const checkOldNamespaces = localStorage.getItem(`r5.0_old_namespace_checked:${usersNamespace.key}`) === null; - existingNamespaceUpdateInterceptor(openmct, usersNamespace, checkOldNamespaces); - missingUserFolderInterceptor(openmct, usersNamespace, ROOT_IDENTIFIERS); - } + let usersNamespace = namespaces.find((namespace) => namespace.containsNamespaces); + const checkOldNamespaces = usersNamespace ? localStorage.getItem(`r5.0_old_namespace_checked:${usersNamespace.key}`) === null : false; + existingNamespaceUpdateInterceptor(openmct, usersNamespace, checkOldNamespaces); + oldPersistenceFolderInterceptor(openmct, namespaces, ROOT_IDENTIFIERS); rootsResolve(ROOT_IDENTIFIERS); };