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 "amadan.net/oficina/sheets/internal/sheets/store"
5 "context"
6)
7
8// visibleSheets returns the sheets a member may see.
9//
10// This is the hand-written grants join AGENTS.md requires, and the
11// reason it is hand-written rather than scope.Owned: scope.Owned is
12// single-column equality on user_id, our sheets are team-shared, and
13// applied here it would return only the sheets a person created — which
14// fails as a plausible short list rather than as an error.
15//
16// The query is deliberately one statement rather than "list all, then
17// filter in Go". A filter in Go reads every sheet on the instance into
18// memory before discarding most of them, which is both wasteful and the
19// shape where somebody later forgets the discard.
20func (a *App) visibleSheets(m Member) ([]store.Sheet, error) {
21 return a.visibleSheetsLimited(context.Background(), m, 0)
22}
23func (a *App) visibleSheetsLimited(ctx context.Context, m Member, limit int) ([]store.Sheet, error) {
24 if a.db == nil {
25 return nil, errNoDB
26 }
27 // filling_since alongside deleted_at, everywhere the other is
28 // checked: a sheet still being filled from an imported file is not
29 // a thing anybody should see yet, and on this instance every team
30 // member sees every sheet, so "not visible" has to mean not in this
31 // query rather than not linked to.
32 q := a.db.WithContext(ctx).Model(&store.Sheet{}).
33 Where("sheets.deleted_at IS NULL AND sheets.filling_since IS NULL")
34
35 switch a.teamRole(m) {
36 case TeamOwner, TeamMember:
37 // The whole instance. One team, one set of sheets.
38 default:
39 if a.team != nil {
40 // Team mode: shares only. The creator's own grant is the
41 // team rung's (ownGrantIgnored), and this person has none.
42 q = q.Joins("JOIN grants ON grants.sheet_id = sheets.id AND grants.member_id = ? AND grants.granted_by != grants.member_id", m.ID)
43 break
44 }
45 // A guest sees exactly what they were granted. The join is an
46 // inner join precisely so a guest with no grants gets an empty
47 // list rather than everything.
48 q = q.Joins("JOIN grants ON grants.sheet_id = sheets.id AND grants.member_id = ?", m.ID)
49 }
50
51 if limit > 0 {
52 q = q.Limit(limit)
53 }
54 var rows []store.Sheet
55 if err := q.Order("sheets.updated_at DESC, sheets.id DESC").Find(&rows).Error; err != nil {
56 return nil, err
57 }
58 return rows, nil
59}
60