package calls // Team mode: a meeting stands on its HOST — the member who made it, or, // for a meeting an agent made, the agent's operator — as a booking page // in Calendar stands on its host and a presentation in Slides on its // presenter (oficina/calendar #35, oficina/slides #33). Calls' meetings // are the team's to run (any member lists, shares, replaces and ends // them), but what lets an outsider in is the meeting's link, and a link // is an invitation somebody on the team gave out. When that somebody // leaves, what they gave out stops working: // // - Member: as before. // - NotMember: the meeting is over. The instance ends it (a system // write: ended_at, every link revoked) and tells every open stream // "ended", exactly as a member's End would. Its links 404 like any // ended meeting's, nobody new is let in, and the call in progress // ends — guests, and the members still in it, who can start another // in one click. Re-adding the host does not revive it. // (Flagged for Paul: the alternative keeps the meeting for members // and ends only the guests.) // - Unknown: nothing is ended. Every new entry — following a link, // joining, an ICE fetch, a signal — is a retryable 503, and a call in // progress is held (team_stream.go): its streams stay open and // deliver nothing until the host's standing is known again. // // Nothing here runs outside team mode. import ( "context" "net/http" "strconv" "strings" "amadan.net/oficina/home/roster" "amadan.net/oficina/home/teammode" "amadan.net/oficina/calls/internal/calls/hub" ) // hostRemovedActor is the ended_by of a meeting the instance ended because // its host left the team: nobody's actor, and never a person's. const hostRemovedActor = "system:host-removed" // hostOf is the person a meeting stands on: its creator, or the operator // of the agent that created it. ok is false for a creator Calls cannot // resolve to a person, which stands on nobody (NotMember). func (a *app) hostOf(ctx context.Context, m Meeting) (id int64, ok bool, err error) { kind, rest, found := strings.Cut(m.CreatedBy, ":") if !found { return 0, false, nil } if kind == "connection" { var operator int64 err := a.store.g.WithContext(ctx).Raw("SELECT operator_person_id FROM calls_connections WHERE coordinate=?", rest).Scan(&operator).Error return operator, operator > 0, err } n, perr := strconv.ParseInt(rest, 10, 64) if perr != nil { return 0, false, nil } switch kind { case "person": return n, true, nil case "agent": _, op, found, err := a.store.agentAnyByID(ctx, n) if err != nil { return 0, false, err } return op.ID, found, nil } return 0, false, nil } // hostAccess is the roster's answer about m's host now: a fresh Access, // never the request's own (a host is somebody else's standing even when // they are the one asking). nil, which reads Unknown, when the host // cannot be read now. func (a *app) hostAccess(ctx context.Context, m Meeting) *teammode.Access { id, ok, err := a.hostOf(ctx, m) if err != nil { return nil } if !ok { return a.team.Access(ctx, "") } return a.standingOf(ctx, id) } // meetingStands decides m on its host's standing and returns the host's // Access for whatever the caller writes on it (a guest's grant). On a // definite NotMember it ends the meeting before it answers, so whatever // the caller does next finds it ended. Outside team mode it is Member, // with no Access. func (a *app) meetingStands(ctx context.Context, m Meeting) (*teammode.Access, roster.State) { if a.team == nil { return nil, roster.Member } acc := a.hostAccess(ctx, m) st := acc.State() if st == roster.NotMember { a.endForHost(ctx, m) } return acc, st } // endForHost ends a meeting whose host the roster says has left the team: // the row first, then the hub, so every stream is told "ended" and every // later entry reads ended_at. A write nobody's standing makes. An ending // that fails is logged and left for the next request, the tick or a // member to retry; the hub is told regardless, because a host who is // definitely gone must not keep the call running for want of a write. func (a *app) endForHost(ctx context.Context, m Meeting) { if m.EndedAt == nil { if _, err := a.store.endMeeting(systemWrite(ctx), m.ID, hostRemovedActor); err != nil { a.logger.Error("calls: end a meeting whose host left the team", "meeting_id", m.ID, "err", err) } } a.hub.End(m.Sqid) } // settleHosts ends every listed meeting whose host the roster says has // left the team, so the list shows it ended rather than offering a // meeting that will refuse every entry. Unknown leaves a meeting as it // is. It answers the views as they now stand. func (a *app) settleHosts(ctx context.Context, views []meetingView) []meetingView { if a.team == nil { return views } // One answer per host, not per meeting: a list is mostly a few // people's meetings. hosts := map[string]roster.State{} for i, v := range views { if v.EndedAt != nil { continue } st, asked := hosts[v.CreatedBy] if !asked { st = a.hostAccess(ctx, v.Meeting).State() hosts[v.CreatedBy] = st } if st != roster.NotMember { continue } a.endForHost(ctx, v.Meeting) if fresh, ok, err := a.store.meetingBySqid(ctx, v.Sqid); err == nil && ok { views[i].Meeting = fresh views[i].LinkPresent = false } } return views } // teamRoomStands is the signal endpoint's host check: true when the // call's host stands and the message may go to the hub. A meeting that is // missing or ended is the hub's to answer (it holds the tombstone); a host // who has left has just ended it, and the sender is told what an ended // room's peer is told; one who cannot be decided now is a retryable 503. func (a *app) teamRoomStands(w http.ResponseWriter, r *http.Request, sqid string, peer hub.PeerID, typ string) bool { m, found, err := a.store.meetingBySqid(r.Context(), sqid) if err != nil { a.logger.Error("calls: signal refused", "room", sqid, "peer", string(peer), "type", safeSignalType(typ), "outcome", "internal", "err", err) streamJSON(w, http.StatusInternalServerError, nil) return false } if !found || m.EndedAt != nil { return true } switch _, st := a.meetingStands(r.Context(), m); st { case roster.NotMember: a.refuseSignal(w, sqid, peer, typ, http.StatusNotFound, "binding", a.notFoundBody()) return false case roster.Unknown: a.logRefusal(sqid, peer, typ, "standing-unknown") teamUnavailableJSON(w) return false } return true }