Files
nidus/internal/web/dashboard.go
T
arnefandCopilot 1707ab4060 Add ICS/webcal HTTP subscription calendars
Users can now subscribe to a remote ICS/webcal feed from the dashboard
(name + color), the same way they set up a real calendar or the virtual
Birthdays calendar. Subscriptions are read-only, per-user, and share the
"/cal/home/<name>/" namespace with real calendars and "birthdays" (name
collisions are rejected in both directions).

- internal/db: new ics_subscriptions table + CRUD (internal/db/ics.go);
  CreateCalendarWithColor checks for a colliding subscription name.
- internal/icssub: shared HTTP-fetch + TTL cache (15 min) for remote ICS
  calendars, with webcal:// -> https:// rewriting and stale-on-error
  fallback, used by both the web UI and the CalDAV backend.
- internal/web: dashboard "Subscribe to an ICS/webcal calendar" form,
  color picker, delete button (internal/web/ics.go,
  templates/dashboard.templ); month view renders subscription events in
  their chosen color, read-only (internal/web/calendar.go).
- internal/caldav: subscriptions are exposed as read-only calendars
  (internal/caldav/ics.go) - listed in PROPFIND, events served via
  GET/REPORT, PUT/DELETE on individual events rejected with 403, but
  DELETE on the calendar itself unsubscribes; calendar-color is injected
  the same way as for real calendars and Birthdays.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-08-20 22:10:49 +02:00

144 lines
4.6 KiB
Go

package web
import (
"context"
"fmt"
"net/http"
"github.com/yourusername/caldav-server/internal/db"
"github.com/yourusername/caldav-server/internal/web/templates"
)
func (s *Server) handleDashboard(w http.ResponseWriter, r *http.Request) {
username := userFromContext(r.Context())
resources, err := s.resourceCards(username)
if err != nil {
s.logger.Error("listing resources", "error", err)
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
var sharedWithMe []templates.SharedWithMeItem
calShares, err := s.dbase.CalendarsSharedWith(username)
if err != nil {
s.logger.Warn("listing calendars shared with user", "error", err)
}
for _, sh := range calShares {
sharedWithMe = append(sharedWithMe, templates.SharedWithMeItem{
Kind: "calendar", Owner: sh.Owner, Name: sh.CalendarName, Permission: string(sh.Permission),
})
}
bookShares, err := s.dbase.AddressBooksSharedWith(username)
if err != nil {
s.logger.Warn("listing address books shared with user", "error", err)
}
for _, sh := range bookShares {
sharedWithMe = append(sharedWithMe, templates.SharedWithMeItem{
Kind: "addressbook", Owner: sh.Owner, Name: sh.AddressBookName, Permission: string(sh.Permission),
})
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_ = templates.Dashboard(username, resources, sharedWithMe).Render(context.Background(), w)
}
// resourceCards builds the full list of ResourceCards (calendars, then
// address books) owned by username, each with its current shares — used
// both for the initial dashboard render and to re-render the whole
// #resources list after a create/delete (since the set of cards changes,
// unlike a share update which only changes one card's contents).
func (s *Server) resourceCards(username string) ([]templates.ResourceCard, error) {
resources := []templates.ResourceCard{
{Kind: "calendar", Name: "Birthdays", Color: s.birthdayCalendarColor(username), Virtual: true},
}
calNames, err := s.dbase.ListCalendars(username)
if err != nil {
return nil, fmt.Errorf("listing calendars: %w", err)
}
for _, cal := range calNames {
card := templates.ResourceCard{Kind: "calendar", Name: cal.Name, Color: cal.Color}
shares, err := s.dbase.SharesOfCalendar(username, cal.Name)
if err != nil {
s.logger.Warn("listing calendar shares", "error", err)
}
for _, sh := range shares {
card.Shares = append(card.Shares, templates.ShareRow{
ResourceName: cal.Name,
SharedWith: sh.SharedWith,
Permission: string(sh.Permission),
})
}
resources = append(resources, card)
}
bookNames, err := s.dbase.ListAddressBooks(username)
if err != nil {
return nil, fmt.Errorf("listing address books: %w", err)
}
for _, bookName := range bookNames {
card := templates.ResourceCard{Kind: "addressbook", Name: bookName}
shares, err := s.dbase.SharesOfAddressBook(username, bookName)
if err != nil {
s.logger.Warn("listing address book shares", "error", err)
}
for _, sh := range shares {
card.Shares = append(card.Shares, templates.ShareRow{
ResourceName: bookName,
SharedWith: sh.SharedWith,
Permission: string(sh.Permission),
})
}
resources = append(resources, card)
}
subs, err := s.dbase.ListICSSubscriptions(username)
if err != nil {
return nil, fmt.Errorf("listing ics subscriptions: %w", err)
}
for _, sub := range subs {
resources = append(resources, templates.ResourceCard{Kind: "ics", Name: sub.Name, Color: sub.Color, URL: sub.URL})
}
return resources, nil
}
// resourceCardFor rebuilds a single ResourceCard (used to re-render just
// the card an htmx request just changed, for partial updates).
func (s *Server) resourceCardFor(username, kind, name string) (templates.ResourceCard, error) {
card := templates.ResourceCard{Kind: kind, Name: name}
var shares []templates.ShareRow
if kind == "calendar" {
if color, err := s.dbase.GetCalendarColor(username, name); err == nil {
card.Color = color
}
rows, err := s.dbase.SharesOfCalendar(username, name)
if err != nil {
return card, err
}
for _, sh := range rows {
shares = append(shares, templates.ShareRow{ResourceName: name, SharedWith: sh.SharedWith, Permission: string(sh.Permission)})
}
} else {
rows, err := s.dbase.SharesOfAddressBook(username, name)
if err != nil {
return card, err
}
for _, sh := range rows {
shares = append(shares, templates.ShareRow{ResourceName: name, SharedWith: sh.SharedWith, Permission: string(sh.Permission)})
}
}
card.Shares = shares
return card, nil
}
func isValidPermission(p string) (db.Permission, bool) {
switch db.Permission(p) {
case db.PermRead, db.PermWrite:
return db.Permission(p), true
}
return "", false
}