Skip to content

Commit

Permalink
Merge pull request #890 from nextcloud/feat/find-principal-collections
Browse files Browse the repository at this point in the history
feat: find principal collections
  • Loading branch information
st3iny authored Jun 10, 2024
2 parents 486fea8 + 47cd7a5 commit b078ef9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* This library is part of the Nextcloud project
*
* @author Georg Ehrke
* @author Richard Steinmetz <richard@steinmetz.cloud>
* @copyright 2019 Georg Ehrke <oc.list@georgehrke.com>
*
* This library is free software; you can redistribute it and/or
Expand Down Expand Up @@ -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<Principal[]>}
*/
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
*
Expand Down
13 changes: 13 additions & 0 deletions src/models/principal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -178,6 +185,8 @@ export class Principal extends DavObject {

/**
* @inheritDoc
*
* @param {PrincipalPropfindOptions} options
*/
static getPropFindList(options = {}) {
const list = [
Expand All @@ -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'],
Expand Down

0 comments on commit b078ef9

Please sign in to comment.