fix: preserve ui state broadcasts in routes
This commit is contained in:
@@ -3,12 +3,40 @@ import express from "express";
|
||||
import { AppError } from "../errors";
|
||||
import { createChildLogger } from "../logger";
|
||||
import type { VoiceController } from "../voiceController";
|
||||
import type { ModerationBroadcaster } from "../moderation/broadcaster";
|
||||
import type { SharedUIState } from "./uiStateRoutes";
|
||||
|
||||
const logger = createChildLogger("voice-routes");
|
||||
|
||||
export function createVoiceRoutes(voiceController: VoiceController): Router {
|
||||
export interface VoiceRouteOptions {
|
||||
voiceController: VoiceController;
|
||||
patchSharedUIState: (patch: Partial<SharedUIState>) => SharedUIState;
|
||||
broadcaster: ModerationBroadcaster;
|
||||
}
|
||||
|
||||
export function createVoiceRoutes(
|
||||
options: VoiceRouteOptions | VoiceController,
|
||||
): Router {
|
||||
const router = express.Router();
|
||||
|
||||
// Support both old signature (VoiceController) and new signature (options object)
|
||||
let voiceController: VoiceController;
|
||||
let patchSharedUIState:
|
||||
| ((patch: Partial<SharedUIState>) => SharedUIState)
|
||||
| undefined;
|
||||
let broadcaster: ModerationBroadcaster | undefined;
|
||||
|
||||
if ("connect" in options && "disconnect" in options) {
|
||||
// Old signature: just VoiceController
|
||||
voiceController = options as VoiceController;
|
||||
} else {
|
||||
// New signature: options object
|
||||
const opts = options as VoiceRouteOptions;
|
||||
voiceController = opts.voiceController;
|
||||
patchSharedUIState = opts.patchSharedUIState;
|
||||
broadcaster = opts.broadcaster;
|
||||
}
|
||||
|
||||
// GET /api/status - Get voice connection status
|
||||
router.get("/status", (_req, res, next) => {
|
||||
try {
|
||||
@@ -96,6 +124,16 @@ export function createVoiceRoutes(voiceController: VoiceController): Router {
|
||||
logger.info({ guildId, channelId }, "Connecting to voice channel");
|
||||
|
||||
const status = await voiceController.connect(guildId, channelId);
|
||||
|
||||
// Update UI state and broadcast to connected clients
|
||||
if (patchSharedUIState && broadcaster) {
|
||||
const updatedState = patchSharedUIState({
|
||||
selectedGuild: guildId,
|
||||
selectedVoiceChannel: channelId,
|
||||
});
|
||||
broadcaster.uiState(updatedState);
|
||||
}
|
||||
|
||||
res.json(status);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
@@ -108,6 +146,16 @@ export function createVoiceRoutes(voiceController: VoiceController): Router {
|
||||
logger.info("Disconnecting from voice channel");
|
||||
|
||||
const status = await voiceController.disconnect();
|
||||
|
||||
// Update UI state and broadcast to connected clients
|
||||
if (patchSharedUIState && broadcaster) {
|
||||
const updatedState = patchSharedUIState({
|
||||
selectedGuild: "",
|
||||
selectedVoiceChannel: "",
|
||||
});
|
||||
broadcaster.uiState(updatedState);
|
||||
}
|
||||
|
||||
res.json(status);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
|
||||
Reference in New Issue
Block a user