|
| 1 | +import { createHash } from "node:crypto"; |
| 2 | +import type { WAMessage } from "baileys"; |
| 3 | +import { createDurableInboundReceiveJournal } from "openclaw/plugin-sdk/channel-message"; |
| 4 | +import { getWhatsAppRuntime } from "../runtime.js"; |
| 5 | +import { BufferJSON } from "../session.runtime.js"; |
| 6 | + |
| 7 | +const WHATSAPP_DURABLE_INBOUND_PENDING_MAX_ENTRIES = 450; |
| 8 | +const WHATSAPP_DURABLE_INBOUND_COMPLETED_MAX_ENTRIES = 450; |
| 9 | +const WHATSAPP_DURABLE_INBOUND_PENDING_TTL_MS = 30 * 24 * 60 * 60 * 1000; |
| 10 | +const WHATSAPP_DURABLE_INBOUND_COMPLETED_TTL_MS = 7 * 24 * 60 * 60 * 1000; |
| 11 | + |
| 12 | +export type WhatsAppReadReceiptTarget = { |
| 13 | + remoteJid: string; |
| 14 | + id: string; |
| 15 | + participant?: string; |
| 16 | +}; |
| 17 | + |
| 18 | +export type WhatsAppDurableInboundPayload = { |
| 19 | + message: unknown; |
| 20 | + upsertType?: string; |
| 21 | + receivedAt: number; |
| 22 | +}; |
| 23 | + |
| 24 | +export type WhatsAppDurableInboundMetadata = { |
| 25 | + readReceipt?: WhatsAppReadReceiptTarget; |
| 26 | +}; |
| 27 | + |
| 28 | +export type WhatsAppDurableInboundCompletedMetadata = { |
| 29 | + readReceipt?: WhatsAppReadReceiptTarget; |
| 30 | +}; |
| 31 | + |
| 32 | +function hashNamespacePart(value: string): string { |
| 33 | + return createHash("sha256").update(value).digest("hex").slice(0, 24); |
| 34 | +} |
| 35 | + |
| 36 | +export function createWhatsAppDurableInboundMessageId(params: { |
| 37 | + remoteJid: string; |
| 38 | + id: string; |
| 39 | +}): string { |
| 40 | + return createHash("sha256").update(`${params.remoteJid}\n${params.id}`).digest("hex"); |
| 41 | +} |
| 42 | + |
| 43 | +export function serializeWhatsAppDurableInboundMessage(message: WAMessage): unknown { |
| 44 | + return JSON.parse(JSON.stringify(message, BufferJSON.replacer)) as unknown; |
| 45 | +} |
| 46 | + |
| 47 | +export function deserializeWhatsAppDurableInboundMessage(message: unknown): WAMessage { |
| 48 | + return JSON.parse(JSON.stringify(message), BufferJSON.reviver) as WAMessage; |
| 49 | +} |
| 50 | + |
| 51 | +export function createWhatsAppDurableInboundReceiveJournal(accountId: string) { |
| 52 | + const runtime = getWhatsAppRuntime(); |
| 53 | + const accountPart = hashNamespacePart(accountId); |
| 54 | + return createDurableInboundReceiveJournal< |
| 55 | + WhatsAppDurableInboundPayload, |
| 56 | + WhatsAppDurableInboundMetadata, |
| 57 | + WhatsAppDurableInboundCompletedMetadata |
| 58 | + >({ |
| 59 | + pendingStore: runtime.state.openKeyedStore({ |
| 60 | + namespace: `inbound.v1.pending.${accountPart}`, |
| 61 | + maxEntries: WHATSAPP_DURABLE_INBOUND_PENDING_MAX_ENTRIES, |
| 62 | + defaultTtlMs: WHATSAPP_DURABLE_INBOUND_PENDING_TTL_MS, |
| 63 | + }), |
| 64 | + completedStore: runtime.state.openKeyedStore({ |
| 65 | + namespace: `inbound.v1.completed.${accountPart}`, |
| 66 | + maxEntries: WHATSAPP_DURABLE_INBOUND_COMPLETED_MAX_ENTRIES, |
| 67 | + defaultTtlMs: WHATSAPP_DURABLE_INBOUND_COMPLETED_TTL_MS, |
| 68 | + }), |
| 69 | + pendingTtlMs: WHATSAPP_DURABLE_INBOUND_PENDING_TTL_MS, |
| 70 | + completedTtlMs: WHATSAPP_DURABLE_INBOUND_COMPLETED_TTL_MS, |
| 71 | + }); |
| 72 | +} |
0 commit comments