| 1 | // Package teamstate keeps team mode's schema and the Home-only members a |
| 2 | // roster admitted. The schema (sheets_team, 0001_team.sql) holds the |
| 3 | // mandatory-mode marker and the roster evidence, which home/teammode's |
| 4 | // State reads and writes (sheets.teamState), and the Home-only identity |
| 5 | // table this package owns: Sheets' local people are its members table, so |
| 6 | // a Home-only person is a members row with no address. The file stays |
| 7 | // Sheets' own because the migration ledger checksums it whole. |
| 8 | // |
| 9 | // None of it is standing. A Home-only member row says who a Home subject |
| 10 | // is on this instance, never whether they are on the team: that is the |
| 11 | // roster's answer, asked again on every request. |
| 12 | package teamstate |
| 13 | |
| 14 | import ( |
| 15 | "context" |
| 16 | "database/sql" |
| 17 | "embed" |
| 18 | "errors" |
| 19 | "strings" |
| 20 | "time" |
| 21 | "unicode/utf8" |
| 22 | |
| 23 | "amadan.net/rastrillo/rastrillo/migrate" |
| 24 | ) |
| 25 | |
| 26 | //go:embed migrations/*.sql |
| 27 | var migrations embed.FS |
| 28 | |
| 29 | // Schema follows Sheets' own and linkstore's: its triggers name members |
| 30 | // and home_identity_links, so it is merged after both (TeamBootSchema). |
| 31 | var Schema = migrate.MustFromFS(migrations, "sheets_team") |
| 32 | |
| 33 | var ( |
| 34 | ErrRefused = errors.New("sheets team: refused") |
| 35 | ErrUnavailable = errors.New("sheets team: unavailable") |
| 36 | ) |
| 37 | |
| 38 | // HomeMember reads the member a Home-only identity was given. It never |
| 39 | // creates one: ErrRefused means there is none. |
| 40 | func HomeMember(ctx context.Context, db *sql.DB, issuer, subject string) (int64, error) { |
| 41 | if !Opaque(subject) { |
| 42 | return 0, ErrRefused |
| 43 | } |
| 44 | var id int64 |
| 45 | e := db.QueryRowContext(ctx, "SELECT member_id FROM sheets_team_home_members WHERE issuer=? AND subject=?", issuer, subject).Scan(&id) |
| 46 | if errors.Is(e, sql.ErrNoRows) { |
| 47 | return 0, ErrRefused |
| 48 | } |
| 49 | if e != nil { |
| 50 | return 0, ErrUnavailable |
| 51 | } |
| 52 | return id, nil |
| 53 | } |
| 54 | |
| 55 | // HomeSubject is the reverse: the Home subject a member is known by on |
| 56 | // this instance, through a Home-only row or an explicit account link. |
| 57 | // ErrRefused means neither, which is no standing, never an error. |
| 58 | func HomeSubject(ctx context.Context, db *sql.DB, issuer string, member int64) (string, error) { |
| 59 | var subject string |
| 60 | e := db.QueryRowContext(ctx, `SELECT subject FROM sheets_team_home_members WHERE issuer=? AND member_id=? |
| 61 | UNION ALL SELECT subject FROM home_identity_links WHERE issuer=? AND person_id=? LIMIT 1`, issuer, member, issuer, member).Scan(&subject) |
| 62 | if errors.Is(e, sql.ErrNoRows) { |
| 63 | return "", ErrRefused |
| 64 | } |
| 65 | if e != nil { |
| 66 | return "", ErrUnavailable |
| 67 | } |
| 68 | return subject, nil |
| 69 | } |
| 70 | |
| 71 | // EnsureHomeMember gives a Home subject an addressless member, or |
| 72 | // returns the one it already has. The caller must have just had a |
| 73 | // current roster answer of Member for this subject: this function |
| 74 | // records identity, not standing, and a later request asks the roster |
| 75 | // again. A subject already linked to a local account is refused — that |
| 76 | // person signs in as that account. |
| 77 | func EnsureHomeMember(ctx context.Context, db *sql.DB, issuer, subject string) (int64, error) { |
| 78 | if !Opaque(subject) || issuer == "" { |
| 79 | return 0, ErrRefused |
| 80 | } |
| 81 | tx, e := db.BeginTx(ctx, nil) |
| 82 | if e != nil { |
| 83 | return 0, ErrUnavailable |
| 84 | } |
| 85 | defer tx.Rollback() |
| 86 | var linked int |
| 87 | if e = tx.QueryRowContext(ctx, "SELECT COUNT(*) FROM home_identity_links WHERE issuer=? AND subject=?", issuer, subject).Scan(&linked); e != nil { |
| 88 | return 0, ErrUnavailable |
| 89 | } |
| 90 | if linked != 0 { |
| 91 | return 0, ErrRefused |
| 92 | } |
| 93 | var id int64 |
| 94 | e = tx.QueryRowContext(ctx, "SELECT member_id FROM sheets_team_home_members WHERE issuer=? AND subject=?", issuer, subject).Scan(&id) |
| 95 | if e == nil { |
| 96 | if tx.Commit() != nil { |
| 97 | return 0, ErrUnavailable |
| 98 | } |
| 99 | return id, nil |
| 100 | } |
| 101 | if !errors.Is(e, sql.ErrNoRows) { |
| 102 | return 0, ErrUnavailable |
| 103 | } |
| 104 | res, e := tx.ExecContext(ctx, "INSERT INTO members(address,team_role,created_at) VALUES(NULL,'guest',?)", time.Now().UTC()) |
| 105 | if e != nil { |
| 106 | return 0, ErrUnavailable |
| 107 | } |
| 108 | if id, e = res.LastInsertId(); e != nil || id <= 0 { |
| 109 | return 0, ErrUnavailable |
| 110 | } |
| 111 | if _, e = tx.ExecContext(ctx, "INSERT INTO sheets_team_home_members(issuer,subject,member_id) VALUES(?,?,?)", issuer, subject, id); e != nil { |
| 112 | return 0, ErrUnavailable |
| 113 | } |
| 114 | if tx.Commit() != nil { |
| 115 | return 0, ErrUnavailable |
| 116 | } |
| 117 | return id, nil |
| 118 | } |
| 119 | |
| 120 | // Opaque is a Home subject's shape: bounded, printable, and never an |
| 121 | // address, so a subject can never be mistaken for one. |
| 122 | func Opaque(s string) bool { |
| 123 | if s == "" || len(s) > 512 || !utf8.ValidString(s) || strings.Contains(s, "@") { |
| 124 | return false |
| 125 | } |
| 126 | for _, r := range s { |
| 127 | if r <= 32 || r == 127 { |
| 128 | return false |
| 129 | } |
| 130 | } |
| 131 | return true |
| 132 | } |
| 133 | |