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

[stable29] fix(files): Correctly create Nodes from WebDAV result in "recent"-view #46951

Merged
merged 3 commits into from
Aug 8, 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
2 changes: 1 addition & 1 deletion apps/files/src/actions/deleteAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const action = new FileAction({
enabled(nodes: Node[]) {
return nodes.length > 0 && nodes
.map(node => node.permissions)
.every(permission => (permission & Permission.DELETE) !== 0)
.every(permission => Boolean(permission & Permission.DELETE))
},

async exec(node: Node, view: View) {
Expand Down
2 changes: 1 addition & 1 deletion apps/files/src/services/Files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
*/
import type { ContentsWithRoot } from '@nextcloud/files'
import type { FileStat, ResponseDataDetailed, DAVResultResponseProps } from 'webdav'
import type { FileStat, ResponseDataDetailed } from 'webdav'

import { CancelablePromise } from 'cancelable-promise'
import { File, Folder, davGetClient, davGetDefaultPropfind, davResultToNode, davRootPath } from '@nextcloud/files'
Expand Down
69 changes: 46 additions & 23 deletions apps/files/src/services/Recent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,27 @@
*
*/
import type { ContentsWithRoot, Node } from '@nextcloud/files'
import type { FileStat, ResponseDataDetailed } from 'webdav'
import type { FileStat, ResponseDataDetailed, SearchResult } from 'webdav'

import { getCurrentUser } from '@nextcloud/auth'
import { Folder, Permission, davGetRecentSearch, davGetClient, davResultToNode, davRootPath, davRemoteURL } from '@nextcloud/files'
import { getBaseUrl } from '@nextcloud/router'
import { CancelablePromise } from 'cancelable-promise'
import { useUserConfigStore } from '../store/userconfig.ts'
import { pinia } from '../store/index.ts'

const client = davGetClient()

const lastTwoWeeksTimestamp = Math.round((Date.now() / 1000) - (60 * 60 * 24 * 14))

/**
* Helper to map a WebDAV result to a Nextcloud node
* The search endpoint already includes the dav remote URL so we must not include it in the source
*
* @param stat the WebDAV result
*/
const resultToNode = (stat: FileStat) => davResultToNode(stat, davRootPath, getBaseUrl())

/**
* Get recently changed nodes
*
Expand All @@ -51,28 +61,41 @@ export const getContents = async (path = '/'): Promise<ContentsWithRoot> => {
|| store.userConfig.show_hidden // If configured to show hidden files we can early return
|| !node.dirname.split('/').some((dir) => dir.startsWith('.')) // otherwise only include the file if non of the parent directories is hidden

const contentsResponse = await client.getDirectoryContents(path, {
details: true,
data: davGetRecentSearch(lastTwoWeeksTimestamp),
headers: {
// Patched in WebdavClient.ts
method: 'SEARCH',
// Somehow it's needed to get the correct response
'Content-Type': 'application/xml; charset=utf-8',
},
deep: true,
}) as ResponseDataDetailed<FileStat[]>
const abort = new AbortController()

return new CancelablePromise(async (resolve, reject, cancel) => {
cancel(() => abort.abort())

let contentsResponse: ResponseDataDetailed<SearchResult>
try {
contentsResponse = await client.search('/', {
details: true,
data: davGetRecentSearch(lastTwoWeeksTimestamp),
signal: abort.signal,
}) as ResponseDataDetailed<SearchResult>
} catch (e) {
reject(e)
return
}

if (abort.signal.aborted) {
reject()
return
}

const contents = contentsResponse.data
const contents = contentsResponse.data.results
.map(resultToNode)
.filter(filterHidden)

return {
folder: new Folder({
id: 0,
source: `${davRemoteURL}${davRootPath}`,
root: davRootPath,
owner: getCurrentUser()?.uid || null,
permissions: Permission.READ,
}),
contents: contents.map((r) => davResultToNode(r)).filter(filterHidden),
}
resolve({
contents,
folder: new Folder({
id: 0,
source: `${davRemoteURL}${davRootPath}`,
root: davRootPath,
owner: getCurrentUser()?.uid || null,
permissions: Permission.READ,
}),
})
})
}
44 changes: 44 additions & 0 deletions cypress/e2e/files/recent-view.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { User } from '@nextcloud/cypress'
import { getRowForFile, triggerActionForFile } from './FilesUtils'

describe('files: Recent view', { testIsolation: true }, () => {
let user: User

beforeEach(() => cy.createRandomUser().then(($user) => {
user = $user

cy.uploadContent(user, new Blob([]), 'text/plain', '/file.txt')
cy.login(user)
}))

it('see the recently created file in the recent view', () => {
cy.visit('/apps/files/recent')
// All are visible by default
getRowForFile('file.txt').should('be.visible')
})

/**
* Regression test: There was a bug that the files were correctly loaded but with invalid source
* so the delete action failed.
*/
it('can delete a file in the recent view', () => {
cy.intercept('DELETE', '**/remote.php/dav/files/**').as('deleteFile')

cy.visit('/apps/files/recent')
// See the row
getRowForFile('file.txt').should('be.visible')
// delete the file
triggerActionForFile('file.txt', 'delete')
cy.wait('@deleteFile')
// See it is not visible anymore
getRowForFile('file.txt').should('not.exist')
// also not existing in default view after reload
cy.visit('/apps/files')
getRowForFile('file.txt').should('not.exist')
})
})
4 changes: 2 additions & 2 deletions dist/files-init.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/files-init.js.map

Large diffs are not rendered by default.

Loading