From 39758c82142c2829ea185807ed7d102537cf77bf Mon Sep 17 00:00:00 2001 From: Jan Ackermann Date: Wed, 17 Apr 2024 14:10:53 +0200 Subject: [PATCH 1/4] Add sse event for move operations and add debug logs --- .../enhancement-add-sse-event-for-moving | 6 ++ packages/web-client/src/sse/index.ts | 1 + .../web-runtime/src/container/bootstrap.ts | 13 +++- packages/web-runtime/src/container/sse.ts | 65 +++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 changelog/unreleased/enhancement-add-sse-event-for-moving diff --git a/changelog/unreleased/enhancement-add-sse-event-for-moving b/changelog/unreleased/enhancement-add-sse-event-for-moving new file mode 100644 index 00000000000..9f9a013f2d3 --- /dev/null +++ b/changelog/unreleased/enhancement-add-sse-event-for-moving @@ -0,0 +1,6 @@ +Enhancement: Add SSE event for moving + +We've added Server-Sent Events (SSE) for moving resources. +This enhancement ensures that the moving action instantly propagated to other browsers and devices where the logged in user has access the relevant files and folders. + + diff --git a/packages/web-client/src/sse/index.ts b/packages/web-client/src/sse/index.ts index 7b851e6d840..6514eec4788 100644 --- a/packages/web-client/src/sse/index.ts +++ b/packages/web-client/src/sse/index.ts @@ -9,6 +9,7 @@ export enum MESSAGE_TYPE { ITEM_RENAMED = 'item-renamed', ITEM_TRASHED = 'item-trashed', ITEM_RESTORED = 'item-restored', + ITEM_MOVED = 'item-moved', FOLDER_CREATED = 'folder-created' } diff --git a/packages/web-runtime/src/container/bootstrap.ts b/packages/web-runtime/src/container/bootstrap.ts index 0f3538d4758..ecb29ee8f83 100644 --- a/packages/web-runtime/src/container/bootstrap.ts +++ b/packages/web-runtime/src/container/bootstrap.ts @@ -61,7 +61,8 @@ import { onSSEItemRestoredEvent, onSSEItemTrashedEvent, onSSEFolderCreatedEvent, - onSSEFileTouchedEvent + onSSEFileTouchedEvent, + onSSEItemMovedEvent } from './sse' const getEmbedConfigFromQuery = ( @@ -726,6 +727,16 @@ export const registerSSEEventListeners = ({ }) ) + clientService.sseAuthenticated.addEventListener(MESSAGE_TYPE.ITEM_MOVED, (msg) => + onSSEItemMovedEvent({ + topic: MESSAGE_TYPE.ITEM_MOVED, + resourcesStore, + spacesStore, + msg, + clientService + }) + ) + clientService.sseAuthenticated.addEventListener(MESSAGE_TYPE.FOLDER_CREATED, (msg) => onSSEFolderCreatedEvent({ topic: MESSAGE_TYPE.FOLDER_CREATED, diff --git a/packages/web-runtime/src/container/sse.ts b/packages/web-runtime/src/container/sse.ts index d47f8043b02..f6c460acbee 100644 --- a/packages/web-runtime/src/container/sse.ts +++ b/packages/web-runtime/src/container/sse.ts @@ -66,6 +66,7 @@ export const onSSEItemRenamedEvent = async ({ }) => { try { const sseData = eventSchema.parse(JSON.parse(msg.data)) + console.debug(`SSE event '${topic}'`, sseData) if (sseData.initiatorid === clientService.initiatorId) { /** @@ -124,6 +125,8 @@ export const onSSEFileLockingEvent = async ({ }) => { try { const sseData = eventSchema.parse(JSON.parse(msg.data)) + console.debug(`SSE event '${topic}'`, sseData) + const resource = resourcesStore.resources.find((f) => f.id === sseData.itemid) const space = spacesStore.spaces.find((s) => s.id === resource.storageId) @@ -170,6 +173,7 @@ export const onSSEProcessingFinishedEvent = async ({ }) => { try { const sseData = eventSchema.parse(JSON.parse(msg.data)) + console.debug(`SSE event '${topic}'`, sseData) if (!itemInCurrentFolder({ resourcesStore, parentFolderId: sseData.parentitemid })) { return false @@ -252,6 +256,7 @@ export const onSSEItemTrashedEvent = ({ }) => { try { const sseData = eventSchema.parse(JSON.parse(msg.data)) + console.debug(`SSE event '${topic}'`, sseData) if (sseData.initiatorid === clientService.initiatorId) { /** @@ -302,6 +307,7 @@ export const onSSEItemRestoredEvent = async ({ }) => { try { const sseData = eventSchema.parse(JSON.parse(msg.data)) + console.debug(`SSE event '${topic}'`, sseData) if (sseData.initiatorid === clientService.initiatorId) { /** @@ -345,6 +351,63 @@ export const onSSEItemRestoredEvent = async ({ } } +export const onSSEItemMovedEvent = async ({ + topic, + resourcesStore, + spacesStore, + msg, + clientService +}: { + topic: string + resourcesStore: ResourcesStore + spacesStore: SpacesStore + msg: MessageEvent + clientService: ClientService +}) => { + try { + const sseData = eventSchema.parse(JSON.parse(msg.data)) + console.debug(`SSE event '${topic}'`, sseData) + + if (sseData.initiatorid === clientService.initiatorId) { + /** + * If the request was initiated by the current client (browser tab), + * there's no need to proceed with the action since the web already + * handles its own business logic. Therefore, we'll return early here. + */ + return + } + + const space = spacesStore.spaces.find((space) => space.id === sseData.spaceid) + if (!space) { + return + } + + const resource = await clientService.webdav.getFileInfo(space, { + fileId: sseData.itemid + }) + + if (!resource) { + return + } + + if (resource.parentFolderId !== resourcesStore.currentFolder.id) { + return resourcesStore.removeResources([resource]) + } + + resourcesStore.upsertResource(resource) + resourcesStore.updateResourceField({ + id: resource.id, + field: 'indicators', + value: getIndicators({ + resource, + ancestorMetaData: resourcesStore.ancestorMetaData + }) + }) + } catch (e) { + console.error(`Unable to parse sse event ${topic} data`, e) + } +} + /** * The FileTouched event is triggered when a new empty file, such as a new text file, * is about to be created on the server. This event is necessary because the @@ -365,6 +428,7 @@ export const onSSEFileTouchedEvent = async ({ }) => { try { const sseData = eventSchema.parse(JSON.parse(msg.data)) + console.debug(`SSE event '${topic}'`, sseData) if (sseData.initiatorid === clientService.initiatorId) { /** @@ -413,6 +477,7 @@ export const onSSEFolderCreatedEvent = async ({ }) => { try { const sseData = eventSchema.parse(JSON.parse(msg.data)) + console.debug(`SSE event '${topic}'`, sseData) if (sseData.initiatorid === clientService.initiatorId) { /** From c728c2e13f164e6834f37b42c3118d872581b3f4 Mon Sep 17 00:00:00 2001 From: Jan Ackermann Date: Wed, 17 Apr 2024 14:11:33 +0200 Subject: [PATCH 2/4] Enhance changelog item --- changelog/unreleased/enhancement-add-sse-event-for-moving | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/changelog/unreleased/enhancement-add-sse-event-for-moving b/changelog/unreleased/enhancement-add-sse-event-for-moving index 9f9a013f2d3..957f501e892 100644 --- a/changelog/unreleased/enhancement-add-sse-event-for-moving +++ b/changelog/unreleased/enhancement-add-sse-event-for-moving @@ -3,4 +3,5 @@ Enhancement: Add SSE event for moving We've added Server-Sent Events (SSE) for moving resources. This enhancement ensures that the moving action instantly propagated to other browsers and devices where the logged in user has access the relevant files and folders. - +https://github.com/owncloud/web/pull/10798 +https://github.com/owncloud/web/issues/10780 From 203a3b3f386caed91868614e24ec5a2aa154d37a Mon Sep 17 00:00:00 2001 From: Jan Ackermann Date: Wed, 17 Apr 2024 15:54:11 +0200 Subject: [PATCH 3/4] Enhance changelog item --- changelog/unreleased/enhancement-add-sse-event-for-moving | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/unreleased/enhancement-add-sse-event-for-moving b/changelog/unreleased/enhancement-add-sse-event-for-moving index 957f501e892..e78678532a1 100644 --- a/changelog/unreleased/enhancement-add-sse-event-for-moving +++ b/changelog/unreleased/enhancement-add-sse-event-for-moving @@ -1,7 +1,7 @@ Enhancement: Add SSE event for moving We've added Server-Sent Events (SSE) for moving resources. -This enhancement ensures that the moving action instantly propagated to other browsers and devices where the logged in user has access the relevant files and folders. +When the user moves files or folders, everyone with access to them instantly sees the changes across their browsers and devices. https://github.com/owncloud/web/pull/10798 https://github.com/owncloud/web/issues/10780 From 2f0dbbd7dfc02a98725bfbc211d576e8450eec9a Mon Sep 17 00:00:00 2001 From: Jan Ackermann Date: Wed, 17 Apr 2024 16:06:55 +0200 Subject: [PATCH 4/4] Adjust to master --- packages/web-runtime/src/container/bootstrap.ts | 1 + packages/web-runtime/src/container/sse.ts | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/packages/web-runtime/src/container/bootstrap.ts b/packages/web-runtime/src/container/bootstrap.ts index ecb29ee8f83..20b3216814f 100644 --- a/packages/web-runtime/src/container/bootstrap.ts +++ b/packages/web-runtime/src/container/bootstrap.ts @@ -732,6 +732,7 @@ export const registerSSEEventListeners = ({ topic: MESSAGE_TYPE.ITEM_MOVED, resourcesStore, spacesStore, + userStore, msg, clientService }) diff --git a/packages/web-runtime/src/container/sse.ts b/packages/web-runtime/src/container/sse.ts index f6c460acbee..ac5259ce4da 100644 --- a/packages/web-runtime/src/container/sse.ts +++ b/packages/web-runtime/src/container/sse.ts @@ -355,12 +355,14 @@ export const onSSEItemMovedEvent = async ({ topic, resourcesStore, spacesStore, + userStore, msg, clientService }: { topic: string resourcesStore: ResourcesStore spacesStore: SpacesStore + userStore: UserStore msg: MessageEvent clientService: ClientService }) => { @@ -400,6 +402,8 @@ export const onSSEItemMovedEvent = async ({ field: 'indicators', value: getIndicators({ resource, + space, + user: userStore.user, ancestorMetaData: resourcesStore.ancestorMetaData }) })