caldav: add support for time filters in client

This commit is contained in:
Simon Ser 2020-02-05 17:36:18 +01:00
parent 57df6bf316
commit 4eb8396edb
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 34 additions and 11 deletions

View File

@ -24,8 +24,15 @@ type CalendarCompRequest struct {
Comps []CalendarCompRequest
}
type CompFilter struct {
Name string
Start, End time.Time
Comps []CompFilter
}
type CalendarQuery struct {
Comp CalendarCompRequest
CompRequest CalendarCompRequest
CompFilter CompFilter
}
type CalendarObject struct {

View File

@ -141,6 +141,20 @@ func encodeCalendarReq(c *CalendarCompRequest) (*internal.Prop, error) {
return internal.EncodeProp(&calDataReq, getLastModReq, getETagReq)
}
func encodeCompFilter(filter *CompFilter) *compFilter {
encoded := compFilter{Name: filter.Name}
if !filter.Start.IsZero() || !filter.End.IsZero() {
encoded.TimeRange = &timeRange{
Start: dateWithUTCTime(filter.Start),
End: dateWithUTCTime(filter.End),
}
}
for _, child := range filter.Comps {
encoded.CompFilters = append(encoded.CompFilters, *encodeCompFilter(&child))
}
return &encoded
}
func decodeCalendarObjectList(ms *internal.Multistatus) ([]CalendarObject, error) {
addrs := make([]CalendarObject, 0, len(ms.Responses))
for _, resp := range ms.Responses {
@ -176,12 +190,13 @@ func decodeCalendarObjectList(ms *internal.Multistatus) ([]CalendarObject, error
}
func (c *Client) QueryCalendar(calendar string, query *CalendarQuery) ([]CalendarObject, error) {
propReq, err := encodeCalendarReq(&query.Comp)
propReq, err := encodeCalendarReq(&query.CompRequest)
if err != nil {
return nil, err
}
calendarQuery := calendarQuery{Prop: propReq}
calendarQuery.Filter.CompFilter = *encodeCompFilter(&query.CompFilter)
req, err := c.ic.NewXMLRequest("REPORT", calendar, &calendarQuery)
if err != nil {
return nil, err

View File

@ -63,7 +63,7 @@ type calendarQuery struct {
// https://tools.ietf.org/html/rfc4791#section-9.7
type filter struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav filter"`
Comp compFilter `xml:"comp-filter"`
CompFilter compFilter `xml:"comp-filter"`
}
// https://tools.ietf.org/html/rfc4791#section-9.7.1
@ -72,14 +72,15 @@ type compFilter struct {
Name string `xml:"name,attr"`
IsNotDefined *struct{} `xml:"is-not-defined,omitempty"`
TimeRange *timeRange `xml:"time-range,omitempty"`
// TODO: prop-filter, comp-filter
CompFilters []compFilter `xml:"comp-filter,omitempty"`
// TODO: prop-filter
}
// https://tools.ietf.org/html/rfc4791#section-9.9
type timeRange struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav time-range"`
Start dateWithUTCTime `xml:"start,attr"`
End dateWithUTCTime `xml:"end,attr"`
Start dateWithUTCTime `xml:"start,attr,omitempty"`
End dateWithUTCTime `xml:"end,attr,omitempty"`
}
const dateWithUTCTimeLayout = "20060102T150405Z"