Skip to content

Commit

Permalink
refactor(files): Migrate favorites view to @nextcloud/files funct…
Browse files Browse the repository at this point in the history
…ions and make it cancelable

Also this fixes the view being writeable

Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Jun 11, 2024
1 parent b1444e7 commit de2276e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 40 deletions.
66 changes: 30 additions & 36 deletions apps/files/src/services/Favorites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}
9 changes: 5 additions & 4 deletions apps/files/src/views/favorites.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit de2276e

Please sign in to comment.