Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): return the edited message id, not the edit envelope's

Yudhi Armyndharis4b8d22d06914
diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.ts
index 56e21d9c5..7534921da 100644
--- a/src/engine/adapters/baileys-messaging.ts
+++ b/src/engine/adapters/baileys-messaging.ts
@@ -577,7 +577,11 @@ export class BaileysMessaging {
// content, so omitting mentions drops whatever tags the original carried.
const editContent = { text: body, ...this.withMentions(mentions), edit: target.key };
const sent = await this.send(jid, this.previewSafe(editContent), this.previewSafeOptions(editContent));
- return { id: sent?.key?.id ?? messageId, timestamp: this.host.toUnixSeconds(sent?.messageTimestamp) };
+ // The edited message keeps its own id, which is what the caller asked to edit and what the stored
+ // row is keyed by. The library answers with the protocol envelope that carried the edit, and that
+ // envelope has a fresh id of its own; returning it would name a message no API can address, and
+ // would disagree with the whatsapp-web.js engine, which returns the original.
+ return { id: messageId, timestamp: this.host.toUnixSeconds(sent?.messageTimestamp) };
}
/**
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index 1d066f419..7cfa3d9c4 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -3731,7 +3731,13 @@ describe('BaileysAdapter store-backed ops', () => {
it('editMessage edits via the stored key and returns the (unchanged) message id', async () => {
fakeStore.getMessage.mockResolvedValue(ownStored);
- fakeSock.sendMessage.mockResolvedValue({ key: { ...ownStored.key }, messageTimestamp: 1700000010 });
+ // The library answers with the protocol envelope that carried the edit, which has an id of its
+ // own; the edited message keeps the id the caller passed. A mock echoing the target id back
+ // would pass whichever of the two the adapter returned.
+ fakeSock.sendMessage.mockResolvedValue({
+ key: { ...ownStored.key, id: '3EB0FRESHENVELOPE' },
+ messageTimestamp: 1700000010,
+ });
const adapter = await ready();
const res = await adapter.editMessage('[email protected]', 'TARGET', 'edited body');
expect(fakeSock.sendMessage).toHaveBeenCalledWith(