| 1 | package calendar |
| 2 | |
| 3 | // Team mode's write guard: the standing a write was admitted on, checked |
| 4 | // again inside the transaction that commits it, before and after |
| 5 | // (teammode.Guard; Home implementation/teammode-package.md §3, invariant |
| 6 | // 13, and Fable's deviation 7 on oficina/sheets #52). The route's gate |
| 7 | // (team_access.go) and the store's reach (team_reach.go) decide whether a |
| 8 | // write may start; this decides whether it may commit, so a lease that |
| 9 | // lapses between the two fails the write instead of landing it. It never |
| 10 | // asks the roster: it reads the Access the context carries, whose lease |
| 11 | // check never waits, so it is safe under the one-connection SQLite writer. |
| 12 | // |
| 13 | // Calendar has no shares (team_reach.go), so every person's write rests on |
| 14 | // standing alone and needs Member: an event, a reminder, a feed, an |
| 15 | // import, settings, a booking page. A public booking write stands on its |
| 16 | // HOST's standing, carried the same way (team_booking.go). |
| 17 | // |
| 18 | // Outside team mode Store.team is false and every helper here is the |
| 19 | // write path exactly as it was. |
| 20 | |
| 21 | import ( |
| 22 | "context" |
| 23 | "errors" |
| 24 | |
| 25 | "gorm.io/gorm" |
| 26 | |
| 27 | "amadan.net/oficina/home/teammode" |
| 28 | ) |
| 29 | |
| 30 | // teamGuard is teammode.Guard. A variable only so a test can let the |
| 31 | // lease run out between a write and its commit on every write route. |
| 32 | var teamGuard = teammode.Guard |
| 33 | |
| 34 | type systemWriteKey struct{} |
| 35 | |
| 36 | // systemWrite marks ctx as carrying writes that nobody's standing makes: |
| 37 | // a booker's own cancellation, on the public routes, which stands on its |
| 38 | // token alone (team_booking.go). The scheduled drain's bookkeeping — |
| 39 | // claims, deferrals, fires, hold expiry, IP forgetting — and a feed's poll |
| 40 | // counter never go through the guard at all. Whatever such work DELIVERS |
| 41 | // to a person is decided on that person's own standing (team_jobs.go). |
| 42 | func systemWrite(ctx context.Context) context.Context { |
| 43 | return context.WithValue(ctx, systemWriteKey{}, true) |
| 44 | } |
| 45 | |
| 46 | func isSystemWrite(ctx context.Context) bool { |
| 47 | v, _ := ctx.Value(systemWriteKey{}).(bool) |
| 48 | return v |
| 49 | } |
| 50 | |
| 51 | // guard runs write under the standing ctx carries. Call it INSIDE the |
| 52 | // write's transaction, with write using that transaction. Outside team |
| 53 | // mode, and for a systemWrite, it is write alone. |
| 54 | func (s *Store) guard(ctx context.Context, write func() error) error { |
| 55 | if !s.team || isSystemWrite(ctx) { |
| 56 | return write() |
| 57 | } |
| 58 | // A context with no Access reads as Unknown (teammode.Standing), so a |
| 59 | // person's write that lost its standing on the way here is refused. |
| 60 | return teamGuard(ctx, teammode.RequireMember, write) |
| 61 | } |
| 62 | |
| 63 | // guarded runs write against the store's database exactly as it always |
| 64 | // has outside team mode (and for a systemWrite), and in team mode in one |
| 65 | // transaction, inside guard, against that transaction (never the outer |
| 66 | // handle: the writer pool has one connection). |
| 67 | func (s *Store) guarded(ctx context.Context, write func(db *gorm.DB) error) error { |
| 68 | if !s.team || isSystemWrite(ctx) { |
| 69 | return write(s.g.WithContext(ctx)) |
| 70 | } |
| 71 | return s.g.WithContext(ctx).Transaction(func(tx *gorm.DB) error { |
| 72 | return s.guard(ctx, func() error { return write(tx) }) |
| 73 | }) |
| 74 | } |
| 75 | |
| 76 | // standingRefusal reports whether err is the guard's (or a gate's) |
| 77 | // refusal: Unknown standing, or a definite NotMember. |
| 78 | func standingRefusal(err error) bool { |
| 79 | return errors.Is(err, teammode.ErrUnavailable) || errors.Is(err, teammode.ErrNotMember) |
| 80 | } |
| 81 | |