Skip to content

Commit

Permalink
fix(files): fetch nodes if we have multiple of the same fileid in the…
Browse files Browse the repository at this point in the history
… store

Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Jun 12, 2024
1 parent 1d7893d commit eddf1f7
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions apps/files/src/store/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,30 @@
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { Folder, Node } from '@nextcloud/files'

import type { FilesStore, RootsStore, RootOptions, Service, FilesState, FileSource } from '../types'
import type { FileStat, ResponseDataDetailed } from 'webdav'
import type { Folder, Node } from '@nextcloud/files'

import { defineStore } from 'pinia'
import { subscribe } from '@nextcloud/event-bus'
import { subscribe, emit } from '@nextcloud/event-bus'
import { davGetDefaultPropfind, davResultToNode, davRootPath } from '@nextcloud/files'
import logger from '../logger'
import Vue from 'vue'

import { client } from '../services/WebdavClient.ts'

window.emitEvent = emit

const fetchNode = async (node: Node): Promise<Node> => {
const propfindPayload = davGetDefaultPropfind()
const result = await client.stat(`${davRootPath}${node.path}`, {
details: true,
data: propfindPayload,
}) as ResponseDataDetailed<FileStat>
return davResultToNode(result.data)
}

export const useFilesStore = function(...args) {
const store = defineStore('files', {
state: (): FilesState => ({
Expand All @@ -31,6 +47,13 @@ export const useFilesStore = function(...args) {
.map(source => state.files[source])
.filter(Boolean),

/**
* Get files or folders by their file ID
* Multiple nodes can have the same file ID but different sources
* (e.g. in a shared context)
*/
getNodesById: (state) => (fileId: number): Node[] => Object.values(state.files).filter(node => node.fileid === fileId),

/**
* Get the root folder of a service
*/
Expand Down Expand Up @@ -73,7 +96,21 @@ export const useFilesStore = function(...args) {
this.updateNodes([node])
},

onUpdatedNode(node: Node) {
async onUpdatedNode(node: Node) {
if (!node.fileid) {
logger.error('Trying to update/set a node without fileid', { node })
return
}

// If we have multiple nodes with the same file ID, we need to update all of them
const nodes = this.getNodesById(node.fileid)
if (nodes.length > 1) {
await Promise.all(nodes.map(fetchNode)).then(this.updateNodes)
logger.debug(nodes.length + ' nodes updated in store', { fileid: node.fileid })
return
}

// Otherwise, update the single node
this.updateNodes([node])
},
},
Expand Down

0 comments on commit eddf1f7

Please sign in to comment.