caldav: get calendar data using PROPFIND

This change adds FindCalendar function to the caldav client. It can be
used to get calendar information without loading all the events, once the
path of the calendar is known.
This commit is contained in:
Gabor Pihaj 2025-12-19 22:45:26 +00:00 committed by Simon Ser
parent 8888fdf9b0
commit 1916c2d907

View File

@ -3,6 +3,7 @@ package caldav
import ( import (
"bytes" "bytes"
"context" "context"
"encoding/xml"
"errors" "errors"
"fmt" "fmt"
"mime" "mime"
@ -17,6 +18,14 @@ import (
"github.com/emersion/go-webdav/internal" "github.com/emersion/go-webdav/internal"
) )
var calendarProps = []xml.Name{
internal.ResourceTypeName,
internal.DisplayNameName,
calendarDescriptionName,
maxResourceSizeName,
supportedCalendarComponentSetName,
}
// DiscoverContextURL performs a DNS-based CardDAV service discovery as // DiscoverContextURL performs a DNS-based CardDAV service discovery as
// described in RFC 6352 section 11. It returns the URL to the CardDAV server. // described in RFC 6352 section 11. It returns the URL to the CardDAV server.
func DiscoverContextURL(ctx context.Context, domain string) (string, error) { func DiscoverContextURL(ctx context.Context, domain string) (string, error) {
@ -42,6 +51,22 @@ func NewClient(c webdav.HTTPClient, endpoint string) (*Client, error) {
return &Client{wc, ic}, nil return &Client{wc, ic}, nil
} }
// Get calendar information without loading all the events once the path of the calendar is known.
func (c *Client) FindCalendar(ctx context.Context, path string) (*Calendar, error) {
propfind := internal.NewPropNamePropFind(calendarProps...)
resp, err := c.ic.PropFindFlat(ctx, path, propfind)
if err != nil {
return nil, err
}
calendar, err := decodeCalendar(path, resp)
if err != nil {
return nil, err
}
return calendar, nil
}
func (c *Client) FindCalendarHomeSet(ctx context.Context, principal string) (string, error) { func (c *Client) FindCalendarHomeSet(ctx context.Context, principal string) (string, error) {
propfind := internal.NewPropNamePropFind(calendarHomeSetName) propfind := internal.NewPropNamePropFind(calendarHomeSetName)
resp, err := c.ic.PropFindFlat(ctx, principal, propfind) resp, err := c.ic.PropFindFlat(ctx, principal, propfind)
@ -58,13 +83,7 @@ func (c *Client) FindCalendarHomeSet(ctx context.Context, principal string) (str
} }
func (c *Client) FindCalendars(ctx context.Context, calendarHomeSet string) ([]Calendar, error) { func (c *Client) FindCalendars(ctx context.Context, calendarHomeSet string) ([]Calendar, error) {
propfind := internal.NewPropNamePropFind( propfind := internal.NewPropNamePropFind(calendarProps...)
internal.ResourceTypeName,
internal.DisplayNameName,
calendarDescriptionName,
maxResourceSizeName,
supportedCalendarComponentSetName,
)
ms, err := c.ic.PropFind(ctx, calendarHomeSet, internal.DepthOne, propfind) ms, err := c.ic.PropFind(ctx, calendarHomeSet, internal.DepthOne, propfind)
if err != nil { if err != nil {
return nil, err return nil, err
@ -87,6 +106,18 @@ func (c *Client) FindCalendars(ctx context.Context, calendarHomeSet string) ([]C
continue continue
} }
calendar, err := decodeCalendar(path, &resp)
if err != nil {
return nil, err
}
l = append(l, *calendar)
}
return l, errors.Join(errs...)
}
func decodeCalendar(path string, resp *internal.Response) (*Calendar, error) {
var desc calendarDescription var desc calendarDescription
if err := resp.DecodeProp(&desc); err != nil && !internal.IsNotFound(err) { if err := resp.DecodeProp(&desc); err != nil && !internal.IsNotFound(err) {
return nil, err return nil, err
@ -115,16 +146,13 @@ func (c *Client) FindCalendars(ctx context.Context, calendarHomeSet string) ([]C
compNames = append(compNames, comp.Name) compNames = append(compNames, comp.Name)
} }
l = append(l, Calendar{ return &Calendar{
Path: path, Path: path,
Name: dispName.Name, Name: dispName.Name,
Description: desc.Description, Description: desc.Description,
MaxResourceSize: maxResSize.Size, MaxResourceSize: maxResSize.Size,
SupportedComponentSet: compNames, SupportedComponentSet: compNames,
}) }, nil
}
return l, errors.Join(errs...)
} }
func encodeCalendarCompReq(c *CalendarCompRequest) (*comp, error) { func encodeCalendarCompReq(c *CalendarCompRequest) (*comp, error) {