caldav: add Calendar.Color
Read the calendar-color property in the http://apple.com/ns/ical/ namespace in FindCalendar and FindCalendars, and report it in PROPFIND responses when it is set, the same way as calendar-description.
This commit is contained in:
parent
ad1766c2ef
commit
2c2af7cdf2
@ -69,6 +69,11 @@ type Calendar struct {
|
|||||||
Description string
|
Description string
|
||||||
MaxResourceSize int64
|
MaxResourceSize int64
|
||||||
SupportedComponentSet []string
|
SupportedComponentSet []string
|
||||||
|
// Color is the value of the calendar-color property in the
|
||||||
|
// http://apple.com/ns/ical/ namespace. That property is not defined by
|
||||||
|
// CalDAV. The value is a hexadecimal string following the #rrggbb syntax,
|
||||||
|
// or #rrggbbaa with an alpha channel, as Apple Calendar writes it.
|
||||||
|
Color string
|
||||||
// ReadOnly reports that the current user may only read this calendar. It
|
// ReadOnly reports that the current user may only read this calendar. It
|
||||||
// controls the DAV:current-user-privilege-set reported by the server. The
|
// controls the DAV:current-user-privilege-set reported by the server. The
|
||||||
// client sets it when the server reports a DAV:current-user-privilege-set
|
// client sets it when the server reports a DAV:current-user-privilege-set
|
||||||
|
|||||||
@ -25,6 +25,7 @@ var calendarProps = []xml.Name{
|
|||||||
maxResourceSizeName,
|
maxResourceSizeName,
|
||||||
supportedCalendarComponentSetName,
|
supportedCalendarComponentSetName,
|
||||||
internal.CurrentUserPrivilegeSetName,
|
internal.CurrentUserPrivilegeSetName,
|
||||||
|
calendarColorName,
|
||||||
}
|
}
|
||||||
|
|
||||||
// DiscoverContextURL performs a DNS-based CardDAV service discovery as
|
// DiscoverContextURL performs a DNS-based CardDAV service discovery as
|
||||||
@ -129,6 +130,11 @@ func decodeCalendar(path string, resp *internal.Response) (*Calendar, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var color calendarColor
|
||||||
|
if err := resp.DecodeProp(&color); err != nil && !internal.IsNotFound(err) {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
var maxResSize maxResourceSize
|
var maxResSize maxResourceSize
|
||||||
if err := resp.DecodeProp(&maxResSize); err != nil && !internal.IsNotFound(err) {
|
if err := resp.DecodeProp(&maxResSize); err != nil && !internal.IsNotFound(err) {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -161,6 +167,7 @@ func decodeCalendar(path string, resp *internal.Response) (*Calendar, error) {
|
|||||||
Description: desc.Description,
|
Description: desc.Description,
|
||||||
MaxResourceSize: maxResSize.Size,
|
MaxResourceSize: maxResSize.Size,
|
||||||
SupportedComponentSet: compNames,
|
SupportedComponentSet: compNames,
|
||||||
|
Color: color.Color,
|
||||||
ReadOnly: readOnly,
|
ReadOnly: readOnly,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,6 +23,8 @@ var (
|
|||||||
|
|
||||||
calendarName = xml.Name{namespace, "calendar"}
|
calendarName = xml.Name{namespace, "calendar"}
|
||||||
calendarDataName = xml.Name{namespace, "calendar-data"}
|
calendarDataName = xml.Name{namespace, "calendar-data"}
|
||||||
|
|
||||||
|
calendarColorName = xml.Name{"http://apple.com/ns/ical/", "calendar-color"}
|
||||||
)
|
)
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc4791#section-6.2.1
|
// https://tools.ietf.org/html/rfc4791#section-6.2.1
|
||||||
@ -41,6 +43,13 @@ type calendarDescription struct {
|
|||||||
Description string `xml:",chardata"`
|
Description string `xml:",chardata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// calendar-color is not defined by any RFC. It comes from Apple's
|
||||||
|
// http://apple.com/ns/ical/ namespace.
|
||||||
|
type calendarColor struct {
|
||||||
|
XMLName xml.Name `xml:"http://apple.com/ns/ical/ calendar-color"`
|
||||||
|
Color string `xml:",chardata"`
|
||||||
|
}
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc4791#section-5.2.4
|
// https://tools.ietf.org/html/rfc4791#section-5.2.4
|
||||||
type supportedCalendarData struct {
|
type supportedCalendarData struct {
|
||||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav supported-calendar-data"`
|
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav supported-calendar-data"`
|
||||||
|
|||||||
@ -559,6 +559,11 @@ func (b *backend) propFindCalendar(ctx context.Context, propfind *internal.PropF
|
|||||||
Description: cal.Description,
|
Description: cal.Description,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
if cal.Color != "" {
|
||||||
|
props[calendarColorName] = internal.PropFindValue(&calendarColor{
|
||||||
|
Color: cal.Color,
|
||||||
|
})
|
||||||
|
}
|
||||||
if cal.MaxResourceSize > 0 {
|
if cal.MaxResourceSize > 0 {
|
||||||
props[maxResourceSizeName] = internal.PropFindValue(&maxResourceSize{
|
props[maxResourceSizeName] = internal.PropFindValue(&maxResourceSize{
|
||||||
Size: cal.MaxResourceSize,
|
Size: cal.MaxResourceSize,
|
||||||
|
|||||||
@ -233,3 +233,34 @@ func (t testBackend) ListCalendarObjects(ctx context.Context, path string, req *
|
|||||||
func (t testBackend) QueryCalendarObjects(ctx context.Context, path string, query *CalendarQuery) ([]CalendarObject, error) {
|
func (t testBackend) QueryCalendarObjects(ctx context.Context, path string, query *CalendarQuery) ([]CalendarObject, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFindCalendarsColor(t *testing.T) {
|
||||||
|
h := Handler{Backend: testBackend{calendars: []Calendar{
|
||||||
|
{Path: "/user/calendars/a/", Color: "#FF2968FF"},
|
||||||
|
{Path: "/user/calendars/b/"},
|
||||||
|
}}}
|
||||||
|
srv := httptest.NewServer(&h)
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
c, err := NewClient(nil, srv.URL)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewClient: %v", err)
|
||||||
|
}
|
||||||
|
cals, err := c.FindCalendars(context.Background(), "/user/calendars/")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("FindCalendars: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
want := map[string]string{
|
||||||
|
"/user/calendars/a/": "#FF2968FF",
|
||||||
|
"/user/calendars/b/": "",
|
||||||
|
}
|
||||||
|
if len(cals) != len(want) {
|
||||||
|
t.Fatalf("got %d calendars, want %d", len(cals), len(want))
|
||||||
|
}
|
||||||
|
for _, cal := range cals {
|
||||||
|
if cal.Color != want[cal.Path] {
|
||||||
|
t.Errorf("%s: Color = %q, want %q", cal.Path, cal.Color, want[cal.Path])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user