Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): hold a foreign-looking change until the id is settled

Yudhi Armyndharis0e2f56d307ff
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 65dfbe9bc..c16e6eb9d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -72,7 +72,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Bound the inbound media a PostgreSQL session holds before storing it to `INBOUND_MEDIA_CONCURRENCY` payloads, keeping each chat's arrival order.
- Drop a Baileys edit, revoke or reaction from another chat, an edit from the wrong author, or a one-to-one revoke from the other side, aimed at a message still being downloaded, on SQLite too.
- Drop a Baileys message that reuses the id of one still being downloaded under another chat or sender, so it cannot take the id or stand in for the original when a later delete is judged.
-- Drop the stored copy of a Baileys message that reuses the id of a message the gateway already holds under another chat or sender, and hold a change that reaches it while that delivery is still being processed, so that message's edit, delete or reaction is no longer ignored as foreign to it.
+- Drop the stored copy of a Baileys message that reuses the id of a message the gateway already holds under another chat, sender or direction, and hold an edit, delete or reaction aimed at that id until the gateway has settled the message, so the real message's change is no longer ignored as foreign.
- Warn at storage export when a file is larger than the per-entry import cap `STORAGE_IMPORT_MAX_BYTES`.
- Refuse a `PORT` with surrounding whitespace at boot instead of binding a port the image healthcheck cannot probe.
- Skip or cancel the Baileys version lookup of a session stopped while it connects, so no proxy dispatcher outlives it.
diff --git a/src/engine/adapters/baileys-events.spec.ts b/src/engine/adapters/baileys-events.spec.ts
index 2aefdc6f7..918facbbb 100644
--- a/src/engine/adapters/baileys-events.spec.ts
+++ b/src/engine/adapters/baileys-events.spec.ts
@@ -1314,6 +1314,10 @@ describe('BaileysEvents encrypted edits and reactions', () => {
copy = msg;
return Promise.resolve();
},
+ removeStoredMessage: () => {
+ copy = null;
+ return Promise.resolve();
+ },
getOnMessage: () => jest.fn(() => ({ settled: answered })),
});
return { ...setups, open: (index: number) => opens.get(index)!() };
@@ -1324,8 +1328,8 @@ describe('BaileysEvents encrypted edits and reactions', () => {
sealed: seal('Message Edit', { target: 'ORIG1', author, modifier: reactor }, editPlain),
});
- it('is held until the original is stored, then left to the session while the original has no answer', async () => {
- const { events, onMessageEdited, open } = withHeldReads([1], new Promise(() => undefined));
+ it('is held until the original is stored, and stays held while the original has no answer', async () => {
+ const { events, onMessageEdited, recordMessageEdit, open } = withHeldReads([1], new Promise(() => undefined));
events.handleMessagesUpsert({ type: 'notify', messages: [original()] });
await flush();
@@ -1335,6 +1339,25 @@ describe('BaileysEvents encrypted edits and reactions', () => {
open(1);
await flush();
+ expect(onMessageEdited).not.toHaveBeenCalled();
+ expect(recordMessageEdit).not.toHaveBeenCalled();
+ });
+
+ it('is applied once the answer says the original was another message and its copy is gone', async () => {
+ let answer!: (outcome: { conflict: boolean }) => void;
+ const answered = new Promise<{ conflict: boolean }>(resolve => (answer = resolve));
+ const { events, onMessageEdited, open } = withHeldReads([1], answered);
+
+ events.handleMessagesUpsert({ type: 'notify', messages: [original()] });
+ await flush();
+ events.handleMessagesUpsert({ type: 'notify', messages: [forged()] });
+ await flush();
+ open(1);
+ await flush();
+ expect(onMessageEdited).not.toHaveBeenCalled();
+
+ answer({ conflict: true });
+ await flush();
expect(onMessageEdited).toHaveBeenCalledWith(expect.objectContaining({ messageId: 'ORIG1', body: 'new text' }));
});
diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.ts
index 6ab5b9ad3..75b9d5409 100644
--- a/src/engine/adapters/baileys-events.ts
+++ b/src/engine/adapters/baileys-events.ts
@@ -332,17 +332,17 @@ export class BaileysEvents {
private readonly editedWhileInFlight = new Map<string, { envelope: WAMessageKey; body: string }>();
/**
- * How many deliveries of an id were handed to onMessage and have no outcome yet, by id. The stored
- * copy of such an id may be a duplicate the session is about to call another message's (the real one
- * lives in its table only), so a change that reads as foreign to it is the session's to judge, which
- * it does against the row (see targetsForeignMessage). An entry lasts at most OUTCOME_HOLD_MS. It begins
- * when the delivery is handed on. A change that arrives while the delivery is still being processed
- * (limiter wait, repeat check, media download) and reads as foreign to its key waits for that processing
- * to end, then is judged again with this hold in place.
+ * The deliveries of an id handed to onMessage that have no outcome yet, by id: how many, and a promise
+ * that settles when the last of them has one (or is let go of after OUTCOME_HOLD_MS). The stored copy
+ * of such an id may be a duplicate the session is about to call another message's (the real one lives
+ * in its table only), so it cannot judge a change that reads as foreign to it yet: that change waits
+ * on `ended` and is judged again (see targetsForeignMessage). A hold begins when the delivery is
+ * handed on. A change that arrives while the delivery is still being processed (limiter wait, repeat
+ * check, media download) waits for that processing to end first.
*/
- private readonly outcomesPending = new Map<string, number>();
+ private readonly outcomesPending = new Map<string, { count: number; ended: Promise<void>; free: () => void }>();
- /** How many changes wait for a delivery to be processed (see awaitDelivery); the rest are judged at once. */
+ /** How many changes wait for a delivery or its outcome (see parkUntil); the rest are judged at once. */
private changesParked = 0;
private static readonly CHANGES_PARKED_LIMIT = 64;
@@ -884,18 +884,27 @@ export class BaileysEvents {
/**
* Count a delivery of `id` as awaiting the session's outcome, until the returned function is called
* or OUTCOME_HOLD_MS passes, whichever is first, so a receipt that never settles (a hook that never
- * returns) cannot keep the id held for the life of the adapter. Ending twice counts once.
+ * returns) cannot keep the id held for the life of the adapter. Ending twice counts once. The changes
+ * parked for the id wake once its last delivery's hold has ended.
*/
private holdOutcome(id: string): () => void {
- this.outcomesPending.set(id, (this.outcomesPending.get(id) ?? 0) + 1);
+ let hold = this.outcomesPending.get(id);
+ if (!hold) {
+ let free!: () => void;
+ const ended = new Promise<void>(resolve => (free = resolve));
+ hold = { count: 0, ended, free };
+ this.outcomesPending.set(id, hold);
+ }
+ const entry = hold;
+ entry.count++;
let ended = false;
const end = (): void => {
if (ended) return;
ended = true;
clearTimeout(timer);
- const pending = (this.outcomesPending.get(id) ?? 1) - 1;
- if (pending > 0) this.outcomesPending.set(id, pending);
- else this.outcomesPending.delete(id);
+ if (--entry.count > 0) return;
+ this.outcomesPending.delete(id);
+ entry.free();
};
const timer = setTimeout(end, BaileysEvents.OUTCOME_HOLD_MS);
timer.unref();
@@ -1070,11 +1079,16 @@ export class BaileysEvents {
* or, with `checkAuthor`, one somebody else sent. WhatsApp clients ignore such a message, but Baileys
* emits it as-is and the projector updates the stored row by id alone, so a contact who knows an id
* could rewrite or erase that message. A message still being processed is checked against the key
- * it arrived under, but only once its processing has ended when that key reads the change as foreign:
- * the delivery may be a duplicate of a message the session holds under another chat, and until it has
- * been stored its key proves nothing about which of the two the change names (see awaitDelivery).
- * Fails open: with no original at all, or ids that cannot be compared (see {@link differentWaIds}),
- * the event goes through as it always has.
+ * it arrived under. Fails open: with no original at all, or ids that cannot be compared (see
+ * {@link differentWaIds}), the event goes through as it always has.
+ *
+ * The copy a change reads as foreign to may be a duplicate of a message the session holds under
+ * another chat, which only the session can say once it has stored the message and answered. Such a
+ * change is not handed on to be judged there, since the adapter acts on it before the session does
+ * (the preview, the stored copy, the delete record): it waits for the delivery to be processed, and
+ * then for the answer to its id (see parkUntil), and is judged again. A conflicting copy has been
+ * removed by then, so the real author's change finds nothing to contradict it, and a copy that stays
+ * is the message the change is foreign to. One still held when the wait ends is dropped.
*
* A reaction to a message the account sent to a broadcast list is exempt: Baileys files the
* own-device copy under the list jid (`<id>@broadcast`), while each recipient reacts from their 1:1
@@ -1093,30 +1107,39 @@ export class BaileysEvents {
if (!targetId) return false;
// The key of the delivery in flight a wait was for, and the removals counted when it began.
let waitedFor: { key: WAMessageKey; removals: number } | undefined;
+ // When the wait for the id's hold runs out, however many deliveries hold it in turn.
+ let heldUntil: number | undefined;
let reread = false;
for (;;) {
const removals = this.copyRemovals;
// A message still being processed has no stored copy yet, but the projector may already hold it
// (PostgreSQL), so its own key is checked instead.
const found = await this.readTarget(targetId, 'checking what an edit, revoke or reaction targets');
- // Nothing to read once the delivery was processed means it was never stored (no store, a failed
- // write), so the key it arrived under still stands, unless a conflicting copy went meanwhile: then
- // the delivery was not the message the id names.
+ // Without a stored copy the key of the delivery that was being processed stands (it may never have
+ // been stored: no store, a failed write), unless a conflicting copy went since the wait for it began:
+ // then that delivery was not the message the id names, and nothing says which one is.
+ const removedSince = waitedFor !== undefined && this.copyRemovals !== waitedFor.removals;
const original =
- found?.key ?? (waitedFor && this.copyRemovals === waitedFor.removals ? waitedFor.key : undefined);
+ found && !found.processing ? found.key : removedSince ? undefined : (found?.key ?? waitedFor?.key);
if (!original || !this.readsAsForeign(original, key, checkAuthor, kind)) return false;
- // The session has not said yet whether the delivery that stored this copy is the message it holds
- // under the id or a duplicate of another one. Its own judgement stands in: it holds the message and
- // checks the change against its key and against the stored row, and the stored copy does not.
- if (this.outcomesPending.has(targetId)) return false;
- // No copy is stored yet, only the key of the delivery being processed, and the real message may be
- // one the store never held (the session has it by row), so this key cannot say which of the two the
- // change names. Judged again once the delivery has been stored and handed on, which is when the
- // hold above and the removal of a conflicting copy answer it. Only a delivery the session reports
- // on counts: a status post is held by no entry there, and an own send gets no receipt.
- if (!waitedFor && found?.processing && original.remoteJid !== 'status@broadcast' && original.fromMe !== true) {
+ const processing = found?.processing;
+ const hold = this.outcomesPending.get(targetId);
+ if (hold) {
+ // The session has not said yet whether the delivery that stored this copy is the message it holds
+ // under the id or a duplicate of another one, so the copy cannot judge the change. Parked until it
+ // has, when a conflicting copy is gone and the real author's change is judged by what remains.
+ if (processing) waitedFor ??= { key: original, removals };
+ heldUntil ??= Date.now() + BaileysEvents.OUTCOME_HOLD_MS;
+ const left = heldUntil - Date.now();
+ if (left > 0 && (await this.parkUntil(hold.ended, left, inbound))) continue;
+ } else if (!waitedFor && processing && original.remoteJid !== 'status@broadcast' && original.fromMe !== true) {
+ // No copy is stored yet, only the key of the delivery being processed, and the real message may be
+ // one the store never held (the session has it by row), so this key cannot say which of the two the
+ // change names. Judged again once the delivery has been stored and handed on, which is when the
+ // hold and the removal of a conflicting copy answer it. Only a delivery the session reports on
+ // counts: a status post is held by no entry there, and an own send gets no receipt.
waitedFor = { key: original, removals };
- if (await this.awaitDelivery(found.processing, inbound)) continue;
+ if (await this.parkUntil(processing, 3 * inboundMediaTimeoutMs(), inbound)) continue;
}
// A conflicting copy went while the store was read, and the read may have returned it: it ends the
// hold only once removed, so the read is the one that can no longer be told from the real copy. Read
@@ -1135,16 +1158,18 @@ export class BaileysEvents {
}
/**
- * Park a change until the delivery it reads as foreign to has been processed: at most three media
- * download deadlines (a wait for room in the session, then the download), and never more than
- * CHANGES_PARKED_LIMIT changes at once. Resolves true once it did wait, false when it was not parked.
+ * Park a change until `until` settles or `limitMs` passes, whichever is first: a delivery's processing
+ * (at most three media download deadlines: a wait for room in the session, then the download) or the
+ * hold on an id's outcome. Never more than CHANGES_PARKED_LIMIT changes at once. Resolves true once it
+ * did wait, false when it was not parked.
* A parked change holds nothing the delivery needs. It gives up its inbound slot, which the delivery
* may have to take back after waiting for room, and its place in its chat's order, which a change
* never uses (it is not announced as a message), so it neither starves the delivery nor holds back the
* messages that follow it in the chat.
*/
- private async awaitDelivery(
- done: Promise<void>,
+ private async parkUntil(
+ until: Promise<void>,
+ limitMs: number,
inbound?: { ticket?: InboundTicket; yieldSlot?: <R>(fn: () => Promise<R>) => Promise<R> },
): Promise<boolean> {
if (this.changesParked >= BaileysEvents.CHANGES_PARKED_LIMIT) return false;
@@ -1154,15 +1179,15 @@ export class BaileysEvents {
const wait = (): Promise<void> =>
new Promise<void>(resolve => {
let wake: (() => void) | null = resolve;
- // Whichever comes first; the reaction left on a delivery that never settles holds nothing.
+ // Whichever comes first; the reaction left on a promise that never settles holds nothing.
const end = (): void => {
clearTimeout(timer);
wake?.();
wake = null;
};
- const timer = setTimeout(end, 3 * inboundMediaTimeoutMs());
+ const timer = setTimeout(end, limitMs);
timer.unref();
- done.then(end, end);
+ until.then(end, end);
});
await (inbound?.yieldSlot ? inbound.yieldSlot(wait) : wait());
return true;
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index bcf3a2221..44c238761 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -8991,6 +8991,7 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
describe('a delivery that reuses the id of another message', () => {
type Copy = { key: { id: string; remoteJid: string; participant?: string; fromMe?: boolean } };
const OTHER = '[email protected]';
+ const dropped = 'Dropping an edit, revoke or reaction aimed at a message from another chat or author';
const copies = new Map<string, Copy>();
beforeEach(() => {
@@ -9198,7 +9199,7 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
expect(copies.has('REUSED')).toBe(false);
});
- it('hands the real edit to the session while it has not answered, then removes the copy', async () => {
+ it('holds the real edit while the session has not answered, and hands it on once the copy is removed', async () => {
const answered = answer();
const onMessageEdited = jest.fn();
await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), onMessageEdited });
@@ -9208,14 +9209,16 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
// The edit reads as foreign to the stored copy, which is the delivery the session has yet to judge.
realEdit();
await settleAll();
- expect(onMessageEdited).toHaveBeenCalledWith(
- expect.objectContaining({ messageId: 'REUSED', chatId: '[email protected]' }),
- );
+ expect(onMessageEdited).not.toHaveBeenCalled();
expect(copies.get('REUSED')?.key.remoteJid).toBe(OTHER);
answered.resolve({ conflict: true });
await settleAll();
expect(copies.has('REUSED')).toBe(false);
+ expect(onMessageEdited).toHaveBeenCalledWith(
+ expect.objectContaining({ messageId: 'REUSED', chatId: '[email protected]' }),
+ );
+ expect(fakeStore.remove.mock.invocationCallOrder[0]).toBeLessThan(onMessageEdited.mock.invocationCallOrder[0]);
});
// The read of the real edit runs on the store's clock, not the session's: it can begin under the hold and
@@ -9249,7 +9252,7 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
);
});
- it('hands the real edit to the session when its store read began before the delivery was stored', async () => {
+ it('holds the real edit until the answer when its store read began before the delivery was stored', async () => {
const answered = answer();
const onMessageEdited = jest.fn();
await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), onMessageEdited });
@@ -9272,6 +9275,10 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
openGate();
await settleAll();
+ expect(onMessageEdited).not.toHaveBeenCalled();
+
+ answered.resolve({ conflict: true });
+ await settleAll();
expect(onMessageEdited).toHaveBeenCalledWith(
expect.objectContaining({ messageId: 'REUSED', chatId: '[email protected]' }),
);
@@ -9343,7 +9350,7 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
expect(onMessageEdited).not.toHaveBeenCalled();
});
- it('keeps the id held until the removal of the copy has run', async () => {
+ it('keeps a change held until the removal of the copy has run', async () => {
const answered = answer();
const onMessageEdited = jest.fn();
await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), onMessageEdited });
@@ -9363,12 +9370,13 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
realEdit();
await settleAll();
- expect(onMessageEdited).toHaveBeenCalledWith(
- expect.objectContaining({ messageId: 'REUSED', chatId: '[email protected]' }),
- );
+ expect(onMessageEdited).not.toHaveBeenCalled();
runRemoval();
await settleAll();
expect(copies.has('REUSED')).toBe(false);
+ expect(onMessageEdited).toHaveBeenCalledWith(
+ expect.objectContaining({ messageId: 'REUSED', chatId: '[email protected]' }),
+ );
});
it('applies the edit of the delivery itself to its copy before the answer, and still removes it', async () => {
@@ -9391,12 +9399,13 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
expect(copies.has('REUSED')).toBe(false);
});
- it('stops judging in the session once every delivery of the id has been answered', async () => {
+ it('judges a change against the copy once every delivery of the id has been answered', async () => {
const first = answer();
const second = answer();
const outcomes = [first.promise, second.promise];
const onMessageEdited = jest.fn();
- await startWith({ onMessage: jest.fn(() => ({ settled: outcomes.shift()! })), onMessageEdited });
+ const adapter = await startWith({ onMessage: jest.fn(() => ({ settled: outcomes.shift()! })), onMessageEdited });
+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);
// Both pass the repeat check before either is stored, as a repeat sent while the first is processed does.
fakeSock.fire('messages.upsert', {
type: 'notify',
@@ -9413,21 +9422,24 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
await settleAll();
realEdit();
await settleAll();
- expect(onMessageEdited).toHaveBeenCalledTimes(1); // the second delivery is still unanswered
+ // The second delivery is still unanswered, so the copy cannot judge the change yet.
+ expect(warn).not.toHaveBeenCalledWith(dropped, expect.anything());
second.resolve(undefined);
await settleAll();
+ expect(warn).toHaveBeenCalledWith(dropped, expect.objectContaining({ targetId: 'REUSED' }));
realEdit();
await settleAll();
- expect(onMessageEdited).toHaveBeenCalledTimes(1); // now judged against the copy again
+ expect(warn.mock.calls.filter(([message]) => message === dropped)).toHaveLength(2);
+ expect(onMessageEdited).not.toHaveBeenCalled();
});
// The reusing delivery is not stored, and its receipt does not exist, until its processing (the limiter
// queue, the repeat check, a media download) is over. A change that arrives meanwhile can only be judged
- // against the key the delivery arrived under, which says nothing of which message the id names.
+ // against the key the delivery arrived under, which says nothing of which message the id names. It waits
+ // for the delivery, and then for the session's answer, and is judged once those are in.
describe('a change that arrives while the delivery is still being processed', () => {
const THIRD = '[email protected]';
- const dropped = 'Dropping an edit, revoke or reaction aimed at a message from another chat or author';
const WAIT_MS = 3 * 30_000;
type Kind = 'edit' | 'delete' | 'reaction';
const kinds: Kind[] = ['edit', 'delete', 'reaction'];
@@ -9481,7 +9493,7 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
reaction: callbacks.onMessageReaction,
})[kind];
- it.each(kinds)('holds a real %s, and hands it to the session once the delivery is stored', async kind => {
+ it.each(kinds)('holds a real %s until the delivery is stored and answered, then hands it on', async kind => {
const answered = answer();
const callbacks = sessionCallbacks();
const adapter = await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), ...callbacks });
@@ -9499,11 +9511,15 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
open(1);
await settleAll();
expect(copies.get('REUSED')?.key.remoteJid).toBe(OTHER);
- expect(handedOn(callbacks, kind)).toHaveBeenCalledTimes(1);
+ // The delivery's copy stands in for a message the session may hold under another chat.
+ expect(handedOn(callbacks, kind)).not.toHaveBeenCalled();
+ expect(warn).not.toHaveBeenCalledWith(dropped, expect.anything());
answered.resolve({ conflict: true });
await settleAll();
expect(copies.has('REUSED')).toBe(false);
+ expect(handedOn(callbacks, kind)).toHaveBeenCalledTimes(1);
+ expect(warn).not.toHaveBeenCalledWith(dropped, expect.anything());
});
it.each(kinds)('lands a real %s when the session reports the conflict before it is judged again', async kind => {
@@ -9558,20 +9574,32 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
},
);
- it('leaves a change from a third chat to the session while it has not answered, which judges it by its row', async () => {
- const callbacks = sessionCallbacks();
- const answered = answer();
- await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), ...callbacks });
- const open = holdReads([1]);
- deliverReuse();
- await settleAll();
- changeFrom('edit', THIRD);
- await settleAll();
- open(1);
- await settleAll();
+ // The copy that is removed was the delivery's, so nothing the adapter holds names the message: the change
+ // goes to the session, which judges it by its row. A copy that stays is the message, and the change is
+ // foreign to it.
+ it.each(kinds.flatMap(kind => [true, false].map(conflict => [kind, conflict] as const)))(
+ 'holds the %s from a third chat until the session answers (delivery conflicts: %s)',
+ async (kind, conflict) => {
+ const callbacks = sessionCallbacks();
+ const answered = answer();
+ const adapter = await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), ...callbacks });
+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);
+ const open = holdReads([1]);
+ deliverReuse();
+ await settleAll();
+ changeFrom(kind, THIRD);
+ await settleAll();
+ open(1);
+ await settleAll();
+ expect(handedOn(callbacks, kind)).not.toHaveBeenCalled();
+ expect(warn).not.toHaveBeenCalledWith(dropped, expect.anything());
- expect(callbacks.onMessageEdited).toHaveBeenCalledTimes(1);
- });
+ answered.resolve({ conflict });
+ await settleAll();
+ expect(handedOn(callbacks, kind)).toHaveBeenCalledTimes(conflict ? 1 : 0);
+ expect(warn.mock.calls.filter(([message]) => message === dropped)).toHaveLength(conflict ? 0 : 1);
+ },
+ );
it('does not hold an edit from the chat the delivery arrived in', async () => {
const callbacks = sessionCallbacks();
@@ -9697,20 +9725,186 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
expect(warn.mock.calls.filter(([message]) => message === dropped)).toHaveLength(6);
expect(jest.getTimerCount() - idle).toBe(limit);
+ // The delivery is stored and waits for its answer: the parked changes wait for that too.
open(1);
await settleAll();
- expect(callbacks.onMessageEdited).toHaveBeenCalledTimes(limit);
+ expect(callbacks.onMessageEdited).not.toHaveBeenCalled();
+ expect(warn.mock.calls.filter(([message]) => message === dropped)).toHaveLength(6);
answered.resolve({ conflict: true });
await settleAll();
+ expect(callbacks.onMessageEdited).toHaveBeenCalledTimes(limit);
expect(jest.getTimerCount()).toBe(idle);
});
+
+ it('parks only so many changes on an answer, dropping the rest at once, and frees every timer', async () => {
+ const limit = (BaileysEvents as unknown as { CHANGES_PARKED_LIMIT: number }).CHANGES_PARKED_LIMIT;
+ const callbacks = sessionCallbacks();
+ const answered = answer();
+ const adapter = await startWith({
+ onMessage: jest.fn(() => ({ settled: answered.promise })),
+ ...callbacks,
+ });
+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);
+ deliverReuse();
+ await settleAll();
+ const idle = jest.getTimerCount();
+
+ fakeSock.fire('messages.upsert', {
+ type: 'notify',
+ messages: Array.from({ length: limit + 6 }, (_, i) => ({
+ key: { id: `FLOOD_${i}`, remoteJid: PEER, fromMe: false },
+ message: {
+ protocolMessage: { key: { id: 'REUSED' }, type: 14, editedMessage: { conversation: `edit ${i}` } },
+ },
+ messageTimestamp: 1700000100,
+ })),
+ });
+ await settleAll();
+ // Never handed on unjudged: what cannot wait is dropped.
+ expect(callbacks.onMessageEdited).not.toHaveBeenCalled();
+ expect(warn.mock.calls.filter(([message]) => message === dropped)).toHaveLength(6);
+ expect(jest.getTimerCount() - idle).toBe(limit);
+
+ answered.resolve({ conflict: true });
+ await settleAll();
+ expect(callbacks.onMessageEdited).toHaveBeenCalledTimes(limit);
+ expect(jest.getTimerCount()).toBe(idle - 1); // the delivery's own hold is gone too
+ });
+ });
+ });
+
+ // The adapter acts on a change itself before the session judges it: the chat preview, the record of a
+ // delete, the stored copy and the callbacks. A change that reads as foreign to the copy it names must not
+ // do any of that while the session has not said whether the copy is the message.
+ describe('a forged change aimed at a delivery whose answer is pending', () => {
+ const GROUP = '[email protected]';
+ const MEMBER_1 = '[email protected]';
+ const MEMBER_2 = '[email protected]';
+ const outcomes: Array<[string, InboundOutcome | undefined]> = [
+ ['the session finds no conflict', { conflict: false }],
+ ['the session says nothing', undefined],
+ ];
+
+ const preview = async (adapter: BaileysAdapter, chat: string): Promise<string | undefined> =>
+ (await adapter.getChats()).find(c => c.id === chat)?.lastMessage;
+
+ const receive = (id: string, key: Record<string, unknown>): void => {
+ fakeSock.fire('messages.upsert', {
+ type: 'notify',
+ messages: [
+ {
+ key: { id, fromMe: false, ...key },
+ message: { conversation: 'original text' },
+ messageTimestamp: 1700000100,
+ },
+ ],
+ });
+ };
+
+ it.each(outcomes)(
+ "keeps a group message's preview and callbacks clear of another member's edit while it waits and after it, when %s",
+ async (_, outcome) => {
+ const answered = answer();
+ const onMessageEdited = jest.fn();
+ const adapter = await startWith({
+ onMessage: jest.fn(() => ({ settled: answered.promise })),
+ onMessageEdited,
+ });
+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);
+ fakeSock.fire('chats.upsert', [{ id: GROUP, name: 'Team' }]);
+ receive('GRP1', { remoteJid: GROUP, participant: MEMBER_1 });
+ await settleAll();
+ expect(await preview(adapter, GROUP)).toBe('original text');
+
+ fireProtocol(
+ { id: 'EDIT_P2', remoteJid: GROUP, participant: MEMBER_2, fromMe: false },
+ { key: { id: 'GRP1' }, type: 14, editedMessage: { conversation: 'FORGED BY P2' } },
+ );
+ await settleAll();
+ expect(await preview(adapter, GROUP)).toBe('original text');
+ expect(onMessageEdited).not.toHaveBeenCalled();
+
+ answered.resolve(outcome);
+ await settleAll();
+ expect(await preview(adapter, GROUP)).toBe('original text');
+ expect(onMessageEdited).not.toHaveBeenCalled();
+ expect(fakeStore.update).not.toHaveBeenCalled();
+ expect(warn).toHaveBeenCalledWith(dropped, expect.objectContaining({ targetId: 'GRP1' }));
+ },
+ );
+
+ it.each(outcomes)(
+ 'does nothing for an edit, a delete and a reaction from another chat while it waits and after it, when %s',
+ async (_, outcome) => {
+ const answered = answer();
+ const onMessageEdited = jest.fn();
+ const onMessageRevoked = jest.fn();
+ const onMessageReaction = jest.fn();
+ const adapter = await startWith({
+ onMessage: jest.fn(() => ({ settled: answered.promise })),
+ onMessageEdited,
+ onMessageRevoked,
+ onMessageReaction,
+ });
+ fakeSock.fire('chats.upsert', [{ id: PEER, name: 'Alice' }]);
+ receive('DM1', { remoteJid: PEER });
+ await settleAll();
+
+ const third = { remoteJid: '[email protected]', fromMe: false };
+ fireProtocol(
+ { id: 'EDIT_ENV', ...third },
+ { key: { id: 'DM1' }, type: 14, editedMessage: { conversation: 'FORGED' } },
+ );
+ fireProtocol({ id: 'REVOKE_ENV', ...third }, { key: { id: 'DM1' }, type: 0 });
+ fakeSock.fire('messages.upsert', {
+ type: 'notify',
+ messages: [
+ {
+ key: { id: 'REACT_ENV', ...third },
+ message: { reactionMessage: { key: { id: 'DM1' }, text: '👎' } },
+ messageTimestamp: 1700000101,
+ },
+ ],
+ });
+ await settleAll();
+ const expectUntouched = async (): Promise<void> => {
+ expect(await preview(adapter, '[email protected]')).toBe('original text');
+ expect(onMessageEdited).not.toHaveBeenCalled();
+ expect(onMessageRevoked).not.toHaveBeenCalled();
+ expect(onMessageReaction).not.toHaveBeenCalled();
+ expect((adapter as unknown as { events: BaileysEvents }).events.wasDeletedForEveryone('DM1')).toBe(false);
+ expect(fakeStore.update).not.toHaveBeenCalled();
+ };
+ await expectUntouched();
+
+ answered.resolve(outcome);
+ await settleAll();
+ await expectUntouched();
+ },
+ );
+
+ it('still applies the edit of the real author while the answer is pending', async () => {
+ const answered = answer();
+ const onMessageEdited = jest.fn();
+ const adapter = await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), onMessageEdited });
+ fakeSock.fire('chats.upsert', [{ id: GROUP, name: 'Team' }]);
+ receive('GRP1', { remoteJid: GROUP, participant: MEMBER_1 });
+ await settleAll();
+
+ fireProtocol(
+ { id: 'EDIT_P1', remoteJid: GROUP, participant: MEMBER_1, fromMe: false },
+ { key: { id: 'GRP1' }, type: 14, editedMessage: { conversation: 'fixed by the author' } },
+ );
+ await settleAll();
+
+ expect(onMessageEdited).toHaveBeenCalledWith(expect.objectContaining({ messageId: 'GRP1' }));
+ expect(await preview(adapter, GROUP)).toBe('fixed by the author');
});
});
describe('an answer that does not come', () => {
const HOLD_MS = 5 * 60_000;
- const edited = 'Dropping an edit, revoke or reaction aimed at a message from another chat or author';
beforeEach(() => {
jest.useFakeTimers({ doNotFake: ['nextTick', 'setImmediate'] });
@@ -9720,7 +9914,7 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
jest.useRealTimers();
});
- it('stops holding the id for the session after a while, so a change is judged against the copy again', async () => {
+ it('stops holding the id for the session after a while, so a change is judged against the copy', async () => {
const onMessageEdited = jest.fn();
const adapter = await startWith({
onMessage: jest.fn(() => ({ settled: new Promise<InboundOutcome | undefined>(() => undefined) })),
@@ -9732,14 +9926,55 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
realEdit();
await settleAll();
- expect(onMessageEdited).toHaveBeenCalledTimes(1);
+ jest.advanceTimersByTime(HOLD_MS - 1);
+ await settleAll();
+ expect(warn).not.toHaveBeenCalledWith(dropped, expect.anything());
+
+ jest.advanceTimersByTime(1);
+ await settleAll();
+ // Never handed on unjudged: the copy it was parked for is all there is to judge it by.
+ expect(onMessageEdited).not.toHaveBeenCalled();
+ expect(warn).toHaveBeenCalledWith(dropped, expect.objectContaining({ targetId: 'REUSED' }));
- jest.advanceTimersByTime(HOLD_MS);
realEdit();
await settleAll();
+ expect(onMessageEdited).not.toHaveBeenCalled();
+ expect(warn.mock.calls.filter(([message]) => message === dropped)).toHaveLength(2);
+ });
- expect(onMessageEdited).toHaveBeenCalledTimes(1);
- expect(warn).toHaveBeenCalledWith(edited, expect.objectContaining({ targetId: 'REUSED' }));
+ it('sets none of its timers to keep the process alive', async () => {
+ const WAIT_MS = 3 * 30_000; // a change waiting for a delivery to be processed
+ const answered = answer();
+ await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), onMessageEdited: jest.fn() });
+ // The delivery's repeat check is held, so the change meets it while it is still being processed.
+ let openRead!: () => void;
+ const gate = new Promise<void>(resolve => (openRead = resolve));
+ fakeStore.getMessage.mockImplementationOnce(async (_session: string, id: string) => {
+ await gate;
+ return structuredClone(copies.get(id) ?? null);
+ });
+ const created: Array<{ ms: number; unref: jest.SpyInstance }> = [];
+ const setTimer = global.setTimeout;
+ const spy = jest.spyOn(global, 'setTimeout').mockImplementation((fn: () => void, ms?: number) => {
+ const timer = setTimer(fn, ms);
+ created.push({ ms: ms ?? 0, unref: jest.spyOn(timer, 'unref') });
+ return timer;
+ });
+ try {
+ // A change parked behind the delivery, then on the hold the delivery puts on its answer.
+ deliverReuse();
+ await settleAll();
+ realEdit();
+ await settleAll();
+ openRead();
+ await settleAll();
+ } finally {
+ spy.mockRestore();
+ }
+
+ const watched = created.filter(t => t.ms === HOLD_MS || t.ms === WAIT_MS);
+ expect(watched.map(t => t.ms)).toEqual([WAIT_MS, HOLD_MS, HOLD_MS]);
+ for (const timer of watched) expect(timer.unref).toHaveBeenCalledTimes(1);
});
it('still removes the copy when the session answers after the hold ended', async () => {
@@ -9778,8 +10013,41 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
realEdit();
await settleAll();
+ // Still held for the second, so the copy cannot judge the change yet.
+ expect(onMessageEdited).not.toHaveBeenCalled();
+ expect(warn).not.toHaveBeenCalledWith(dropped, expect.anything());
+
+ second.resolve({ conflict: true });
+ await settleAll();
expect(onMessageEdited).toHaveBeenCalledTimes(1);
- expect(warn).not.toHaveBeenCalledWith(edited, expect.anything());
+ expect(warn).not.toHaveBeenCalledWith(dropped, expect.anything());
+ });
+
+ it('waits no longer than the hold lasts, however many deliveries of the id hold it in turn', async () => {
+ const first = answer();
+ const second = answer();
+ const outcomes = [first.promise, second.promise];
+ const onMessageEdited = jest.fn();
+ const adapter = await startWith({
+ onMessage: jest.fn(() => ({ settled: outcomes.shift()! })),
+ onMessageEdited,
+ });
+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);
+ deliverReuse();
+ await settleAll();
+ realEdit();
+ await settleAll();
+ jest.advanceTimersByTime(2 * 60_000);
+ copies.delete('REUSED'); // the store let it go, so the repeat is not skipped as one it holds
+ deliverReuse();
+ await settleAll();
+
+ // The first hold ends on its timer, the second still has two minutes: the change was parked for the
+ // hold, which has lasted as long as a hold may, and is not handed on for the later delivery.
+ jest.advanceTimersByTime(HOLD_MS - 2 * 60_000);
+ await settleAll();
+ expect(onMessageEdited).not.toHaveBeenCalled();
+ expect(warn).toHaveBeenCalledWith(dropped, expect.objectContaining({ targetId: 'REUSED' }));
});
it('frees the hold at once when the answer comes', async () => {
@@ -9797,6 +10065,45 @@ describe('BaileysAdapter keeps the message store in step with edits and deletes'
});
});
+ // A delivery the store cannot keep leaves no copy to judge by, so the key it arrived under stands in until
+ // a conflict is reported: then that delivery was not the message the id names.
+ it.each([
+ ['lands the real edit when the session reports the conflict', true],
+ ['drops a forged edit when the session finds the delivery was the message', false],
+ ])('%s for a delivery the store never held', async (_, conflict) => {
+ const answered = answer();
+ const onMessageEdited = jest.fn();
+ const adapter = await startWith({ onMessage: jest.fn(() => ({ settled: answered.promise })), onMessageEdited });
+ const warn = jest.spyOn(loggerOf(adapter), 'warn').mockImplementation(() => undefined);
+ fakeStore.put.mockRejectedValue(new Error('disk full'));
+ // An earlier delivery of the id is still being processed, which keeps the id in flight after the
+ // repeat of it has been handed on.
+ let openFirst!: () => void;
+ const firstGate = new Promise<void>(resolve => (openFirst = resolve));
+ fakeStore.getMessage.mockImplementationOnce(async () => {
+ await firstGate;
+ return null;
+ });
+ deliverReuse();
+ deliverReuse();
+ await settleAll();
+ expect(copies.has('REUSED')).toBe(false);
+
+ realEdit();
+ await settleAll();
+ expect(onMessageEdited).not.toHaveBeenCalled();
+ expect(warn).not.toHaveBeenCalledWith(dropped, expect.anything());
+
+ answered.resolve({ conflict });
+ await settleAll();
+ expect(onMessageEdited).toHaveBeenCalledTimes(conflict ? 1 : 0);
+ expect(warn.mock.calls.filter(([message]) => message === dropped)).toHaveLength(conflict ? 0 : 1);
+
+ openFirst();
+ await settleAll();
+ expect(onMessageEdited).toHaveBeenCalledTimes(conflict ? 1 : 0);
+ });
+
it('is not held up by an answer that rejects, and warns once', async () => {
const unhandled: unknown[] = [];
const listen = (reason: unknown): number => unhandled.push(reason);
diff --git a/src/engine/adapters/inbound-hold.spec.ts b/src/engine/adapters/inbound-hold.spec.ts
index 55638922a..cdcdd2422 100644
--- a/src/engine/adapters/inbound-hold.spec.ts
+++ b/src/engine/adapters/inbound-hold.spec.ts
@@ -731,7 +731,7 @@ describe('Baileys inbound messages held for storage', () => {
expect(h.announced()).toEqual([]);
});
- it('hands the real edit to the session while the repeat is held, and removes the copy once it is refused', async () => {
+ it('holds the real edit while the repeat is held, and lands it once the copy is refused', async () => {
const h = heldProjector({ unique: true });
h.rows.push(seeded());
const release = h.stall('X');
@@ -745,7 +745,8 @@ describe('Baileys inbound messages held for storage', () => {
// Judged against the copy alone it would be foreign, and lost with the copy's removal still to come.
upsert(events, realEdit());
await flush();
- expect(h.rows[0]).toEqual(expect.objectContaining({ body: 'fixed' }));
+ expect(h.rows).toEqual([seeded()]);
+ expect(h.announcedAs('message.edited')).toEqual([]);
expect(store.copies.has('X')).toBe(true);
release();
@@ -859,17 +860,24 @@ describe('Baileys inbound messages held for storage', () => {
},
);
- // The delivery's answer is pending when the change is handed on, so only the session can drop it:
- // by the entry it holds the delivery under, and by the row once there is one.
+ // The delivery's answer is pending when the change is judged, so the adapter holds it: it does nothing
+ // for it until the answer, when it drops it, or, with the copy gone, leaves it to the session, which
+ // drops it by the row.
it.each(kinds.flatMap(kind => [true, false].map(conflicts => [kind, conflicts] as const)))(
- 'leaves a forged %s from a third chat to the session, which drops it (delivery conflicts: %s)',
+ 'holds a forged %s from a third chat until the delivery is answered, then drops it (delivery conflicts: %s)',
async (kind, conflicts) => {
const h = heldProjector({ unique: true, sqlite });
// Without a row of another message, the delivery is the message the id names.
if (conflicts) h.rows.push(seeded());
const release = h.stall('X');
const { store, open } = holdingReads([1]);
- const { events } = build(4, h.callbacks, store);
+ const handedOn = {
+ onMessageEdited: jest.fn(h.callbacks.onMessageEdited),
+ onMessageRevoked: jest.fn(h.callbacks.onMessageRevoked),
+ onMessageReaction: jest.fn(h.callbacks.onMessageReaction),
+ };
+ const { events } = build(4, { ...h.callbacks, ...handedOn }, store);
+ const reached = (): number => Object.values(handedOn).reduce((n, fn) => n + fn.mock.calls.length, 0);
upsert(events, text('X', CHAT_B));
await flush();
@@ -877,11 +885,11 @@ describe('Baileys inbound messages held for storage', () => {
await flush();
open(1);
await flush();
- expect(h.announcedAs('message.edited')).toEqual([]);
- expect(h.announcedAs('message.revoked')).toEqual([]);
+ expect(reached()).toBe(0);
release();
await flush();
+ expect(reached()).toBe(conflicts ? 1 : 0);
expect(effect(h, kind)).toEqual(untouched(kind, conflicts));
expect(h.announcedAs('message.edited')).toEqual([]);
expect(h.announcedAs('message.revoked')).toEqual([]);
@@ -942,6 +950,30 @@ describe('Baileys inbound messages held for storage', () => {
);
});
+ it('keeps no limiter slot while the change waits for the answer', async () => {
+ process.env.INBOUND_MEDIA_CONCURRENCY = '1';
+ const h = heldProjector({ unique: true });
+ h.rows.push(seeded());
+ const release = h.stall('X');
+ const { events } = build(1, h.callbacks, memoryStore());
+
+ upsert(events, text('X', CHAT_B));
+ await flush();
+ upsert(events, change('edit', CHAT_A, 'REAL_ENV'));
+ await flush();
+ // The only slot is free for what arrives next, though the change has not been judged.
+ upsert(events, text('N', CHAT_C));
+ await flush();
+ expect(h.inserted()).toContain('N');
+ expect(h.rows.find(r => r.waMessageId === 'X')?.body).toBe('the real one');
+
+ release();
+ await flush();
+ expect(h.rows.find(r => r.waMessageId === 'X')).toEqual(
+ expect.objectContaining({ chatId: CHAT_A, body: 'fixed' }),
+ );
+ });
+
it('does not hold back the later messages of its own chat', async () => {
const h = heldProjector({ unique: true });
h.rows.push(seeded());
diff --git a/src/engine/interfaces/whatsapp-engine.interface.ts b/src/engine/interfaces/whatsapp-engine.interface.ts
index 1b017a4c0..3ebba0939 100644
--- a/src/engine/interfaces/whatsapp-engine.interface.ts
+++ b/src/engine/interfaces/whatsapp-engine.interface.ts
@@ -895,8 +895,9 @@ export interface EngineEventCallbacks {
}) => InboundTicket | undefined;
/**
* Fired for each received message. The session may answer with a receipt for what it makes of the
- * message once its row is settled (see {@link InboundReceipt}); the engine never waits for it, and
- * a receipt that fails, or is still unsettled after minutes, is let go of.
+ * message once its row is settled (see {@link InboundReceipt}); the engine never holds the message back
+ * for it, though a change aimed at that message waits for it before it is judged, and a receipt that
+ * fails, or is still unsettled after minutes, is let go of.
*/
onMessage?: (message: IncomingMessage, ticket?: InboundTicket) => void | InboundReceipt;
/**