Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): announce an edit or reaction after its message

Yudhi Armyndharise12155375a5b
diff --git a/src/engine/adapters/baileys-events.spec.ts b/src/engine/adapters/baileys-events.spec.ts
index 1ec210cf2..c938bd6bf 100644
--- a/src/engine/adapters/baileys-events.spec.ts
+++ b/src/engine/adapters/baileys-events.spec.ts
@@ -1379,10 +1379,13 @@ describe('BaileysEvents encrypted edits and reactions', () => {
await flush();
events.handleMessagesUpsert({ type: 'notify', messages: [editEnvelope()] });
await flush();
+ // The edit was read, but it is announced once the session holds the message it changes.
+ expect(onMessageEdited).not.toHaveBeenCalled();
- expect(onMessageEdited).toHaveBeenCalledWith(expect.objectContaining({ messageId: 'ORIG1', body: 'new text' }));
release(null);
await flush();
+
+ expect(onMessageEdited).toHaveBeenCalledWith(expect.objectContaining({ messageId: 'ORIG1', body: 'new text' }));
});
it('reads the edit of an original that finishes while the edit is still reading the store', async () => {
@@ -1686,3 +1689,110 @@ describe('BaileysEvents encrypted edits and reactions', () => {
expect(onMessageReaction).not.toHaveBeenCalled();
});
});
+
+// Where nothing is admitted before the emit (SQLite) a reaction of a message still being processed waits for its
+// emit; the wait ends at the latest after three media deadlines, and its timer is neither kept nor left running.
+describe('BaileysEvents changes waiting for the message they change', () => {
+ const chat = '[email protected]';
+ const flush = (): Promise<void> => new Promise(resolve => setImmediate(resolve));
+ const original: WAMessage = {
+ key: { id: 'M', remoteJid: chat, fromMe: false },
+ messageTimestamp: 1_700_000_000,
+ message: { conversation: 'hi' },
+ };
+ const reaction: WAMessage = {
+ key: { id: 'R', remoteJid: chat, fromMe: false },
+ messageTimestamp: 1_700_000_001,
+ message: { reactionMessage: { key: { id: 'M', remoteJid: chat, fromMe: false }, text: 'ok' } },
+ };
+ const saved = process.env.MEDIA_DOWNLOAD_TIMEOUT_MS;
+
+ beforeEach(() => {
+ process.env.MEDIA_DOWNLOAD_TIMEOUT_MS = '1000';
+ jest.useFakeTimers({ doNotFake: ['nextTick', 'setImmediate'] });
+ });
+ afterEach(() => {
+ jest.useRealTimers();
+ if (saved === undefined) delete process.env.MEDIA_DOWNLOAD_TIMEOUT_MS;
+ else process.env.MEDIA_DOWNLOAD_TIMEOUT_MS = saved;
+ });
+
+ /** The original never finishes: its check for a repeat delivery is not answered. */
+ const withMessageStuck = () => {
+ let checked = false;
+ const onMessageReaction = jest.fn();
+ const events = new BaileysEvents(
+ makeHost({
+ getStoredMessage: (id: string) => {
+ if (id !== 'M' || checked) return Promise.resolve(null);
+ checked = true;
+ return new Promise<WAMessage | null>(() => undefined);
+ },
+ getOnMessageReaction: () => onMessageReaction,
+ }),
+ );
+ events.handleMessagesUpsert({ type: 'notify', messages: [original] });
+ return { events, onMessageReaction };
+ };
+
+ it('lets a change go three media deadlines after it began to wait, and leaves no timer behind', async () => {
+ const { events, onMessageReaction } = withMessageStuck();
+ await flush();
+ events.handleMessagesUpsert({ type: 'notify', messages: [reaction] });
+ await flush();
+ expect(onMessageReaction).not.toHaveBeenCalled();
+ expect(jest.getTimerCount()).toBe(1);
+
+ jest.advanceTimersByTime(2_999);
+ expect(onMessageReaction).not.toHaveBeenCalled();
+ jest.advanceTimersByTime(1);
+
+ expect(onMessageReaction).toHaveBeenCalledTimes(1);
+ expect(jest.getTimerCount()).toBe(0);
+ });
+
+ it('does not keep the process alive while a change waits', async () => {
+ const waits: Array<{ unref: jest.SpyInstance }> = [];
+ const fake = global.setTimeout;
+ jest.spyOn(global, 'setTimeout').mockImplementation(((fn: () => void, ms?: number) => {
+ const handle = fake(fn, ms) as unknown as { unref: () => unknown };
+ if (ms === 3_000) waits.push({ unref: jest.spyOn(handle, 'unref') });
+ return handle;
+ }) as never);
+ const { events } = withMessageStuck();
+ await flush();
+
+ events.handleMessagesUpsert({ type: 'notify', messages: [reaction] });
+ await flush();
+
+ expect(waits).toHaveLength(1);
+ expect(waits[0].unref).toHaveBeenCalledTimes(1);
+ });
+
+ it('stops the timer once the message has been handed on', async () => {
+ let release!: (stored: WAMessage | null) => void;
+ let checked = false;
+ const onMessageReaction = jest.fn();
+ const events = new BaileysEvents(
+ makeHost({
+ getStoredMessage: (id: string) => {
+ if (id !== 'M' || checked) return Promise.resolve(null);
+ checked = true;
+ return new Promise<WAMessage | null>(resolve => (release = resolve));
+ },
+ getOnMessageReaction: () => onMessageReaction,
+ }),
+ );
+ events.handleMessagesUpsert({ type: 'notify', messages: [original] });
+ await flush();
+ events.handleMessagesUpsert({ type: 'notify', messages: [reaction] });
+ await flush();
+ expect(jest.getTimerCount()).toBe(1);
+
+ release(null);
+ await flush();
+
+ expect(onMessageReaction).toHaveBeenCalledTimes(1);
+ expect(jest.getTimerCount()).toBe(0);
+ });
+});
diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.ts
index 62c170827..b4af26edd 100644
--- a/src/engine/adapters/baileys-events.ts
+++ b/src/engine/adapters/baileys-events.ts
@@ -346,7 +346,22 @@ export class BaileysEvents {
*/
private readonly outcomesPending = new Map<string, { count: number; ended: Promise<void>; free: () => void }>();
- /** How many changes wait for a delivery or its outcome (see parkUntil); the rest are judged at once. */
+ /**
+ * Reactions, edits and deletes that reached a message still being processed where nothing is admitted before
+ * the emit (SQLite), by its id, in the order they arrived, with the timer that lets them go if the message is
+ * never handed on. The session holds no entry for such a message until it is emitted, so a reaction announced
+ * earlier is stored nowhere and an edit leaves no marker: they are announced right after the emit instead (see
+ * announceWhenHandedOn).
+ */
+ private readonly changesAwaitingEmit = new Map<
+ string,
+ { announce: Array<() => void>; timer: ReturnType<typeof setTimeout> }
+ >();
+
+ /**
+ * How many changes wait for a delivery, its outcome (see parkUntil) or its emit (see changesAwaitingEmit); the
+ * rest are judged, or announced, at once.
+ */
private changesParked = 0;
private static readonly CHANGES_PARKED_LIMIT = 64;
@@ -511,6 +526,8 @@ export class BaileysEvents {
this.inboundInFlight.delete(id);
// The edit's store write was chained behind this, so the store carries it (or a newer one) now.
this.editedWhileInFlight.delete(id);
+ // A change that came in after the message was handed on, while it still counted as in flight.
+ this.handOverChanges(id);
});
}
}
@@ -673,7 +690,14 @@ export class BaileysEvents {
this.changeStoredMessage(pm.key?.id, stored =>
this.mayChange(stored.key, msg.key, true) ? { ...stored, message: null } : null,
);
- this.host.getOnMessageRevoked()?.(revoked, { fromMe: msg.key.fromMe === true });
+ // Behind the reactions and edits of the message that wait for its emit, never for the message itself:
+ // the delete keeps it from being emitted, and they were made before it.
+ this.announceWhenHandedOn(
+ revoked.id,
+ opts.ticket,
+ () => this.host.getOnMessageRevoked()?.(revoked, { fromMe: msg.key.fromMe === true }),
+ true,
+ );
return;
}
if (pm?.type === b.proto.Message.ProtocolMessage.Type.MESSAGE_EDIT) {
@@ -731,7 +755,7 @@ export class BaileysEvents {
: undefined;
return content && setBaileysText(content, edited.body) ? stored : null;
});
- this.host.getOnMessageEdited()?.(edited);
+ this.announceWhenHandedOn(edited.messageId, opts.ticket, () => this.host.getOnMessageEdited()?.(edited));
return;
}
// Other protocol messages (ephemeral, history sync, etc.) — skip silently.
@@ -753,7 +777,7 @@ export class BaileysEvents {
keyParticipant(msg.key) ?? (msg.key.fromMe === true ? this.host.normalizedSelfJid() : remoteJid),
),
};
- this.host.getOnMessageReaction()?.(event);
+ this.announceWhenHandedOn(rm?.key?.id, opts.ticket, () => this.host.getOnMessageReaction()?.(event));
return;
}
@@ -864,6 +888,60 @@ export class BaileysEvents {
`Unhandled error processing inbound message (id=${msg.key?.id ?? 'unknown'}); dropping`,
err instanceof Error ? err.message : String(err),
);
+ } finally {
+ // Emitted by now, or never to be: what waited for this message goes to the session in the same turn, so
+ // it reaches the message before its row is written.
+ this.handOverChanges(msg.key?.id);
+ }
+ }
+
+ /**
+ * Announce a reaction, an edit or a delete of message `id`. With a ticket the session holds the message from
+ * its arrival and keeps the change for its row, so it is announced at once. Without one the session knows
+ * nothing of a message before its emit, so a change that reaches a message still being processed is announced
+ * right after the emit instead, in the order the changes arrived, when the session holds the message. A delete
+ * (`behindChangesOnly`) never waits for the message: it waits only behind the changes already waiting, so they
+ * are announced in the order they arrived. A change waits at most the three media deadlines a change aimed at a
+ * delivery in flight waits (see targetsForeignMessage), and no more than CHANGES_PARKED_LIMIT wait at once.
+ */
+ private announceWhenHandedOn(
+ messageId: string | null | undefined,
+ ticket: InboundTicket | undefined,
+ announce: () => void,
+ behindChangesOnly = false,
+ ): void {
+ const waiting = messageId && !ticket ? this.changesAwaitingEmit.get(messageId) : undefined;
+ const waits = !!waiting || (!behindChangesOnly && !ticket && !!messageId && this.inboundInFlight.has(messageId));
+ if (!messageId || !waits || this.changesParked >= BaileysEvents.CHANGES_PARKED_LIMIT) {
+ announce();
+ return;
+ }
+ this.changesParked++;
+ if (waiting) {
+ waiting.announce.push(announce);
+ return;
+ }
+ const timer = setTimeout(() => this.handOverChanges(messageId), 3 * inboundMediaTimeoutMs());
+ timer.unref();
+ this.changesAwaitingEmit.set(messageId, { announce: [announce], timer });
+ }
+
+ /** Announce, in order, what waited for message `id` to be handed on (see announceWhenHandedOn). */
+ private handOverChanges(messageId: string | null | undefined): void {
+ const waiting = messageId ? this.changesAwaitingEmit.get(messageId) : undefined;
+ if (!messageId || !waiting) return;
+ this.changesAwaitingEmit.delete(messageId);
+ clearTimeout(waiting.timer);
+ this.changesParked -= waiting.announce.length;
+ for (const announce of waiting.announce) {
+ try {
+ announce();
+ } catch (err) {
+ this.host.logger.warn('Failed to announce a change that waited for its message', {
+ msgId: messageId,
+ error: err instanceof Error ? err.message : String(err),
+ });
+ }
}
}
diff --git a/src/engine/adapters/baileys-quote-on-receipt.spec.ts b/src/engine/adapters/baileys-quote-on-receipt.spec.ts
index dfe5c6d56..fc17be370 100644
--- a/src/engine/adapters/baileys-quote-on-receipt.spec.ts
+++ b/src/engine/adapters/baileys-quote-on-receipt.spec.ts
@@ -357,7 +357,8 @@ describe('quoting a Baileys message the moment it is announced', () => {
downloads[0]();
await ticks();
- expect(order).toEqual(['edited', 'received']);
+ // The edit is announced right after the message it changes, when the session holds it.
+ expect(order).toEqual(['received', 'edited']);
expect(heard).toEqual(['fixed caption']);
expect(writtenContents('TARGET')).toEqual([
{ imageMessage: { mimetype: 'image/jpeg', caption: 'fixed caption' } },
@@ -387,13 +388,8 @@ describe('quoting a Baileys message the moment it is announced', () => {
expect(heard).toEqual(['fixed caption', 'fixed caption']);
});
- it('lets a consumer of the edit quote the edited text while a repeat of the original is stored', async () => {
- let reply: Promise<unknown> | undefined;
- const { events, messaging, sock } = build(() => undefined, {
- edited: () => {
- reply = messaging.replyToMessage(CHAT, 'TARGET', 'ok');
- },
- });
+ it('lets a quote carry the edited text while a repeat of the original is stored', async () => {
+ const { events, messaging, sock } = build(() => undefined);
events.handleMessagesUpsert({ messages: [photo('TARGET')], type: 'notify' });
await ticks();
events.handleMessagesUpsert({ messages: [photo('TARGET')], type: 'notify' });
@@ -403,6 +399,7 @@ describe('quoting a Baileys message the moment it is announced', () => {
events.handleMessagesUpsert({ messages: [edit()], type: 'notify' });
await ticks();
+ const reply = messaging.replyToMessage(CHAT, 'TARGET', 'ok');
await expect(reply).resolves.toMatchObject({ id: 'R1' });
const [, , options] = sock.sendMessage.mock.calls[0] as [unknown, unknown, { quoted?: WAMessage }];
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index b6b4fca5f..ef5a708ab 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -9787,10 +9787,10 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
expect(warn).not.toHaveBeenCalledWith(dropped, expect.anything());
});
- it('does not hold an edit from the chat the delivery arrived in', async () => {
+ it('does not hold an edit from the chat the delivery arrived in for the answer', async () => {
const callbacks = sessionCallbacks();
await startWith({ onMessage: jest.fn(() => ({ settled: answer().promise })), ...callbacks });
- holdReads([1]);
+ const open = holdReads([1]);
deliverReuse();
await settleAll();
@@ -9799,6 +9799,10 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
{ key: { id: 'REUSED' }, type: 14, editedMessage: { conversation: 'late, edited' } },
);
await settleAll();
+ // It follows the delivery's emit, which the answer of the session does not hold up.
+ expect(callbacks.onMessageEdited).not.toHaveBeenCalled();
+ open(1);
+ await settleAll();
expect(callbacks.onMessageEdited).toHaveBeenCalledTimes(1);
});
@@ -10335,8 +10339,9 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
answered.resolve({ conflict });
await settleAll();
- expect(onMessageEdited).toHaveBeenCalledTimes(conflict ? 1 : 0);
expect(warn.mock.calls.filter(([message]) => message === dropped)).toHaveLength(conflict ? 0 : 1);
+ // The real edit is judged, but follows the delivery that is still being processed.
+ expect(onMessageEdited).not.toHaveBeenCalled();
openFirst();
await settleAll();
diff --git a/src/engine/adapters/inbound-hold.spec.ts b/src/engine/adapters/inbound-hold.spec.ts
index 65e1d341a..969885f5d 100644
--- a/src/engine/adapters/inbound-hold.spec.ts
+++ b/src/engine/adapters/inbound-hold.spec.ts
@@ -161,6 +161,7 @@ describe('Baileys inbound messages held for storage', () => {
Object.keys(content ?? {}).find(k => k === 'conversation' || k.endsWith('Message')),
downloadMediaMessage,
proto: { Message: { ProtocolMessage: { Type: { REVOKE: 0, MESSAGE_EDIT: 14 } } } },
+ BufferJSON: { replacer: (_k: string, v: unknown) => v, reviver: (_k: string, v: unknown) => v },
} as unknown as Awaited<ReturnType<BaileysEventsHost['loadLib']>>;
const stream = (bytes = 3) => ({
@@ -1142,6 +1143,326 @@ describe('Baileys inbound messages held for storage', () => {
);
});
});
+
+ // A reaction, an edit or a delete of a message that is still being processed. PostgreSQL holds the message from
+ // its arrival, so the session keeps the change for the row whenever it comes. SQLite holds nothing before the
+ // emit, so the adapter announces the change right after it, when the session holds the message.
+ describe.each([
+ ['PostgreSQL', false],
+ ['SQLite', true],
+ ])('a reaction, an edit or a delete of media still downloading, on %s', (_, sqlite) => {
+ const CHAT = '[email protected]';
+ const target = (id: string) => ({ id, remoteJid: CHAT, fromMe: false });
+ const change = (id: string, content: Row): WAMessage => ({
+ key: { id, remoteJid: CHAT, fromMe: false },
+ messageTimestamp: 1_700_000_001,
+ message: content,
+ });
+ const react = (id: string, of: string, reaction: string): WAMessage =>
+ change(id, { reactionMessage: { key: target(of), text: reaction } });
+ const edit = (id: string, of: string, body: string): WAMessage =>
+ change(id, {
+ protocolMessage: {
+ type: 14,
+ key: target(of),
+ editedMessage: { conversation: body },
+ timestampMs: 1_700_000_002_000,
+ },
+ });
+ const revoke = (id: string, of: string): WAMessage => change(id, { protocolMessage: { type: 0, key: target(of) } });
+ /** The next download goes on until the returned function is called. */
+ const download = (): (() => void) => {
+ const pending = deferred<ReturnType<typeof stream>>();
+ downloadMediaMessage.mockImplementationOnce(() => pending.promise);
+ return () => pending.resolve(stream());
+ };
+ /** The callbacks the session is handed, noted in the order the adapter calls them. */
+ const traced = (h: ReturnType<typeof heldProjector>) => {
+ const log: string[] = [];
+ const to = h.callbacks;
+ const callbacks: EngineEventCallbacks = {
+ ...to,
+ onMessage: (m, t) => (log.push(`message ${m.id}`), to.onMessage!(m, t)),
+ onMessageReaction: r => (log.push(`reaction ${r.messageId} ${r.reaction}`), to.onMessageReaction!(r)),
+ onMessageEdited: e => (log.push(`edited ${e.messageId} ${e.body}`), to.onMessageEdited!(e)),
+ onMessageRevoked: (m, actor) => (log.push(`revoked ${m.id}`), to.onMessageRevoked!(m, actor)),
+ };
+ return { log, callbacks };
+ };
+ const changesOnly = (log: string[]): string[] => log.filter(entry => !entry.startsWith('message '));
+
+ it('keeps a reaction for the row of the message', async () => {
+ const h = heldProjector({ unique: true, sqlite });
+ const { events } = build(4, h.callbacks);
+ const release = download();
+ upsert(events, media('M', CHAT));
+ await flush();
+ upsert(events, react('R', 'M', 'THUMB'));
+ await flush();
+ release();
+ await flush();
+
+ expect(h.rows.map(r => [r.waMessageId, (r.metadata as Row | null)?.reactions])).toEqual([
+ ['M', { [CHAT]: 'THUMB' }],
+ ]);
+ expect(h.announcedAs('message.reaction')).toEqual([
+ expect.objectContaining({ messageId: 'M', reaction: 'THUMB' }),
+ ]);
+ });
+
+ it('has the session hold the message when an edit reaches it, and the row takes the text', async () => {
+ const h = heldProjector({ unique: true, sqlite });
+ const heldWhenEdited: boolean[] = [];
+ const rowsWhenEdited: number[] = [];
+ const onMessageEdited: EngineEventCallbacks['onMessageEdited'] = e => {
+ heldWhenEdited.push(h.projector.inFlightInbound('s1', e.messageId) !== undefined);
+ rowsWhenEdited.push(h.rows.length);
+ h.callbacks.onMessageEdited!(e);
+ };
+ const { events } = build(4, { ...h.callbacks, onMessageEdited });
+ const release = download();
+ upsert(events, media('M', CHAT, { caption: 'caption' }));
+ await flush();
+ upsert(events, edit('E', 'M', 'fixed caption'));
+ await flush();
+ release();
+ await flush();
+
+ expect(h.rows).toEqual([expect.objectContaining({ waMessageId: 'M', body: 'fixed caption' })]);
+ expect(h.announcedAs('message.edited')).toEqual([
+ expect.objectContaining({ messageId: 'M', body: 'fixed caption' }),
+ ]);
+ // PostgreSQL holds it from the arrival, before the emit, which inFlightInbound does not report yet.
+ expect(heldWhenEdited).toEqual([sqlite]);
+ // Either way the edit reaches it before its row is written, and an edit that meets the row with the same
+ // text is no edit to the session.
+ expect(rowsWhenEdited).toEqual([0]);
+ });
+
+ it(
+ sqlite
+ ? 'announces them once the message has been handed on'
+ : 'announces them at once, the session holds the message already',
+ async () => {
+ const h = heldProjector({ unique: true, sqlite });
+ const { events } = build(4, h.callbacks);
+ const release = download();
+ upsert(events, media('M', CHAT));
+ await flush();
+ upsert(events, react('R', 'M', 'ok'), edit('E', 'M', 'fixed'));
+ await flush();
+ const early = [h.announcedAs('message.reaction').length, h.announcedAs('message.edited').length];
+ release();
+ await flush();
+
+ expect(early).toEqual(sqlite ? [0, 0] : [1, 1]);
+ expect([h.announcedAs('message.reaction').length, h.announcedAs('message.edited').length]).toEqual([1, 1]);
+ },
+ );
+
+ it('announces a delete at once, and neither emits nor announces the message after it', async () => {
+ const h = heldProjector({ unique: true, sqlite });
+ const { events } = build(4, h.callbacks);
+ const release = download();
+ upsert(events, media('M', CHAT, { caption: 'caption' }));
+ await flush();
+ upsert(events, revoke('V', 'M'));
+ await flush();
+ expect(h.announcedAs('message.revoked')).toHaveLength(1);
+ release();
+ await flush();
+
+ expect(h.announced()).toEqual([]);
+ // As on whatsapp-web.js a held message is stored cleared; where nothing holds it (SQLite) it is not stored.
+ expect(h.rows).toEqual(sqlite ? [] : [expect.objectContaining({ waMessageId: 'M', body: '', type: 'revoked' })]);
+ });
+
+ it.each([
+ ['a reaction', (): WAMessage => react('R', 'M', 'ok'), 'reaction M ok'],
+ ['an edit', (): WAMessage => edit('E', 'M', 'fixed'), 'edited M fixed'],
+ ])(
+ 'announces %s before the delete that follows it, and no content after the delete',
+ async (_kind, first, seen) => {
+ const h = heldProjector({ unique: true, sqlite });
+ const { log, callbacks } = traced(h);
+ const { events } = build(4, callbacks);
+ const release = download();
+ upsert(events, media('M', CHAT, { caption: 'caption' }));
+ await flush();
+ upsert(events, first(), revoke('V', 'M'));
+ await flush();
+ release();
+ await flush();
+
+ expect(changesOnly(log)).toEqual([seen, 'revoked M']);
+ expect(h.announced()).toEqual([]);
+ expect(h.rows.map(r => r.body)).toEqual(sqlite ? [] : ['']);
+ },
+ );
+ });
+
+ // Where nothing is admitted before the emit (SQLite), a change waits for its message, and the wait is bounded.
+ describe('a change of a message still being processed, where nothing is admitted before the emit', () => {
+ const CHAT = '[email protected]';
+ const target = (id: string) => ({ id, remoteJid: CHAT, fromMe: false });
+ const reaction = (id: string, of: string, text: string): WAMessage => ({
+ key: { id, remoteJid: CHAT, fromMe: false },
+ messageTimestamp: 1_700_000_001,
+ message: { reactionMessage: { key: target(of), text } },
+ });
+ const edit = (id: string, of: string, body: string): WAMessage => ({
+ key: { id, remoteJid: CHAT, fromMe: false },
+ messageTimestamp: 1_700_000_002,
+ message: {
+ protocolMessage: {
+ type: 14,
+ key: target(of),
+ editedMessage: { conversation: body },
+ timestampMs: 1_700_000_002_000,
+ },
+ },
+ });
+ const download = (): (() => void) => {
+ const pending = deferred<ReturnType<typeof stream>>();
+ downloadMediaMessage.mockImplementationOnce(() => pending.promise);
+ return () => pending.resolve(stream());
+ };
+
+ it.each([1, 4])(
+ 'hands the session the message, then its reactions and edits in the order they arrived (limiter of %i)',
+ async k => {
+ const h = heldProjector({ unique: true, sqlite: true });
+ const log: string[] = [];
+ const to = h.callbacks;
+ const { events } = build(k, {
+ ...to,
+ onMessage: (m, t) => (log.push(`message ${m.id}`), to.onMessage!(m, t)),
+ onMessageReaction: r => (log.push(`reaction ${r.messageId} ${r.reaction}`), to.onMessageReaction!(r)),
+ onMessageEdited: e => (log.push(`edited ${e.messageId} ${e.body}`), to.onMessageEdited!(e)),
+ });
+ const release = download();
+ upsert(events, media('M', CHAT, { caption: 'caption' }));
+ await flush();
+ upsert(events, reaction('R1', 'M', 'ok'), edit('E1', 'M', 'fixed'), reaction('R2', 'M', 'ok2'));
+ await flush();
+ expect(log).toEqual([]);
+ release();
+ await flush();
+
+ expect(log).toEqual(['message M', 'reaction M ok', 'edited M fixed', 'reaction M ok2']);
+ },
+ );
+
+ // In the turn of the emit, so they reach the message before the session has written its row: an edit that
+ // meets the row with the text it already has is no edit to the session, and takes no marker.
+ it('announces what waited in the turn of the emit', async () => {
+ const h = heldProjector({ unique: true, sqlite: true });
+ const log: string[] = [];
+ const to = h.callbacks;
+ const { events } = build(4, {
+ ...to,
+ onMessage: (m, t) => {
+ log.push(`message ${m.id}`);
+ queueMicrotask(() => log.push('end of the turn'));
+ return to.onMessage!(m, t);
+ },
+ onMessageReaction: r => (log.push(`reaction ${r.messageId}`), to.onMessageReaction!(r)),
+ onMessageEdited: e => (log.push(`edited ${e.messageId}`), to.onMessageEdited!(e)),
+ });
+ const release = download();
+ upsert(events, media('M', CHAT));
+ await flush();
+ upsert(events, reaction('R', 'M', 'ok'), edit('E', 'M', 'fixed'));
+ await flush();
+ release();
+ await flush();
+
+ expect(log).toEqual(['message M', 'reaction M', 'edited M', 'end of the turn']);
+ });
+
+ it('lets a change go three media deadlines after it began to wait, when the message is never handed on', async () => {
+ process.env.MEDIA_DOWNLOAD_TIMEOUT_MS = '30';
+ const h = heldProjector({ unique: true, sqlite: true });
+ const store = memoryStore();
+ const read = store.getStoredMessage;
+ let checked = false;
+ // The message's own check for a repeat delivery never answers, which keeps it in flight for good.
+ store.getStoredMessage = (id: string) => {
+ if (id !== 'M' || checked) return read(id);
+ checked = true;
+ return new Promise<WAMessage | null>(() => undefined);
+ };
+ const { events } = build(4, h.callbacks, store);
+ upsert(events, text('M', CHAT));
+ await flush();
+ upsert(events, reaction('R', 'M', 'ok'));
+ await sleep(40);
+ expect(h.announcedAs('message.reaction')).toEqual([]);
+
+ await sleep(120);
+ expect(h.announcedAs('message.reaction')).toHaveLength(1);
+ expect(internals(events)).toEqual({ waiting: 0, queued: 0 });
+ });
+
+ it('holds at most 64 changes at a time, and nothing once the message is handed on', async () => {
+ const h = heldProjector({ unique: true, sqlite: true });
+ const { events } = build(4, h.callbacks);
+ const release = download();
+ upsert(events, media('M', CHAT));
+ await flush();
+ upsert(events, ...Array.from({ length: 70 }, (_, i) => reaction(`R${i}`, 'M', `r${i}`)));
+ await flush();
+ // The six past the limit go out at once, as they did before.
+ expect(h.announcedAs('message.reaction')).toHaveLength(6);
+ expect(internals(events)).toEqual({ waiting: 64, queued: 1 });
+
+ release();
+ await flush();
+ expect(h.announcedAs('message.reaction')).toHaveLength(70);
+ expect(internals(events)).toEqual({ waiting: 0, queued: 0 });
+ });
+
+ it('announces the other changes, and does not process the message again, when a consumer throws', async () => {
+ jest.spyOn(console, 'warn').mockImplementation(() => undefined);
+ const h = heldProjector({ unique: true, sqlite: true });
+ let thrown = false;
+ const onMessageReaction: EngineEventCallbacks['onMessageReaction'] = r => {
+ if (!thrown) {
+ thrown = true;
+ throw new Error('consumer failed');
+ }
+ h.callbacks.onMessageReaction!(r);
+ };
+ const { events, emitted } = build(4, { ...h.callbacks, onMessageReaction });
+ const release = download();
+ upsert(events, media('M', CHAT));
+ await flush();
+ upsert(events, reaction('R1', 'M', 'a'), reaction('R2', 'M', 'b'));
+ await flush();
+ release();
+ await flush();
+
+ expect(emitted).toEqual(['M']);
+ expect(downloadMediaMessage).toHaveBeenCalledTimes(1);
+ expect(h.announcedAs('message.reaction')).toEqual([expect.objectContaining({ reaction: 'b' })]);
+ });
+
+ it('does not make a reaction wait for itself, nor two reactions for each other', async () => {
+ const h = heldProjector({ unique: true, sqlite: true });
+ const { events } = build(4, h.callbacks);
+ upsert(events, reaction('SELF', 'SELF', 'a'), reaction('A', 'B', 'b'), reaction('B', 'A', 'c'));
+ await flush();
+
+ expect(h.announcedAs('message.reaction')).toHaveLength(3);
+ expect(internals(events)).toEqual({ waiting: 0, queued: 0 });
+ });
+
+ /** How many changes wait, and for how many messages. */
+ const internals = (events: BaileysEvents) => {
+ const state = events as unknown as { changesParked: number; changesAwaitingEmit: Map<string, unknown> };
+ return { waiting: state.changesParked, queued: state.changesAwaitingEmit?.size ?? 0 };
+ };
+ });
});
describe('whatsapp-web.js inbound messages held for storage', () => {