oficina / calendar Public

Clone
git clone https://amadan.net/oficina/calendar

Plain git — no account needed to clone.

Download

Download this file

1package calendar
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "log/slog"
8 "sync"
9 "time"
10
11 "gorm.io/gorm"
12
13 "amadan.net/oficina/calendar/internal/calendar/seal"
14)
15
16// defaultZone is the zone a person gets before they have chosen one.
17// UTC, not the server's local zone: a server's zone is an accident of
18// where it was provisioned, and inheriting it would silently shift
19// everyone's day boundaries the first time the fleet moved.
20const defaultZone = "UTC"
21
22// Store is every database access this app makes. Handlers never touch
23// gorm directly — one seam, so owner scoping is applied in one place
24// and can be tested in one place, and so every sealed column is sealed
25// and opened on the way past rather than at each call site.
26type Store struct {
27 g *gorm.DB
28 seal *seal.Sealer
29 logger *slog.Logger
30
31 // team is true in roster team mode: every scoped read joins through
32 // the team rung only while the context's standing is Member
33 // (team_reach.go), and every person's write is guarded inside its
34 // transaction (team_guard.go). False, and both are exactly what they
35 // were.
36 team bool
37
38 // holdMu serialises hold creation and rescheduling (booking spec
39 // B12). SQLite begins deferred, so two transactions can both read
40 // "free" before either takes the write lock; one process per
41 // instance makes a mutex the honest serialisation. It orders
42 // booking requests against each other — the write lock taken
43 // inside the transaction orders them against everything else.
44 holdMu sync.Mutex
45
46 // afterSlotCheckForTest, when set, runs between a hold's
47 // response-time slot check and its transaction. A test uses it to
48 // commit an ordinary event over the slot and prove the in-
49 // transaction re-check catches it. Nil in production.
50 afterSlotCheckForTest func()
51}
52
53func NewStore(g *gorm.DB, sealer *seal.Sealer, logger *slog.Logger) *Store {
54 return &Store{g: g, seal: sealer, logger: logger}
55}
56
57// EnsurePerson returns the Person row for a verified address, creating
58// it on first sign-in.
59//
60// The address is normalised first, so the unique index sees one
61// spelling per person. The insert is an upsert on that index rather
62// than a read-then-write: two sign-in links clicked at the same moment
63// would otherwise both read "no row" and both insert.
64func (s *Store) EnsurePerson(ctx context.Context, address string) (Person, error) {
65 addr := normalizeAddress(address)
66 if addr == "" {
67 return Person{}, errors.New("calendar: EnsurePerson: empty address")
68 }
69 now := time.Now().UTC()
70 var p Person
71
72 // FirstOrCreate on the unique index, then read back: the row we
73 // want may be the one this statement wrote or the one a concurrent
74 // sign-in wrote, and either is correct. Two links clicked at the
75 // same moment would otherwise both read "no row" and both insert.
76 if err := s.g.WithContext(ctx).
77 Where(Person{Address: addr}).
78 Attrs(Person{
79 Zone: defaultZone,
80 WeekStart: WeekStartMonday,
81 DefaultReminderMin: DefaultReminderMin,
82 DefaultAllDayMin: DefaultAllDayMin,
83 CreatedAt: now,
84 UpdatedAt: now,
85 }).
86 FirstOrCreate(&p).Error; err != nil {
87 return Person{}, fmt.Errorf("calendar: ensure person: %w", err)
88 }
89 return p, nil
90}
91
92// PersonByAddress reads the row for an already-known address. A missing
93// row is (Person{}, false, nil) rather than an error: "this address has
94// never signed in" is an ordinary answer, not a failure.
95func (s *Store) PersonByAddress(ctx context.Context, address string) (Person, bool, error) {
96 addr := normalizeAddress(address)
97 if addr == "" {
98 return Person{}, false, nil
99 }
100 var p Person
101 err := s.g.WithContext(ctx).Where("address = ?", addr).First(&p).Error
102 switch {
103 case errors.Is(err, gorm.ErrRecordNotFound):
104 return Person{}, false, nil
105 case err != nil:
106 return Person{}, false, fmt.Errorf("calendar: person by address: %w", err)
107 }
108 return p, true, nil
109}
110