Add calendar color support (DAVx5 calendar-color)

Calendars can now have a color (hex, e.g. #3b82f6) that DAVx5 and other
CalDAV clients pick up via the Apple/dav4jvm calendar-color property.

- db: add calendars.color column with migration for existing DBs;
  CreateCalendarWithColor, SetCalendarColor, GetCalendarColor;
  ListCalendars now returns []Calendar{Name, Color} instead of []string
- caldav: since go-webdav's caldav.Backend interface has no extension
  point for vendor properties, wrap the handler with a response-rewriting
  middleware that injects <calendar-color xmlns="http://apple.com/ns/ical/">
  into PROPFIND responses for calendars that have a color set
- web: color picker on the "New calendar" form and an inline color swatch/
  picker on each calendar card (calendars only, not address books)
- nidusctl: `calendar create --color` flag and a new `calendar color`
  subcommand; `calendar list` now also prints the color if set

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-08-20 06:43:56 +02:00
co-authored by Copilot
parent 37399d1ceb
commit ebbc7a2a2b
15 changed files with 618 additions and 163 deletions
+51 -1
View File
@@ -14,6 +14,10 @@ import (
// are safe as both a URL path segment and a filesystem directory name.
var resourceNameRe = regexp.MustCompile(`^[a-zA-Z0-9_-]{1,64}$`)
// hexColorRe validates the 6-digit hex color format produced by an HTML
// <input type="color">, e.g. "#3b82f6".
var hexColorRe = regexp.MustCompile(`^#[0-9a-fA-F]{6}$`)
// handleCalendarResource handles POST (create) and DELETE (remove) for
// the current user's own calendars, mounted at /resources/calendar.
func (s *Server) handleCalendarResource(w http.ResponseWriter, r *http.Request) {
@@ -24,6 +28,47 @@ func (s *Server) handleAddressBookResource(w http.ResponseWriter, r *http.Reques
s.handleResource(w, r, "addressbook")
}
// handleCalendarColor updates the color of one of the current user's own
// calendars, mounted at /resources/calendar/color. It re-renders just
// that calendar's card (not the whole list), since the set of cards
// doesn't change.
func (s *Server) handleCalendarColor(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.Header().Set("Allow", "POST")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
username := userFromContext(r.Context())
if err := r.ParseForm(); err != nil {
http.Error(w, "invalid form", http.StatusBadRequest)
return
}
name := strings.TrimSpace(r.PostForm.Get("name"))
color := strings.TrimSpace(r.PostForm.Get("color"))
if !resourceNameRe.MatchString(name) {
http.Error(w, "name must be 1-64 letters, digits, '-' or '_'", http.StatusBadRequest)
return
}
if color != "" && !hexColorRe.MatchString(color) {
http.Error(w, "color must be a hex value like #3b82f6", http.StatusBadRequest)
return
}
if err := s.dbase.SetCalendarColor(username, name, color); err != nil && err != db.ErrResourceNotFound {
s.logger.Error("setting calendar color", "error", err)
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
card, err := s.resourceCardFor(username, "calendar", name)
if err != nil {
s.logger.Error("rendering resource card", "error", err)
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_ = templates.ResourceCardView(card).Render(r.Context(), w)
}
func (s *Server) handleResource(w http.ResponseWriter, r *http.Request, kind string) {
username := userFromContext(r.Context())
@@ -50,7 +95,12 @@ func (s *Server) handleResource(w http.ResponseWriter, r *http.Request, kind str
case http.MethodPost:
var err error
if kind == "calendar" {
err = s.dbase.CreateCalendar(username, name)
color := strings.TrimSpace(r.PostForm.Get("color"))
if color != "" && !hexColorRe.MatchString(color) {
http.Error(w, "color must be a hex value like #3b82f6", http.StatusBadRequest)
return
}
err = s.dbase.CreateCalendarWithColor(username, name, color)
} else {
err = s.dbase.CreateAddressBook(username, name)
}