36 lines
1.5 KiB
TypeScript
36 lines
1.5 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
|
|
import { publishOutboxEvent, type OutboxPublisherTransaction } from "@hivegrid/db/outbox";
|
|
import type { Prisma } from "@hivegrid/db/prisma-browser";
|
|
import { isPersistentChatEvent, validateChatServerEvent, type ChatPersistentEvent } from "@hivegrid/realtime";
|
|
|
|
export const CHAT_PERSISTENT_REALTIME_OUTBOX_EVENT_TYPE = "chat.realtime.persistent" as const;
|
|
|
|
export type PublishChatPersistentRealtimeWakeupInput = Readonly<{
|
|
event: ChatPersistentEvent;
|
|
}>;
|
|
|
|
/**
|
|
* Persists a crash-safe Redis wakeup descriptor in the caller's transaction.
|
|
* The durable TeamChatEvent ledger remains the source of truth; Redis only
|
|
* prompts connected gateways to gap-fill that ledger from the event cursor.
|
|
*/
|
|
export async function publishChatPersistentRealtimeWakeup(tx: OutboxPublisherTransaction, input: PublishChatPersistentRealtimeWakeupInput) {
|
|
const event = validateChatServerEvent(input.event);
|
|
if (!isPersistentChatEvent(event)) {
|
|
throw new Error("chat realtime wakeup requires a persistent chat event");
|
|
}
|
|
|
|
return publishOutboxEvent(tx, {
|
|
tenantId: event.tenantId,
|
|
type: CHAT_PERSISTENT_REALTIME_OUTBOX_EVENT_TYPE,
|
|
payload: { event } as unknown as Prisma.InputJsonValue,
|
|
dispatchKey: buildChatPersistentRealtimeDispatchKey(event),
|
|
});
|
|
}
|
|
|
|
export function buildChatPersistentRealtimeDispatchKey(event: ChatPersistentEvent): string {
|
|
const digest = createHash("sha256").update(event.tenantId).update("\0").update(event.chatId).update("\0").update(event.cursor).digest("hex");
|
|
return `chat-realtime:${digest}`;
|
|
}
|