// SPDX-License-Identifier: AGPL-3.0-or-later package httpapi import ( "context" "net/http" "docwell/server/internal/apperr" "docwell/server/internal/push" ) // PushService registers the caller's phones for push notifications. type PushService interface { Register(ctx context.Context, user, endpoint, p256dh, auth, name string) (push.Device, error) List(ctx context.Context, user string) ([]push.Device, error) Remove(ctx context.Context, user, id string) error } var errPushOff = apperr.NotFound("push notifications are off on this server") func (a *api) pushRoutes(mux *routeMux) { mux.HandleFunc("GET /api/v1/me/devices", a.requireUser(func(w http.ResponseWriter, r *http.Request) { if a.push == nil { a.respond(w, nil, errPushOff) return } list, err := a.push.List(r.Context(), caller(r).ID) a.respond(w, list, err) })) mux.HandleFunc("POST /api/v1/me/devices", a.requireUser(func(w http.ResponseWriter, r *http.Request) { if a.push == nil { a.respond(w, nil, errPushOff) return } var in struct{ Endpoint, P256dh, Auth, Name string } if !a.decode(w, r, &in) { return } d, err := a.push.Register(r.Context(), caller(r).ID, in.Endpoint, in.P256dh, in.Auth, in.Name) a.respondStatus(w, http.StatusCreated, d, err) })) mux.HandleFunc("DELETE /api/v1/me/devices/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) { if a.push == nil { a.respond(w, nil, errPushOff) return } a.respondNoContent(w, a.push.Remove(r.Context(), caller(r).ID, r.PathValue("id"))) })) }