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 "context"
5 "math"
6 "strconv"
7
8 access "amadan.net/oficina/agent-access.git"
9 "amadan.net/oficina/home/roster"
10 "amadan.net/oficina/sheets/internal/sheets/calc"
11 "amadan.net/oficina/sheets/internal/sheets/store"
12)
13
14type agentSheetSummary struct {
15 ID string `json:"id"`
16 Title string `json:"title"`
17 URL string `json:"url"`
18}
19type agentTabSummary struct {
20 ID string `json:"id"`
21 Name string `json:"name"`
22}
23
24func agentPageSize(limit int) (int, error) {
25 if limit == 0 {
26 return 20, nil
27 }
28 if limit < 1 || limit > 100 {
29 return 0, access.Fail("invalid", "Choose a page size from 1 to 100.")
30 }
31 return limit, nil
32}
33func (a *sheetAgent) list(ctx context.Context, member Member, limit int, cursor string) (any, error) {
34 limit, err := agentPageSize(limit)
35 if err != nil {
36 return nil, err
37 }
38 if a.app.team != nil && member.team.state() == roster.Unknown {
39 return nil, sheetAgentFailure(errTeamUnavailable)
40 }
41 if state, ok := ctx.Value(sheetAgentKey{}).(*sheetAgentContext); ok {
42 state.list = true
43 }
44 team := a.app.teamRole(member) == TeamOwner || a.app.teamRole(member) == TeamMember
45 selfGrant := a.app.team == nil || team
46 q := a.app.db.WithContext(ctx).Model(&store.Sheet{}).Where("sheets.deleted_at IS NULL AND sheets.filling_since IS NULL").Where(`EXISTS(SELECT 1 FROM grants g WHERE g.sheet_id=sheets.id AND g.member_id=? AND g.role IN ('owner','editor','commenter','viewer') AND (? OR g.granted_by != g.member_id)) OR (? AND NOT EXISTS(SELECT 1 FROM grants g WHERE g.sheet_id=sheets.id AND g.member_id=?))`, member.ID, selfGrant, team, member.ID)
47 if cursor != "" {
48 id, e := sheetAgentID(cursor)
49 if e != nil {
50 return nil, e
51 }
52 q = q.Where("sheets.id < ?", id)
53 }
54 var rows []store.Sheet
55 if err = q.Order("sheets.id DESC").Limit(limit + 1).Find(&rows).Error; err != nil {
56 return nil, sheetAgentFailure(err)
57 }
58 next := ""
59 if len(rows) > limit {
60 rows = rows[:limit]
61 next = strconv.FormatUint(rows[len(rows)-1].ID, 10)
62 }
63 out := make([]agentSheetSummary, 0, len(rows))
64 for _, row := range rows {
65 title, e := store.ReadTitle(a.app.sealer, row)
66 if e != nil {
67 return nil, sheetAgentFailure(e)
68 }
69 a.remember(ctx, row.ID)
70 id := strconv.FormatUint(row.ID, 10)
71 out = append(out, agentSheetSummary{id, string(title), a.app.origin + "/sheets/" + id})
72 }
73 return sheetAgentBounded(struct {
74 Sheets []agentSheetSummary `json:"sheets"`
75 Next string `json:"next_cursor"`
76 }{out, next})
77}
78func (a *sheetAgent) tabs(ctx context.Context, member Member, sheetID uint64, limit int, cursor string) (any, error) {
79 role, err := a.role(ctx, a.app.db, sheetID, member)
80 if err != nil {
81 return nil, sheetAgentFailure(err)
82 }
83 if !role.Allows(SheetViewer) {
84 return nil, sheetAgentFailure(errNoAccess)
85 }
86 limit, err = agentPageSize(limit)
87 if err != nil {
88 return nil, err
89 }
90 q := a.app.db.WithContext(ctx).Where("sheet_id=?", sheetID)
91 if cursor != "" {
92 id, e := sheetAgentID(cursor)
93 if e != nil {
94 return nil, e
95 }
96 q = q.Where("id > ?", id)
97 }
98 var rows []store.Tab
99 if err = q.Order("id ASC").Limit(limit + 1).Find(&rows).Error; err != nil {
100 return nil, sheetAgentFailure(err)
101 }
102 next := ""
103 if len(rows) > limit {
104 rows = rows[:limit]
105 next = strconv.FormatUint(rows[len(rows)-1].ID, 10)
106 }
107 out := make([]agentTabSummary, 0, len(rows))
108 for _, row := range rows {
109 name, e := store.ReadTabName(a.app.sealer, row)
110 if e != nil {
111 return nil, sheetAgentFailure(e)
112 }
113 out = append(out, agentTabSummary{strconv.FormatUint(row.ID, 10), string(name)})
114 }
115 a.remember(ctx, sheetID)
116 return sheetAgentBounded(struct {
117 Tabs []agentTabSummary `json:"tabs"`
118 Next string `json:"next_cursor"`
119 }{out, next})
120}
121
122type agentRangeCell struct {
123 Row int `json:"row"`
124 Column int `json:"column"`
125 Raw string `json:"raw"`
126 Kind string `json:"kind"`
127 Value any `json:"value"`
128 Display string `json:"display"`
129}
130type agentRange struct {
131 SheetID string `json:"sheet_id"`
132 TabID string `json:"tab_id"`
133 Revision string `json:"revision"`
134 Cells []agentRangeCell `json:"cells"`
135}
136
137func (a *sheetAgent) readRange(ctx context.Context, member Member, sheetID, tabID uint64, row, col, height, width int) (any, error) {
138 if height < 1 || width < 1 || height > 500 || width > 500 || height > 500/width || !inCellRange(row, col) || !inCellRange(row+height-1, col+width-1) {
139 return nil, access.Fail("invalid", "Choose a supported range of at most 500 cells.")
140 }
141 if err := a.tab(ctx, a.app.db, sheetID, tabID, member); err != nil {
142 return nil, sheetAgentFailure(err)
143 }
144 handle, err := store.Open(a.app.db, a.app.sealer, tabID)
145 if err != nil {
146 return nil, sheetAgentFailure(err)
147 }
148 defer handle.Release()
149 snapshot, err := handle.ReadRange(row, col, height, width)
150 if err != nil {
151 return nil, sheetAgentFailure(err)
152 }
153 cells := make([]agentRangeCell, 0, len(snapshot.Cells))
154 for _, cell := range snapshot.Cells {
155 out := agentRangeCell{Row: cell.Row, Column: cell.Col, Raw: cell.Raw, Kind: "empty", Display: display(cell.Value, nil)}
156 switch cell.Value.Kind {
157 case calc.KindNum:
158 if math.IsNaN(cell.Value.Num) || math.IsInf(cell.Value.Num, 0) {
159 return nil, access.Fail("unavailable", "A calculated value is not finite.")
160 }
161 out.Kind = "number"
162 out.Value = cell.Value.Num
163 case calc.KindStr:
164 out.Kind = "string"
165 out.Value = cell.Value.Str
166 case calc.KindBool:
167 out.Kind = "boolean"
168 out.Value = cell.Value.Bool
169 case calc.KindErr:
170 out.Kind = "error"
171 out.Value = out.Display
172 case calc.KindEmpty:
173 default:
174 return nil, access.Fail("unavailable", "A calculated value is unavailable.")
175 }
176 cells = append(cells, out)
177 }
178 a.remember(ctx, sheetID)
179 return sheetAgentBounded(agentRange{strconv.FormatUint(sheetID, 10), strconv.FormatUint(tabID, 10), strconv.FormatUint(snapshot.Version, 10), cells})
180}
181