| 1 | package teammode |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sync/atomic" |
| 6 | "time" |
| 7 | |
| 8 | "amadan.net/oficina/home/roster" |
| 9 | ) |
| 10 | |
| 11 | // Access is one request's team standing: |
| 12 | // |
| 13 | // - roster.Member: on the team, with the roster's role ("owner" or |
| 14 | // "member"). The app's team rung. |
| 15 | // - roster.NotMember: a guest. Only what was shared with them. |
| 16 | // - roster.Unknown: the roster could not be read within its lease. Shares |
| 17 | // still work; anything that would have needed standing answers a |
| 18 | // retryable 503, never a 404 that would claim the item is not theirs. |
| 19 | // |
| 20 | // A Member answer carries a lease. State checks it every time it is asked, |
| 21 | // without waiting (roster.Lease.Valid never queues behind a publication: on |
| 22 | // contention it answers from the roster's last published state), and |
| 23 | // latches Unknown the first time it finds the lease lapsed (expired, the |
| 24 | // subject removed or their role changed, or the cache fenced), so one |
| 25 | // request can never see Member again after it has seen Unknown. A held |
| 26 | // decision therefore ends within roster.MaximumAge, existing sessions |
| 27 | // included. |
| 28 | // |
| 29 | // Never store an Access beyond its request; a stream asks for a new one |
| 30 | // before each delivery and at Remaining. |
| 31 | type Access struct { |
| 32 | subject string |
| 33 | decision roster.Decision |
| 34 | uncertain atomic.Bool |
| 35 | } |
| 36 | |
| 37 | // State is the standing now. Safe under the SQLite writer: it never asks |
| 38 | // the roster and never blocks. A nil Access is Unknown. |
| 39 | func (a *Access) State() roster.State { |
| 40 | if a == nil || a.uncertain.Load() { |
| 41 | return roster.Unknown |
| 42 | } |
| 43 | if a.decision.State == roster.Member && !a.decision.Lease.Valid() { |
| 44 | a.uncertain.Store(true) |
| 45 | return roster.Unknown |
| 46 | } |
| 47 | return a.decision.State |
| 48 | } |
| 49 | |
| 50 | // Role is the roster's role while State is Member ("owner" or "member"), |
| 51 | // and "" otherwise. An app's stored role is never consulted for standing. |
| 52 | func (a *Access) Role() string { |
| 53 | if a.State() != roster.Member { |
| 54 | return "" |
| 55 | } |
| 56 | return a.decision.Role |
| 57 | } |
| 58 | |
| 59 | // Subject is the Home subject the roster was asked about, "" for none. |
| 60 | func (a *Access) Subject() string { |
| 61 | if a == nil { |
| 62 | return "" |
| 63 | } |
| 64 | return a.subject |
| 65 | } |
| 66 | |
| 67 | // Remaining is how long a Member answer may stand before it must be asked |
| 68 | // again: the time a stream may run before re-deciding, or closing. 0 when |
| 69 | // the answer does not rest on a lease (NotMember: nothing expires on a |
| 70 | // timer) or when it has lapsed, which latches Unknown as State does. |
| 71 | func (a *Access) Remaining() time.Duration { |
| 72 | if a.State() != roster.Member { |
| 73 | return 0 |
| 74 | } |
| 75 | left := a.decision.Lease.Remaining() |
| 76 | if left <= 0 { |
| 77 | a.uncertain.Store(true) |
| 78 | return 0 |
| 79 | } |
| 80 | return left |
| 81 | } |
| 82 | |
| 83 | // Partial reports whether a list built for this request is incomplete |
| 84 | // because standing is Unknown. The ruling for list pages (implementation/ |
| 85 | // teammode-package.md, "Unknown on list pages"): under Unknown, list what |
| 86 | // the person reaches without standing — their shares — and render a |
| 87 | // "partial" notice, rather than a 503 for the whole page. Ask it after |
| 88 | // building the list, from the same Access: State latches, so a lease that |
| 89 | // lapsed while listing marks the page partial. |
| 90 | func (a *Access) Partial() bool { return a.State() == roster.Unknown } |
| 91 | |
| 92 | type accessKey struct{} |
| 93 | |
| 94 | // WithAccess carries a request's Access to the store, so a check inside the |
| 95 | // writer transaction reads the same latched standing. |
| 96 | func WithAccess(ctx context.Context, a *Access) context.Context { |
| 97 | return context.WithValue(ctx, accessKey{}, a) |
| 98 | } |
| 99 | |
| 100 | // FromContext is the Access WithAccess carried. |
| 101 | func FromContext(ctx context.Context) (*Access, bool) { |
| 102 | a, ok := ctx.Value(accessKey{}).(*Access) |
| 103 | return a, ok && a != nil |
| 104 | } |
| 105 | |
| 106 | // Standing is the carried Access's State, Unknown when none was carried. |
| 107 | func Standing(ctx context.Context) roster.State { |
| 108 | a, _ := FromContext(ctx) |
| 109 | return a.State() |
| 110 | } |
| 111 | |
| 112 | // Guard threads the lease check into the app's store transaction: it runs |
| 113 | // authorize against the request's standing, then write, then authorize |
| 114 | // again, so the transaction commits only if the standing that admitted the |
| 115 | // write still holds after it. Call it inside the writer transaction: |
| 116 | // |
| 117 | // return db.Transaction(func(tx *gorm.DB) error { |
| 118 | // return teammode.Guard(ctx, mayEdit(tx, doc), func() error { return save(tx) }) |
| 119 | // }) |
| 120 | // |
| 121 | // authorize is the app's own rule over the standing — Unknown may still |
| 122 | // reach a share — so Guard decides nothing itself. It never asks the roster |
| 123 | // and never waits on it: a lapsed lease reads as Unknown, and a contended one |
| 124 | // is answered from the roster's last published state, so a refresh |
| 125 | // publication queued behind this transaction's writer cannot deadlock it, and |
| 126 | // a decision that lapses mid-write fails the commit. |
| 127 | func Guard(ctx context.Context, authorize func(roster.State) error, write func() error) error { |
| 128 | if err := authorize(Standing(ctx)); err != nil { |
| 129 | return err |
| 130 | } |
| 131 | if err := write(); err != nil { |
| 132 | return err |
| 133 | } |
| 134 | return authorize(Standing(ctx)) |
| 135 | } |
| 136 | |
| 137 | // RequireMember is the common authorize for Guard and for member-only |
| 138 | // actions (create, import): nil for Member, ErrNotMember for NotMember, |
| 139 | // ErrUnavailable for Unknown. |
| 140 | func RequireMember(s roster.State) error { |
| 141 | switch s { |
| 142 | case roster.Member: |
| 143 | return nil |
| 144 | case roster.NotMember: |
| 145 | return ErrNotMember |
| 146 | } |
| 147 | return ErrUnavailable |
| 148 | } |
| 149 | |