caldav: add sync-collection functionality

This commit is contained in:
LQR471814 2025-12-17 12:37:41 -08:00 committed by Simon Ser
parent ca45607edf
commit 82bb8a7aea
2 changed files with 73 additions and 0 deletions

View File

@ -131,3 +131,17 @@ type CalendarObject struct {
ETag string
Data *ical.Calendar
}
// SyncQuery is the query struct represents a sync-collection request
type SyncQuery struct {
CompRequest CalendarCompRequest
SyncToken string
Limit int // <= 0 means unlimited
}
// SyncResponse contains the returned sync-token for next time
type SyncResponse struct {
SyncToken string
Updated []CalendarObject
Deleted []string
}

View File

@ -430,3 +430,62 @@ func (c *Client) PutCalendarObject(ctx context.Context, path string, cal *ical.C
}
return co, nil
}
// SyncCollection performs a collection synchronization operation on the
// specified resource, as defined in RFC 6578.
func (c *Client) SyncCollection(ctx context.Context, path string, query *SyncQuery) (*SyncResponse, error) {
var limit *internal.Limit
if query.Limit > 0 {
limit = &internal.Limit{NResults: uint(query.Limit)}
}
compReq, err := encodeCalendarReq(&query.CompRequest)
if err != nil {
return nil, err
}
ms, err := c.ic.SyncCollection(ctx, path, query.SyncToken, internal.DepthOne, limit, compReq)
if err != nil {
return nil, err
}
ret := &SyncResponse{SyncToken: ms.SyncToken}
var errs []error
for _, resp := range ms.Responses {
p, err := resp.Path()
if err != nil {
var httpErr *internal.HTTPError
hasStatus := errors.As(err, &httpErr)
if !hasStatus || httpErr.Code != http.StatusNotFound {
errs = append(errs, err)
continue
}
ret.Deleted = append(ret.Deleted, p)
continue
}
if p == path || path == fmt.Sprintf("%s/", p) {
continue
}
var getLastMod internal.GetLastModified
if err := resp.DecodeProp(&getLastMod); err != nil && !internal.IsNotFound(err) {
return nil, err
}
var getETag internal.GetETag
if err := resp.DecodeProp(&getETag); err != nil && !internal.IsNotFound(err) {
return nil, err
}
o := CalendarObject{
Path: p,
ModTime: time.Time(getLastMod.LastModified),
ETag: string(getETag.ETag),
}
ret.Updated = append(ret.Updated, o)
}
return ret, errors.Join(errs...)
}