oficina / meet Public

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

Plain git — no account needed to clone.

Download

Download this file

1package calls
2
3// Team mode: a meeting stands on its HOST — the member who made it, or,
4// for a meeting an agent made, the agent's operator — as a booking page
5// in Calendar stands on its host and a presentation in Slides on its
6// presenter (oficina/calendar #35, oficina/slides #33). Calls' meetings
7// are the team's to run (any member lists, shares, replaces and ends
8// them), but what lets an outsider in is the meeting's link, and a link
9// is an invitation somebody on the team gave out. When that somebody
10// leaves, what they gave out stops working:
11//
12// - Member: as before.
13// - NotMember: the meeting is over. The instance ends it (a system
14// write: ended_at, every link revoked) and tells every open stream
15// "ended", exactly as a member's End would. Its links 404 like any
16// ended meeting's, nobody new is let in, and the call in progress
17// ends — guests, and the members still in it, who can start another
18// in one click. Re-adding the host does not revive it.
19// (Flagged for Paul: the alternative keeps the meeting for members
20// and ends only the guests.)
21// - Unknown: nothing is ended. Every new entry — following a link,
22// joining, an ICE fetch, a signal — is a retryable 503, and a call in
23// progress is held (team_stream.go): its streams stay open and
24// deliver nothing until the host's standing is known again.
25//
26// Nothing here runs outside team mode.
27
28import (
29 "context"
30 "net/http"
31 "strconv"
32 "strings"
33
34 "amadan.net/oficina/home/roster"
35 "amadan.net/oficina/home/teammode"
36
37 "amadan.net/oficina/calls/internal/calls/hub"
38)
39
40// hostRemovedActor is the ended_by of a meeting the instance ended because
41// its host left the team: nobody's actor, and never a person's.
42const hostRemovedActor = "system:host-removed"
43
44// hostOf is the person a meeting stands on: its creator, or the operator
45// of the agent that created it. ok is false for a creator Calls cannot
46// resolve to a person, which stands on nobody (NotMember).
47func (a *app) hostOf(ctx context.Context, m Meeting) (id int64, ok bool, err error) {
48 kind, rest, found := strings.Cut(m.CreatedBy, ":")
49 if !found {
50 return 0, false, nil
51 }
52 if kind == "connection" {
53 var operator int64
54 err := a.store.g.WithContext(ctx).Raw("SELECT operator_person_id FROM calls_connections WHERE coordinate=?", rest).Scan(&operator).Error
55 return operator, operator > 0, err
56 }
57 n, perr := strconv.ParseInt(rest, 10, 64)
58 if perr != nil {
59 return 0, false, nil
60 }
61 switch kind {
62 case "person":
63 return n, true, nil
64 case "agent":
65 _, op, found, err := a.store.agentAnyByID(ctx, n)
66 if err != nil {
67 return 0, false, err
68 }
69 return op.ID, found, nil
70 }
71 return 0, false, nil
72}
73
74// hostAccess is the roster's answer about m's host now: a fresh Access,
75// never the request's own (a host is somebody else's standing even when
76// they are the one asking). nil, which reads Unknown, when the host
77// cannot be read now.
78func (a *app) hostAccess(ctx context.Context, m Meeting) *teammode.Access {
79 id, ok, err := a.hostOf(ctx, m)
80 if err != nil {
81 return nil
82 }
83 if !ok {
84 return a.team.Access(ctx, "")
85 }
86 return a.standingOf(ctx, id)
87}
88
89// meetingStands decides m on its host's standing and returns the host's
90// Access for whatever the caller writes on it (a guest's grant). On a
91// definite NotMember it ends the meeting before it answers, so whatever
92// the caller does next finds it ended. Outside team mode it is Member,
93// with no Access.
94func (a *app) meetingStands(ctx context.Context, m Meeting) (*teammode.Access, roster.State) {
95 if a.team == nil {
96 return nil, roster.Member
97 }
98 acc := a.hostAccess(ctx, m)
99 st := acc.State()
100 if st == roster.NotMember {
101 a.endForHost(ctx, m)
102 }
103 return acc, st
104}
105
106// endForHost ends a meeting whose host the roster says has left the team:
107// the row first, then the hub, so every stream is told "ended" and every
108// later entry reads ended_at. A write nobody's standing makes. An ending
109// that fails is logged and left for the next request, the tick or a
110// member to retry; the hub is told regardless, because a host who is
111// definitely gone must not keep the call running for want of a write.
112func (a *app) endForHost(ctx context.Context, m Meeting) {
113 if m.EndedAt == nil {
114 if _, err := a.store.endMeeting(systemWrite(ctx), m.ID, hostRemovedActor); err != nil {
115 a.logger.Error("calls: end a meeting whose host left the team", "meeting_id", m.ID, "err", err)
116 }
117 }
118 a.hub.End(m.Sqid)
119}
120
121// settleHosts ends every listed meeting whose host the roster says has
122// left the team, so the list shows it ended rather than offering a
123// meeting that will refuse every entry. Unknown leaves a meeting as it
124// is. It answers the views as they now stand.
125func (a *app) settleHosts(ctx context.Context, views []meetingView) []meetingView {
126 if a.team == nil {
127 return views
128 }
129 // One answer per host, not per meeting: a list is mostly a few
130 // people's meetings.
131 hosts := map[string]roster.State{}
132 for i, v := range views {
133 if v.EndedAt != nil {
134 continue
135 }
136 st, asked := hosts[v.CreatedBy]
137 if !asked {
138 st = a.hostAccess(ctx, v.Meeting).State()
139 hosts[v.CreatedBy] = st
140 }
141 if st != roster.NotMember {
142 continue
143 }
144 a.endForHost(ctx, v.Meeting)
145 if fresh, ok, err := a.store.meetingBySqid(ctx, v.Sqid); err == nil && ok {
146 views[i].Meeting = fresh
147 views[i].LinkPresent = false
148 }
149 }
150 return views
151}
152
153// teamRoomStands is the signal endpoint's host check: true when the
154// call's host stands and the message may go to the hub. A meeting that is
155// missing or ended is the hub's to answer (it holds the tombstone); a host
156// who has left has just ended it, and the sender is told what an ended
157// room's peer is told; one who cannot be decided now is a retryable 503.
158func (a *app) teamRoomStands(w http.ResponseWriter, r *http.Request, sqid string, peer hub.PeerID, typ string) bool {
159 m, found, err := a.store.meetingBySqid(r.Context(), sqid)
160 if err != nil {
161 a.logger.Error("calls: signal refused", "room", sqid, "peer", string(peer), "type", safeSignalType(typ), "outcome", "internal", "err", err)
162 streamJSON(w, http.StatusInternalServerError, nil)
163 return false
164 }
165 if !found || m.EndedAt != nil {
166 return true
167 }
168 switch _, st := a.meetingStands(r.Context(), m); st {
169 case roster.NotMember:
170 a.refuseSignal(w, sqid, peer, typ, http.StatusNotFound, "binding", a.notFoundBody())
171 return false
172 case roster.Unknown:
173 a.logRefusal(sqid, peer, typ, "standing-unknown")
174 teamUnavailableJSON(w)
175 return false
176 }
177 return true
178}
179