CalDAV and Google calendars can now be written to from the journal on
the web and in the phone app. Each change goes to the source at once
with If-Match set to the ETag the last sync read, so a change made
elsewhere is refused (409) instead of overwritten; the account is then
read again. Repeating events, iCal feeds and read-only Google calendars
stay read-only.
- calendar_events keep each event's calendar, object/event id, ETag and
whether it repeats; accounts keep their writable calendars and the
one new events go to (migration 1764800000_calendar_writes).
- Google sign-in asks for calendar.events; older read-only accounts are
offered "Connect again".
- New API: POST /calendar/events, PATCH and DELETE /calendar/events/{id}.
- Translations, Docs-Site guides and changelog updated.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
3.8 KiB
Go
94 lines
3.8 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"docwell/server/internal/access"
|
|
"docwell/server/internal/apperr"
|
|
"docwell/server/internal/calendar"
|
|
)
|
|
|
|
// CalendarService reads and writes people's calendars for their journals (ADR-16, ADR-19).
|
|
type CalendarService interface {
|
|
List(ctx context.Context, c access.Caller) ([]calendar.Account, error)
|
|
Create(ctx context.Context, c access.Caller, in calendar.AccountInput) (calendar.Account, error)
|
|
Update(ctx context.Context, c access.Caller, id string, in calendar.AccountInput) (calendar.Account, error)
|
|
Delete(ctx context.Context, c access.Caller, id string) error
|
|
Refresh(ctx context.Context, c access.Caller, id string) (calendar.Account, error)
|
|
Events(ctx context.Context, c access.Caller, from, to, tz string) ([]calendar.Event, error)
|
|
CreateEvent(ctx context.Context, c access.Caller, in calendar.EventInput) (calendar.Event, error)
|
|
UpdateEvent(ctx context.Context, c access.Caller, id string, in calendar.EventInput) (calendar.Event, error)
|
|
DeleteEvent(ctx context.Context, c access.Caller, id string) error
|
|
GoogleEnabled() bool
|
|
GoogleConnectURL(ctx context.Context, c access.Caller) (string, error)
|
|
}
|
|
|
|
var errNoCalendars = apperr.NotFound("calendars are not available on this server")
|
|
|
|
func (a *api) withCalendars(next http.HandlerFunc) http.HandlerFunc {
|
|
return a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
|
if a.calendars == nil {
|
|
a.writeError(w, errNoCalendars)
|
|
return
|
|
}
|
|
next(w, r)
|
|
})
|
|
}
|
|
|
|
func (a *api) calendarRoutes(mux *routeMux) {
|
|
mux.HandleFunc("GET /api/v1/calendars", a.withCalendars(func(w http.ResponseWriter, r *http.Request) {
|
|
v, err := a.calendars.List(r.Context(), caller(r))
|
|
a.respond(w, v, err)
|
|
}))
|
|
mux.HandleFunc("POST /api/v1/calendars", a.withCalendars(func(w http.ResponseWriter, r *http.Request) {
|
|
var in calendar.AccountInput
|
|
if !a.decode(w, r, &in) {
|
|
return
|
|
}
|
|
v, err := a.calendars.Create(r.Context(), caller(r), in)
|
|
a.respondStatus(w, http.StatusCreated, v, err)
|
|
}))
|
|
mux.HandleFunc("PATCH /api/v1/calendars/{id}", a.withCalendars(func(w http.ResponseWriter, r *http.Request) {
|
|
var in calendar.AccountInput
|
|
if !a.decode(w, r, &in) {
|
|
return
|
|
}
|
|
v, err := a.calendars.Update(r.Context(), caller(r), r.PathValue("id"), in)
|
|
a.respond(w, v, err)
|
|
}))
|
|
mux.HandleFunc("DELETE /api/v1/calendars/{id}", a.withCalendars(func(w http.ResponseWriter, r *http.Request) {
|
|
a.respondNoContent(w, a.calendars.Delete(r.Context(), caller(r), r.PathValue("id")))
|
|
}))
|
|
mux.HandleFunc("POST /api/v1/calendars/{id}/refresh", a.withCalendars(func(w http.ResponseWriter, r *http.Request) {
|
|
v, err := a.calendars.Refresh(r.Context(), caller(r), r.PathValue("id"))
|
|
a.respond(w, v, err)
|
|
}))
|
|
mux.HandleFunc("GET /api/v1/calendar/events", a.withCalendars(func(w http.ResponseWriter, r *http.Request) {
|
|
q := r.URL.Query()
|
|
v, err := a.calendars.Events(r.Context(), caller(r), q.Get("from"), q.Get("to"), q.Get("tz"))
|
|
a.respond(w, v, err)
|
|
}))
|
|
mux.HandleFunc("POST /api/v1/calendar/events", a.withCalendars(func(w http.ResponseWriter, r *http.Request) {
|
|
var in calendar.EventInput
|
|
if !a.decode(w, r, &in) {
|
|
return
|
|
}
|
|
v, err := a.calendars.CreateEvent(r.Context(), caller(r), in)
|
|
a.respondStatus(w, http.StatusCreated, v, err)
|
|
}))
|
|
mux.HandleFunc("PATCH /api/v1/calendar/events/{id}", a.withCalendars(func(w http.ResponseWriter, r *http.Request) {
|
|
var in calendar.EventInput
|
|
if !a.decode(w, r, &in) {
|
|
return
|
|
}
|
|
v, err := a.calendars.UpdateEvent(r.Context(), caller(r), r.PathValue("id"), in)
|
|
a.respond(w, v, err)
|
|
}))
|
|
mux.HandleFunc("DELETE /api/v1/calendar/events/{id}", a.withCalendars(func(w http.ResponseWriter, r *http.Request) {
|
|
a.respondNoContent(w, a.calendars.DeleteEvent(r.Context(), caller(r), r.PathValue("id")))
|
|
}))
|
|
}
|