Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): report the edited message's send time, not the edit's

Yudhi Armyndharise5b37e141d1e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bda67f308..ddc3b1cff 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,7 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- A WebSocket client that emits without an ack callback receives command replies again. The gateway answered by returning a frame, which Socket.IO delivers through the ack callback and nowhere else, so a client written to the documented `message` event saw no subscribe confirmation, no pong, and none of the refusals, including the session-scope denial. Replies now go out on `message` as well as through the ack.
- A Baileys message the account sent from its phone is no longer lost when the message store cannot be read. The repeat-delivery check threw on a locked database or an unparseable row and the message was dropped with it, and WhatsApp does not re-deliver one it has already acked; the check now fails open, so at worst such a message is reported twice rather than never.
-- Baileys `editMessage` returns the edited message's id instead of the edit envelope's, which named a message no route could address and disagreed with the whatsapp-web.js engine.
+- Baileys `editMessage` answers with the edited message's id and original send time instead of the edit envelope's. The id named a message no route could address, and both fields disagreed with the whatsapp-web.js engine, which reports the message rather than the edit.
- Saved Baileys contacts are no longer evicted by peers seen once in a group or a broadcast. Both populations shared one capped map, so a busy session answered `GET /api/sessions/:sessionId/contacts` with fewer and fewer of the contacts the account actually saved. `BAILEYS_SESSION_STORE_MAX_ENTRIES` now bounds the peer side of that map; the saved side is bounded by the account's own address book.
- A Baileys business prompt no longer offers a call-to-action as a clickable choice. A button that opens a URL or dials a number reached `buttons[]` on `message.received` and was accepted by `POST /api/sessions/:sessionId/messages/click-button`, although WhatsApp has no reply form for it.
- A webhook or automation filter written against an `@lid` chat or sender keeps matching once the gateway learns that identity's phone number. Only the event side was resolved through the lid mapping, so an exclusion started delivering the chat it was meant to keep off the wire, and an inclusion stopped delivering, with the rule still reading correctly in the dashboard.
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.md
index 2680c4631..1d9e8d40e 100644
--- a/docs/06-api-specification.md
+++ b/docs/06-api-specification.md
@@ -2204,9 +2204,7 @@ Edit the text of a message sent by this account; also updates the stored record'
**Response** `200`
-The edited message keeps its original id, on both engines, so the id you passed is the id you get back.
-
-`timestamp` is the one field the two engines report differently: Baileys answers with the time the edit was sent, whatsapp-web.js re-reads the message and answers with the time the ORIGINAL message was sent. Read the stored message if you need one of the two specifically.
+Both fields describe the edited message rather than the edit itself, on both engines: `messageId` is the id you passed, and `timestamp` is the time that message was originally sent, since an edit does not move a message in the chat.
```json
{ "messageId": "[email protected]_3EB0ABCD", "timestamp": 1760000000 }
diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.ts
index 7534921da..ab62ced4b 100644
--- a/src/engine/adapters/baileys-messaging.ts
+++ b/src/engine/adapters/baileys-messaging.ts
@@ -576,12 +576,13 @@ export class BaileysMessaging {
// protocolMessage edit envelope, so an edit can re-tag participants. An edit REPLACES the
// 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));
- // 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) };
+ await this.send(jid, this.previewSafe(editContent), this.previewSafeOptions(editContent));
+ // Both fields describe the EDITED MESSAGE, not the protocol envelope that carried the edit.
+ // That envelope has an id and a send time of its own; answering with either would name something
+ // no route can address and no stored row is keyed by, and would disagree with the
+ // whatsapp-web.js engine, which re-reads the message and reports the original of both. An edit
+ // does not move a message in the chat, so its timestamp is still the one it was sent at.
+ return { id: messageId, timestamp: this.host.toUnixSeconds(target.messageTimestamp) };
}
/**
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index bb007d8e0..15741a8e8 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -3486,6 +3486,8 @@ describe('BaileysAdapter store-backed ops', () => {
const ownStored = {
key: { id: 'TARGET', remoteJid: '[email protected]', fromMe: true },
message: { conversation: 'hi' },
+ // Distinct from the edit envelope's send time below, so a spec cannot pass on either one.
+ messageTimestamp: 1700000000,
};
it('replyToMessage quotes the stored message', async () => {
@@ -3709,9 +3711,9 @@ describe('BaileysAdapter store-backed ops', () => {
it('editMessage edits via the stored key and returns the (unchanged) message id', async () => {
fakeStore.getMessage.mockResolvedValue(ownStored);
- // 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.
+ // The library answers with the protocol envelope that carried the edit, which has an id AND a
+ // send time of its own; the edited message keeps both of the caller's. A mock echoing the
+ // target's values back would pass whichever of the two the adapter returned.
fakeSock.sendMessage.mockResolvedValue({
key: { ...ownStored.key, id: '3EB0FRESHENVELOPE' },
messageTimestamp: 1700000010,
@@ -3727,15 +3729,15 @@ describe('BaileysAdapter store-backed ops', () => {
},
expect.objectContaining({ getUrlInfo: expect.any(Function) as unknown }) as unknown,
);
- expect(res).toEqual({ id: 'TARGET', timestamp: 1700000010 });
+ expect(res).toEqual({ id: 'TARGET', timestamp: 1700000000 });
});
- it('editMessage falls back to the requested id when the send echoes nothing back', async () => {
+ it('editMessage answers from the stored message even when the send echoes nothing back', async () => {
fakeStore.getMessage.mockResolvedValue(ownStored);
fakeSock.sendMessage.mockResolvedValue(undefined);
const adapter = await ready();
const res = await adapter.editMessage('[email protected]', 'TARGET', 'edited body');
- expect(res.id).toBe('TARGET');
+ expect(res).toEqual({ id: 'TARGET', timestamp: 1700000000 });
});
it('editMessage throws MessageNotFoundError when the message is not in the store', async () => {