diff options
author | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-06-10 22:55:57 +0200 |
---|---|---|
committer | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-06-11 15:45:22 +0200 |
commit | b1444e78e257c12729147a881e8c7e854c5ea0ed (patch) | |
tree | b1c7c39200af079c3bc92f251384cc0c5200d45d /apps | |
parent | 85e6cb4cccac2e0108c5053360d7d80049e34fd8 (diff) | |
download | nextcloud-server-b1444e78e257c12729147a881e8c7e854c5ea0ed.tar.gz nextcloud-server-b1444e78e257c12729147a881e8c7e854c5ea0ed.zip |
refactor(files): Migrate `recent` view to `@nextcloud/files` functions and make it cancelable
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
Diffstat (limited to 'apps')
-rw-r--r-- | apps/files/src/services/Recent.ts | 62 |
1 files changed, 34 insertions, 28 deletions
diff --git a/apps/files/src/services/Recent.ts b/apps/files/src/services/Recent.ts index 68e66ed121c..c8cde136069 100644 --- a/apps/files/src/services/Recent.ts +++ b/apps/files/src/services/Recent.ts @@ -3,14 +3,15 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ import type { ContentsWithRoot, Node } from '@nextcloud/files' -import type { FileStat, ResponseDataDetailed } from 'webdav' +import type { ResponseDataDetailed, SearchResult } from 'webdav' import { getCurrentUser } from '@nextcloud/auth' -import { Folder, Permission, davGetRecentSearch, davGetClient, davResultToNode, davRootPath, davRemoteURL } from '@nextcloud/files' +import { Folder, Permission, davGetRecentSearch, davRootPath, davRemoteURL } from '@nextcloud/files' +import { CancelablePromise } from 'cancelable-promise' import { useUserConfigStore } from '../store/userconfig.ts' import { pinia } from '../store/index.ts' - -const client = davGetClient() +import { client } from './WebdavClient.ts' +import { resultToNode } from './Files.ts' const lastTwoWeeksTimestamp = Math.round((Date.now() / 1000) - (60 * 60 * 24 * 14)) @@ -22,8 +23,9 @@ const lastTwoWeeksTimestamp = Math.round((Date.now() / 1000) - (60 * 60 * 24 * 1 * * @param path Path to search for recent changes */ -export const getContents = async (path = '/'): Promise<ContentsWithRoot> => { +export const getContents = (path = '/'): CancelablePromise<ContentsWithRoot> => { const store = useUserConfigStore(pinia) + /** * Filter function that returns only the visible nodes - or hidden if explicitly configured * @param node The node to check @@ -33,28 +35,32 @@ export const getContents = async (path = '/'): Promise<ContentsWithRoot> => { || store.userConfig.show_hidden // If configured to show hidden files we can early return || !node.dirname.split('/').some((dir) => dir.startsWith('.')) // otherwise only include the file if non of the parent directories is hidden - const contentsResponse = await client.getDirectoryContents(path, { - details: true, - data: davGetRecentSearch(lastTwoWeeksTimestamp), - headers: { - // Patched in WebdavClient.ts - method: 'SEARCH', - // Somehow it's needed to get the correct response - 'Content-Type': 'application/xml; charset=utf-8', - }, - deep: true, - }) as ResponseDataDetailed<FileStat[]> - - const contents = contentsResponse.data - - return { - folder: new Folder({ - id: 0, - source: `${davRemoteURL}${davRootPath}`, - root: davRootPath, - owner: getCurrentUser()?.uid || null, - permissions: Permission.READ, - }), - contents: contents.map((r) => davResultToNode(r)).filter(filterHidden), + const controller = new AbortController() + const handler = async () => { + const contentsResponse = await client.search('/', { + signal: controller.signal, + details: true, + data: davGetRecentSearch(lastTwoWeeksTimestamp), + }) as ResponseDataDetailed<SearchResult> + + const contents = contentsResponse.data.results + .map(resultToNode) + .filter(filterHidden) + + return { + folder: new Folder({ + id: 0, + source: `${davRemoteURL}${davRootPath}`, + root: davRootPath, + owner: getCurrentUser()?.uid || null, + permissions: Permission.READ, + }), + contents, + } } + + return new CancelablePromise(async (resolve, reject, cancel) => { + cancel(() => controller.abort()) + resolve(handler()) + }) } |