refactor(files): Migrate `favorites` view to `@nextcloud/files` functions and make it cancelable

Also this fixes the view being writeable

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
pull/45788/head
Ferdinand Thiessen 2024-06-10 23:04:59 +07:00
parent b1444e78e2
commit de2276e6d7
No known key found for this signature in database
GPG Key ID: 45FAE7268762B400
2 changed files with 35 additions and 40 deletions

@ -3,44 +3,38 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { ContentsWithRoot } from '@nextcloud/files'
import type { FileStat, ResponseDataDetailed } from 'webdav'
import { Folder, davGetDefaultPropfind, davGetFavoritesReport } from '@nextcloud/files'
import { getCurrentUser } from '@nextcloud/auth'
import { Folder, Permission, davRemoteURL, davRootPath, getFavoriteNodes } from '@nextcloud/files'
import { CancelablePromise } from 'cancelable-promise'
import { getContents as filesContents } from './Files.ts'
import { client } from './WebdavClient.ts'
import { getClient } from './WebdavClient'
import { resultToNode } from './Files'
const client = getClient()
export const getContents = async (path = '/'): Promise<ContentsWithRoot> => {
const propfindPayload = davGetDefaultPropfind()
const reportPayload = davGetFavoritesReport()
// Get root folder
let rootResponse
if (path === '/') {
rootResponse = await client.stat(path, {
details: true,
data: propfindPayload,
}) as ResponseDataDetailed<FileStat>
export const getContents = (path = '/'): CancelablePromise<ContentsWithRoot> => {
// We only filter root files for favorites, for subfolders we can simply reuse the files contents
if (path !== '/') {
return filesContents(path)
}
const contentsResponse = await client.getDirectoryContents(path, {
details: true,
// Only filter favorites if we're at the root
data: path === '/' ? reportPayload : propfindPayload,
headers: {
// Patched in WebdavClient.ts
method: path === '/' ? 'REPORT' : 'PROPFIND',
},
includeSelf: true,
}) as ResponseDataDetailed<FileStat[]>
const root = rootResponse?.data || contentsResponse.data[0]
const contents = contentsResponse.data.filter(node => node.filename !== path)
return {
folder: resultToNode(root) as Folder,
contents: contents.map(resultToNode),
}
return new CancelablePromise((resolve, reject, cancel) => {
const promise = getFavoriteNodes(client)
.catch(reject)
.then((contents) => {
if (!contents) {
reject()
return
}
resolve({
contents,
folder: new Folder({
id: 0,
source: `${davRemoteURL}${davRootPath}`,
root: davRootPath,
owner: getCurrentUser()?.uid || null,
permissions: Permission.READ,
}),
})
})
cancel(() => promise.cancel())
})
}

@ -6,6 +6,7 @@
import { basename } from 'path'
import { expect } from '@jest/globals'
import { Folder, Navigation, getNavigation } from '@nextcloud/files'
import { CancelablePromise } from 'cancelable-promise'
import eventBus from '@nextcloud/event-bus'
import * as initialState from '@nextcloud/initial-state'
@ -40,7 +41,7 @@ describe('Favorites view definition', () => {
test('Default empty favorite view', () => {
jest.spyOn(eventBus, 'subscribe')
jest.spyOn(favoritesService, 'getContents').mockReturnValue(Promise.resolve({ folder: {} as Folder, contents: [] }))
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as Folder, contents: [] }))
registerFavoritesView()
const favoritesView = Navigation.views.find(view => view.id === 'favorites')
@ -71,7 +72,7 @@ describe('Favorites view definition', () => {
{ fileid: 3, path: '/foo/bar' },
]
jest.spyOn(initialState, 'loadState').mockReturnValue(favoriteFolders)
jest.spyOn(favoritesService, 'getContents').mockReturnValue(Promise.resolve({ folder: {} as Folder, contents: [] }))
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as Folder, contents: [] }))
registerFavoritesView()
const favoritesView = Navigation.views.find(view => view.id === 'favorites')
@ -114,7 +115,7 @@ describe('Dynamic update of favourite folders', () => {
test('Add a favorite folder creates a new entry in the navigation', async () => {
jest.spyOn(eventBus, 'emit')
jest.spyOn(initialState, 'loadState').mockReturnValue([])
jest.spyOn(favoritesService, 'getContents').mockReturnValue(Promise.resolve({ folder: {} as Folder, contents: [] }))
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as Folder, contents: [] }))
registerFavoritesView()
const favoritesView = Navigation.views.find(view => view.id === 'favorites')
@ -143,7 +144,7 @@ describe('Dynamic update of favourite folders', () => {
jest.spyOn(eventBus, 'emit')
jest.spyOn(eventBus, 'subscribe')
jest.spyOn(initialState, 'loadState').mockReturnValue([{ fileid: 42, path: '/Foo/Bar' }])
jest.spyOn(favoritesService, 'getContents').mockReturnValue(Promise.resolve({ folder: {} as Folder, contents: [] }))
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as Folder, contents: [] }))
registerFavoritesView()
let favoritesView = Navigation.views.find(view => view.id === 'favorites')