Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 185 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
export default class DavClient {

/**
* @param {object} options

Check warning on line 28 in src/index.js

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "options" description
* @param {string} options.rootUrl

Check warning on line 29 in src/index.js

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "options.rootUrl" description
* @param {{[name: string]: any}} [options.defaultHeaders] A dictionary of default headers to apply to each request.
* @param {object} factories

Check warning on line 31 in src/index.js

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "factories" description
*/
constructor(options, factories = {}) {
/**
Expand Down Expand Up @@ -111,7 +111,7 @@

/**
* initializes the DAVClient
* @param {object} options

Check warning on line 114 in src/index.js

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "options" description
* @return {Promise<DavClient>}
*/
async connect(options = { enableCalDAV: false, enableCardDAV: false }) {
Expand Down Expand Up @@ -176,7 +176,7 @@
/**
* performs a principal property search based on a principal's displayname
*
* @param {string} name

Check warning on line 179 in src/index.js

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "name" description
* @return {Promise<Principal[]>}
*/
async principalPropertySearchByDisplayname(name) {
Expand All @@ -188,7 +188,7 @@
/**
* performs a principal property search based on a principal's displayname OR email address
*
* @param {string} value

Check warning on line 191 in src/index.js

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "value" description
* @return {Promise<Principal[]>}
*/
async principalPropertySearchByDisplaynameOrEmail(value) {
Expand Down Expand Up @@ -374,8 +374,8 @@
* performs a principal property search
* @see https://tools.ietf.org/html/rfc3744#section-9.4
*
* @param {Array} props

Check warning on line 377 in src/index.js

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "props" description
* @param {string} match

Check warning on line 378 in src/index.js

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "match" description
* @param {string} test 'anyof', 'allof' or none
* @return {Promise<Principal[]>}
*/
Expand Down Expand Up @@ -485,6 +485,191 @@
return this._request.pathname(response.body['{DAV:}current-user-principal'].href)
}

/**
* Creates a CalendarHome instance for an arbitrary calendar home URL.
*
* This can be used to access calendars that are not in the current user's
* own calendar home – for example when acting as a calendar proxy (delegate)
* for another user.
*
* @param {string} calendarHomeUrl Absolute URL of the calendar home
* @return {CalendarHome}
*/
getCalendarHomeForUrl(calendarHomeUrl) {
const url = this._request.pathname(calendarHomeUrl)
return new CalendarHome(this, this._request, url, {})
}
Comment on lines +498 to +501
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the calendar homes are mapped to this.calendarHomes already, there should be no need to do this again


/**
* Fetches the group-member-set of a principal collection (e.g. a calendar-proxy group).
* @see https://tools.ietf.org/html/rfc3744#section-4.3
*
* @param {string} groupUrl Absolute URL of the proxy group principal
* @return {Promise<string[]>} Absolute URLs of member principals
*/
async getGroupMemberSet(groupUrl) {
const { body } = await this._request.propFind(groupUrl, [
[NS.DAV, 'group-member-set'],
])
const members = body[`{${NS.DAV}}group-member-set`] ?? []
return members.map((href) => this._request.absoluteUrl(href))
}

/**
* Sets the group-member-set of a principal collection (e.g. a calendar-proxy group).
* @see https://tools.ietf.org/html/rfc3744#section-4.3
*
* @param {string} groupUrl Absolute URL of the proxy group principal
* @param {string[]} memberUrls Absolute URLs of the new member set
* @return {Promise<void>}
*/
async setGroupMemberSet(groupUrl, memberUrls) {
const [skeleton] = XMLUtility.getRootSkeleton([NS.DAV, 'propertyupdate'])
skeleton.children.push({
name: [NS.DAV, 'set'],
children: [{
name: [NS.DAV, 'prop'],
children: [{
name: [NS.DAV, 'group-member-set'],
children: memberUrls.map((url) => ({
name: [NS.DAV, 'href'],
value: url,
})),
}],
}],
})
const body = XMLUtility.serialize(skeleton)
await this._request.propPatch(groupUrl, {}, body)
}

/**
* Fetches the group-membership of a principal (the groups it belongs to).
* @see https://tools.ietf.org/html/rfc3744#section-4.4
*
* @param {string} principalUrl Absolute URL of the principal
* @return {Promise<string[]>} Absolute URLs of groups the principal belongs to
*/
async getGroupMembership(principalUrl) {
const { body } = await this._request.propFind(principalUrl, [
[NS.DAV, 'group-membership'],
])
const groups = body[`{${NS.DAV}}group-membership`] ?? []
return groups.map((href) => this._request.absoluteUrl(href))
}

/**
* Discovers the calendar home URL for a principal via CalDAV PROPFIND.
*
* Performs a depth-0 PROPFIND on the principal URL requesting the
* calendar-home-set property (RFC 4791 §6.2.1).
*
* @param {string} principalUrl Absolute URL of the principal
* @return {Promise<string|null>} Absolute URL of the calendar home, or null if not found
*/
async getCalendarHomeUrlForPrincipal(principalUrl) {
const { body } = await this._request.propFind(principalUrl, [
[NS.IETF_CALDAV, 'calendar-home-set'],
])
const homes = body[`{${NS.IETF_CALDAV}}calendar-home-set`]
if (!homes || !homes.length) {
return null
}
return this._request.absoluteUrl(homes[0])
}
Comment on lines +569 to +578
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already do this in _extractCalendarHomes (at the bottom) all the homes are map to this.calendarHomes


/**
* Returns the absolute URLs of all write-delegates for a principal.
*
* Delegates are members of the principal's calendar-proxy-write group.
*
* @param {string} proxyWriteGroupUrl Absolute URL of the principal's calendar-proxy-write group
* @return {Promise<string[]>} Absolute principal URLs of delegates
*/
async getDelegateUrls(proxyWriteGroupUrl) {
return this.getGroupMemberSet(proxyWriteGroupUrl)
}

/**
* Adds a delegate to a calendar-proxy-write group.
*
* Fetches the current member set and appends the new delegate if not already present.
*
* @param {string} proxyWriteGroupUrl Absolute URL of the principal's calendar-proxy-write group
* @param {string} delegatePrincipalUrl Absolute or relative URL of the principal to add as delegate
* @return {Promise<void>}
*/
async addDelegate(proxyWriteGroupUrl, delegatePrincipalUrl) {
const normalizedUrl = this._request.absoluteUrl(delegatePrincipalUrl)
const current = await this.getGroupMemberSet(proxyWriteGroupUrl)
if (!current.includes(normalizedUrl)) {
await this.setGroupMemberSet(proxyWriteGroupUrl, [...current, normalizedUrl])
}
}

/**
* Removes a delegate from a calendar-proxy-write group.
*
* @param {string} proxyWriteGroupUrl Absolute URL of the principal's calendar-proxy-write group
* @param {string} delegatePrincipalUrl Absolute or relative URL of the principal to remove
* @return {Promise<void>}
*/
async removeDelegate(proxyWriteGroupUrl, delegatePrincipalUrl) {
const normalizedUrl = this._request.absoluteUrl(delegatePrincipalUrl)
const current = await this.getGroupMemberSet(proxyWriteGroupUrl)
await this.setGroupMemberSet(proxyWriteGroupUrl, current.filter((url) => url !== normalizedUrl))
}

/**
* Returns the principal URLs of users who have granted the given principal
* write-proxy (delegate) access.
*
* Inspects the group-membership property for groups ending in
* /calendar-proxy-write and strips that suffix to obtain the owner's
* principal URL.
*
* @param {string} principalUrl Absolute URL of the principal
* @return {Promise<string[]>} Absolute principal URLs of users who delegated to this principal
*/
async getDelegatorPrincipalUrls(principalUrl) {
const groups = await this.getGroupMembership(principalUrl)
return groups
.filter((url) => url.includes('calendar-proxy-write'))
.map((url) => url.replace(/\/calendar-proxy-write\/?$/, '') || null)
.filter(Boolean)
}

/**
* Returns the principal URLs and permission level of users who have granted
* the given principal proxy access (both read and write).
*
* Inspects the group-membership property for groups ending in
* /calendar-proxy-write or /calendar-proxy-read and returns objects with
* the owner's principal URL and the permission granted.
*
* @param {string} principalUrl Absolute URL of the principal
* @return {Promise<Array<{principalUrl: string, permission: 'write'|'read'}>>}
*/
async getDelegatorsWithPermission(principalUrl) {
const groups = await this.getGroupMembership(principalUrl)
const result = []

for (const groupUrl of groups) {
if (groupUrl.includes('calendar-proxy-write')) {
const ownerUrl = groupUrl.replace(/\/calendar-proxy-write\/?$/, '')
if (ownerUrl) {
result.push({ principalUrl: ownerUrl, permission: 'write' })
}
} else if (groupUrl.includes('calendar-proxy-read')) {
const ownerUrl = groupUrl.replace(/\/calendar-proxy-read\/?$/, '')
if (ownerUrl) {
result.push({ principalUrl: ownerUrl, permission: 'read' })
}
}
}

return result
}

/**
* discovers all calendar-homes in this account, all principal collections
* and advertised features
Expand Down
Loading