package calls // Team mode's write guard: the standing a write was admitted on, checked // again inside the transaction that commits it, before and after // (teammode.Guard; Home implementation/teammode-package.md §3, invariant // 13). The route's gate (team_access.go) decides whether a write may // start; this decides whether it may commit, so a lease that lapses // between the two fails the write instead of landing it. It never asks // the roster: it reads the Access the context carries, whose lease check // never waits, so it is safe under the one-connection SQLite writer. // // Whose standing a write rests on is the caller's to put on the context: // // - a member's own write (a meeting, a link, an ending, settings, an // agent token, a device): the request's Access, put there by // requirePerson; // - an agent's write: its operator's, put there by requireAgent; // - a guest's grant, minted by following a link: the meeting HOST's // (team_host.go), because a link stands on its host. // // Writes nobody's standing makes — a meeting ended because its host left // the team, sign-in bookkeeping, identity rows — are systemWrite or never // reach the guard. Outside team mode store.team is false and every helper // here is the write path exactly as it was. import ( "context" "gorm.io/gorm" "amadan.net/oficina/home/teammode" ) // teamGuard is teammode.Guard. A variable only so a test can let the // lease run out between a write and its commit on every write route. var teamGuard = teammode.Guard type systemWriteKey struct{} // systemWrite marks ctx as carrying a write that nobody's standing makes: // the instance ending a meeting whose host the roster says has left. func systemWrite(ctx context.Context) context.Context { return context.WithValue(ctx, systemWriteKey{}, true) } func isSystemWrite(ctx context.Context) bool { v, _ := ctx.Value(systemWriteKey{}).(bool) return v } // guard runs write under the standing ctx carries. Call it INSIDE the // write's transaction, with write using that transaction. Outside team // mode, and for a systemWrite, it is write alone. A context with no // Access reads as Unknown (teammode.Standing), so a write that lost its // standing on the way here is refused. func (s *store) guard(ctx context.Context, write func() error) error { if !s.team || isSystemWrite(ctx) { return write() } return teamGuard(ctx, teammode.RequireMember, write) } // guarded runs write against the store's database exactly as it always // has outside team mode (and for a systemWrite), and in team mode in one // transaction, inside guard, against that transaction (never the outer // handle: the writer pool has one connection). func (s *store) guarded(ctx context.Context, write func(db *gorm.DB) error) error { if !s.team || isSystemWrite(ctx) { return write(s.g.WithContext(ctx)) } return s.g.WithContext(ctx).Transaction(func(tx *gorm.DB) error { return s.guard(ctx, func() error { return write(tx) }) }) }