oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package conversation
2
3import (
4 "net/http"
5 "strconv"
6
7 "amadan.net/oficina/equipo/internal/equipo/identity"
8 "github.com/go-chi/chi/v5"
9)
10
11type PreferenceHandler struct {
12 App *Application
13 Failure func(http.ResponseWriter, *http.Request, error)
14 Render func(http.ResponseWriter, *http.Request, PreferencePage)
15}
16
17func (h *PreferenceHandler) Routes(r chi.Router) {
18 r.Get("/preferences", h.Page)
19 r.Get("/preferences/sources/{source}", h.Page)
20 r.Post("/preferences", h.Set)
21 r.Post("/preferences/{id}", h.Set)
22 r.Post("/preferences/{id}/delete", h.Delete)
23}
24func (h *PreferenceHandler) actor(w http.ResponseWriter, r *http.Request) (Actor, bool) {
25 w.Header().Set("Cache-Control", "no-store")
26 id, ok := identity.From(r)
27 if !ok {
28 h.Failure(w, r, h.App.Forbidden)
29 return Actor{}, false
30 }
31 return Actor{Address: id.Address}, true
32}
33func (h *PreferenceHandler) Page(w http.ResponseWriter, r *http.Request) {
34 actor, ok := h.actor(w, r)
35 if !ok {
36 return
37 }
38 out, err := h.App.PreferencePage(r.Context(), actor, chi.URLParam(r, "source"))
39 if err != nil {
40 h.Failure(w, r, err)
41 return
42 }
43 h.Render(w, r, out)
44}
45func (h *PreferenceHandler) mutation(w http.ResponseWriter, r *http.Request) (Mutation, error) {
46 r.Body = http.MaxBytesReader(w, r.Body, 64<<10)
47 if err := r.ParseForm(); err != nil {
48 return Mutation{}, h.App.Invalid
49 }
50 for _, v := range r.PostForm {
51 if len(v) != 1 {
52 return Mutation{}, h.App.Invalid
53 }
54 }
55 revision, err := strconv.ParseInt(r.PostForm.Get("revision"), 10, 64)
56 if err != nil {
57 return Mutation{}, h.App.Invalid
58 }
59 m := Mutation{RequestID: r.PostForm.Get("request_id"), Revision: revision}
60 if !m.Valid() {
61 return Mutation{}, h.App.Invalid
62 }
63 return m, nil
64}
65func (h *PreferenceHandler) Set(w http.ResponseWriter, r *http.Request) {
66 actor, ok := h.actor(w, r)
67 if !ok {
68 return
69 }
70 m, err := h.mutation(w, r)
71 if err == nil {
72 // Confirmation is available only on this authenticated personal route.
73 _, err = h.App.SetPreference(r.Context(), actor, Preference{ID: chi.URLParam(r, "id"), Text: r.PostForm.Get("text"), Status: "confirmed"}, m)
74 }
75 if err != nil {
76 h.Failure(w, r, err)
77 return
78 }
79 http.Redirect(w, r, "/preferences", http.StatusSeeOther)
80}
81func (h *PreferenceHandler) Delete(w http.ResponseWriter, r *http.Request) {
82 actor, ok := h.actor(w, r)
83 if !ok {
84 return
85 }
86 m, err := h.mutation(w, r)
87 if err == nil {
88 err = h.App.DeletePreference(r.Context(), actor, chi.URLParam(r, "id"), m)
89 }
90 if err != nil {
91 h.Failure(w, r, err)
92 return
93 }
94 http.Redirect(w, r, "/preferences", http.StatusSeeOther)
95}
96
97func (h *PreferenceHandler) Landing(w http.ResponseWriter, r *http.Request) {
98 actor, ok := h.actor(w, r)
99 if !ok {
100 return
101 }
102 settings, err := h.App.PersonalSettings(r.Context(), actor)
103 if err != nil {
104 h.Failure(w, r, err)
105 return
106 }
107 http.Redirect(w, r, "/"+settings.Landing, http.StatusSeeOther)
108}
109