| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/equipo/personapi" |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | "github.com/go-chi/chi/v5" |
| 10 | ) |
| 11 | |
| 12 | // registerPush is POST /api/v1/push-registrations: 200 with the |
| 13 | // registration, without its tokens. A device bearer only. |
| 14 | func (h *HTTP) registerPush(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| 15 | var in wire.RegisterPush |
| 16 | if err := readJSON(r, &in); err != nil { |
| 17 | h.fail(w, r, err) |
| 18 | return |
| 19 | } |
| 20 | out, err := h.Store.RegisterPush(r.Context(), p.Address, p.CredentialID, in) |
| 21 | if err != nil { |
| 22 | h.fail(w, r, err) |
| 23 | return |
| 24 | } |
| 25 | writeJSON(w, http.StatusOK, out) |
| 26 | } |
| 27 | |
| 28 | // registerActivity is POST /api/v1/push-registrations/activities: 204. |
| 29 | func (h *HTTP) registerActivity(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| 30 | var in wire.RegisterActivity |
| 31 | if err := readJSON(r, &in); err != nil { |
| 32 | h.fail(w, r, err) |
| 33 | return |
| 34 | } |
| 35 | if err := h.Store.RegisterActivity(r.Context(), p.Address, p.CredentialID, in); err != nil { |
| 36 | h.fail(w, r, err) |
| 37 | return |
| 38 | } |
| 39 | w.WriteHeader(http.StatusNoContent) |
| 40 | } |
| 41 | |
| 42 | // cardVisibility is POST /api/v1/cards/{id}/visibility: 200 with the |
| 43 | // card; 409 card_stale with the card as it is when its revision moved or |
| 44 | // it no longer waits on the person. |
| 45 | func (h *HTTP) cardVisibility(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| 46 | var in wire.SetCardVisibility |
| 47 | if err := readJSON(r, &in); err != nil { |
| 48 | h.fail(w, r, err) |
| 49 | return |
| 50 | } |
| 51 | out, err := h.Store.SetCardVisibility(r.Context(), p.Address, chi.URLParam(r, "id"), in) |
| 52 | if errors.Is(err, ErrCardConflict) { |
| 53 | writeJSON(w, http.StatusConflict, wire.CardConflict{Code: "card_stale", CopyKey: "api.card_stale", RequestID: r.Header.Get("Idempotency-Key"), Card: out}) |
| 54 | return |
| 55 | } |
| 56 | if err != nil { |
| 57 | h.fail(w, r, err) |
| 58 | return |
| 59 | } |
| 60 | writeJSON(w, http.StatusOK, out) |
| 61 | } |
| 62 | |