Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): carry an edit that overtakes its downloading original

Yudhi Armyndharis0fd6c979197b
diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.ts
index 5afec9346..c0f01ed41 100644
--- a/src/engine/adapters/baileys-events.ts
+++ b/src/engine/adapters/baileys-events.ts
@@ -197,6 +197,14 @@ export class BaileysEvents {
*/
private readonly deletedForEveryone = new Set<string>();
+ /**
+ * The latest edit of a message still being processed, by id, with the key it was sent under. The
+ * edit is announced first and finds no row or preview to change, and a repeat delivery can be stored
+ * after the edit was applied, so the original is stored and announced with this text instead.
+ * Bounded like deletedForEveryone, which wins over it.
+ */
+ private readonly editedWhileInFlight = new Map<string, { envelope: WAMessageKey; body: string }>();
+
constructor(private readonly host: BaileysEventsHost) {}
/** Whether a delete for everyone of this message was accepted (see deletedForEveryone). */
@@ -403,6 +411,15 @@ export class BaileysEvents {
editedContentType === 'stickerMessage';
const edited: EditedMessage = buildEditedMessage(base, hasMedia);
this.host.recordMessageEdit(remoteJid, edited.messageId, edited.body);
+ const target = this.inboundInFlight.get(edited.messageId);
+ if (target && this.mayChange(target.key, msg.key, false)) {
+ this.editedWhileInFlight.delete(edited.messageId); // re-inserted as the newest
+ this.editedWhileInFlight.set(edited.messageId, { envelope: msg.key, body: edited.body });
+ if (this.editedWhileInFlight.size > BaileysEvents.DELETED_FOR_EVERYONE_LIMIT) {
+ const [oldest] = this.editedWhileInFlight.keys();
+ this.editedWhileInFlight.delete(oldest);
+ }
+ }
this.changeStoredMessage(edited.messageId, stored => {
const content = this.mayChange(stored.key, msg.key, false)
? b.normalizeMessageContent(stored.message ?? undefined)
@@ -484,15 +501,27 @@ export class BaileysEvents {
});
// Stored before it is announced: whoever hears about this message may act on it at once (a quoted
// reply, a reaction, a read receipt), and the store holds a read of an id until its write lands.
- // A message deleted for everyone while it was being processed is stored as the delete leaves it.
+ // A message deleted for everyone or edited while it was being processed is stored as the change
+ // leaves it, and a delete wins over an edit.
const deleted = storedId !== null && this.deletedForEveryone.has(storedId);
- void this.host.putStoredMessage(deleted ? { ...msg, message: null } : msg)?.catch(err =>
+ const edit = storedId !== null && !deleted ? this.editedWhileInFlight.get(storedId) : undefined;
+ const editedBody = edit && this.mayChange(msg.key, edit.envelope, false) ? edit.body : undefined;
+ let toStore = deleted ? { ...msg, message: null } : msg;
+ if (editedBody !== undefined) {
+ // A copy, so the edit reaches neither Baileys' object nor anyone else holding it.
+ toStore = JSON.parse(JSON.stringify(msg, b.BufferJSON.replacer), b.BufferJSON.reviver) as WAMessage;
+ const content = b.normalizeMessageContent(toStore.message ?? undefined);
+ if (content) setBaileysText(content, editedBody);
+ incoming.body = editedBody;
+ }
+ void this.host.putStoredMessage(toStore)?.catch(err =>
this.host.logger.warn('Failed to persist message to store', {
error: err instanceof Error ? err.message : String(err),
}),
);
// Its delete was announced first and found nothing to clear, so announcing the message now, or
- // leaving its text as the chat preview, would publish what the sender took back.
+ // leaving its text as the chat preview, would publish what the sender took back. An edit announced
+ // first found nothing to change either, so the message carries it here and in the preview.
if (!deleted) {
if (msg.key.fromMe === true) {
this.host.getOnMessageCreate()?.(incoming);
@@ -501,7 +530,11 @@ export class BaileysEvents {
}
}
this.host.recordMessage(msg);
- if (deleted) this.host.recordMessageEdit(remoteJid, storedId, '');
+ if (deleted) {
+ this.host.recordMessageEdit(remoteJid, storedId, '');
+ } else if (editedBody !== undefined && storedId !== null) {
+ this.host.recordMessageEdit(remoteJid, storedId, editedBody);
+ }
} catch (err) {
this.host.logger.error(
`Unhandled error processing inbound message (id=${msg.key?.id ?? 'unknown'}); dropping`,
diff --git a/src/engine/adapters/baileys-quote-on-receipt.spec.ts b/src/engine/adapters/baileys-quote-on-receipt.spec.ts
index fb0a25ae2..da06d8e95 100644
--- a/src/engine/adapters/baileys-quote-on-receipt.spec.ts
+++ b/src/engine/adapters/baileys-quote-on-receipt.spec.ts
@@ -93,6 +93,7 @@ describe('quoting a Baileys message the moment it is announced', () => {
normalizeMessageContent: (c: unknown) => c,
getContentType: (c: Record<string, unknown> | undefined) => Object.keys(c ?? {})[0],
proto: { Message: { ProtocolMessage: { Type: { REVOKE: 0, MESSAGE_EDIT: 14 } } } },
+ BufferJSON: { replacer: (_k: string, v: unknown) => v, reviver: (_k: string, v: unknown) => v },
downloadMediaMessage: () =>
new Promise(resolve =>
downloads.push(() =>
@@ -323,4 +324,94 @@ describe('quoting a Baileys message the moment it is announced', () => {
expect(writtenContents('TARGET')).toEqual([photo('TARGET').message]);
});
});
+
+ describe('an edit that arrives while the original is still downloading its media', () => {
+ beforeEach(() => release()); // the held step here is the download, not the store write
+
+ const edit = (chat = CHAT): WAMessage =>
+ change({ type: 14, editedMessage: { conversation: 'fixed caption' } }, chat);
+ const caption = async (): Promise<string | null | undefined> =>
+ (await store.getMessage('s1', 'TARGET'))?.message?.imageMessage?.caption;
+
+ it('announces, stores and previews the message with its edited text', async () => {
+ const order: string[] = [];
+ const heard: string[] = [];
+ const { events } = build(
+ m => {
+ order.push('received');
+ heard.push(m.body);
+ },
+ { edited: () => order.push('edited') },
+ );
+ events.handleMessagesUpsert({ messages: [photo('TARGET')], type: 'notify' });
+ await ticks();
+ events.handleMessagesUpsert({ messages: [edit()], type: 'notify' });
+ await ticks();
+
+ downloads[0]();
+ await ticks();
+
+ expect(order).toEqual(['edited', 'received']);
+ expect(heard).toEqual(['fixed caption']);
+ expect(writtenContents('TARGET')).toEqual([
+ { imageMessage: { mimetype: 'image/jpeg', caption: 'fixed caption' } },
+ ]);
+ expect(await caption()).toBe('fixed caption');
+ // The edit found no preview to change, so the original, recorded after it, carries the edit.
+ expect(preview).toEqual(['edit:TARGET:fixed caption', 'record:TARGET', 'edit:TARGET:fixed caption']);
+ });
+
+ it('keeps a repeat delivery that passed the repeat check from restoring the old text', async () => {
+ const heard: string[] = [];
+ const { events } = build(m => heard.push(m.body));
+ events.handleMessagesUpsert({ messages: [photo('TARGET')], type: 'notify' });
+ await ticks();
+ events.handleMessagesUpsert({ messages: [edit()], type: 'notify' });
+ await ticks();
+ // Nothing is stored yet, so the repeat is not recognised as one and downloads too.
+ events.handleMessagesUpsert({ messages: [photo('TARGET')], type: 'notify' });
+ await ticks();
+
+ downloads[0](); // the first delivery is stored, and the edit lands on top of it
+ await ticks();
+ downloads[1](); // then the repeat is stored
+ await ticks();
+
+ expect(await caption()).toBe('fixed caption');
+ expect(heard).toEqual(['fixed caption', 'fixed caption']);
+ });
+
+ it('lets a delete for everyone win over an earlier edit', async () => {
+ const heard = jest.fn();
+ const { events } = build(heard);
+ events.handleMessagesUpsert({ messages: [photo('TARGET')], type: 'notify' });
+ await ticks();
+ events.handleMessagesUpsert({ messages: [edit()], type: 'notify' });
+ await ticks();
+ events.handleMessagesUpsert({ messages: [change({ type: 0 })], type: 'notify' });
+ await ticks();
+
+ downloads[0]();
+ await ticks();
+
+ expect(heard).not.toHaveBeenCalled();
+ expect(writtenContents('TARGET')).toEqual([null]);
+ expect(preview.slice(-1)).toEqual(['edit:TARGET:']);
+ });
+
+ it('ignores an edit of the same id sent from another chat', async () => {
+ const heard: string[] = [];
+ const { events } = build(m => heard.push(m.body));
+ events.handleMessagesUpsert({ messages: [photo('TARGET')], type: 'notify' });
+ await ticks();
+ events.handleMessagesUpsert({ messages: [edit('[email protected]')], type: 'notify' });
+ await ticks();
+
+ downloads[0]();
+ await ticks();
+
+ expect(heard).toEqual(['about to be deleted']);
+ expect(await caption()).toBe('about to be deleted');
+ });
+ });
});