Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): preview the latest message of a burst
Yudhi Armyndharis1ee3ebb3aa07
diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.tsindex b2c3fcf30..7d162bc93 100644--- a/src/engine/adapters/baileys-events.ts+++ b/src/engine/adapters/baileys-events.ts@@ -221,7 +221,9 @@ export interface BaileysEventsHost {key: Pick<WAMessageKey, 'remoteJid' | 'remoteJidAlt' | 'participant' | 'participantAlt' | 'fromMe'>,): void;/** Seed the chat's last-message preview + sort time from an inbound message. */- recordMessage(msg: WAMessage, type?: MessageType): void;+ recordMessage(msg: WAMessage, type?: MessageType, order?: number): void;+ /** Reserve a message's place in the observed order on arrival, ahead of any limiter, store or media wait. */+ nextMessageOrder?(): number;/** Apply a message edit to the stored body. */recordMessageEdit(chatId: string, messageId: string, text: string, type?: MessageType): void;findPersistedLidPhone?(jid: string): Promise<string | null>;@@ -534,11 +536,12 @@ export class BaileysEvents {// a closed or bounded limiter, so the message is still emitted rather than lost to an unhandled// rejection.const generation = this.storeGeneration;+ const order = this.host.nextMessageOrder?.();// Where the session holds a message until its row is written (PostgreSQL), admit it now, in// arrival order: its chat's order, and a slot of the session's room when it downloads media.const ticket = this.admitInbound(msg);const processed = this.host.inboundLimiter- .run(yieldSlot => this.processInboundMessage(msg, { generation, ticket, yieldSlot }))+ .run(yieldSlot => this.processInboundMessage(msg, { generation, order, ticket, yieldSlot })).catch((error: unknown) => {// Names the failure rather than calling it "saturated", which used to send operators to look// at concurrency settings for a problem that was never there. The retry below skips the@@ -550,7 +553,7 @@ export class BaileysEvents {: 'Inbound media download failed; emitting message without media',{ msgId: msg.key?.id ?? 'unknown', ...(closed ? {} : { error: String(error) }) },);- return this.processInboundMessage(msg, { generation, skipMedia: true, ticket });+ return this.processInboundMessage(msg, { generation, order, skipMedia: true, ticket });});// The room stays held until the processing settles too, and a message never emitted drops its turn.ticket?.holdUntil(processed);@@ -699,6 +702,7 @@ export class BaileysEvents {msg: WAMessage,opts: {generation: number;+ order?: number;skipMedia?: boolean;ticket?: InboundTicket;yieldSlot?: <R>(fn: () => Promise<R>) => Promise<R>;@@ -843,7 +847,7 @@ export class BaileysEvents {const receipt = opts.ticket ? emit?.(incoming, opts.ticket) : emit?.(incoming);this.watchOutcome(msg.key, receipt);}- this.host.recordMessage(msg, incoming.type);+ this.host.recordMessage(msg, incoming.type, opts.order);if (deleted) {this.host.recordMessageEdit(chatJid, storedId, '', 'revoked');} else if (editedBody !== undefined && storedId !== null) {diff --git a/src/engine/adapters/baileys-history.ts b/src/engine/adapters/baileys-history.tsindex 73dae8d6e..aadfbbcbf 100644--- a/src/engine/adapters/baileys-history.ts+++ b/src/engine/adapters/baileys-history.ts@@ -33,7 +33,7 @@ export interface BaileysHistoryHost {/** Lazily loaded @whiskeysockets/baileys module (ESM-only; loaded on first connect, not at boot). */loadLib(): Promise<typeof BaileysLib>;/** Seed the chat's last-message preview + sort time from a history/live message. */- recordMessage(msg: WAMessage, type?: MessageType): void;+ recordMessage(msg: WAMessage, type?: MessageType, order?: number): void;recordMessageEdit(chatId: string, messageId: string, text: string, type?: MessageType): void;applyHistoryRevoke(target: WAMessageKey, envelope: WAMessageKey): Promise<WAMessageKey | undefined>;upsertContacts(records: Partial<BaileysContact>[]): void;@@ -159,7 +159,8 @@ export class BaileysHistory {if (incoming.type === 'revoked') continue;const original = targets.get(incoming.id)!;const deleted = revokedKeys.has(JSON.stringify([incoming.id, incoming.chatId, incoming.fromMe]));- this.host.recordMessage(original, deleted ? 'revoked' : incoming.type);+ // History has no observed order: equal-time messages keep the first one met, and give way to a live one.+ this.host.recordMessage(original, deleted ? 'revoked' : incoming.type, 0);}for (const m of revoked) {this.host.recordMessageEdit(m.chatId, m.id, '', 'revoked');diff --git a/src/engine/adapters/baileys-session-store-ordering.spec.ts b/src/engine/adapters/baileys-session-store-ordering.spec.tsnew file mode 100644index 000000000..4fbc744d4--- /dev/null+++ b/src/engine/adapters/baileys-session-store-ordering.spec.ts@@ -0,0 +1,128 @@+import type { WAMessage } from '@whiskeysockets/baileys';+import { ConcurrencyLimiter } from '../../common/utils/concurrency-limiter';+import type { IncomingMessage } from '../interfaces/whatsapp-engine.interface';+import { BaileysEvents, BaileysEventsHost } from './baileys-events';+import { BaileysHistory, BaileysHistoryHost } from './baileys-history';+import { BaileysSessionStore } from './baileys-session-store';++const CHAT = '[email protected]';+const SECOND = 1800000000;+const msg = (id: string, timestamp = SECOND): WAMessage => ({+ key: { id, remoteJid: CHAT, fromMe: false },+ message: { conversation: id },+ messageTimestamp: timestamp,+});++function historyFor(store: BaileysSessionStore): BaileysHistory {+ return new BaileysHistory({+ loadLib: () =>+ Promise.resolve({+ normalizeMessageContent: (m: unknown) => m,+ getContentType: (m: object) => Object.keys(m)[0],+ }),+ normalizedSelfJid: () => '[email protected]',+ toNeutralJid: (id: string) => store.toNeutralJid(id),+ extractEphemeralDuration: (message: WAMessage) => store.extractEphemeralDuration(message),+ recordMessage: (...args: Parameters<typeof store.recordMessage>) => store.recordMessage(...args),+ recordMessageEdit: (...args: Parameters<typeof store.recordMessageEdit>) => store.recordMessageEdit(...args),+ upsertContacts: () => undefined,+ getOnHistoryMessages: () => () => undefined,+ } as unknown as BaileysHistoryHost);+}++describe('chat preview among messages of one second', () => {+ it('follows the order messages were observed in', () => {+ const store = new BaileysSessionStore();+ store.recordMessage(msg('A'), 'image');+ store.recordMessage(msg('B'), 'text');++ expect(store.lastMessage(CHAT)?.key.id).toBe('B');+ expect(store.lastInboundMessage(CHAT)?.key.id).toBe('B');+ expect(store.listChats()[0]).toMatchObject({ lastMessage: 'B', lastMessageType: 'text' });+ });++ it('keeps the newer second whichever message is observed last', () => {+ const store = new BaileysSessionStore();+ store.recordMessage(msg('B', SECOND + 1));+ store.recordMessage(msg('A'));++ expect(store.lastMessage(CHAT)?.key.id).toBe('B');+ expect(store.lastInboundMessage(CHAT)?.key.id).toBe('B');+ });++ it('does not bring back the original text when the current message is delivered again after an edit', () => {+ const store = new BaileysSessionStore();+ store.recordMessage(msg('A'), 'text');+ store.recordMessageEdit(CHAT, 'A', 'edited');+ store.recordMessage(msg('A'), 'text');++ expect(store.listChats()[0].lastMessage).toBe('edited');+ });++ it('keeps the first of equal-time history, in either order, and gives way to a live message', async () => {+ for (const ids of [+ ['A', 'B'],+ ['B', 'A'],+ ]) {+ const store = new BaileysSessionStore();+ await historyFor(store).captureHistoryMessages(ids.map(id => msg(id)));+ expect(store.lastMessage(CHAT)?.key.id).toBe(ids[0]);++ store.recordMessage(msg('LIVE'));+ expect(store.lastMessage(CHAT)?.key.id).toBe('LIVE');+ }+ });++ it('does not let equal-time history displace a live message', async () => {+ const store = new BaileysSessionStore();+ store.recordMessage(msg('B'));+ await historyFor(store).captureHistoryMessages([msg('A')]);++ expect(store.lastMessage(CHAT)?.key.id).toBe('B');+ });++ it.each([false, true])('keeps arrival order when the first message is slow to process (slow=%s)', async slow => {+ const store = new BaileysSessionStore();+ const emitted: string[] = [];+ let release!: () => void;+ const gate = new Promise<void>(resolve => (release = resolve));+ let sequence = 0;+ const host = {+ loadLib: () =>+ Promise.resolve({+ normalizeMessageContent: (content: unknown) => content,+ getContentType: (content: object) => Object.keys(content)[0],+ }),+ logger: { debug: () => undefined, warn: () => undefined, error: () => undefined },+ toNeutralJid: (id: string) => store.toNeutralJid(id),+ normalizedSelfJid: () => '[email protected]',+ inboundLimiter: new ConcurrencyLimiter(2),+ nextMessageOrder: () => ++sequence,+ getStoredMessage: async (id: string) => {+ if (slow && id === 'A') await gate;+ return null;+ },+ putStoredMessage: () => undefined,+ recordKeyLidMappings: () => undefined,+ recordMessage: (...args: Parameters<typeof store.recordMessage>) => store.recordMessage(...args),+ recordMessageEdit: (...args: Parameters<typeof store.recordMessageEdit>) => store.recordMessageEdit(...args),+ consumeOwnSend: () => false,+ getOnMessage: () => (message: IncomingMessage) => emitted.push(message.id),+ getOnMessageCreate: () => undefined,+ } as unknown as BaileysEventsHost;+ const events = new BaileysEvents(host);+ const flush = () => new Promise<void>(resolve => setImmediate(resolve));++ events.handleMessagesUpsert({ messages: [msg('A')], type: 'notify' });+ await flush();+ events.handleMessagesUpsert({ messages: [msg('B')], type: 'notify' });+ await flush();+ expect(emitted).toEqual(slow ? ['B'] : ['A', 'B']);+ release();+ await flush();++ expect(emitted).toEqual(slow ? ['B', 'A'] : ['A', 'B']);+ expect(store.lastMessage(CHAT)?.key.id).toBe('B');+ expect(store.lastInboundMessage(CHAT)?.key.id).toBe('B');+ });+});diff --git a/src/engine/adapters/baileys-session-store.ts b/src/engine/adapters/baileys-session-store.tsindex 70d1f67fe..f3b47010c 100644--- a/src/engine/adapters/baileys-session-store.ts+++ b/src/engine/adapters/baileys-session-store.ts@@ -11,6 +11,8 @@ interface LastMessage {timestamp: number;text: string;type?: MessageType;+ /** Where the message stands in the order this session observed them, which settles a tie in `timestamp`. */+ order: number;}// Default per-map entry cap, matching the other per-session bounds (LID_MAPPING_CACHE_MAX,@@ -199,7 +201,8 @@ export class BaileysSessionStore {* answering with the preview after an API reply sent nothing while the route reported success.* An evicted entry reads null, which the receipt path answers as "nothing known".*/- private readonly lastInbound: LruMap<string, { key: WAMessageKey; timestamp: number }>;+ private readonly lastInbound: LruMap<string, { key: WAMessageKey; timestamp: number; order: number }>;+ private messageOrder = 0;private readonly lidToPn: LruMap<string, string>;/*** Per-chat disappearing-messages timer (seconds) learned from inbound messages (#473), the reliable@@ -423,7 +426,15 @@ export class BaileysSessionStore {else void this.lidStore?.rememberIfUnknown?.(userPart(lidJid), userPart(pnJid), this.sessionId);}- recordMessage(msg: WAMessage, type?: MessageType): void {+ /**+ * The next place in the observed order. A live message takes its place on arrival, before the media+ * download and the store reads that can let a later one finish first; history, which has none, records 0.+ */+ nextMessageOrder(): number {+ return ++this.messageOrder;+ }++ recordMessage(msg: WAMessage, type?: MessageType, order = this.nextMessageOrder()): void {const chatId = msg.key?.remoteJid;if (!chatId || !msg.key) {return;@@ -437,14 +448,32 @@ export class BaileysSessionStore {const timestamp = this.toUnixSeconds(msg.messageTimestamp);if (!msg.key.fromMe) {const inbound = this.lastInbound.get(key);- if (!inbound || inbound.timestamp < timestamp) this.lastInbound.set(key, { key: msg.key, timestamp });+ if (+ !inbound ||+ inbound.timestamp < timestamp ||+ (inbound.timestamp === timestamp && this.followsOther(msg, order, inbound))+ ) {+ this.lastInbound.set(key, { key: msg.key, timestamp, order });+ }}const existing = this.lastMessages.get(key);- if (existing && existing.timestamp >= timestamp) {+ if (+ existing &&+ (existing.timestamp > timestamp || (existing.timestamp === timestamp && !this.followsOther(msg, order, existing)))+ ) {return; // keep the newest}const text = msg.message?.conversation ?? msg.message?.extendedTextMessage?.text ?? '';- this.lastMessages.set(key, { key: msg.key, timestamp, text, type });+ this.lastMessages.set(key, { key: msg.key, timestamp, text, type, order });+ }++ /**+ * Whether a message sent in the same second as `other` came after it. Timestamps have a resolution of+ * a second, so two messages of a burst tie; the order they were observed in decides, and a repeat of+ * the same message (a redelivery) is no later than itself.+ */+ private followsOther(msg: WAMessage, order: number, other: { key: WAMessageKey; order: number }): boolean {+ return !!msg.key.id && other.key.id !== msg.key.id && order > other.order;}/**@@ -640,7 +669,9 @@ export class BaileysSessionStore {for (const [key, m] of [...this.lastMessages.entries()]) {const id = this.toNeutralJid(key);const seen = previews.get(id);- if (!seen || m.timestamp > seen.timestamp) previews.set(id, m);+ if (!seen || m.timestamp > seen.timestamp || (m.timestamp === seen.timestamp && m.order > seen.order)) {+ previews.set(id, m);+ }}const byId = new Map<string, Chat[]>();for (const c of [...this.chats.values()]) {@@ -681,7 +712,8 @@ export class BaileysSessionStore {/** The newest message the chat received (not one this account sent), or null when none is known. */lastInboundMessage(chatId: string): { key: WAMessageKey; timestamp: number } | null {- return this.newestAcrossTwins(this.lastInbound, chatId) ?? null;+ const m = this.newestAcrossTwins(this.lastInbound, chatId);+ return m ? { key: m.key, timestamp: m.timestamp } : null;}/**@@ -689,11 +721,19 @@ export class BaileysSessionStore {* contact's lid before the lid->phone mapping was learned stays on the lid twin while the chat key* moves to the phone-keyed chat record, so reading the chat key alone would lose it.*/- private newestAcrossTwins<T extends { timestamp: number }>(map: LruMap<string, T>, chatId: string): T | undefined {+ private newestAcrossTwins<T extends { timestamp: number; order: number }>(+ map: LruMap<string, T>,+ chatId: string,+ ): T | undefined {let newest: T | undefined;for (const k of [this.chatKey(chatId), ...this.chatTwins(chatId)]) {const v = map.get(k);- if (v && (!newest || v.timestamp > newest.timestamp)) newest = v;+ if (+ v &&+ (!newest || v.timestamp > newest.timestamp || (v.timestamp === newest.timestamp && v.order > newest.order))+ ) {+ newest = v;+ }}return newest;}diff --git a/src/engine/adapters/baileys.adapter.ts b/src/engine/adapters/baileys.adapter.tsindex 7c349504b..586196156 100644--- a/src/engine/adapters/baileys.adapter.ts+++ b/src/engine/adapters/baileys.adapter.ts@@ -148,7 +148,8 @@ export class BaileysAdapter implements IWhatsAppEngine {toUnixSeconds,inboundLimiter: this.inboundLimiter,recordKeyLidMappings: key => this.sessionStore.recordKeyLidMappings(key),- recordMessage: (msg, type) => this.sessionStore.recordMessage(msg, type),+ recordMessage: (msg, type, order?: number) => this.sessionStore.recordMessage(msg, type, order),+ nextMessageOrder: () => this.sessionStore.nextMessageOrder(),recordMessageEdit: (chatId, messageId, text, type) =>this.sessionStore.recordMessageEdit(chatId, messageId, text, type),putStoredMessage: msg => this.config.messageStore?.put(this.config.dbSessionId, msg),