Discord: several radio streams per voice channel; adding never overwrites (#213)

Owner-reported on live: adding a radio channel for a server + voice channel
that already had one (different label, system, even bot) overwrote the
existing stream and made its bot leave, because stream_id was guild:channel.

- New streams get guild:channel:<12 hex> ids (leases.new_stream_id); old
  guild:channel docs keep working for read/stop/remove/node reports. Route
  validators accept both (leases.STREAM_ID_PATTERN).
- start_stream: no stream_id = ADD. Same system already on in that channel
  is idempotent (already_on, label/mode untouched); a stopped one of that
  system is restarted; anything else is a new doc. stream_id = explicit
  restart/edit (admin Start button, reconciler), incl. moving to another bot.
- preferred_token_id is strict: bot already in this server -> 409
  preferred_busy naming the stream; on another node -> preferred_other_node;
  unusable -> preferred_unavailable. No silent swap. pool_exhausted now says
  one-bot-per-server and to add + invite another bot.
- One-token-per-guild was already enforced inside the lease transaction;
  now covered by race tests. Per-guild asyncio lock serialises the
  already-on / server-cap check with the create (guild half of #223).
- Stop by stream_id (POST /discord/guilds/{id}/stop {stream_id}); admin UI
  Stop/Start/Remove act on the row's stream; node picker lists every stream
  in a channel and adds beside other systems.
- Bot: /leave stream: (autocomplete) stops one; /leave channel: with several
  streams asks which unless all:True; bare /leave still stops the server.
  /status and /join name each stream's bot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Logan Cusano
2026-10-05 00:30:40 -04:00
co-authored by Claude Opus 5.5
parent 16a609e35b
commit 8af410aede
15 changed files with 913 additions and 113 deletions
@@ -20,6 +20,11 @@ import { DIM, Field, INPUT, InviteLinks, Panel, Status, Table, TD } from "./ui";
// POST /discord/guilds/{id}/stop), which accept a platform-admin token.
// Remove is DELETE /discord/streams/{id} (admin): c2-core stops the channel
// through desired state first, then deletes it.
//
// One row per STREAM, not per voice channel: a channel can carry several,
// each with its own radio bot and system (server-26#213 follow-up). Stop,
// Start and Remove all act on the row's stream_id, so the other streams in
// the same channel keep playing. Add never replaces an existing stream.
type Invite = { name: string; invite_url: string | null }[];
@@ -48,6 +53,14 @@ function AddChannelForm({
const org = guilds.find((g) => g.guild_id === guildId)?.acting_org_id ?? null;
const systems = overview.systems.filter((s) => s.org_id == null || s.org_id === org);
const usableBots = bots.filter((b) => b.valid !== false && (b.org_id == null || b.org_id === org));
// A bot can be in one voice channel per server: name the stream holding it.
const busyHere = new Map<string, DiscordStream>();
for (const s of overview.streams) {
if (s.desired === "on" && s.guild_id === guildId && s.token_id) busyHere.set(s.token_id, s);
}
const inChannel = overview.streams.filter(
(s) => s.desired === "on" && s.guild_id === guildId && s.channel_id === channelId.trim(),
);
async function submit(e: React.FormEvent) {
e.preventDefault();
@@ -117,11 +130,25 @@ function AddChannelForm({
<Field label="Radio bot">
<select value={botId} onChange={(e) => setBotId(e.target.value)} className={INPUT}>
<option value="">Any free bot in this server</option>
{usableBots.map((b) => (
<option key={b.token_id} value={b.token_id}>Prefer {b.name}</option>
))}
{usableBots.map((b) => {
const busy = busyHere.get(b.token_id);
return (
<option key={b.token_id} value={b.token_id} disabled={!!busy}>
{busy
? `${b.name} — in use here (${busy.system_name || busy.system_id || "radio"}${busy.channel_name ? ` in ${busy.channel_name}` : ""})`
: `Use ${b.name}`}
</option>
);
})}
</select>
</Field>
{inChannel.length > 0 && (
<div className="sm:col-span-3 text-xs text-ink-muted">
This voice channel already plays{" "}
{inChannel.map((s) => `${s.system_name || s.system_id}${s.token_name ? ` (${s.token_name})` : ""}`).join(", ")}.
A new system is added beside it with another bot; nothing that is playing is replaced.
</div>
)}
{error && (
<div className="sm:col-span-3 text-xs text-sev-major">
{error.message}
@@ -154,6 +181,15 @@ export function StreamsPanel({
const [busy, setBusy] = useState<string | null>(null);
const [rowError, setRowError] = useState<{ id: string; message: string; invite: Invite } | null>(null);
const guildsById = useMemo(() => new Map(overview.guilds.map((g) => [g.guild_id, g])), [overview.guilds]);
// Streams sharing a voice channel (c2-core sorts them next to each other).
const perChannel = useMemo(() => {
const m = new Map<string, number>();
for (const s of overview.streams) {
const k = `${s.guild_id}:${s.channel_id}`;
m.set(k, (m.get(k) ?? 0) + 1);
}
return m;
}, [overview.streams]);
async function act(s: DiscordStream, fn: () => Promise<unknown>) {
setBusy(s.stream_id);
@@ -171,7 +207,7 @@ export function StreamsPanel({
return (
<Panel
title="Radio channels"
description="One row per Discord voice channel. Persistent channels come back by themselves."
description="One row per radio stream — a voice channel can carry several, each with its own bot and system. Persistent streams come back by themselves."
action={
<Button size="sm" onClick={() => setAdding((v) => !v)}>+ Add radio channel</Button>
}
@@ -205,11 +241,19 @@ export function StreamsPanel({
const g = guildsById.get(s.guild_id);
const err = rowError?.id === s.stream_id ? rowError : null;
const isOn = s.desired === "on";
const shared = (perChannel.get(`${s.guild_id}:${s.channel_id}`) ?? 1) - 1;
const name = s.channel_name || "this channel";
const what = `${s.system_name || s.system_id || "radio"} in ${name}`;
return (
<tr key={s.stream_id}>
<td className={TD}>
<b>{s.channel_name ? `🔊 ${s.channel_name}` : `channel ${shortId(s.channel_id)}`}</b>
<div className={DIM}>{g ? guildLabel(g) : guildLabel({ guild_id: s.guild_id, guild_name: s.guild_name })}</div>
{shared > 0 && (
<div className={DIM}>
+ {shared} other stream{shared === 1 ? "" : "s"} in this voice channel
</div>
)}
</td>
<td className={TD}>{source(s)}</td>
<td className={TD}><span className={DIM}>All talkgroups</span></td>
@@ -238,8 +282,8 @@ export function StreamsPanel({
variant="secondary"
disabled={busy !== null}
onClick={() => {
if (!confirm(`Stop radio in ${s.channel_name || "this channel"}?`)) return;
act(s, () => c2api.stopDiscordStream(s.guild_id, s.channel_id));
if (!confirm(`Stop ${what}?`)) return;
act(s, () => c2api.stopDiscordStream(s.guild_id, s.stream_id));
}}
>
{busy === s.stream_id ? "Stopping…" : "Stop"}
@@ -256,6 +300,7 @@ export function StreamsPanel({
channel_id: s.channel_id,
system_id: s.system_id as string,
mode: s.mode ?? "persistent",
stream_id: s.stream_id,
}),
)
}
@@ -269,10 +314,9 @@ export function StreamsPanel({
className="ml-1"
disabled={busy !== null}
onClick={() => {
const name = s.channel_name || "this channel";
const msg = isOn
? `Remove radio from ${name} permanently? It is stopped first (the node logs its radio bot out), then deleted from DRB.`
: `Remove ${name} from DRB permanently?`;
? `Remove ${what} permanently? It is stopped first (the node logs its radio bot out), then deleted from DRB.${shared > 0 ? " The other streams in this voice channel keep playing." : ""}`
: `Remove ${what} from DRB permanently?`;
if (!confirm(msg)) return;
act(s, () => c2api.deleteDiscordStream(s.stream_id));
}}