oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3import (
4 "net/http"
5 "strconv"
6
7 "amadan.net/oficina/sheets/internal/sheets/store"
8)
9
10type commentsView struct {
11 Sheet SheetSummary
12 Threads []CommentThread
13 MayComment bool
14 Notice string
15 NoticeTone string `json:"-"`
16}
17
18func commentNotice(code string) string {
19 switch code {
20 case "added":
21 return "Comment added."
22 case "resolved":
23 return "Thread resolved. It stays here, so the conversation is not lost."
24 case "empty":
25 return "A comment needs something in it."
26 case "no-cell":
27 return "That cell is not part of this sheet."
28 default:
29 return ""
30 }
31}
32
33// handleComments lists every thread on a sheet.
34//
35// Reading needs only a viewer's grant: a comment is about the sheet,
36// and somebody who can read the cells can read what was said about
37// them. Writing needs commenter, which is what that role is for.
38func (a *App) handleComments(w http.ResponseWriter, r *http.Request) {
39 s, ok := a.sheetFromURL(w, r)
40 if !ok {
41 return
42 }
43 viewer, ok := a.actor(r)
44 if !ok {
45 a.notFound(w, r)
46 return
47 }
48 h, err := a.openTabFor(s)
49 if err != nil {
50 a.fail(w, r, err)
51 return
52 }
53 defer h.Release()
54
55 threads, err := a.commentThreads(s.TabID, newPosIndex(h))
56 if err != nil {
57 a.fail(w, r, err)
58 return
59 }
60 a.render(w, r, "comments", onSheet(s, "Comments"), http.StatusOK, commentsView{
61 Sheet: s, Threads: threads,
62 MayComment: a.mayComment(s.ID, viewer),
63 Notice: commentNotice(r.URL.Query().Get("notice")),
64 NoticeTone: noticeTone(r.URL.Query().Get("notice")),
65 })
66}
67
68// handleCommentAdd writes a comment about a cell.
69func (a *App) handleCommentAdd(w http.ResponseWriter, r *http.Request) {
70 s, viewer, h, ok := a.commentTarget(w, r)
71 if !ok {
72 return
73 }
74 defer h.Release()
75
76 rowID, colID, ok := a.cellIDsFromForm(r, h)
77 if !ok {
78 a.commentRedirect(w, r, s, "no-cell")
79 return
80 }
81 if err := a.addComment(a.writeGuard(r.Context(), s.ID, viewer, SheetCommenter), s.TabID, rowID, colID, r.FormValue("body"), viewer); err != nil {
82 if a.guardRefused(w, r, err) {
83 return
84 }
85 // The body is not echoed. It is user content, and an error
86 // string is a log line waiting to happen (AGENTS.md).
87 a.commentRedirect(w, r, s, "empty")
88 return
89 }
90 a.commentRedirect(w, r, s, "added")
91}
92
93// handleCommentResolve settles a thread.
94func (a *App) handleCommentResolve(w http.ResponseWriter, r *http.Request) {
95 s, viewer, h, ok := a.commentTarget(w, r)
96 if !ok {
97 return
98 }
99 defer h.Release()
100
101 rowID, colID, ok := a.cellIDsFromForm(r, h)
102 if !ok {
103 a.commentRedirect(w, r, s, "no-cell")
104 return
105 }
106 if err := a.resolveThread(a.writeGuard(r.Context(), s.ID, viewer, SheetCommenter), s.TabID, rowID, colID, viewer); err != nil {
107 if a.guardRefused(w, r, err) {
108 return
109 }
110 a.fail(w, r, err)
111 return
112 }
113 a.commentRedirect(w, r, s, "resolved")
114}
115
116// commentTarget resolves the sheet, proves the viewer may comment, and
117// opens the tab. The caller must Release the handle.
118func (a *App) commentTarget(w http.ResponseWriter, r *http.Request) (SheetSummary, Member, *store.Handle, bool) {
119 s, ok := a.sheetFromURL(w, r)
120 if !ok {
121 return SheetSummary{}, Member{}, nil, false
122 }
123 viewer, ok := a.actor(r)
124 if !ok {
125 a.notFound(w, r)
126 return SheetSummary{}, Member{}, nil, false
127 }
128 if !a.mayComment(s.ID, viewer) {
129 // 404 like every other refusal: a viewer who cannot comment
130 // learns nothing about who can.
131 a.notFound(w, r)
132 return SheetSummary{}, Member{}, nil, false
133 }
134 h, err := a.openTabFor(s)
135 if err != nil {
136 a.fail(w, r, err)
137 return SheetSummary{}, Member{}, nil, false
138 }
139 return s, viewer, h, true
140}
141
142// cellIDsFromForm turns the r/c a form submitted into stable ids.
143//
144// EnsureRow/EnsureCol, so a comment can be left on a cell nobody has
145// typed into yet — which is a real thing to want ("this column should
146// hold X"). It is also why comments anchor on ids: the row exists from
147// the moment somebody talks about it.
148func (a *App) cellIDsFromForm(r *http.Request, h *store.Handle) (uint64, uint64, bool) {
149 row, err1 := strconv.Atoi(r.FormValue("r"))
150 col, err2 := strconv.Atoi(r.FormValue("c"))
151 if err1 != nil || err2 != nil || row < 0 || col < 0 ||
152 row >= maxWindowRows || col >= tableCols {
153 return 0, 0, false
154 }
155 rowID, err := h.EnsureRow(row)
156 if err != nil {
157 return 0, 0, false
158 }
159 colID, err := h.EnsureCol(col)
160 if err != nil {
161 return 0, 0, false
162 }
163 return rowID, colID, true
164}
165
166func (a *App) commentRedirect(w http.ResponseWriter, r *http.Request, s SheetSummary, notice string) {
167 if nativeNotice(w, r, notice) {
168 return
169 }
170 http.Redirect(w, r, "/sheets/"+strconv.FormatUint(s.ID, 10)+"/comments?notice="+notice,
171 http.StatusSeeOther)
172}
173