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 "errors"
5 "strings"
6 "time"
7
8 "gorm.io/gorm"
9
10 "amadan.net/oficina/sheets/internal/sheets/store"
11)
12
13// maxCommentRunes caps one comment.
14//
15// A note about a cell, not a document. Long enough for a real
16// paragraph, short enough that a thread stays readable beside the grid.
17const maxCommentRunes = 2000
18
19// CommentEntry is one comment, unsealed and ready to render.
20type CommentEntry struct {
21 ID uint64
22 Author string
23 Body string
24 When time.Time
25 Resolved bool
26}
27
28// CommentThread is every comment on one cell.
29//
30// A thread is defined by the cell, not by a parent id. That is the
31// simplest structure that matches what people do — they discuss a cell
32// — and it is the one that survives adopting Docs' primitive later,
33// since their thread will still be anchored somewhere.
34type CommentThread struct {
35 RowID, ColID uint64
36 // RowPos and ColPos are what a form posts back. The stable ids are
37 // how the thread is anchored; the positions are how a person and
38 // the routes address a cell, and the resolve form has to speak the
39 // same language cellIDsFromForm reads. Sending the ids instead is
40 // a mismatch that resolves the wrong thread — or, more likely,
41 // none, since an id and a position collide only by accident.
42 RowPos, ColPos int
43 Ref string // A1 notation, for reading
44 Entries []CommentEntry
45 Resolved bool
46}
47
48// mayComment is the gate the comment routes use.
49//
50// Commenter is enough — that is the entire point of the role, which
51// until now granted nothing beyond reading. Editors and owners clear it
52// too, because Allows is a floor.
53func (a *App) mayComment(sheetID uint64, m Member) bool {
54 role, ok := a.roleFor(sheetID, m)
55 return ok && role.Allows(SheetCommenter)
56}
57
58// addComment writes a comment on a cell.
59func (a *App) addComment(g store.Guard, tabID, rowID, colID uint64, body string, by Member) error {
60 if a.db == nil {
61 return errNoDB
62 }
63 if a.sealer == nil {
64 return errNoSealer
65 }
66 clean := strings.TrimSpace(body)
67 if clean == "" {
68 return errors.New("sheets: a comment needs something in it")
69 }
70 if r := []rune(clean); len(r) > maxCommentRunes {
71 clean = string(r[:maxCommentRunes])
72 }
73 sealed, err := a.sealer.Seal("comment.body", []byte(clean))
74 if err != nil {
75 return err
76 }
77 return a.guarded(g, func(db *gorm.DB) error {
78 return db.Create(&Comment{
79 TabID: tabID, RowID: rowID, ColID: colID,
80 AuthorID: by.ID, BodySealed: sealed,
81 }).Error
82 })
83}
84
85// resolveThread marks every comment on a cell as settled.
86//
87// The whole cell, not one comment: a thread is the unit people think
88// in, and resolving one message of a conversation says nothing useful.
89func (a *App) resolveThread(g store.Guard, tabID, rowID, colID uint64, by Member) error {
90 if a.db == nil {
91 return errNoDB
92 }
93 now := time.Now().UTC()
94 return a.guarded(g, func(db *gorm.DB) error {
95 return db.Model(&Comment{}).
96 Where("tab_id = ? AND row_id = ? AND col_id = ? AND resolved_at IS NULL",
97 tabID, rowID, colID).
98 Updates(map[string]any{"resolved_at": now, "resolved_by": by.ID}).Error
99 })
100}
101
102// commentThreads reads every thread on a tab, unsealed.
103//
104// Threads are keyed by cell and returned in a stable order — resolved
105// last, then by first comment — so a page of them does not reshuffle
106// between loads.
107func (a *App) commentThreads(tabID uint64, idx posIndex) ([]CommentThread, error) {
108 if a.db == nil {
109 return nil, errNoDB
110 }
111 if a.sealer == nil {
112 return nil, errNoSealer
113 }
114 var rows []Comment
115 if err := a.db.Where("tab_id = ?", tabID).Order("id ASC").Find(&rows).Error; err != nil {
116 return nil, err
117 }
118 names := a.memberNames()
119
120 type key struct{ r, c uint64 }
121 order := make([]key, 0, len(rows))
122 byCell := map[key]*CommentThread{}
123
124 for _, c := range rows {
125 body, err := a.sealer.Open("comment.body", c.BodySealed)
126 if err != nil {
127 // One unreadable comment must not take the thread with it.
128 continue
129 }
130 k := key{c.RowID, c.ColID}
131 th, ok := byCell[k]
132 if !ok {
133 rp, okR := idx.rowPos[c.RowID]
134 cp, okC := idx.colPos[c.ColID]
135 if !okR || !okC {
136 // A comment whose row or column is gone has nothing to
137 // point at. Skipped rather than shown against a
138 // coordinate we would have to invent.
139 continue
140 }
141 th = &CommentThread{
142 RowID: c.RowID, ColID: c.ColID,
143 RowPos: rp, ColPos: cp,
144 Ref: colLabel(cp) + itoaPos(rp+1), Resolved: true,
145 }
146 byCell[k] = th
147 order = append(order, k)
148 }
149 th.Entries = append(th.Entries, CommentEntry{
150 ID: c.ID, Author: names[c.AuthorID], Body: string(body),
151 When: c.CreatedAt, Resolved: c.ResolvedAt != nil,
152 })
153 if c.ResolvedAt == nil {
154 th.Resolved = false
155 }
156 }
157
158 out := make([]CommentThread, 0, len(order))
159 for _, k := range order {
160 if !byCell[k].Resolved {
161 out = append(out, *byCell[k])
162 }
163 }
164 for _, k := range order {
165 if byCell[k].Resolved {
166 out = append(out, *byCell[k])
167 }
168 }
169 return out, nil
170}
171
172// commentedCells reports which cells carry an unresolved thread, so the
173// grid and table can mark them.
174func (a *App) commentedCells(tabID uint64) map[[2]uint64]bool {
175 out := map[[2]uint64]bool{}
176 if a.db == nil {
177 return out
178 }
179 var rows []Comment
180 if err := a.db.Where("tab_id = ? AND resolved_at IS NULL", tabID).Find(&rows).Error; err != nil {
181 return out
182 }
183 for _, c := range rows {
184 out[[2]uint64{c.RowID, c.ColID}] = true
185 }
186 return out
187}
188
189// itoaPos is strconv.Itoa, named here so the coordinate-building code
190// reads the same as colLabel beside it.
191func itoaPos(n int) string {
192 if n == 0 {
193 return "0"
194 }
195 var b []byte
196 for n > 0 {
197 b = append([]byte{byte('0' + n%10)}, b...)
198 n /= 10
199 }
200 return string(b)
201}
202