| 1 | package calls |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | |
| 8 | "amadan.net/oficina/home/roster" |
| 9 | ) |
| 10 | |
| 11 | func normalizeAddress(address string) string { |
| 12 | return strings.ToLower(strings.TrimSpace(address)) |
| 13 | } |
| 14 | |
| 15 | // allowlist is the team in lone shape (spec §7.1): the addresses that |
| 16 | // are members. A nil map admits everyone, which is allowed only in |
| 17 | // development. |
| 18 | type allowlist struct { |
| 19 | addrs map[string]struct{} |
| 20 | } |
| 21 | |
| 22 | func (a allowlist) allows(address string) bool { |
| 23 | if a.addrs == nil { |
| 24 | return true |
| 25 | } |
| 26 | _, ok := a.addrs[normalizeAddress(address)] |
| 27 | return ok |
| 28 | } |
| 29 | |
| 30 | func resolveAllowlist(cfg Config) (allowlist, error) { |
| 31 | addrs := make(map[string]struct{}) |
| 32 | for _, addr := range cfg.AllowedAddresses { |
| 33 | addr = normalizeAddress(addr) |
| 34 | if addr == "" { |
| 35 | continue |
| 36 | } |
| 37 | addrs[addr] = struct{}{} |
| 38 | } |
| 39 | if len(addrs) > 0 { |
| 40 | return allowlist{addrs: addrs}, nil |
| 41 | } |
| 42 | if cfg.Team != nil { |
| 43 | // Team mode: the roster decides standing, and the list is only |
| 44 | // local sign-in's admission. Empty admits nobody locally, in |
| 45 | // every Env: a team instance never takes the development |
| 46 | // default that admits everyone. |
| 47 | return allowlist{addrs: addrs}, nil |
| 48 | } |
| 49 | if cfg.development() { |
| 50 | return allowlist{}, nil |
| 51 | } |
| 52 | return allowlist{}, fmt.Errorf("calls: $CALLS_ALLOWED_ADDRESSES is empty and Env is %q, not %q; refusing to start with no team", cfg.Env, EnvDevelopment) |
| 53 | } |
| 54 | |
| 55 | // Standing is a person's entry authority toward a meeting (spec §2.3). |
| 56 | // Computed, never stored. |
| 57 | type Standing int |
| 58 | |
| 59 | const ( |
| 60 | StandingNone Standing = iota |
| 61 | StandingGuest |
| 62 | StandingMember |
| 63 | ) |
| 64 | |
| 65 | func (s Standing) String() string { |
| 66 | switch s { |
| 67 | case StandingMember: |
| 68 | return "member" |
| 69 | case StandingGuest: |
| 70 | return "guest" |
| 71 | } |
| 72 | return "none" |
| 73 | } |
| 74 | |
| 75 | // allowNow is the team as it stands right now. It is read through an |
| 76 | // atomic rather than straight off a.cfg because TestHandle.SetAllowlist |
| 77 | // swaps it while it is being read — by request handlers on their own |
| 78 | // goroutines, which is what the race detector actually catches here, and |
| 79 | // by the hub's tick when a test drives one. A member dropped from the |
| 80 | // team mid-call is what spec §2.4's tick has to notice, and there is no |
| 81 | // other way to make that happen in a test without rewriting an |
| 82 | // environment variable and restarting the process. |
| 83 | // Production never calls the setter, so this is one atomic load on a |
| 84 | // path that already does a database read. |
| 85 | func (a *app) allowNow() allowlist { |
| 86 | if l := a.allow.Load(); l != nil { |
| 87 | return *l |
| 88 | } |
| 89 | return a.cfg.allow |
| 90 | } |
| 91 | |
| 92 | // isMember is the lone-shape roster read: the allowlist. |
| 93 | func (a *app) isMember(p Person) bool { |
| 94 | l := a.allowNow() |
| 95 | return !p.Anonymous && l.addrs != nil && l.allows(p.Email) |
| 96 | } |
| 97 | |
| 98 | func (a *app) standingFor(ctx context.Context, p Person, m Meeting) (Standing, error) { |
| 99 | if a.team != nil { |
| 100 | return a.teamStandingFor(ctx, p, m) |
| 101 | } |
| 102 | if p.Anonymous && m.RequireSignin { |
| 103 | return StandingNone, nil |
| 104 | } |
| 105 | if a.isMember(p) { |
| 106 | return StandingMember, nil |
| 107 | } |
| 108 | g, ok, err := a.store.grantFor(ctx, m.ID, p.ID) |
| 109 | if err != nil { |
| 110 | return StandingNone, err |
| 111 | } |
| 112 | if ok && g.ExpiresAt.After(a.store.now()) { |
| 113 | return StandingGuest, nil |
| 114 | } |
| 115 | return StandingNone, nil |
| 116 | } |
| 117 | |
| 118 | // teamStandingFor is standingFor in team mode. The meeting stands on its |
| 119 | // host first (team_host.go): a host who has left ends it, and nobody has |
| 120 | // standing toward an ended meeting; a host who cannot be decided now is |
| 121 | // errTeamUnavailable for everyone. Then the person's own: Member is a |
| 122 | // member, Unknown is errTeamUnavailable (whether they need a grant cannot |
| 123 | // be told), and a non-member stands on an unexpired grant, as always. |
| 124 | func (a *app) teamStandingFor(ctx context.Context, p Person, m Meeting) (Standing, error) { |
| 125 | switch _, st := a.meetingStands(ctx, m); st { |
| 126 | case roster.NotMember: |
| 127 | return StandingNone, nil |
| 128 | case roster.Unknown: |
| 129 | return StandingNone, errTeamUnavailable |
| 130 | } |
| 131 | if p.Anonymous && m.RequireSignin { |
| 132 | return StandingNone, nil |
| 133 | } |
| 134 | switch a.memberState(ctx, p) { |
| 135 | case roster.Member: |
| 136 | return StandingMember, nil |
| 137 | case roster.Unknown: |
| 138 | return StandingNone, errTeamUnavailable |
| 139 | } |
| 140 | g, ok, err := a.store.grantFor(ctx, m.ID, p.ID) |
| 141 | if err != nil { |
| 142 | return StandingNone, err |
| 143 | } |
| 144 | if ok && g.ExpiresAt.After(a.store.now()) { |
| 145 | return StandingGuest, nil |
| 146 | } |
| 147 | return StandingNone, nil |
| 148 | } |
| 149 | |