Skip to content

Commit

Permalink
fix(FilePicker): Use search function from webdav package instead …
Browse files Browse the repository at this point in the history
…of workaround

Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Sep 12, 2023
1 parent e409a98 commit 26c62b2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
10 changes: 7 additions & 3 deletions lib/usables/dav.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ describe('dav usable', () => {
it('reloads on view change', async () => {
const client = {
getDirectoryContents: vi.fn((str) => ({ data: [] })),
search: vi.fn((str) => ({ data: { results: [], truncated: false } })),
}
nextcloudFiles.davGetClient.mockImplementationOnce(() => client)

Expand All @@ -143,13 +144,16 @@ describe('dav usable', () => {
// wait until files are loaded
await waitLoaded(vue)

expect(client.search).not.toBeCalled()
expect(client.getDirectoryContents).toBeCalledTimes(1)
expect(client.getDirectoryContents.mock.calls[0][0]).toBe(`${nextcloudFiles.davRootPath}/`)

vue.setProps({ currentView: 'recent' })
await waitLoaded(vue)

expect(client.getDirectoryContents).toBeCalledTimes(2)
// Uses search instead of getDirectoryContents
expect(client.getDirectoryContents).toBeCalledTimes(1)
expect(client.search).toBeCalledTimes(1)
})

it('getFile works', async () => {
Expand All @@ -172,6 +176,7 @@ describe('dav usable', () => {
const client = {
stat: vi.fn((v) => ({ data: { path: v } })),
getDirectoryContents: vi.fn((p, o) => ({ data: [] })),
search: vi.fn((p, o) => ({ data: { results: [], truncated: false } })),
}
nextcloudFiles.davGetClient.mockImplementationOnce(() => client)
nextcloudFiles.davResultToNode.mockImplementationOnce((v) => v)
Expand All @@ -188,8 +193,7 @@ describe('dav usable', () => {
view.value = 'recent'
await loadFiles()
expect(isLoading.value).toBe(false)
expect(client.getDirectoryContents).toBeCalledWith(`${nextcloudFiles.davRootPath}/`, { details: true })
expect(client.getDirectoryContents.mock.calls.at(-1)?.[1]?.data).toMatch('recent')
expect(client.search).toBeCalled()

view.value = 'favorites'
await loadFiles()
Expand Down
19 changes: 8 additions & 11 deletions lib/usables/dav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
import type { Node } from '@nextcloud/files'
import type { ComputedRef, Ref } from 'vue'
import type { FileStat, ResponseDataDetailed, WebDAVClient } from 'webdav'
import type { FileStat, ResponseDataDetailed, SearchResult, WebDAVClient } from 'webdav'

import { davGetClient, davGetDefaultPropfind, davGetRecentSearch, davResultToNode, davRootPath, getFavoriteNodes } from '@nextcloud/files'
import { onMounted, ref, watch } from 'vue'
Expand All @@ -32,7 +32,10 @@ import { onMounted, ref, watch } from 'vue'
* @param currentView Reference to the current files view
* @param currentPath Reference to the current files path
*/
export const useDAVFiles = function(currentView: Ref<'files'|'recent'|'favorites'> | ComputedRef<'files'|'recent'|'favorites'>, currentPath: Ref<string> | ComputedRef<string>): { isLoading: Ref<boolean>, client: WebDAVClient, files: Ref<Node[]>, loadFiles: () => void, getFile: (path: string) => Promise<Node> } {
export const useDAVFiles = function(
currentView: Ref<'files'|'recent'|'favorites'> | ComputedRef<'files'|'recent'|'favorites'>,
currentPath: Ref<string> | ComputedRef<string>,
): { isLoading: Ref<boolean>, client: WebDAVClient, files: Ref<Node[]>, loadFiles: () => void, getFile: (path: string) => Promise<Node> } {
/**
* The WebDAV client
*/
Expand Down Expand Up @@ -72,17 +75,11 @@ export const useDAVFiles = function(currentView: Ref<'files'|'recent'|'favorites
} else if (currentView.value === 'recent') {
// unix timestamp in seconds, two weeks ago
const lastTwoWeek = Math.round(Date.now() / 1000) - (60 * 60 * 24 * 14)
const results = await client.getDirectoryContents(currentPath.value, {
const { data } = await client.search('/', {
details: true,
data: davGetRecentSearch(lastTwoWeek),
headers: {
method: 'SEARCH',
'Content-Type': 'application/xml; charset=utf-8',
},
deep: true,
}) as ResponseDataDetailed<FileStat[]>

files.value = results.data.map((r) => davResultToNode(r))
}) as ResponseDataDetailed<SearchResult>
files.value = data.results.map((r) => davResultToNode(r))
} else {
const results = await client.getDirectoryContents(`${davRootPath}${currentPath.value}`, {
details: true,
Expand Down

0 comments on commit 26c62b2

Please sign in to comment.