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 "net/url"
6 "strconv"
7
8 "amadan.net/oficina/equipo/internal/equipo/identity"
9 "amadan.net/oficina/equipo/internal/equipo/planning"
10 "amadan.net/rastrillo/rastrillo/live"
11 "github.com/go-chi/chi/v5"
12)
13
14type Handler struct {
15 Live *live.Hub
16 App *Application
17 Plans *planning.Application
18 Failure func(http.ResponseWriter, *http.Request, error)
19 Render func(http.ResponseWriter, *http.Request, Page)
20 RenderSettings func(http.ResponseWriter, *http.Request, CoordinatorPage)
21}
22
23func (h *Handler) Routes(r chi.Router) {
24 r.Get("/tasks/{id}/thread", h.Page)
25 r.Post("/tasks/{id}/thread", h.Send)
26 r.Post("/conversation/messages/{id}/route", h.Route)
27 r.Post("/conversation/questions/{id}/answer", h.Answer)
28 r.Post("/conversation/projects/{id}/answer", h.AnswerProject)
29 r.Get("/settings/coordinator", h.Settings)
30 r.Post("/settings/coordinator", h.SaveSettings)
31}
32func (h *Handler) actor(w http.ResponseWriter, r *http.Request) (Actor, bool) {
33 w.Header().Set("Cache-Control", "no-store")
34 id, ok := identity.From(r)
35 if !ok {
36 h.Failure(w, r, h.App.Forbidden)
37 }
38 return Actor{Address: id.Address}, ok
39}
40func (h *Handler) Page(w http.ResponseWriter, r *http.Request) {
41 a, ok := h.actor(w, r)
42 if !ok {
43 return
44 }
45 snapshot := h.Live.View(r)
46 out, err := h.App.View(r.Context(), a, chi.URLParam(r, "id"), r.URL.Query().Get("cursor"), h.Plans)
47 if err != nil {
48 h.Failure(w, r, err)
49 return
50 }
51 out.Live = snapshot
52 h.Render(w, r, out)
53}
54func (h *Handler) Send(w http.ResponseWriter, r *http.Request) {
55 a, ok := h.actor(w, r)
56 if !ok {
57 return
58 }
59 m, err := planning.ReadMutation(w, r, h.App.Invalid)
60 task := chi.URLParam(r, "id")
61 if err == nil {
62 if raw := r.PostForm.Get("project_id"); raw != "" {
63 project, parseErr := strconv.ParseInt(raw, 10, 64)
64 if parseErr != nil || project <= 0 || task != "" {
65 err = h.App.Invalid
66 } else {
67 _, err = h.App.SendProjectMessage(r.Context(), a, r.PostForm.Get("body"), project, m)
68 }
69 } else {
70 _, err = h.App.SendMessage(r.Context(), a, task, r.PostForm.Get("body"), m)
71 }
72 }
73 if err != nil {
74 h.Failure(w, r, err)
75 return
76 }
77 path := "/conversation"
78 if task != "" {
79 path = "/tasks/" + url.PathEscape(task) + "/thread"
80 }
81 http.Redirect(w, r, path, http.StatusSeeOther)
82}
83func (h *Handler) Route(w http.ResponseWriter, r *http.Request) {
84 a, ok := h.actor(w, r)
85 if !ok {
86 return
87 }
88 m, err := planning.ReadMutation(w, r, h.App.Invalid)
89 if err == nil {
90 _, err = h.App.RouteMessage(r.Context(), a, chi.URLParam(r, "id"), r.PostForm.Get("task_id"), m)
91 }
92 if err != nil {
93 h.Failure(w, r, err)
94 return
95 }
96 http.Redirect(w, r, "/conversation", http.StatusSeeOther)
97}
98func (h *Handler) Answer(w http.ResponseWriter, r *http.Request) {
99 a, ok := h.actor(w, r)
100 if !ok {
101 return
102 }
103 m, err := planning.ReadMutation(w, r, h.App.Invalid)
104 if err == nil {
105 err = h.App.AnswerClarification(r.Context(), a, chi.URLParam(r, "id"), r.PostForm.Get("option_id"), r.PostForm.Get("text"), m)
106 }
107 if err != nil {
108 h.Failure(w, r, err)
109 return
110 }
111 http.Redirect(w, r, "/conversation", http.StatusSeeOther)
112}
113
114func (h *Handler) AnswerProject(w http.ResponseWriter, r *http.Request) {
115 a, ok := h.actor(w, r)
116 if !ok {
117 return
118 }
119 m, err := planning.ReadMutation(w, r, h.App.Invalid)
120 if err == nil {
121 choice := r.PostForm.Get("choice")
122 if choice != "setup" && choice != "decline" {
123 err = h.App.Invalid
124 } else {
125 err = h.App.AnswerProjectProposal(r.Context(), a, chi.URLParam(r, "id"), choice == "setup", m)
126 }
127 }
128 if err != nil {
129 h.Failure(w, r, err)
130 return
131 }
132 http.Redirect(w, r, "/conversation", http.StatusSeeOther)
133}
134