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
3// Team mode's answers: the retryable 503 for Unknown standing, the
4// partial list, the member-only gate, a stream's lease-bounded re-check, and the label an
5// addressless member is shown by. Each is a no-op outside team mode.
6
7import (
8 "context"
9 "net/http"
10 "strconv"
11 "strings"
12 "time"
13
14 "amadan.net/oficina/home/roster"
15)
16
17// teamUnknown reports whether this request's standing could not be
18// decided. notFound answers it with a retryable 503: a 404 would tell a
19// team member their sheet is gone because Home was slow.
20func (a *App) teamUnknown(r *http.Request) bool {
21 if a.team == nil {
22 return false
23 }
24 t, ok := teamAccessFrom(r)
25 return ok && t.state() == roster.Unknown
26}
27
28// unavailable is the retryable answer for Unknown standing.
29func (a *App) unavailable(w http.ResponseWriter, r *http.Request) {
30 w.Header().Set("Cache-Control", "no-store")
31 w.Header().Set("Retry-After", strconv.Itoa(int(roster.RetryAfter/time.Second)))
32 a.answer(w, r, http.StatusServiceUnavailable, kindStatus, "Try again in a moment.")
33}
34
35// teamPartial reports whether a list built for viewer is incomplete
36// because their standing is Unknown: Paul's ruling (2026-10-28, Home
37// implementation/teammode-package.md §4) is a partial list with a
38// notice, not a 503. visibleSheets has already withheld the team rung, so
39// the list holds the person's shares and nothing more. Ask it after
40// building the list, of the same Access the list was built with: State
41// latches, so a lease that lapsed while listing marks the page partial.
42// NotMember is never partial: a guest's shares are their whole list.
43// Always false outside team mode.
44func (a *App) teamPartial(viewer Member) bool {
45 if a.team == nil {
46 return false
47 }
48 return viewer.team == nil || viewer.team.Partial()
49}
50
51// teamMemberOnly gates what makes something new on the instance (create,
52// import): team members only, as Docs' create. A guest gets 403, Unknown
53// a retryable 503. Outside team mode it lets everybody through, as
54// before.
55func (a *App) teamMemberOnly(w http.ResponseWriter, r *http.Request) bool {
56 if a.team == nil {
57 return true
58 }
59 t, ok := teamAccessFrom(r)
60 if !ok {
61 a.unavailable(w, r)
62 return false
63 }
64 switch t.state() {
65 case roster.Member:
66 return true
67 case roster.NotMember:
68 a.answer(w, r, http.StatusForbidden, kindStatus, "Only team members can do that.")
69 default:
70 a.unavailable(w, r)
71 }
72 return false
73}
74
75// streamAccess decides one stream delivery afresh and says how long a
76// standing-backed answer may stand before it must be asked again: the
77// Member lease's remaining time, or 0 when nothing expires on a timer
78// (outside team mode, or when the answer did not rest on a lease).
79//
80// Standing is read first, from this decision's Access, and reach is
81// decided against it. Not Member: only a share reaches, and a share does
82// not expire on a timer. Member: the reach may rest on the team rung, so
83// the answer always carries a timer — the lease's remainder, or a second
84// if it lapsed after standing was read — and the stream re-decides then.
85// Deciding reach first and asking standing after (oficina/slides #33)
86// let a lease that lapsed between the two read as "reaches, nothing to
87// renew", and a silent stream on a team-rung sheet stayed open unwatched.
88func (a *App) streamAccess(r *http.Request, sheetID uint64) (bool, time.Duration) {
89 if a.team == nil {
90 return a.streamAuthorized(r, sheetID), 0
91 }
92 t, err := a.resolveTeamAccess(r)
93 if err != nil {
94 return false, 0
95 }
96 if t.state() != roster.Member {
97 return a.mayRead(sheetID, a.asGuest(r.Context(), t.member)), 0
98 }
99 if !a.mayRead(sheetID, t.actor()) {
100 return false, 0
101 }
102 renew := t.Remaining()
103 if renew <= 0 {
104 renew = time.Second
105 }
106 return true, renew
107}
108
109// teamLabel is how an addressless (Home-only) member is shown where an
110// address would be: a share list row, a history author.
111const teamLabel = "Oficina account"
112
113// Label is the address, or for a member who has none — only a team-mode
114// Home-only member can lack one, since memberFor refuses to mint an
115// addressless member — teamLabel and the last four characters of their
116// person ref when the roster lookup supplied it ("Oficina account …k7qz",
117// as Home's Team page shows a ref). Outside team mode this is always
118// Address.
119func (m Member) Label() string {
120 if strings.TrimSpace(m.Address) != "" {
121 return m.Address
122 }
123 if m.team != nil {
124 return homeOnlyLabel(m.team.Subject())
125 }
126 return teamLabel
127}
128
129func homeOnlyLabel(subject string) string {
130 if len(subject) <= 4 {
131 return teamLabel
132 }
133 return teamLabel + " …" + subject[len(subject)-4:]
134}
135
136// homeOnlyRefs maps each Home-only member to its subject, for labels on
137// pages that list members without asking the roster (history). Nil
138// outside team mode or on a read failure, which only costs the suffix.
139func (a *App) homeOnlyRefs(ctx context.Context) map[uint64]string {
140 if a.team == nil {
141 return nil
142 }
143 rows, err := a.team.db.QueryContext(ctx, "SELECT member_id, subject FROM sheets_team_home_members WHERE issuer=?", a.team.issuer())
144 if err != nil {
145 return nil
146 }
147 defer rows.Close()
148 out := map[uint64]string{}
149 for rows.Next() {
150 var id uint64
151 var subject string
152 if rows.Scan(&id, &subject) != nil {
153 return nil
154 }
155 out[id] = subject
156 }
157 if rows.Err() != nil {
158 return nil
159 }
160 return out
161}
162