From a73f494fe9f5ca2245f01e08e06c547915056e8e Mon Sep 17 00:00:00 2001 From: Jan Ackermann Date: Fri, 8 Mar 2024 10:54:11 +0100 Subject: [PATCH 1/2] Add sse item renamed event --- packages/web-client/src/sse.ts | 3 +- .../web-runtime/src/container/bootstrap.ts | 97 ++--------- packages/web-runtime/src/container/sse.ts | 157 ++++++++++++++++++ 3 files changed, 170 insertions(+), 87 deletions(-) create mode 100644 packages/web-runtime/src/container/sse.ts diff --git a/packages/web-client/src/sse.ts b/packages/web-client/src/sse.ts index f64a0f3b786..8142dd23f8d 100644 --- a/packages/web-client/src/sse.ts +++ b/packages/web-client/src/sse.ts @@ -2,7 +2,8 @@ import { fetchEventSource, FetchEventSourceInit } from '@microsoft/fetch-event-s export enum MESSAGE_TYPE { NOTIFICATION = 'userlog-notification', - POSTPROCESSING_FINISHED = 'postprocessing-finished' + POSTPROCESSING_FINISHED = 'postprocessing-finished', + ITEM_RENAMED = 'item-renamed' } export class RetriableError extends Error { diff --git a/packages/web-runtime/src/container/bootstrap.ts b/packages/web-runtime/src/container/bootstrap.ts index 9f0490ff938..a3cb01b7e31 100644 --- a/packages/web-runtime/src/container/bootstrap.ts +++ b/packages/web-runtime/src/container/bootstrap.ts @@ -27,8 +27,7 @@ import { useSharesStore, useResourcesStore, ResourcesStore, - SpacesStore, - ImageDimension + SpacesStore } from '@ownclouders/web-pkg' import { authService } from '../services/auth' import { @@ -49,15 +48,14 @@ import { } from '@ownclouders/web-pkg' import { MESSAGE_TYPE } from '@ownclouders/web-client/src/sse' import { getQueryParam } from '../helpers/url' -import { z } from 'zod' import PQueue from 'p-queue' -import { extractNodeId, extractStorageId } from '@ownclouders/web-client/src/helpers' import { storeToRefs } from 'pinia' import { getExtensionNavItems } from '../helpers/navItems' import { RawConfigSchema, SentryConfig } from '@ownclouders/web-pkg/src/composables/piniaStores/config/types' +import { onSSEItemRenamedEvent, onSSEProcessingFinishedEvent } from './sse' const getEmbedConfigFromQuery = ( doesEmbedEnabledOptionExists: boolean @@ -641,88 +639,6 @@ export const announceCustomStyles = ({ configStore }: { configStore?: ConfigStor }) } -const fileReadyEventSchema = z.object({ - itemid: z.string(), - parentitemid: z.string() -}) - -const onSSEProcessingFinishedEvent = async ({ - resourcesStore, - spacesStore, - msg, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - clientService, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - resourceQueue, - previewService -}: { - resourcesStore: ResourcesStore - spacesStore: SpacesStore - msg: MessageEvent - clientService: ClientService - resourceQueue: PQueue - previewService: PreviewService -}) => { - try { - const postProcessingData = fileReadyEventSchema.parse(JSON.parse(msg.data)) - const currentFolder = resourcesStore.currentFolder - if (!currentFolder) { - return - } - - // UPDATE_RESOURCE_FIELD only handles files in the currentFolder, so we can shortcut here for now - if (!extractNodeId(currentFolder.id)) { - // if we don't have a nodeId here, we have a space (root) as current folder and can only check against the storageId - if (currentFolder.id !== extractStorageId(postProcessingData.parentitemid)) { - return - } - } else { - if (currentFolder.id !== postProcessingData.parentitemid) { - return - } - } - - const resource = resourcesStore.resources.find((f) => f.id === postProcessingData.itemid) - const matchingSpace = spacesStore.spaces.find((s) => s.id === resource.storageId) - const isFileLoaded = !!resource - if (isFileLoaded) { - resourcesStore.updateResourceField({ - id: postProcessingData.itemid, - field: 'processing', - value: false - }) - - if (matchingSpace) { - const preview = await previewService.loadPreview({ - resource, - space: matchingSpace, - dimensions: ImageDimension.Thumbnail - }) - - if (preview) { - resourcesStore.updateResourceField({ - id: postProcessingData.itemid, - field: 'thumbnail', - value: preview - }) - } - } - } else { - // FIXME: we currently cannot do this, we need to block this for ongoing uploads and copy operations - // when fixing revert the changelog removal - // resourceQueue.add(async () => { - // const { resource } = await clientService.webdav.listFilesById({ - // fileId: postProcessingData.itemid - // }) - // resource.path = urlJoin(currentFolder.path, resource.name) - // resourcesStore.upsertResource(resource) - // }) - } - } catch (e) { - console.error('Unable to parse sse postprocessing data', e) - } -} - export const registerSSEEventListeners = ({ resourcesStore, spacesStore, @@ -747,6 +663,15 @@ export const registerSSEEventListeners = ({ } ) + clientService.sseAuthenticated.addEventListener(MESSAGE_TYPE.ITEM_RENAMED, (msg) => + onSSEItemRenamedEvent({ + resourcesStore, + spacesStore, + msg, + clientService + }) + ) + clientService.sseAuthenticated.addEventListener(MESSAGE_TYPE.POSTPROCESSING_FINISHED, (msg) => onSSEProcessingFinishedEvent({ resourcesStore, diff --git a/packages/web-runtime/src/container/sse.ts b/packages/web-runtime/src/container/sse.ts new file mode 100644 index 00000000000..74724ec693d --- /dev/null +++ b/packages/web-runtime/src/container/sse.ts @@ -0,0 +1,157 @@ +import { + ClientService, + ImageDimension, + PreviewService, + ResourcesStore, + SpacesStore +} from '@ownclouders/web-pkg' +import PQueue from 'p-queue' +import { extractNodeId, extractStorageId } from '@ownclouders/web-client/src/helpers' +import { z } from 'zod' + +const fileReadyEventSchema = z.object({ + itemid: z.string(), + parentitemid: z.string() +}) + +type SSEMessageData = { + itemid?: string + parentitemid?: string +} + +const itemInCurrentFolder = ({ + resourcesStore, + sseData +}: { + resourcesStore: ResourcesStore + sseData: SSEMessageData +}) => { + const currentFolder = resourcesStore.currentFolder + if (!currentFolder) { + return false + } + + if (!extractNodeId(currentFolder.id)) { + // if we don't have a nodeId here, we have a space (root) as current folder and can only check against the storageId + if (currentFolder.id !== extractStorageId(sseData.parentitemid)) { + return false + } + } else { + if (currentFolder.id !== sseData.parentitemid) { + return false + } + } + + return true +} + +export const onSSEItemRenamedEvent = async ({ + resourcesStore, + spacesStore, + msg, + clientService +}: { + resourcesStore: ResourcesStore + spacesStore: SpacesStore + msg: MessageEvent + clientService: ClientService +}) => { + try { + const sseData = fileReadyEventSchema.parse(JSON.parse(msg.data)) + + if (!itemInCurrentFolder({ resourcesStore, sseData })) { + return false + } + + const resource = resourcesStore.resources.find((f) => f.id === sseData.itemid) + const space = spacesStore.spaces.find((s) => s.id === resource.storageId) + if (!resource || !space) { + return + } + + const updatedResource = await clientService.webdav.getFileInfo(space, { + fileId: sseData.itemid + }) + + resourcesStore.updateResourceField({ + id: sseData.itemid, + field: 'name', + value: updatedResource.name + }) + + resourcesStore.updateResourceField({ + id: sseData.itemid, + field: 'path', + value: updatedResource.path + }) + + console.log(updatedResource) + } catch (e) { + console.error('Unable to parse sse event item renamed data', e) + } +} +export const onSSEProcessingFinishedEvent = async ({ + resourcesStore, + spacesStore, + msg, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + clientService, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + resourceQueue, + previewService +}: { + resourcesStore: ResourcesStore + spacesStore: SpacesStore + msg: MessageEvent + clientService: ClientService + resourceQueue: PQueue + previewService: PreviewService +}) => { + try { + const sseData = fileReadyEventSchema.parse(JSON.parse(msg.data)) + + if (!itemInCurrentFolder({ resourcesStore, sseData })) { + return false + } + + const resource = resourcesStore.resources.find((f) => f.id === sseData.itemid) + const space = spacesStore.spaces.find((s) => s.id === resource.storageId) + const isFileLoaded = !!resource + + if (isFileLoaded) { + resourcesStore.updateResourceField({ + id: sseData.itemid, + field: 'processing', + value: false + }) + + if (space) { + const preview = await previewService.loadPreview({ + resource, + space, + dimensions: ImageDimension.Thumbnail + }) + + if (preview) { + resourcesStore.updateResourceField({ + id: sseData.itemid, + field: 'thumbnail', + value: preview + }) + } + } + } else { + // FIXME: we currently cannot do this, we need to block this for ongoing uploads and copy operations + // when fixing revert the changelog removal + // resourceQueue.add(async () => { + // const { resource } = await clientService.webdav.listFilesById({ + // fileId: sseData.itemid + // }) + // resource.path = urlJoin(currentFolder.path, resource.name) + // resourcesStore.upsertResource(resource) + // }) + } + } catch (e) { + console.error('Unable to parse sse event postprocessing-finished data', e) + } +} From 574c36a0b3da02413eec200b5df7e1a0de945818 Mon Sep 17 00:00:00 2001 From: Jan Ackermann Date: Fri, 8 Mar 2024 11:17:37 +0100 Subject: [PATCH 2/2] Fix rename of current folder has no effect --- .../web-runtime/src/container/bootstrap.ts | 7 +++-- packages/web-runtime/src/container/sse.ts | 29 +++++++++++++++---- packages/web-runtime/src/index.ts | 3 +- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/packages/web-runtime/src/container/bootstrap.ts b/packages/web-runtime/src/container/bootstrap.ts index a3cb01b7e31..857c2b34205 100644 --- a/packages/web-runtime/src/container/bootstrap.ts +++ b/packages/web-runtime/src/container/bootstrap.ts @@ -644,13 +644,15 @@ export const registerSSEEventListeners = ({ spacesStore, clientService, previewService, - configStore + configStore, + router }: { resourcesStore: ResourcesStore spacesStore: SpacesStore clientService: ClientService previewService: PreviewService configStore: ConfigStore + router: Router }): void => { const resourceQueue = new PQueue({ concurrency: configStore.options.concurrentRequests.sse @@ -668,7 +670,8 @@ export const registerSSEEventListeners = ({ resourcesStore, spacesStore, msg, - clientService + clientService, + router }) ) diff --git a/packages/web-runtime/src/container/sse.ts b/packages/web-runtime/src/container/sse.ts index 74724ec693d..cd324d4389c 100644 --- a/packages/web-runtime/src/container/sse.ts +++ b/packages/web-runtime/src/container/sse.ts @@ -1,5 +1,6 @@ import { ClientService, + createFileRouteOptions, ImageDimension, PreviewService, ResourcesStore, @@ -8,6 +9,7 @@ import { import PQueue from 'p-queue' import { extractNodeId, extractStorageId } from '@ownclouders/web-client/src/helpers' import { z } from 'zod' +import { Router } from 'vue-router' const fileReadyEventSchema = z.object({ itemid: z.string(), @@ -49,22 +51,31 @@ export const onSSEItemRenamedEvent = async ({ resourcesStore, spacesStore, msg, - clientService + clientService, + router }: { resourcesStore: ResourcesStore spacesStore: SpacesStore msg: MessageEvent clientService: ClientService + router: Router }) => { try { const sseData = fileReadyEventSchema.parse(JSON.parse(msg.data)) - if (!itemInCurrentFolder({ resourcesStore, sseData })) { + const currentFolder = resourcesStore.currentFolder + const resourceIsCurrentFolder = currentFolder.id === sseData.itemid + + if (!resourceIsCurrentFolder && !itemInCurrentFolder({ resourcesStore, sseData })) { return false } - const resource = resourcesStore.resources.find((f) => f.id === sseData.itemid) + const resource = resourceIsCurrentFolder + ? currentFolder + : resourcesStore.resources.find((f) => f.id === sseData.itemid) + const space = spacesStore.spaces.find((s) => s.id === resource.storageId) + if (!resource || !space) { return } @@ -73,6 +84,16 @@ export const onSSEItemRenamedEvent = async ({ fileId: sseData.itemid }) + if (resourceIsCurrentFolder) { + resourcesStore.setCurrentFolder(updatedResource) + return router.push( + createFileRouteOptions(space, { + path: updatedResource.path, + fileId: updatedResource.fileId + }) + ) + } + resourcesStore.updateResourceField({ id: sseData.itemid, field: 'name', @@ -84,8 +105,6 @@ export const onSSEItemRenamedEvent = async ({ field: 'path', value: updatedResource.path }) - - console.log(updatedResource) } catch (e) { console.error('Unable to parse sse event item renamed data', e) } diff --git a/packages/web-runtime/src/index.ts b/packages/web-runtime/src/index.ts index 040fbe34e37..68b3bfa2858 100644 --- a/packages/web-runtime/src/index.ts +++ b/packages/web-runtime/src/index.ts @@ -196,7 +196,8 @@ export const bootstrapApp = async (configurationPath: string): Promise => spacesStore, clientService, previewService, - configStore + configStore, + router }) }