oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "errors"
5 "net/http"
6 "strconv"
7
8 "github.com/go-chi/chi/v5"
9
10 "amadan.net/oficina/equipo/internal/equipo/personapi"
11 "amadan.net/oficina/equipo/internal/wire"
12)
13
14// mountHousekeeping adds the housekeeping resources (spec §6) to the
15// person API v1 router.
16func (h *HTTP) mountHousekeeping(r chi.Router) {
17 r.Get("/housekeeping", h.bearer(h.housekeeping))
18 r.Put("/housekeeping", h.bearer(h.setHousekeeping))
19 r.Put("/housekeeping/detectors/{id}", h.bearer(h.setDetector))
20 r.Get("/housekeeping/actions", h.bearer(h.housekeepingActions))
21 r.Post("/housekeeping/actions/{id}/undo", h.bearer(h.undoAction))
22}
23
24func (h *HTTP) housekeeping(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) {
25 out, err := h.Store.Housekeeping(r.Context(), p.Address)
26 if err != nil {
27 h.fail(w, r, err)
28 return
29 }
30 writeJSON(w, 200, out)
31}
32
33// setHousekeeping is Pause: PUT /api/v1/housekeeping {paused, revision}.
34func (h *HTTP) setHousekeeping(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) {
35 var in wire.SetHousekeeping
36 if err := readJSON(r, &in); err != nil {
37 h.fail(w, r, err)
38 return
39 }
40 out, err := h.Store.SetHousekeeping(r.Context(), p.Address, in)
41 if err != nil {
42 h.fail(w, r, err)
43 return
44 }
45 writeJSON(w, 200, out)
46}
47
48func (h *HTTP) setDetector(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) {
49 var in wire.SetDetector
50 if err := readJSON(r, &in); err != nil {
51 h.fail(w, r, err)
52 return
53 }
54 out, err := h.Store.SetDetector(r.Context(), p.Address, chi.URLParam(r, "id"), in)
55 if err != nil {
56 h.fail(w, r, err)
57 return
58 }
59 writeJSON(w, 200, out)
60}
61
62func (h *HTTP) housekeepingActions(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) {
63 limit := 50
64 if v := r.URL.Query().Get("limit"); v != "" {
65 n, err := strconv.Atoi(v)
66 if err != nil || n < 1 || n > 100 {
67 h.fail(w, r, ErrInvalid)
68 return
69 }
70 limit = n
71 }
72 out, err := h.Store.HousekeepingActions(r.Context(), p.Address, r.URL.Query().Get("before"), limit)
73 if err != nil {
74 h.fail(w, r, err)
75 return
76 }
77 writeJSON(w, 200, out)
78}
79
80// undoAction is POST /api/v1/housekeeping/actions/{id}/undo: 409 with the
81// action as it is now when Undo no longer applies.
82func (h *HTTP) undoAction(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) {
83 out, err := h.Store.UndoAction(r.Context(), p.Address, chi.URLParam(r, "id"))
84 if errors.Is(err, ErrActionConflict) {
85 writeJSON(w, http.StatusConflict, wire.ActionConflict{Code: "not_undoable", CopyKey: "api.not_undoable", RequestID: r.Header.Get("Idempotency-Key"), Action: out})
86 return
87 }
88 if err != nil {
89 h.fail(w, r, err)
90 return
91 }
92 writeJSON(w, 200, out)
93}
94