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 Comps []CalendarCompRequest
} }
type CompFilter struct {
Name string
Start, End time.Time
Comps []CompFilter
}
type CalendarQuery struct { type CalendarQuery struct {
Comp CalendarCompRequest CompRequest CalendarCompRequest
CompFilter CompFilter
} }
type CalendarObject struct { type CalendarObject struct {

View File

@ -141,6 +141,20 @@ func encodeCalendarReq(c *CalendarCompRequest) (*internal.Prop, error) {
return internal.EncodeProp(&calDataReq, getLastModReq, getETagReq) 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) { func decodeCalendarObjectList(ms *internal.Multistatus) ([]CalendarObject, error) {
addrs := make([]CalendarObject, 0, len(ms.Responses)) addrs := make([]CalendarObject, 0, len(ms.Responses))
for _, resp := range 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) { func (c *Client) QueryCalendar(calendar string, query *CalendarQuery) ([]CalendarObject, error) {
propReq, err := encodeCalendarReq(&query.Comp) propReq, err := encodeCalendarReq(&query.CompRequest)
if err != nil { if err != nil {
return nil, err return nil, err
} }
calendarQuery := calendarQuery{Prop: propReq} calendarQuery := calendarQuery{Prop: propReq}
calendarQuery.Filter.CompFilter = *encodeCompFilter(&query.CompFilter)
req, err := c.ic.NewXMLRequest("REPORT", calendar, &calendarQuery) req, err := c.ic.NewXMLRequest("REPORT", calendar, &calendarQuery)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -62,24 +62,25 @@ type calendarQuery struct {
// https://tools.ietf.org/html/rfc4791#section-9.7 // https://tools.ietf.org/html/rfc4791#section-9.7
type filter struct { type filter struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav filter"` 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 // https://tools.ietf.org/html/rfc4791#section-9.7.1
type compFilter struct { type compFilter struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav comp-filter"` XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav comp-filter"`
Name string `xml:"name,attr"` Name string `xml:"name,attr"`
IsNotDefined *struct{} `xml:"is-not-defined,omitempty"` IsNotDefined *struct{} `xml:"is-not-defined,omitempty"`
TimeRange *timeRange `xml:"time-range,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 // https://tools.ietf.org/html/rfc4791#section-9.9
type timeRange struct { type timeRange struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav time-range"` XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav time-range"`
Start dateWithUTCTime `xml:"start,attr"` Start dateWithUTCTime `xml:"start,attr,omitempty"`
End dateWithUTCTime `xml:"end,attr"` End dateWithUTCTime `xml:"end,attr,omitempty"`
} }
const dateWithUTCTimeLayout = "20060102T150405Z" const dateWithUTCTimeLayout = "20060102T150405Z"