From bd8a3e406de74619ac338d7a5c50ec02dfae2eab Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 28 Jul 2023 16:16:01 +0200 Subject: [PATCH] feat(dav): Add "recent files" SEARCH payload as an export Signed-off-by: Ferdinand Thiessen --- __tests__/dav/davProperties.spec.ts | 6 ++ lib/dav/davProperties.ts | 88 ++++++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 3 deletions(-) diff --git a/__tests__/dav/davProperties.spec.ts b/__tests__/dav/davProperties.spec.ts index 312c306c8..5fedfdb6c 100644 --- a/__tests__/dav/davProperties.spec.ts +++ b/__tests__/dav/davProperties.spec.ts @@ -9,6 +9,7 @@ import { registerDavProperty, defaultDavNamespaces, defaultDavProperties, + davGetRecentSearch, } from '../../lib/dav/davProperties' import logger from '../../lib/utils/logger' @@ -44,6 +45,11 @@ describe('DAV Properties', () => { expect(XMLValidator.validate(davGetFavoritesReport())).toBe(true) }) + test('davGetFavoritesReport', () => { + expect(typeof davGetRecentSearch(1337)).toBe('string') + expect(XMLValidator.validate(davGetRecentSearch(1337))).toBe(true) + }) + test('registerDavProperty registers successfully', () => { logger.error = vi.fn() diff --git a/lib/dav/davProperties.ts b/lib/dav/davProperties.ts index 5686aec80..7cfa55edb 100644 --- a/lib/dav/davProperties.ts +++ b/lib/dav/davProperties.ts @@ -20,6 +20,7 @@ * along with this program. If not, see . * */ +import { getCurrentUser } from '@nextcloud/auth' import logger from '../utils/logger' export type DavProperty = { [key: string]: string } @@ -70,7 +71,7 @@ export const registerDavProperty = function(prop: string, namespace: DavProperty const namespaces = { ...window._nc_dav_namespaces, ...namespace } // Check duplicates - if (window._nc_dav_properties.find(search => search === prop)) { + if (window._nc_dav_properties.find((search) => search === prop)) { logger.error(`${prop} already registered`, { prop }) return false } @@ -99,7 +100,7 @@ export const getDavProperties = function(): string { window._nc_dav_properties = [...defaultDavProperties] } - return window._nc_dav_properties.map(prop => `<${prop} />`).join(' ') + return window._nc_dav_properties.map((prop) => `<${prop} />`).join(' ') } /** @@ -110,7 +111,9 @@ export const getDavNameSpaces = function(): string { window._nc_dav_namespaces = { ...defaultDavNamespaces } } - return Object.keys(window._nc_dav_namespaces).map(ns => `xmlns:${ns}="${window._nc_dav_namespaces?.[ns]}"`).join(' ') + return Object.keys(window._nc_dav_namespaces) + .map((ns) => `xmlns:${ns}="${window._nc_dav_namespaces?.[ns]}"`) + .join(' ') } /** @@ -139,3 +142,82 @@ export const davGetFavoritesReport = function(): string { ` } + +/** + * Get the SEARCH body to search for recently modified files + * + * @param lastModified Oldest timestamp to include (Unix timestamp) + * @example + * ```ts + * // SEARCH for recent files need a different DAV endpoint + * const client = davGetClient(generateRemoteUrl('dav')) + * // Timestamp of last week + * const lastWeek = Math.round(Date.now() / 1000) - (60 * 60 * 24 * 7) + * const contentsResponse = await client.getDirectoryContents(path, { + * details: true, + * data: davGetRecentSearch(lastWeek), + * headers: { + * method: 'SEARCH', + * 'Content-Type': 'application/xml; charset=utf-8', + * }, + * deep: true, + * }) as ResponseDataDetailed + * ``` + */ +export const davGetRecentSearch = function(lastModified: number): string { + return ` + + + + + ${getDavProperties()} + + + + + /files/${getCurrentUser()?.uid}/ + infinity + + + + + + + + + + + httpd/unix-directory + + + + + + + 0 + + + + + + + ${lastModified} + + + + + + + + + + + + + 100 + 0 + + +` +}