package teammode import ( "context" "sync/atomic" "time" "amadan.net/oficina/home/roster" ) // Access is one request's team standing: // // - roster.Member: on the team, with the roster's role ("owner" or // "member"). The app's team rung. // - roster.NotMember: a guest. Only what was shared with them. // - roster.Unknown: the roster could not be read within its lease. Shares // still work; anything that would have needed standing answers a // retryable 503, never a 404 that would claim the item is not theirs. // // A Member answer carries a lease. State checks it every time it is asked, // without waiting (roster.Lease.Valid never queues behind a publication: on // contention it answers from the roster's last published state), and // latches Unknown the first time it finds the lease lapsed (expired, the // subject removed or their role changed, or the cache fenced), so one // request can never see Member again after it has seen Unknown. A held // decision therefore ends within roster.MaximumAge, existing sessions // included. // // Never store an Access beyond its request; a stream asks for a new one // before each delivery and at Remaining. type Access struct { subject string decision roster.Decision uncertain atomic.Bool } // State is the standing now. Safe under the SQLite writer: it never asks // the roster and never blocks. A nil Access is Unknown. func (a *Access) State() roster.State { if a == nil || a.uncertain.Load() { return roster.Unknown } if a.decision.State == roster.Member && !a.decision.Lease.Valid() { a.uncertain.Store(true) return roster.Unknown } return a.decision.State } // Role is the roster's role while State is Member ("owner" or "member"), // and "" otherwise. An app's stored role is never consulted for standing. func (a *Access) Role() string { if a.State() != roster.Member { return "" } return a.decision.Role } // Subject is the Home subject the roster was asked about, "" for none. func (a *Access) Subject() string { if a == nil { return "" } return a.subject } // Remaining is how long a Member answer may stand before it must be asked // again: the time a stream may run before re-deciding, or closing. 0 when // the answer does not rest on a lease (NotMember: nothing expires on a // timer) or when it has lapsed, which latches Unknown as State does. func (a *Access) Remaining() time.Duration { if a.State() != roster.Member { return 0 } left := a.decision.Lease.Remaining() if left <= 0 { a.uncertain.Store(true) return 0 } return left } // Partial reports whether a list built for this request is incomplete // because standing is Unknown. The ruling for list pages (implementation/ // teammode-package.md, "Unknown on list pages"): under Unknown, list what // the person reaches without standing — their shares — and render a // "partial" notice, rather than a 503 for the whole page. Ask it after // building the list, from the same Access: State latches, so a lease that // lapsed while listing marks the page partial. func (a *Access) Partial() bool { return a.State() == roster.Unknown } type accessKey struct{} // WithAccess carries a request's Access to the store, so a check inside the // writer transaction reads the same latched standing. func WithAccess(ctx context.Context, a *Access) context.Context { return context.WithValue(ctx, accessKey{}, a) } // FromContext is the Access WithAccess carried. func FromContext(ctx context.Context) (*Access, bool) { a, ok := ctx.Value(accessKey{}).(*Access) return a, ok && a != nil } // Standing is the carried Access's State, Unknown when none was carried. func Standing(ctx context.Context) roster.State { a, _ := FromContext(ctx) return a.State() } // Guard threads the lease check into the app's store transaction: it runs // authorize against the request's standing, then write, then authorize // again, so the transaction commits only if the standing that admitted the // write still holds after it. Call it inside the writer transaction: // // return db.Transaction(func(tx *gorm.DB) error { // return teammode.Guard(ctx, mayEdit(tx, doc), func() error { return save(tx) }) // }) // // authorize is the app's own rule over the standing — Unknown may still // reach a share — so Guard decides nothing itself. It never asks the roster // and never waits on it: a lapsed lease reads as Unknown, and a contended one // is answered from the roster's last published state, so a refresh // publication queued behind this transaction's writer cannot deadlock it, and // a decision that lapses mid-write fails the commit. func Guard(ctx context.Context, authorize func(roster.State) error, write func() error) error { if err := authorize(Standing(ctx)); err != nil { return err } if err := write(); err != nil { return err } return authorize(Standing(ctx)) } // RequireMember is the common authorize for Guard and for member-only // actions (create, import): nil for Member, ErrNotMember for NotMember, // ErrUnavailable for Unknown. func RequireMember(s roster.State) error { switch s { case roster.Member: return nil case roster.NotMember: return ErrNotMember } return ErrUnavailable }