oficina / calendar Public

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

Plain git — no account needed to clone.

Download

Download this file

1// Package teamstate keeps the Home-only people a roster admitted in team
2// mode, and the reverse lookup from a person to the Home subject the
3// roster knows them by.
4//
5// The marker and the roster evidence are not here: they are
6// home/teammode's State ("calendar", set calendar_team), merged beside
7// this package's own set (calendar_team_home). Calendar's local people
8// are its people table, whose address column is NOT NULL and UNIQUE, so a
9// Home-only person is a people row whose address is a random "home-only:"
10// placeholder that holds no address — no "@", never mailed, never shown —
11// and this package's table says which Home subject it belongs to.
12//
13// None of it is standing. A Home-only row says who a Home subject is on
14// this instance, never whether they are on the team: that is the
15// roster's answer, asked again on every request.
16package teamstate
17
18import (
19 "context"
20 "crypto/rand"
21 "database/sql"
22 "embed"
23 "encoding/hex"
24 "errors"
25 "strings"
26 "time"
27 "unicode/utf8"
28
29 "amadan.net/rastrillo/rastrillo/migrate"
30)
31
32//go:embed migrations/*.sql
33var migrations embed.FS
34
35// Schema is the Home-only people table and its triggers. They name people
36// and home_identity_links, so it is merged after Calendar's own schema and
37// linkstore's (team_config.go's teamBoot).
38var Schema = migrate.MustFromFS(migrations, "calendar_team_home")
39
40var (
41 // ErrRefused is a definite "no": no such person, or a subject that
42 // cannot be one.
43 ErrRefused = errors.New("calendar team: refused")
44 // ErrUnavailable is "cannot read it now".
45 ErrUnavailable = errors.New("calendar team: unavailable")
46)
47
48// placeholderPrefix starts every Home-only person's address column. The
49// triggers check it, and Addressless recognises it: nothing that sends
50// mail or shows an address may use such a value.
51const placeholderPrefix = "home-only:"
52
53// Addressless reports whether a people.address value is a Home-only
54// placeholder rather than an address.
55func Addressless(address string) bool {
56 return strings.HasPrefix(address, placeholderPrefix) || !strings.Contains(address, "@")
57}
58
59// HomePerson reads the person a Home-only identity was given. It never
60// creates one: ErrRefused means there is none.
61func HomePerson(ctx context.Context, db *sql.DB, issuer, subject string) (int64, error) {
62 if !Opaque(subject) {
63 return 0, ErrRefused
64 }
65 var id int64
66 e := db.QueryRowContext(ctx, "SELECT person_id FROM calendar_team_home_people WHERE issuer=? AND subject=?", issuer, subject).Scan(&id)
67 if errors.Is(e, sql.ErrNoRows) {
68 return 0, ErrRefused
69 }
70 if e != nil {
71 return 0, ErrUnavailable
72 }
73 return id, nil
74}
75
76// HomeSubject is the reverse: the Home subject a person is known by on
77// this instance, through a Home-only row or an explicit account link.
78// ErrRefused means neither, which is no team standing, never an error.
79func HomeSubject(ctx context.Context, db *sql.DB, issuer string, person int64) (string, error) {
80 var subject string
81 e := db.QueryRowContext(ctx, `SELECT subject FROM calendar_team_home_people WHERE issuer=? AND person_id=?
82 UNION ALL SELECT subject FROM home_identity_links WHERE issuer=? AND person_id=? LIMIT 1`, issuer, person, issuer, person).Scan(&subject)
83 if errors.Is(e, sql.ErrNoRows) {
84 return "", ErrRefused
85 }
86 if e != nil {
87 return "", ErrUnavailable
88 }
89 return subject, nil
90}
91
92// EnsureHomePerson gives a Home subject an addressless person, or returns
93// the one it already has; created says which. The caller must have just
94// had a current roster answer of Member for this subject: this records
95// identity, not standing, and a later request asks the roster again. A
96// subject already linked to a local account is refused — that person
97// signs in as that account.
98//
99// The person's preferences start as a first local sign-in's do
100// (EnsurePerson): zone, week start and the two reminder defaults are the
101// caller's. Their calendars are made on their first page, as everybody's
102// are (requirePerson).
103func EnsureHomePerson(ctx context.Context, db *sql.DB, issuer, subject string, defaults Defaults) (id int64, created bool, err error) {
104 if !Opaque(subject) || issuer == "" {
105 return 0, false, ErrRefused
106 }
107 tx, e := db.BeginTx(ctx, nil)
108 if e != nil {
109 return 0, false, ErrUnavailable
110 }
111 defer tx.Rollback()
112 var linked int
113 if e = tx.QueryRowContext(ctx, "SELECT COUNT(*) FROM home_identity_links WHERE issuer=? AND subject=?", issuer, subject).Scan(&linked); e != nil {
114 return 0, false, ErrUnavailable
115 }
116 if linked != 0 {
117 return 0, false, ErrRefused
118 }
119 e = tx.QueryRowContext(ctx, "SELECT person_id FROM calendar_team_home_people WHERE issuer=? AND subject=?", issuer, subject).Scan(&id)
120 if e == nil {
121 if tx.Commit() != nil {
122 return 0, false, ErrUnavailable
123 }
124 return id, false, nil
125 }
126 if !errors.Is(e, sql.ErrNoRows) {
127 return 0, false, ErrUnavailable
128 }
129 placeholder, e := newPlaceholder()
130 if e != nil {
131 return 0, false, ErrUnavailable
132 }
133 now := time.Now().UTC()
134 res, e := tx.ExecContext(ctx, `INSERT INTO people(address,zone,created_at,updated_at,week_start,default_reminder_min,default_all_day_min)
135 VALUES(?,?,?,?,?,?,?)`, placeholder, defaults.Zone, now, now, defaults.WeekStart, defaults.ReminderMin, defaults.AllDayMin)
136 if e != nil {
137 return 0, false, ErrUnavailable
138 }
139 if id, e = res.LastInsertId(); e != nil || id <= 0 {
140 return 0, false, ErrUnavailable
141 }
142 if _, e = tx.ExecContext(ctx, "INSERT INTO calendar_team_home_people(issuer,subject,person_id) VALUES(?,?,?)", issuer, subject, id); e != nil {
143 return 0, false, ErrUnavailable
144 }
145 if tx.Commit() != nil {
146 return 0, false, ErrUnavailable
147 }
148 return id, true, nil
149}
150
151// Defaults are a new person's starting preferences.
152type Defaults struct {
153 Zone string
154 WeekStart, ReminderMin, AllDayMin int
155}
156
157// newPlaceholder is the people.address a Home-only person holds: the
158// prefix and sixteen random bytes, unique (the column is) and never an
159// address (no "@").
160func newPlaceholder() (string, error) {
161 b := make([]byte, 16)
162 if _, err := rand.Read(b); err != nil {
163 return "", err
164 }
165 return placeholderPrefix + hex.EncodeToString(b), nil
166}
167
168// Opaque is a Home subject's shape: bounded, printable, and never an
169// address, so a subject can never be mistaken for one.
170func Opaque(s string) bool {
171 if s == "" || len(s) > 512 || !utf8.ValidString(s) || strings.Contains(s, "@") {
172 return false
173 }
174 for _, r := range s {
175 if r <= 32 || r == 127 {
176 return false
177 }
178 }
179 return true
180}
181