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:
parent
8888fdf9b0
commit
1916c2d907
@ -3,6 +3,7 @@ package caldav
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime"
|
||||
@ -17,6 +18,14 @@ import (
|
||||
"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
|
||||
// described in RFC 6352 section 11. It returns the URL to the CardDAV server.
|
||||
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
|
||||
}
|
||||
|
||||
// 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) {
|
||||
propfind := internal.NewPropNamePropFind(calendarHomeSetName)
|
||||
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) {
|
||||
propfind := internal.NewPropNamePropFind(
|
||||
internal.ResourceTypeName,
|
||||
internal.DisplayNameName,
|
||||
calendarDescriptionName,
|
||||
maxResourceSizeName,
|
||||
supportedCalendarComponentSetName,
|
||||
)
|
||||
propfind := internal.NewPropNamePropFind(calendarProps...)
|
||||
ms, err := c.ic.PropFind(ctx, calendarHomeSet, internal.DepthOne, propfind)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -87,6 +106,18 @@ func (c *Client) FindCalendars(ctx context.Context, calendarHomeSet string) ([]C
|
||||
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
|
||||
if err := resp.DecodeProp(&desc); err != nil && !internal.IsNotFound(err) {
|
||||
return nil, err
|
||||
@ -115,16 +146,13 @@ func (c *Client) FindCalendars(ctx context.Context, calendarHomeSet string) ([]C
|
||||
compNames = append(compNames, comp.Name)
|
||||
}
|
||||
|
||||
l = append(l, Calendar{
|
||||
return &Calendar{
|
||||
Path: path,
|
||||
Name: dispName.Name,
|
||||
Description: desc.Description,
|
||||
MaxResourceSize: maxResSize.Size,
|
||||
SupportedComponentSet: compNames,
|
||||
})
|
||||
}
|
||||
|
||||
return l, errors.Join(errs...)
|
||||
}, nil
|
||||
}
|
||||
|
||||
func encodeCalendarCompReq(c *CalendarCompRequest) (*comp, error) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user