Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sse event for move operations and add debug logs #10798

Merged
merged 4 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/unreleased/enhancement-add-sse-event-for-moving
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Enhancement: Add SSE event for moving

We've added Server-Sent Events (SSE) for moving resources.
When the user moves files or folders, everyone with access to them instantly sees the changes across their browsers and devices.

AlexAndBear marked this conversation as resolved.
Show resolved Hide resolved
https://github.com/owncloud/web/pull/10798
https://github.com/owncloud/web/issues/10780
1 change: 1 addition & 0 deletions packages/web-client/src/sse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand Down
14 changes: 13 additions & 1 deletion packages/web-runtime/src/container/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ import {
onSSEItemRestoredEvent,
onSSEItemTrashedEvent,
onSSEFolderCreatedEvent,
onSSEFileTouchedEvent
onSSEFileTouchedEvent,
onSSEItemMovedEvent
} from './sse'

const getEmbedConfigFromQuery = (
Expand Down Expand Up @@ -726,6 +727,17 @@ export const registerSSEEventListeners = ({
})
)

clientService.sseAuthenticated.addEventListener(MESSAGE_TYPE.ITEM_MOVED, (msg) =>
onSSEItemMovedEvent({
topic: MESSAGE_TYPE.ITEM_MOVED,
resourcesStore,
spacesStore,
userStore,
msg,
clientService
})
)

clientService.sseAuthenticated.addEventListener(MESSAGE_TYPE.FOLDER_CREATED, (msg) =>
onSSEFolderCreatedEvent({
topic: MESSAGE_TYPE.FOLDER_CREATED,
Expand Down
69 changes: 69 additions & 0 deletions packages/web-runtime/src/container/sse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
/**
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
/**
Expand Down Expand Up @@ -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) {
/**
Expand Down Expand Up @@ -345,6 +351,67 @@ export const onSSEItemRestoredEvent = async ({
}
}

export const onSSEItemMovedEvent = async ({
topic,
resourcesStore,
spacesStore,
userStore,
msg,
clientService
}: {
topic: string
resourcesStore: ResourcesStore
spacesStore: SpacesStore
userStore: UserStore
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,
space,
user: userStore.user,
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
Expand All @@ -365,6 +432,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) {
/**
Expand Down Expand Up @@ -413,6 +481,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) {
/**
Expand Down