import type { AuthContext, ClientInfo, GatewayAction, Message, } from "../types/types.js"; import type { ChannelManager } from "./channel-manager.js"; export interface ChannelOpsDeps { readonly clients: Map; readonly channelMgr: ChannelManager; readonly clock: () => number; readonly runGate: ( ctx: AuthContext, action: GatewayAction ) => Promise; readonly requireAuthCtx: (clientId: string) => AuthContext; } export async function subscribeToChannel( deps: ChannelOpsDeps, channelName: string, clientId: string, topic: string ): Promise { if (!deps.clients.has(clientId)) return false; await deps.runGate(deps.requireAuthCtx(clientId), "channel.subscribe"); return deps.channelMgr.subscribe(channelName, clientId, topic).subscribed; } export function unsubscribeFromChannel( channelMgr: ChannelManager, channelName: string, clientId: string, topic: string ): boolean { return channelMgr.unsubscribe(channelName, clientId, topic).unsubscribed; } export async function publishToChannel( deps: ChannelOpsDeps, channelName: string, senderId: string, topic: string, payload: unknown ): Promise<{ readonly message: Message; readonly recipients: string[]; } | null> { await deps.runGate(deps.requireAuthCtx(senderId), "channel.publish"); return deps.channelMgr.publish( channelName, senderId, topic, payload, deps.clock() ); }