diff --git a/lib/usables/dav.spec.ts b/lib/usables/dav.spec.ts index 4645a641..845ffda3 100644 --- a/lib/usables/dav.spec.ts +++ b/lib/usables/dav.spec.ts @@ -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) @@ -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 () => { @@ -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) @@ -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() diff --git a/lib/usables/dav.ts b/lib/usables/dav.ts index 0f8d0f55..b1a365e1 100644 --- a/lib/usables/dav.ts +++ b/lib/usables/dav.ts @@ -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' @@ -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 | ComputedRef): { isLoading: Ref, client: WebDAVClient, files: Ref, loadFiles: () => void, getFile: (path: string) => Promise } { +export const useDAVFiles = function( + currentView: Ref<'files'|'recent'|'favorites'> | ComputedRef<'files'|'recent'|'favorites'>, + currentPath: Ref | ComputedRef, +): { isLoading: Ref, client: WebDAVClient, files: Ref, loadFiles: () => void, getFile: (path: string) => Promise } { /** * The WebDAV client */ @@ -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 - - files.value = results.data.map((r) => davResultToNode(r)) + }) as ResponseDataDetailed + files.value = data.results.map((r) => davResultToNode(r)) } else { const results = await client.getDirectoryContents(`${davRootPath}${currentPath.value}`, { details: true,