47 lines
1.8 KiB
JavaScript
47 lines
1.8 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.publishSupportEvent = publishSupportEvent;
|
|
exports.setTypingIndicator = setTypingIndicator;
|
|
exports.getTypingIndicators = getTypingIndicators;
|
|
const client_1 = require("../realtime/redis/client");
|
|
const supportKeys_1 = require("../realtime/redis/supportKeys");
|
|
const TYPING_TTL_SEC = 5;
|
|
async function publishSupportEvent(conversationId, event) {
|
|
const redis = (0, client_1.getRedisClient)();
|
|
if (!redis)
|
|
return;
|
|
const payload = JSON.stringify({ ...event, ts: Date.now() });
|
|
await redis.publish(supportKeys_1.supportRedisKeys.pubsubConversation(conversationId), payload);
|
|
if (event.type !== 'typing') {
|
|
await redis.publish(supportKeys_1.supportRedisKeys.pubsubStaffInbox(), payload);
|
|
}
|
|
}
|
|
async function setTypingIndicator(conversationId, party, active) {
|
|
const redis = (0, client_1.getRedisClient)();
|
|
if (!redis)
|
|
return;
|
|
const key = supportKeys_1.supportRedisKeys.typing(conversationId, party);
|
|
if (active) {
|
|
await redis.set(key, '1', 'EX', TYPING_TTL_SEC);
|
|
}
|
|
else {
|
|
await redis.del(key);
|
|
}
|
|
await publishSupportEvent(conversationId, {
|
|
type: 'typing',
|
|
conversationId,
|
|
party,
|
|
active,
|
|
});
|
|
}
|
|
async function getTypingIndicators(conversationId) {
|
|
const redis = (0, client_1.getRedisClient)();
|
|
if (!redis)
|
|
return { userTyping: false, staffTyping: false };
|
|
const [user, staff] = await Promise.all([
|
|
redis.get(supportKeys_1.supportRedisKeys.typing(conversationId, 'user')),
|
|
redis.get(supportKeys_1.supportRedisKeys.typing(conversationId, 'staff')),
|
|
]);
|
|
return { userTyping: user === '1', staffTyping: staff === '1' };
|
|
}
|
|
//# sourceMappingURL=supportRealtimeService.js.map
|