Files
2026-05-27 23:47:53 +01:00

59 lines
1.5 KiB
TypeScript

import type {
AuthContext,
ClientInfo,
GatewayAction,
Message,
} from "../types/types.js";
import type { ChannelManager } from "./channel-manager.js";
export interface ChannelOpsDeps {
readonly clients: Map<string, ClientInfo>;
readonly channelMgr: ChannelManager;
readonly clock: () => number;
readonly runGate: (
ctx: AuthContext,
action: GatewayAction
) => Promise<void>;
readonly requireAuthCtx: (clientId: string) => AuthContext;
}
export async function subscribeToChannel(
deps: ChannelOpsDeps,
channelName: string,
clientId: string,
topic: string
): Promise<boolean> {
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()
);
}