| 1 | package slides |
| 2 | |
| 3 | // Team mode and live presentations. A presentation is a capability like a |
| 4 | // share link — the session sqid reaches the projector, the remote token |
| 5 | // drives it, and none of the screens has a session — but unlike a share |
| 6 | // link it is a person's, not the deck's: it reads the deck out on the |
| 7 | // authority of whoever started it, which is why offboarding has always |
| 8 | // ended a removed person's talks (auth.go's offboard). In team mode the |
| 9 | // roster replaces the allowlist, so every request a presentation answers |
| 10 | // and every delivery its streams make re-decide whether the presenter |
| 11 | // still reaches the deck: |
| 12 | // |
| 13 | // - they do: the talk goes on, and a stream that rests on their Member |
| 14 | // lease re-decides when it runs out; |
| 15 | // - they definitely do not (the roster removed them, and no grant names |
| 16 | // them on the deck): the talk is ended, as offboard ends it, and |
| 17 | // every screen on it is told; |
| 18 | // - their standing cannot be read now: a retryable 503, and the talk is |
| 19 | // left running for when it can. |
| 20 | |
| 21 | import ( |
| 22 | "context" |
| 23 | "strconv" |
| 24 | "strings" |
| 25 | "time" |
| 26 | |
| 27 | "amadan.net/oficina/home/roster" |
| 28 | |
| 29 | "amadan.net/oficina/slides/internal/slides/present" |
| 30 | ) |
| 31 | |
| 32 | // presenterReach is what a presentation's presenter reaches now. |
| 33 | type presenterReach int |
| 34 | |
| 35 | const ( |
| 36 | presenterReaches presenterReach = iota |
| 37 | presenterUnknown |
| 38 | presenterGone |
| 39 | ) |
| 40 | |
| 41 | // presenterOf is the user a presentation's actor stands for: the person, |
| 42 | // or an agent's operator. ok is false when the actor names nobody here. |
| 43 | func (a *app) presenterOf(ctx context.Context, actor string) (User, bool) { |
| 44 | kind, raw, found := strings.Cut(actor, ":") |
| 45 | if !found { |
| 46 | return User{}, false |
| 47 | } |
| 48 | id, err := strconv.ParseInt(raw, 10, 64) |
| 49 | if err != nil { |
| 50 | return User{}, false |
| 51 | } |
| 52 | if kind == "agent" { |
| 53 | var ag Agent |
| 54 | if err := a.db.G.WithContext(ctx).Take(&ag, id).Error; err != nil { |
| 55 | return User{}, false |
| 56 | } |
| 57 | id = ag.OperatorID |
| 58 | } else if kind != "person" { |
| 59 | return User{}, false |
| 60 | } |
| 61 | var u User |
| 62 | if err := a.db.G.WithContext(ctx).Take(&u, id).Error; err != nil { |
| 63 | return User{}, false |
| 64 | } |
| 65 | return u, true |
| 66 | } |
| 67 | |
| 68 | // presenterStanding decides p's presenter's reach to p's deck now, and |
| 69 | // how long a Member answer may stand (0 when it does not rest on a |
| 70 | // lease). Team mode only; it asks the roster, so never under the writer. |
| 71 | func (a *app) presenterStanding(ctx context.Context, p Presentation) (presenterReach, time.Duration) { |
| 72 | u, ok := a.presenterOf(ctx, p.Actor) |
| 73 | if !ok { |
| 74 | return presenterGone, 0 |
| 75 | } |
| 76 | u = a.withStanding(ctx, u) |
| 77 | var n int64 |
| 78 | if err := a.visibleDecks(ctx, u).Where("decks.id = ?", p.DeckID).Count(&n).Error; err != nil { |
| 79 | return presenterUnknown, 0 |
| 80 | } |
| 81 | // Asked after the reach, of the same Access (teamStreamAccess says |
| 82 | // why): without a Member lease only a grant holds. |
| 83 | state := u.team.state() |
| 84 | if n > 0 && state != roster.Member && !a.grantedTo(ctx, u, Deck{ID: p.DeckID}) { |
| 85 | n = 0 |
| 86 | } |
| 87 | switch { |
| 88 | case n == 0 && state == roster.Unknown: |
| 89 | return presenterUnknown, 0 |
| 90 | case n == 0: |
| 91 | return presenterGone, 0 |
| 92 | case state != roster.Member: |
| 93 | return presenterReaches, 0 |
| 94 | } |
| 95 | renew := u.team.Remaining() |
| 96 | if renew <= 0 { |
| 97 | renew = time.Second |
| 98 | } |
| 99 | return presenterReaches, renew |
| 100 | } |
| 101 | |
| 102 | // endPresentation ends p because its presenter no longer reaches the |
| 103 | // deck, and tells every screen watching it, as offboard's |
| 104 | // endPresentationsOf does. Best effort and logged: the request that found |
| 105 | // out answers 410 either way. |
| 106 | func (a *app) endPresentation(ctx context.Context, p Presentation) { |
| 107 | res := a.db.G.WithContext(ctx).Model(&Presentation{}). |
| 108 | Where("id = ? AND ended_at IS NULL", p.ID).Update("ended_at", time.Now().UTC()) |
| 109 | if res.Error != nil { |
| 110 | a.logger.Error("slides: end a presentation whose presenter left the team", "session", p.Sqid, "err", res.Error) |
| 111 | return |
| 112 | } |
| 113 | a.logger.Warn("slides: a presenter no longer reaches the deck; ending the presentation", "session", p.Sqid, "actor", p.Actor) |
| 114 | if res.RowsAffected > 0 { |
| 115 | a.hub.Publish(p.Sqid, present.Event{Ended: true}) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // presentationStands is presenterStanding with its consequence: a talk |
| 120 | // whose presenter is gone is ended here. Outside team mode it always |
| 121 | // stands, with nothing to renew. |
| 122 | func (a *app) presentationStands(ctx context.Context, p Presentation) (presenterReach, time.Duration) { |
| 123 | if a.team == nil { |
| 124 | return presenterReaches, 0 |
| 125 | } |
| 126 | reach, renew := a.presenterStanding(ctx, p) |
| 127 | if reach == presenterGone { |
| 128 | a.endPresentation(ctx, p) |
| 129 | } |
| 130 | return reach, renew |
| 131 | } |
| 132 | |