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 file locked/unlocked event #10572

Merged
merged 8 commits into from
Mar 11, 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
4 changes: 3 additions & 1 deletion packages/web-client/src/sse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { fetchEventSource, FetchEventSourceInit } from '@microsoft/fetch-event-s
export enum MESSAGE_TYPE {
NOTIFICATION = 'userlog-notification',
POSTPROCESSING_FINISHED = 'postprocessing-finished',
ITEM_RENAMED = 'item-renamed'
ITEM_RENAMED = 'item-renamed',
FILE_LOCKED = 'file-locked',
FILE_UNLOCKED = 'file-unlocked'
}

export class RetriableError extends Error {
Expand Down
24 changes: 23 additions & 1 deletion packages/web-runtime/src/container/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import {
RawConfigSchema,
SentryConfig
} from '@ownclouders/web-pkg/src/composables/piniaStores/config/types'
import { onSSEItemRenamedEvent, onSSEProcessingFinishedEvent } from './sse'
import { onSSEFileLockingEvent, onSSEItemRenamedEvent, onSSEProcessingFinishedEvent } from './sse'

const getEmbedConfigFromQuery = (
doesEmbedEnabledOptionExists: boolean
Expand Down Expand Up @@ -667,6 +667,7 @@ export const registerSSEEventListeners = ({

clientService.sseAuthenticated.addEventListener(MESSAGE_TYPE.ITEM_RENAMED, (msg) =>
onSSEItemRenamedEvent({
topic: MESSAGE_TYPE.ITEM_RENAMED,
resourcesStore,
spacesStore,
msg,
Expand All @@ -677,6 +678,7 @@ export const registerSSEEventListeners = ({

clientService.sseAuthenticated.addEventListener(MESSAGE_TYPE.POSTPROCESSING_FINISHED, (msg) =>
onSSEProcessingFinishedEvent({
topic: MESSAGE_TYPE.POSTPROCESSING_FINISHED,
resourcesStore,
spacesStore,
msg,
Expand All @@ -685,6 +687,26 @@ export const registerSSEEventListeners = ({
resourceQueue
})
)

clientService.sseAuthenticated.addEventListener(MESSAGE_TYPE.FILE_LOCKED, (msg) =>
onSSEFileLockingEvent({
topic: MESSAGE_TYPE.FILE_LOCKED,
resourcesStore,
spacesStore,
msg,
clientService
})
)

clientService.sseAuthenticated.addEventListener(MESSAGE_TYPE.FILE_UNLOCKED, (msg) =>
onSSEFileLockingEvent({
topic: MESSAGE_TYPE.FILE_UNLOCKED,
resourcesStore,
spacesStore,
msg,
clientService
})
)
}

export const setViewOptions = ({ resourcesStore }: { resourcesStore: ResourcesStore }) => {
Expand Down
71 changes: 50 additions & 21 deletions packages/web-runtime/src/container/sse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ClientService,
createFileRouteOptions,
getIndicators,
ImageDimension,
PreviewService,
ResourcesStore,
Expand All @@ -23,10 +24,10 @@ type SSEMessageData = {

const itemInCurrentFolder = ({
resourcesStore,
sseData
parentFolderId
}: {
resourcesStore: ResourcesStore
sseData: SSEMessageData
parentFolderId: string
}) => {
const currentFolder = resourcesStore.currentFolder
if (!currentFolder) {
Expand All @@ -35,11 +36,11 @@ const itemInCurrentFolder = ({

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)) {
if (currentFolder.id !== extractStorageId(parentFolderId)) {
return false
}
} else {
if (currentFolder.id !== sseData.parentitemid) {
if (currentFolder.id !== parentFolderId) {
return false
}
}
Expand All @@ -48,12 +49,14 @@ const itemInCurrentFolder = ({
}

export const onSSEItemRenamedEvent = async ({
topic,
resourcesStore,
spacesStore,
msg,
clientService,
router
}: {
topic: string
resourcesStore: ResourcesStore
spacesStore: SpacesStore
msg: MessageEvent
Expand All @@ -62,18 +65,11 @@ export const onSSEItemRenamedEvent = async ({
}) => {
try {
const sseData = fileReadyEventSchema.parse(JSON.parse(msg.data))

const currentFolder = resourcesStore.currentFolder
const resourceIsCurrentFolder = currentFolder.id === sseData.itemid

if (!resourceIsCurrentFolder && !itemInCurrentFolder({ resourcesStore, sseData })) {
AlexAndBear marked this conversation as resolved.
Show resolved Hide resolved
return false
}

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) {
Expand All @@ -94,22 +90,54 @@ export const onSSEItemRenamedEvent = async ({
)
}

resourcesStore.updateResourceField({
id: sseData.itemid,
field: 'name',
value: updatedResource.name
resourcesStore.upsertResource(updatedResource)
} catch (e) {
console.error(`Unable to parse sse event ${topic} data`, e)
}
}

export const onSSEFileLockingEvent = async ({
topic,
resourcesStore,
spacesStore,
msg,
clientService
}: {
topic: string
resourcesStore: ResourcesStore
spacesStore: SpacesStore
msg: MessageEvent
clientService: ClientService
}) => {
try {
const sseData = fileReadyEventSchema.parse(JSON.parse(msg.data))
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.upsertResource(updatedResource)
resourcesStore.updateResourceField({
id: sseData.itemid,
field: 'path',
value: updatedResource.path
id: updatedResource.id,
field: 'indicators',
value: getIndicators({
resource: updatedResource,
ancestorMetaData: resourcesStore.ancestorMetaData
})
})
} catch (e) {
console.error('Unable to parse sse event item renamed data', e)
console.error(`Unable to parse sse event ${topic} data`, e)
}
}

export const onSSEProcessingFinishedEvent = async ({
topic,
resourcesStore,
spacesStore,
msg,
Expand All @@ -119,6 +147,7 @@ export const onSSEProcessingFinishedEvent = async ({
resourceQueue,
previewService
}: {
topic: string
resourcesStore: ResourcesStore
spacesStore: SpacesStore
msg: MessageEvent
Expand All @@ -129,7 +158,7 @@ export const onSSEProcessingFinishedEvent = async ({
try {
const sseData = fileReadyEventSchema.parse(JSON.parse(msg.data))

if (!itemInCurrentFolder({ resourcesStore, sseData })) {
if (!itemInCurrentFolder({ resourcesStore, parentFolderId: sseData.parentitemid })) {
return false
}

Expand Down Expand Up @@ -171,6 +200,6 @@ export const onSSEProcessingFinishedEvent = async ({
// })
}
} catch (e) {
console.error('Unable to parse sse event postprocessing-finished data', e)
console.error(`Unable to parse sse event ${topic} data`, e)
}
}