Files
piik/src/client/channel/components/channel-connection-data.ts
T
AAA c55205f92d feat(profiles): represent unnamed members across room surfaces
Use an empty stored name for unnamed members and one localized formatter for guest labels and same-list duplicates. Full member IDs retain authority; archived empty names remain distinct from unknown live profiles. Reuse the profile editor for an owner-controlled optional nickname prompt and remember dismissal per site and member. The current private Domain 55 phase carries the setting; existing stored headers default it off. Core, Browser, SDK and archive controls passed, including an actual two-browser hosted-room skip and reentry check.
2026-11-27 04:08:31 -05:00

120 lines
7.9 KiB
TypeScript

import { memberName, duplicateMemberNames } from "../member-name";
import type { ChannelSnapshot, ChannelTopology } from "../protocol";
import type { ChannelConnectionSample } from "../types";
import type { ConnectionDetail } from "../../components/living/ConnectionDetails";
import type { RouteGraphEdge, RouteGraphNode } from "../../components/living/RouteGraph";
import type { CopyKey } from "../../ui/copy";
import { EMPTY_METRICS } from "../../types";
type Translate = (key: CopyKey, vars?: Record<string, string>) => string;
function hopRoute(topology: ChannelTopology, from: string, to: string): "p2p" | "sfu" {
return from === topology.transitSession || to === topology.transitSession ? "sfu" : "p2p";
}
export function hasSelectedLocalDisplay(topology: ChannelTopology | null, sessionId: string): boolean {
return topology?.screen?.edges.some(edge => edge.presentation && edge.selected && edge.receiver === sessionId) ?? false;
}
/** A member may appear at several endpoints in the same view. */
export function connectionMemberNames(users: ChannelSnapshot["users"], userIds: readonly string[]): Map<string, string> {
const ids = new Set(userIds);
const members = users.filter(user => ids.has(user.id));
const duplicates = duplicateMemberNames(members);
return new Map(members.map(user => [user.id, memberName(user.id, user.name, duplicates.has(user.name))]));
}
function connectionSessionName(state: ChannelSnapshot, sessionId: string, t: Translate): string {
const session = state.sessions.find(value => value.id === sessionId);
const user = state.users.find(user => user.id === session?.userId);
return user ? memberName(user.id, user.name) : t("voice.participant");
}
export function channelConnectionGraph(state: ChannelSnapshot, roomId: string, sessionId: string,
topology: ChannelTopology, t: Translate) {
const screen = topology.screen;
const rootId = screen?.sourceSessionId ?? null;
const edges: RouteGraphEdge[] = screen ? screen.edges.map(edge => ({
from: edge.sender, to: edge.receiver, selected: edge.selected,
ready: edge.proof === "forwardable" || edge.proof === "decoded", presentation: edge.presentation,
route: edge.presentation ? undefined : hopRoute(topology, edge.sender, edge.receiver),
})) : topology.pairs.map(pair => ({ from: pair.first, to: pair.second, selected: true, ready: pair.confirmed }));
const apps = new Set(screen?.edges.filter(edge => edge.presentation).map(edge => edge.sender));
const display = new Set(screen?.edges.filter(edge => edge.presentation).map(edge => edge.receiver));
const room = state.channel.rooms.find(room => room.id === roomId);
const ids = screen ? [...new Set([screen.sourceSessionId, ...edges.flatMap(edge => [edge.from, edge.to])])]
: topology.sessionIds;
const sessionUsers = new Map(state.sessions.map(session => [session.id, session.userId]));
const identity = (id: string) => sessionUsers.get(id) ?? id;
const people = ids.filter(id => id !== topology.transitSession && sessionUsers.has(id));
const userIds = [...new Set(people.map(identity))];
const names = connectionMemberNames(state.users, userIds);
const groups = userIds.map(userId => ({ userId,
name: names.get(userId) ?? t("voice.participant"),
ids: people.filter(id => identity(id) === userId).sort(),
}));
const endpointKind = (id: string) => apps.has(id) ? "app" as const
: display.has(id) || id === sessionId ? "webpage" as const : undefined;
const endpoint = (id: string) => {
const kind = endpointKind(id);
if (kind) return kind === "app" ? "App" : t("connection.webpage");
const group = groups.find(group => group.userId === identity(id));
return t("connection.endpoint", { number: String((group?.ids.indexOf(id) ?? 0) + 1) });
};
const roomMembers = new Set(room?.participantSessionIds);
const nodes = ids.map((id, index): RouteGraphNode => {
if (id === topology.transitSession) return { id, identity: id, kind: "sfu", label: t(screen ? "connection.siteForwarding" : "connection.siteService") };
const userId = sessionUsers.get(id);
if (!userId) return { id, kind: "endpoint", you: id === sessionId,
label: t("connection.endpoint", { number: String(index + 1) }) };
const name = names.get(userId) ?? t("voice.participant");
const multiple = groups.some(group => group.userId === identity(id) && group.ids.length > 1);
const host = id === room?.delivery?.hostSessionId && userId === room?.creatorId;
const kind = endpointKind(id);
const showEndpoint = multiple || !!kind || !host && !roomMembers.has(id);
return { id, identity: userId ?? id,
kind: showEndpoint ? "endpoint" : "person", endpointKind: kind,
endpointLabel: showEndpoint ? endpoint(id) : undefined,
you: id === sessionId, host, label: name };
});
return { nodes, rootId, edges, endpointGroups: groups.filter(group => group.ids.length > 1)
.map(group => ({ ...group, endpoints: group.ids.map(endpoint) })) };
}
export function channelConnectionRows(state: ChannelSnapshot, sessionId: string,
topology: ChannelTopology | null, samples: readonly ChannelConnectionSample[], t: Translate): ConnectionDetail[] {
return samples.flatMap(sample => {
const publication = state.publications.find(publication => publication.id === sample.publicationId);
if (!publication) return [];
if (sample.ownerSessionId === sessionId) {
const pair = state.pairs.find(pair => pair.id === sample.pairId);
const flows = sample.direction === "send" ? pair?.outgoing : pair?.incoming;
if (!flows?.some(flow => flow.id === sample.flowId && flow.publicationId === sample.publicationId && flow.selected)) return [];
}
const video = publication.kind === "screen" ? samples.find(other => other.connectionId === sample.connectionId
&& other.flowId === sample.flowId && other.direction === sample.direction && other.media === "video") : undefined;
const sourceSound = publication.kind === "screen" ? samples.find(other => other.connectionId === sample.connectionId
&& other.flowId === sample.flowId && other.direction === sample.direction && other.media === "audio") : undefined;
if (sourceSound === sample && video) return [];
const metrics = sourceSound && (!video || video === sample) ? { ...(sample.media === "video" ? sample.metrics : { ...EMPTY_METRICS, rttMs: sample.metrics.rttMs }),
audioBitrateKbps: sourceSound.metrics.audioBitrateKbps, audioCodec: sourceSound.metrics.audioCodec,
audioPacketLossPercent: sourceSound.metrics.audioPacketLossPercent, audioJitterMs: sourceSound.metrics.audioJitterMs } : sample.metrics;
const from = sample.direction === "send" ? sample.ownerSessionId : sample.peerId;
const to = sample.direction === "send" ? sample.peerId : sample.ownerSessionId;
const localDisplay = topology?.screen?.publicationId === sample.publicationId
? topology.screen.edges.find(edge => edge.presentation && edge.sender === from && edge.receiver === to)
: undefined;
const source = connectionSessionName(state, publication.sessionId, t);
const peer = sample.peerId === topology?.transitSession ? t("connection.siteForwarding") : connectionSessionName(state, sample.peerId, t);
const label = localDisplay ? t("connection.localDisplay")
: sample.direction === "receive" ? t("voice.details.receiveFrom", { name: peer })
: publication.sessionId !== sample.ownerSessionId ? t("voice.details.forwardTo", { source, name: peer })
: t("voice.details.sendTo", { name: peer });
// Without picture topology, a sample may be the App's local display hop.
return [{ key: `${sample.ownerSessionId}/${sample.flowId}/${sample.metrics.rtpStatsId}`, connectionId: sample.connectionId,
media: publication.kind === "voice" ? "audio" : "video", direction: sample.direction,
route: topology && !localDisplay ? hopRoute(topology, from, to) : undefined, label, metrics,
detailOnly: publication.kind === "screen" && !topology?.screen || !!localDisplay || !sample.selected || undefined }];
});
}