oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package equipo
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "net/http"
8 "strconv"
9
10 "github.com/go-chi/chi/v5"
11
12 "amadan.net/oficina/equipo/internal/equipo/chat"
13 "amadan.net/oficina/equipo/internal/equipo/housekeeping"
14 "amadan.net/oficina/equipo/internal/wire"
15)
16
17// housekeepingView is the Housekeeping page beside the Notebook: Pause,
18// today's budgets as quiet meters, what Equipo did newest first with
19// Undo while it can, what it refused, and the detectors with their
20// switches. The Suggestions lane arrives with slice 2.
21type housekeepingView struct {
22 Paused bool
23 Revision int64
24 Meters []hkMeter
25 Actions []hkLine
26 Refusals []hkLine
27 Detectors []hkDetectorLine
28}
29
30type hkMeter struct {
31 Label, Used, Held string
32 Percent int
33}
34
35type hkLine struct {
36 ID, Line, Evidence, State, When, Until string
37 At string
38 Undoable, Undone, Waiting bool
39}
40
41type hkDetectorLine struct {
42 ID, Name, Note string
43 Enabled bool
44}
45
46func housekeepingLines(h wire.Housekeeping) housekeepingView {
47 v := housekeepingView{Paused: h.Paused, Revision: h.Revision}
48 for _, m := range []struct {
49 key string
50 meter wire.BudgetMeter
51 }{{"housekeeping.meter.turns", h.Budget.Turns}, {"housekeeping.meter.tokens", h.Budget.Tokens}, {"housekeeping.meter.retries", h.Budget.Retries}} {
52 used := m.meter.Reserved + m.meter.Charged
53 line := hkMeter{Label: copyText(m.key), Used: fmt.Sprintf(copyText("housekeeping.meter.used"), groupDigits(used), groupDigits(m.meter.Limit))}
54 if m.meter.Reserved > 0 {
55 line.Held = fmt.Sprintf(copyText("housekeeping.meter.held"), groupDigits(m.meter.Reserved))
56 }
57 if m.meter.Limit > 0 {
58 line.Percent = int(min(100, used*100/m.meter.Limit))
59 }
60 v.Meters = append(v.Meters, line)
61 }
62 for _, a := range h.Actions {
63 v.Actions = append(v.Actions, actionLine(a))
64 }
65 for _, a := range h.Refusals {
66 v.Refusals = append(v.Refusals, actionLine(a))
67 }
68 for _, d := range h.Detectors {
69 line := hkDetectorLine{ID: d.ID, Name: copyText("housekeeping.detector." + d.ID), Enabled: d.Enabled}
70 if !d.Enabled && d.DisabledReason == wire.ReasonDetectorFailures {
71 line.Note = copyText("housekeeping.detector.failures")
72 }
73 v.Detectors = append(v.Detectors, line)
74 }
75 return v
76}
77
78func actionLine(a wire.HousekeepingAction) hkLine {
79 l := hkLine{ID: a.ID, State: a.State, Undoable: a.Undoable, Undone: a.State == wire.ActionUndone, Waiting: a.State == wire.ActionHeld || a.State == wire.ActionClaimed, When: a.At.UTC().Format("2 Jan 15:04"), At: a.At.UTC().Format("2006-01-02T15:04:05Z")}
80 titles := make([]string, 0, 2)
81 for _, k := range a.Evidence.Tasks {
82 titles = append(titles, k.Title)
83 }
84 switch {
85 case len(titles) == 2 && a.Detector == "duplicate_same_creator":
86 l.Line = fmt.Sprintf(copyText("housekeeping.action.combine"), titles[0], titles[1])
87 case len(titles) == 2:
88 l.Line = fmt.Sprintf(copyText("housekeeping.action.link"), titles[0], titles[1])
89 default:
90 l.Line = copyText("housekeeping.detector." + a.Detector)
91 }
92 if a.Evidence.Shared > 0 {
93 l.Evidence = fmt.Sprintf(copyText("housekeeping.evidence"), int(a.Evidence.Similarity*100+0.5), a.Evidence.Shared)
94 }
95 switch a.State {
96 case wire.ActionUndone:
97 l.State = copyText("housekeeping.state.undone")
98 case wire.ActionHeld:
99 l.State = copyText("housekeeping.held." + a.Reason)
100 case wire.ActionRefused:
101 l.State = copyText("housekeeping.state.refused")
102 case wire.ActionFailed:
103 l.State = copyText("housekeeping.state.failed")
104 default:
105 l.State = ""
106 }
107 if a.Undoable && a.UndoUntil != nil {
108 l.Until = fmt.Sprintf(copyText("housekeeping.until"), a.UndoUntil.UTC().Format("2 Jan"))
109 }
110 return l
111}
112
113// groupDigits writes 150000 as 150,000.
114func groupDigits(n int64) string {
115 s := strconv.FormatInt(n, 10)
116 for i := len(s) - 3; i > 0; i -= 3 {
117 s = s[:i] + "," + s[i:]
118 }
119 return s
120}
121
122func (a *application) housekeepingPage(w http.ResponseWriter, r *http.Request) {
123 w.Header().Set("Cache-Control", "no-store")
124 ctx, address, ok := sessionContext(r)
125 if !ok {
126 a.failure(w, r, ErrForbidden)
127 return
128 }
129 h, err := a.store.Chat().Housekeeping(ctx, address)
130 if err != nil {
131 a.failure(w, r, chatError(err))
132 return
133 }
134 data, err := a.page(r, "conversation")
135 if err != nil {
136 a.failure(w, r, err)
137 return
138 }
139 data.Title = "housekeeping.title"
140 data.Housekeeping, data.HousekeepingStream = housekeepingLines(h), true
141 render(w, "housekeeping", data)
142}
143
144// hkSettingsView is Settings' Housekeeping section: Pause, the person's
145// quiet hours and the way to the activity view.
146type hkSettingsView struct {
147 Paused, QuietOn bool
148 Revision int64
149 From, Until int
150 Hours []hkHour
151}
152
153type hkHour struct {
154 Value int
155 Label string
156}
157
158func housekeepingSettings(h wire.Housekeeping) *hkSettingsView {
159 v := &hkSettingsView{Paused: h.Paused, Revision: h.Revision, QuietOn: h.Quiet.From != h.Quiet.Until, From: h.Quiet.From, Until: h.Quiet.Until}
160 if !v.QuietOn {
161 v.From, v.Until = housekeeping.DefaultQuiet.From, housekeeping.DefaultQuiet.Until
162 }
163 for i := range 24 {
164 v.Hours = append(v.Hours, hkHour{Value: i, Label: fmt.Sprintf("%02d:00", i)})
165 }
166 return v
167}
168
169// housekeepingQuiet saves the person's quiet hours from Settings; off is
170// a window of no hours.
171func (a *application) housekeepingQuiet(w http.ResponseWriter, r *http.Request) {
172 a.chatActionTo(w, r, "/conversation/settings/housekeeping", func(ctx context.Context, address string) error {
173 revision, _ := strconv.ParseInt(r.PostFormValue("revision"), 10, 64)
174 from, err1 := strconv.Atoi(r.PostFormValue("from"))
175 until, err2 := strconv.Atoi(r.PostFormValue("until"))
176 if err1 != nil || err2 != nil {
177 return ErrInvalid
178 }
179 if r.PostFormValue("on") != "1" {
180 until = from
181 } else if from == until {
182 return ErrInvalid
183 }
184 h, err := a.store.Chat().Housekeeping(ctx, address)
185 if err != nil {
186 return err
187 }
188 _, err = a.store.Chat().SetHousekeeping(ctx, address, wire.SetHousekeeping{Paused: h.Paused, Quiet: &wire.QuietHours{From: from, Until: until}, Revision: revision})
189 return hkPageError(err)
190 })
191}
192
193// housekeepingPause is Pause and Resume, from Settings.
194func (a *application) housekeepingPause(w http.ResponseWriter, r *http.Request) {
195 a.chatActionTo(w, r, "/conversation/settings/housekeeping", func(ctx context.Context, address string) error {
196 revision, _ := strconv.ParseInt(r.PostFormValue("revision"), 10, 64)
197 _, err := a.store.Chat().SetHousekeeping(ctx, address, wire.SetHousekeeping{Paused: r.PostFormValue("paused") == "1", Revision: revision})
198 return hkPageError(err)
199 })
200}
201
202// housekeepingDetector switches a detector on or off.
203func (a *application) housekeepingDetector(w http.ResponseWriter, r *http.Request) {
204 a.chatActionTo(w, r, "/housekeeping", func(ctx context.Context, address string) error {
205 revision, _ := strconv.ParseInt(r.PostFormValue("revision"), 10, 64)
206 _, err := a.store.Chat().SetDetector(ctx, address, chi.URLParam(r, "id"), wire.SetDetector{Enabled: r.PostFormValue("enabled") == "1", Revision: revision})
207 return hkPageError(err)
208 })
209}
210
211// housekeepingUndo is Undo on an action from the page.
212func (a *application) housekeepingUndo(w http.ResponseWriter, r *http.Request) {
213 a.chatActionTo(w, r, "/housekeeping", func(ctx context.Context, address string) error {
214 _, err := a.store.Chat().UndoAction(ctx, address, chi.URLParam(r, "id"))
215 return hkPageError(err)
216 })
217}
218
219// hkPageError: a stale form or an Undo that no longer applies just shows
220// the page as it is now.
221func hkPageError(err error) error {
222 if errors.Is(err, chat.ErrActionConflict) || errors.Is(err, chat.ErrRevision) {
223 return chat.ErrCardConflict
224 }
225 return err
226}
227