From 47cd7a5a372f89c11c798b997d66314fa58c8a73 Mon Sep 17 00:00:00 2001 From: Richard Steinmetz Date: Fri, 26 Apr 2024 13:29:01 +0200 Subject: [PATCH] feat: find principal collections Signed-off-by: Richard Steinmetz --- src/index.js | 31 +++++++++++++++++++++++++++++++ src/models/principal.js | 13 +++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/index.js b/src/index.js index d2642a16..44f30977 100644 --- a/src/index.js +++ b/src/index.js @@ -4,6 +4,7 @@ * This library is part of the Nextcloud project * * @author Georg Ehrke + * @author Richard Steinmetz * @copyright 2019 Georg Ehrke * * This library is free software; you can redistribute it and/or @@ -445,10 +446,40 @@ export default class DavClient { return this._request.propFind(principalUrl, Principal.getPropFindList()).then(({ body }) => { return new Principal(null, this._request, principalUrl, body) }).catch((err) => { + // TODO: improve error handling console.debug(err) }) } + /** + * finds all principals in a collection at a given principalCollectionUrl + * + * @param {string} principalCollectionUrl + * @param {import('./models/principal.js').PrincipalPropfindOptions} options Passed to Principal.getPropFindList() + * @return {Promise} + */ + async findPrincipalsInCollection(principalCollectionUrl, options = {}) { + try { + const { body } = await this._request.propFind( + principalCollectionUrl, + Principal.getPropFindList(options), + 1, + ) + const principals = Object.entries(body) + .filter(([principalUrl]) => !principalCollectionUrl.endsWith(principalUrl)) + .map(([principalUrl, principal]) => new Principal( + null, + this._request, + principalUrl, + principal, + )) + return principals + } catch (err) { + // TODO: improve error handling + console.debug(err) + } + } + /** * discovers the accounts principal uri solely based on rootURL * diff --git a/src/models/principal.js b/src/models/principal.js index 6ab36fe5..38af23a2 100644 --- a/src/models/principal.js +++ b/src/models/principal.js @@ -28,6 +28,13 @@ import * as XMLUtility from '../utility/xmlUtility.js' import prinicipalPropSet from '../propset/principalPropSet.js' +/** + * @typedef {object} PrincipalPropfindOptions + * @property {boolean=} PrincipalPropfindOptions.enableCalDAV + * @property {boolean=} PrincipalPropfindOptions.enableCalDAVResourceBooking + * @property {boolean=} PrincipalPropfindOptions.enableCardDAV + */ + /** * @class */ @@ -178,6 +185,8 @@ export class Principal extends DavObject { /** * @inheritDoc + * + * @param {PrincipalPropfindOptions} options */ static getPropFindList(options = {}) { const list = [ @@ -196,6 +205,10 @@ export class Principal extends DavObject { [NS.IETF_CALDAV, 'schedule-inbox-URL'], [NS.IETF_CALDAV, 'schedule-outbox-URL'], [NS.IETF_CALDAV, 'schedule-default-calendar-URL'], + ) + } + if (options.enableCalDAVResourceBooking || options.enableCalDAV) { + list.push( // Room and Resource booking related [NS.NEXTCLOUD, 'resource-type'], [NS.NEXTCLOUD, 'resource-vehicle-type'],