"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.supportWsHub = exports.SupportWsHub = void 0; const config_1 = require("../../config"); const client_1 = require("../redis/client"); const supportKeys_1 = require("../redis/supportKeys"); const supportService_1 = require("../../services/supportService"); const auth_1 = require("./auth"); const STAFF_CHANNEL = supportKeys_1.supportRedisKeys.pubsubStaffInbox(); const CONV_CHANNEL_PREFIX = 'prediqo:pubsub:support:conv:'; function parseSupportEvent(raw) { try { const parsed = JSON.parse(raw); if (!parsed?.type) return null; return parsed; } catch { return null; } } function serialize(msg) { return JSON.stringify(msg); } class SupportWsHub { constructor() { this.clients = new Set(); } async start() { if (!config_1.config.redis.enabled) { console.warn('[support-ws] Redis disabled — support push unavailable'); return; } await (0, client_1.connectRedis)(); const sub = (0, client_1.getRedisSubscriber)(); if (!sub) return; sub.on('pmessage', (_pattern, channel, message) => { const event = parseSupportEvent(message); if (!event) return; this.dispatch(channel, event); }); await sub.psubscribe('prediqo:pubsub:support:*'); console.log('[support-ws] subscribed to prediqo:pubsub:support:*'); } register(socket, reqUrl, authHeader) { const url = new URL(reqUrl, 'http://localhost'); const queryToken = url.searchParams.get('token') ?? undefined; const visitorToken = url.searchParams.get('visitorToken')?.trim() || undefined; const auth = (0, auth_1.authenticateWsToken)((0, auth_1.extractBearerToken)(authHeader) ?? queryToken); const state = { socket, userId: auth?.userId, role: auth?.role, visitorToken: visitorToken && visitorToken.length >= 16 ? visitorToken : undefined, conversations: new Set(), inbox: false, }; this.clients.add(state); if (auth && (0, supportService_1.isStaffRole)(auth.role)) { state.inbox = true; } socket.on('message', (data) => { void this.handleClientMessage(state, String(data)); }); socket.on('close', () => { this.clients.delete(state); }); socket.send(serialize({ type: 'connected', authenticated: !!auth, role: auth?.role ?? null, path: config_1.config.ws.supportPath, })); } async handleClientMessage(state, raw) { let msg; try { msg = JSON.parse(raw); } catch { return; } if (msg.type === 'ping') { this.send(state, { type: 'pong', ts: Date.now() }); return; } if (msg.type === 'subscribe' && msg.conversationId) { try { await (0, supportService_1.assertConversationAccess)(msg.conversationId, { userId: state.userId, role: state.role, visitorToken: state.visitorToken, }); state.conversations.add(msg.conversationId); this.send(state, { type: 'subscribed', conversationId: msg.conversationId }); } catch { this.send(state, { type: 'error', code: 'forbidden', message: 'Cannot subscribe to conversation' }); } return; } if (msg.type === 'unsubscribe' && msg.conversationId) { state.conversations.delete(msg.conversationId); return; } if (msg.type === 'subscribe_inbox' && (0, supportService_1.isStaffRole)(state.role)) { state.inbox = true; this.send(state, { type: 'subscribed_inbox' }); } } dispatch(channel, event) { const payload = { ...event, ts: event.ts ?? Date.now() }; if (channel === STAFF_CHANNEL) { for (const client of this.clients) { if (!client.inbox) continue; this.send(client, payload); } return; } if (!channel.startsWith(CONV_CHANNEL_PREFIX)) return; const conversationId = channel.slice(CONV_CHANNEL_PREFIX.length); for (const client of this.clients) { const watchesConv = client.conversations.has(conversationId); const staffInbox = client.inbox && (0, supportService_1.isStaffRole)(client.role); if (!watchesConv && !staffInbox) continue; this.send(client, payload); } } send(client, msg) { if (client.socket.readyState !== 1) return; try { client.socket.send(serialize(msg)); } catch { /* ignore */ } } } exports.SupportWsHub = SupportWsHub; exports.supportWsHub = new SupportWsHub(); //# sourceMappingURL=supportHub.js.map