| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "net/url" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/go-chi/chi/v5" |
| 11 | |
| 12 | "amadan.net/oficina/suite" |
| 13 | ) |
| 14 | |
| 15 | // §8.10's correction page and the person's own reports. |
| 16 | |
| 17 | type correctPage struct { |
| 18 | page |
| 19 | Query string |
| 20 | Kinds []CorrectionKind |
| 21 | Notices []string |
| 22 | // Gone is set when a POSTed choice was no longer among the person's |
| 23 | // candidates, so the page says so above a fresh list. |
| 24 | Gone bool |
| 25 | } |
| 26 | |
| 27 | type reportsPage struct { |
| 28 | page |
| 29 | Reports []ReportView |
| 30 | Feedback []FeedbackView |
| 31 | } |
| 32 | |
| 33 | type feedbackPage struct { |
| 34 | page |
| 35 | Feedback FeedbackView |
| 36 | } |
| 37 | |
| 38 | // asker is who a search, a report or a rating is for: the signed-in |
| 39 | // address, lowercased (lone shape, suite §3). In team mode it is the Home |
| 40 | // subject the roster knows them by (suite shape, where Home is the |
| 41 | // identity authority), for everyone — a Home-only member has no address, |
| 42 | // and one person must ask as one subject however they signed in, or their |
| 43 | // own reports would split in two. An unlinked local account has no |
| 44 | // subject, and no standing to ask with. |
| 45 | func (a *app) asker(r *http.Request) (suite.Subject, bool) { |
| 46 | if a.teamMode() && !a.allowUnauthenticated { |
| 47 | p, ok := teamPersonFrom(r.Context()) |
| 48 | if !ok || p.subject == "" { |
| 49 | return suite.Subject{}, false |
| 50 | } |
| 51 | return suite.Subject{Kind: suite.SubjectPersonRef, Value: p.subject}, true |
| 52 | } |
| 53 | addr, ok := a.identity(r) |
| 54 | if !ok || addr == "" { |
| 55 | return suite.Subject{}, false |
| 56 | } |
| 57 | return suite.Subject{Kind: suite.SubjectAddress, Value: addr}, true |
| 58 | } |
| 59 | |
| 60 | // correctFacts is GET /facts/correct?q=: the query's deterministic |
| 61 | // candidates, each a radio button whose value is an opaque handle. |
| 62 | func (a *app) correctFacts(w http.ResponseWriter, r *http.Request) { |
| 63 | a.renderCorrect(w, r, strings.TrimSpace(r.URL.Query().Get("q")), false) |
| 64 | } |
| 65 | |
| 66 | func (a *app) renderCorrect(w http.ResponseWriter, r *http.Request, q string, gone bool) { |
| 67 | asker, ok := a.asker(r) |
| 68 | if !ok { |
| 69 | a.errorPage(w, r, http.StatusNotFound, kindDocument, "") |
| 70 | return |
| 71 | } |
| 72 | var kinds []CorrectionKind |
| 73 | var notices []string |
| 74 | if q != "" { |
| 75 | var err error |
| 76 | if kinds, notices, err = a.store.SearchForCorrection(r.Context(), asker, q); err != nil { |
| 77 | // No query text in the log line (gate 4). |
| 78 | a.logger.Error("correction search failed", "err", err) |
| 79 | a.errorPage(w, r, http.StatusInternalServerError, kindDocument, "") |
| 80 | return |
| 81 | } |
| 82 | } |
| 83 | render(w, "correct", correctPage{ |
| 84 | page: a.pageFor(r, frame{parts: []string{"Is this wrong?"}}), |
| 85 | Query: q, |
| 86 | Kinds: kinds, |
| 87 | Notices: notices, |
| 88 | Gone: gone, |
| 89 | }) |
| 90 | } |
| 91 | |
| 92 | // saveCorrection is POST /facts/correct. The store re-runs the ruled |
| 93 | // search and accepts the choice only if it is still this person's. |
| 94 | func (a *app) saveCorrection(w http.ResponseWriter, r *http.Request) { |
| 95 | asker, ok := a.asker(r) |
| 96 | if !ok { |
| 97 | a.errorPage(w, r, http.StatusNotFound, kindDocument, "") |
| 98 | return |
| 99 | } |
| 100 | if err := r.ParseForm(); err != nil { |
| 101 | a.errorPage(w, r, http.StatusBadRequest, kindDocument, "") |
| 102 | return |
| 103 | } |
| 104 | q := strings.TrimSpace(r.PostForm.Get("q")) |
| 105 | err := a.store.SaveCorrection(r.Context(), asker, q, |
| 106 | r.PostForm.Get("kind"), r.PostForm.Get("choice"), r.PostForm.Get("note")) |
| 107 | switch { |
| 108 | case a.guardRefused(w, r, err): |
| 109 | return |
| 110 | case errors.Is(err, ErrChoiceGone): |
| 111 | w.WriteHeader(http.StatusConflict) |
| 112 | a.renderCorrect(w, r, q, true) |
| 113 | return |
| 114 | case err != nil: |
| 115 | a.logger.Error("saving a correction failed", "err", err) |
| 116 | a.errorPage(w, r, http.StatusInternalServerError, kindDocument, "") |
| 117 | return |
| 118 | } |
| 119 | indexNotice.set(w, "success", "Thanks — saved. Your corrections are under Reports.") |
| 120 | http.Redirect(w, r, "/?q="+url.QueryEscape(q), http.StatusSeeOther) |
| 121 | } |
| 122 | |
| 123 | // reports is GET /reports: the asker's own corrections, and nobody else's. |
| 124 | func (a *app) reports(w http.ResponseWriter, r *http.Request) { |
| 125 | asker, ok := a.asker(r) |
| 126 | if !ok { |
| 127 | a.errorPage(w, r, http.StatusNotFound, kindDocument, "") |
| 128 | return |
| 129 | } |
| 130 | pg := reportsPage{page: a.pageFor(r, frame{parts: []string{"Reports"}})} |
| 131 | // Team mode: standing before the lists, and again after (team_access.go). |
| 132 | if a.teamReaches(r) { |
| 133 | list, err := a.store.Reports(r.Context(), asker) |
| 134 | if err != nil { |
| 135 | a.logger.Error("listing reports failed", "err", err) |
| 136 | a.errorPage(w, r, http.StatusInternalServerError, kindDocument, "") |
| 137 | return |
| 138 | } |
| 139 | fb, err := a.store.Feedback(r.Context(), asker) |
| 140 | if err != nil { |
| 141 | a.logger.Error("listing answer feedback failed", "err", err) |
| 142 | a.errorPage(w, r, http.StatusInternalServerError, kindDocument, "") |
| 143 | return |
| 144 | } |
| 145 | pg.Reports, pg.Feedback = list, fb |
| 146 | } |
| 147 | if a.teamPartial(r) { |
| 148 | pg.withhold() |
| 149 | } |
| 150 | render(w, "reports", pg) |
| 151 | } |
| 152 | |
| 153 | // deleteReport is POST /reports/{id}/delete. Another person's report is a |
| 154 | // 404, never a 403. |
| 155 | func (a *app) deleteReport(w http.ResponseWriter, r *http.Request) { |
| 156 | asker, ok := a.asker(r) |
| 157 | id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) |
| 158 | if !ok || err != nil { |
| 159 | a.errorPage(w, r, http.StatusNotFound, kindDocument, "") |
| 160 | return |
| 161 | } |
| 162 | switch err := a.store.DeleteReport(r.Context(), asker, id); { |
| 163 | case a.guardRefused(w, r, err): |
| 164 | return |
| 165 | case errors.Is(err, ErrReportNotFound): |
| 166 | a.errorPage(w, r, http.StatusNotFound, kindDocument, "") |
| 167 | return |
| 168 | case err != nil: |
| 169 | a.logger.Error("deleting a report failed", "err", err) |
| 170 | a.errorPage(w, r, http.StatusInternalServerError, kindDocument, "") |
| 171 | return |
| 172 | } |
| 173 | http.Redirect(w, r, "/reports", http.StatusSeeOther) |
| 174 | } |
| 175 | |
| 176 | // answer is GET /answer?q=: §8.11's AI answer as a fragment for |
| 177 | // search.js. It takes nothing but the query: Store.Answer runs its own |
| 178 | // ruled search (§8.4's invariant). Nothing to answer is an empty 204. |
| 179 | func (a *app) answer(w http.ResponseWriter, r *http.Request) { |
| 180 | asker, ok := a.asker(r) |
| 181 | if !ok { |
| 182 | a.errorPage(w, r, http.StatusNotFound, kindDocument, "") |
| 183 | return |
| 184 | } |
| 185 | q := strings.TrimSpace(r.URL.Query().Get("q")) |
| 186 | ans, err := a.store.Answer(r.Context(), asker, q) |
| 187 | if err != nil { |
| 188 | a.logger.Error("ai answer search failed", "err", err) |
| 189 | ans = &AIAnswer{State: AnswerFailed} |
| 190 | } |
| 191 | // A model answer can take seconds, and in team mode the standing it |
| 192 | // was asked on must still hold when it is delivered: a person removed |
| 193 | // while the model was thinking gets nothing it said (team_access.go). |
| 194 | if !a.teamReaches(r) { |
| 195 | a.teamUnavailable(w, r) |
| 196 | return |
| 197 | } |
| 198 | if ans == nil { |
| 199 | w.WriteHeader(http.StatusNoContent) |
| 200 | return |
| 201 | } |
| 202 | renderFragment(w, "answer", ans) |
| 203 | } |
| 204 | |
| 205 | // saveFeedback is POST /answer/feedback: a 👍 or 👎 on an AI answer |
| 206 | // (§8.11). The handle says what was shown; a handle that does not open |
| 207 | // or is someone else's is a 404. search.js posts 👍 with inline=1 and |
| 208 | // gets a 204; a form submit returns to the search, or, for 👎, to the |
| 209 | // page that asks what was wrong. |
| 210 | func (a *app) saveFeedback(w http.ResponseWriter, r *http.Request) { |
| 211 | asker, ok := a.asker(r) |
| 212 | if !ok { |
| 213 | a.errorPage(w, r, http.StatusNotFound, kindDocument, "") |
| 214 | return |
| 215 | } |
| 216 | if err := r.ParseForm(); err != nil { |
| 217 | a.errorPage(w, r, http.StatusBadRequest, kindDocument, "") |
| 218 | return |
| 219 | } |
| 220 | verdict := r.PostForm.Get("verdict") |
| 221 | id, q, err := a.store.SaveFeedback(r.Context(), asker, r.PostForm.Get("handle"), verdict) |
| 222 | switch { |
| 223 | case a.guardRefused(w, r, err): |
| 224 | return |
| 225 | case errors.Is(err, ErrFeedbackNotFound): |
| 226 | a.errorPage(w, r, http.StatusNotFound, kindDocument, "") |
| 227 | return |
| 228 | case err != nil: |
| 229 | a.logger.Error("saving answer feedback failed", "err", err) |
| 230 | a.errorPage(w, r, http.StatusInternalServerError, kindDocument, "") |
| 231 | return |
| 232 | } |
| 233 | if verdict == "down" { |
| 234 | http.Redirect(w, r, "/answer/feedback/"+strconv.FormatInt(id, 10), http.StatusSeeOther) |
| 235 | return |
| 236 | } |
| 237 | if r.PostForm.Get("inline") == "1" { |
| 238 | w.WriteHeader(http.StatusNoContent) |
| 239 | return |
| 240 | } |
| 241 | indexNotice.set(w, "success", "Thanks — saved under Reports.") |
| 242 | http.Redirect(w, r, "/?q="+url.QueryEscape(q), http.StatusSeeOther) |
| 243 | } |
| 244 | |
| 245 | // feedbackID reads {id} and the asker, or answers 404. |
| 246 | func (a *app) feedbackID(w http.ResponseWriter, r *http.Request) (suite.Subject, int64, bool) { |
| 247 | asker, ok := a.asker(r) |
| 248 | id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) |
| 249 | if !ok || err != nil { |
| 250 | a.errorPage(w, r, http.StatusNotFound, kindDocument, "") |
| 251 | return asker, 0, false |
| 252 | } |
| 253 | return asker, id, true |
| 254 | } |
| 255 | |
| 256 | // feedbackPage is GET /answer/feedback/{id}: "What was wrong?" for the |
| 257 | // asker's own 👎. Anyone else's id is a 404. |
| 258 | func (a *app) feedbackPage(w http.ResponseWriter, r *http.Request) { |
| 259 | asker, id, ok := a.feedbackID(w, r) |
| 260 | if !ok { |
| 261 | return |
| 262 | } |
| 263 | v, err := a.store.FeedbackFor(r.Context(), asker, id) |
| 264 | switch { |
| 265 | case errors.Is(err, ErrFeedbackNotFound): |
| 266 | a.errorPage(w, r, http.StatusNotFound, kindDocument, "") |
| 267 | return |
| 268 | case err != nil: |
| 269 | a.logger.Error("reading answer feedback failed", "err", err) |
| 270 | a.errorPage(w, r, http.StatusInternalServerError, kindDocument, "") |
| 271 | return |
| 272 | } |
| 273 | render(w, "feedback", feedbackPage{page: a.pageFor(r, frame{parts: []string{"What was wrong?"}}), Feedback: v}) |
| 274 | } |
| 275 | |
| 276 | // saveFeedbackNote is POST /answer/feedback/{id}: the note, on the |
| 277 | // asker's own row. |
| 278 | func (a *app) saveFeedbackNote(w http.ResponseWriter, r *http.Request) { |
| 279 | asker, id, ok := a.feedbackID(w, r) |
| 280 | if !ok { |
| 281 | return |
| 282 | } |
| 283 | if err := r.ParseForm(); err != nil { |
| 284 | a.errorPage(w, r, http.StatusBadRequest, kindDocument, "") |
| 285 | return |
| 286 | } |
| 287 | q, err := a.store.SetFeedbackNote(r.Context(), asker, id, r.PostForm.Get("note")) |
| 288 | switch { |
| 289 | case a.guardRefused(w, r, err): |
| 290 | return |
| 291 | case errors.Is(err, ErrFeedbackNotFound): |
| 292 | a.errorPage(w, r, http.StatusNotFound, kindDocument, "") |
| 293 | return |
| 294 | case err != nil: |
| 295 | a.logger.Error("saving a feedback note failed", "err", err) |
| 296 | a.errorPage(w, r, http.StatusInternalServerError, kindDocument, "") |
| 297 | return |
| 298 | } |
| 299 | indexNotice.set(w, "success", "Thanks — saved under Reports.") |
| 300 | http.Redirect(w, r, "/?q="+url.QueryEscape(q), http.StatusSeeOther) |
| 301 | } |
| 302 | |
| 303 | // deleteFeedback is POST /answer/feedback/{id}/delete. |
| 304 | func (a *app) deleteFeedback(w http.ResponseWriter, r *http.Request) { |
| 305 | asker, id, ok := a.feedbackID(w, r) |
| 306 | if !ok { |
| 307 | return |
| 308 | } |
| 309 | switch err := a.store.DeleteFeedback(r.Context(), asker, id); { |
| 310 | case a.guardRefused(w, r, err): |
| 311 | return |
| 312 | case errors.Is(err, ErrFeedbackNotFound): |
| 313 | a.errorPage(w, r, http.StatusNotFound, kindDocument, "") |
| 314 | return |
| 315 | case err != nil: |
| 316 | a.logger.Error("deleting answer feedback failed", "err", err) |
| 317 | a.errorPage(w, r, http.StatusInternalServerError, kindDocument, "") |
| 318 | return |
| 319 | } |
| 320 | http.Redirect(w, r, "/reports", http.StatusSeeOther) |
| 321 | } |
| 322 | |