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

fix(FilePicker): Use search function from webdav package instead of workaround #992

Merged
merged 1 commit into from
Sep 13, 2023
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
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
Loading