Skip to content

Commit 85cbf28

Browse files
committed
Signed-off-by: Grigory Vodyanov <scratchx@gmx.com>
1 parent c0822ce commit 85cbf28

1 file changed

Lines changed: 34 additions & 16 deletions

File tree

src/index.js

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -578,45 +578,63 @@ export default class DavClient {
578578
}
579579

580580
/**
581-
* Returns the absolute URLs of all write-delegates for a principal.
581+
* Returns the absolute URL of a calendar proxy group for a given principal.
582582
*
583-
* Delegates are members of the principal's calendar-proxy-write group.
583+
* @param {string} principalUrl Absolute URL of the principal
584+
* @param {'write'|'read'} type The proxy group type
585+
* @return {string}
586+
* @private
587+
*/
588+
_getProxyGroupUrl(principalUrl, type) {
589+
return principalUrl.replace(/\/?$/, '') + '/calendar-proxy-' + type
590+
}
591+
592+
/**
593+
* Returns the absolute URLs of all delegates for a principal, separated by permission level.
584594
*
585-
* @param {string} proxyWriteGroupUrl Absolute URL of the principal's calendar-proxy-write group
586-
* @return {Promise<string[]>} Absolute principal URLs of delegates
595+
* @param {string} principalUrl Absolute URL of the principal
596+
* @return {Promise<{write: string[], read: string[]}>} Absolute principal URLs of delegates
587597
*/
588-
async getDelegateUrls(proxyWriteGroupUrl) {
589-
return this.getGroupMemberSet(proxyWriteGroupUrl)
598+
async getDelegatesForPrincipal(principalUrl) {
599+
const [write, read] = await Promise.all([
600+
this.getGroupMemberSet(this._getProxyGroupUrl(principalUrl, 'write')),
601+
this.getGroupMemberSet(this._getProxyGroupUrl(principalUrl, 'read')),
602+
])
603+
return { write, read }
590604
}
591605

592606
/**
593-
* Adds a delegate to a calendar-proxy-write group.
607+
* Adds a delegate to a principal's calendar-proxy group.
594608
*
595609
* Fetches the current member set and appends the new delegate if not already present.
596610
*
597-
* @param {string} proxyWriteGroupUrl Absolute URL of the principal's calendar-proxy-write group
611+
* @param {string} ownerPrincipalUrl Absolute URL of the principal who owns the proxy group
598612
* @param {string} delegatePrincipalUrl Absolute or relative URL of the principal to add as delegate
613+
* @param {'write'|'read'} [permission='write'] The proxy group to add the delegate to
599614
* @return {Promise<void>}
600615
*/
601-
async addDelegate(proxyWriteGroupUrl, delegatePrincipalUrl) {
616+
async addDelegate(ownerPrincipalUrl, delegatePrincipalUrl, permission = 'write') {
617+
const proxyGroupUrl = this._getProxyGroupUrl(ownerPrincipalUrl, permission)
602618
const normalizedUrl = this._request.absoluteUrl(delegatePrincipalUrl)
603-
const current = await this.getGroupMemberSet(proxyWriteGroupUrl)
619+
const current = await this.getGroupMemberSet(proxyGroupUrl)
604620
if (!current.includes(normalizedUrl)) {
605-
await this.setGroupMemberSet(proxyWriteGroupUrl, [...current, normalizedUrl])
621+
await this.setGroupMemberSet(proxyGroupUrl, [...current, normalizedUrl])
606622
}
607623
}
608624

609625
/**
610-
* Removes a delegate from a calendar-proxy-write group.
626+
* Removes a delegate from a principal's calendar-proxy group.
611627
*
612-
* @param {string} proxyWriteGroupUrl Absolute URL of the principal's calendar-proxy-write group
628+
* @param {string} ownerPrincipalUrl Absolute URL of the principal who owns the proxy group
613629
* @param {string} delegatePrincipalUrl Absolute or relative URL of the principal to remove
630+
* @param {'write'|'read'} [permission='write'] The proxy group to remove the delegate from
614631
* @return {Promise<void>}
615632
*/
616-
async removeDelegate(proxyWriteGroupUrl, delegatePrincipalUrl) {
633+
async removeDelegate(ownerPrincipalUrl, delegatePrincipalUrl, permission = 'write') {
634+
const proxyGroupUrl = this._getProxyGroupUrl(ownerPrincipalUrl, permission)
617635
const normalizedUrl = this._request.absoluteUrl(delegatePrincipalUrl)
618-
const current = await this.getGroupMemberSet(proxyWriteGroupUrl)
619-
await this.setGroupMemberSet(proxyWriteGroupUrl, current.filter((url) => url !== normalizedUrl))
636+
const current = await this.getGroupMemberSet(proxyGroupUrl)
637+
await this.setGroupMemberSet(proxyGroupUrl, current.filter((url) => url !== normalizedUrl))
620638
}
621639

622640
/**

0 commit comments

Comments
 (0)