Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): compare a wwebjs edit with the first arrival text

Yudhi Armyndharis371012c1e3b9
diff --git a/src/engine/adapters/wwebjs-message-changes.spec.ts b/src/engine/adapters/wwebjs-message-changes.spec.ts
index 908f5db32..2da865281 100644
--- a/src/engine/adapters/wwebjs-message-changes.spec.ts
+++ b/src/engine/adapters/wwebjs-message-changes.spec.ts
@@ -511,6 +511,29 @@ describe.each([
expect(rows).toEqual([expect.objectContaining({ waMessageId: 'A', body: 'fixed', metadata: { edited: true } })]);
expect(events('message.received')).toEqual([expect.objectContaining({ body: 'fixed', edited: true })]);
});
+
+ // whatsapp-web.js builds a repeat delivery from the message as it is now, so one raised after an edit carries the
+ // edited text, and its lookups can end before those of the first delivery.
+ it.each([
+ ['the edited text', 'v1'],
+ ['the text the message arrived with', 'orig'],
+ ])('marks an edit made before a repeat delivery carrying %s is handed over first', async (_, repeated) => {
+ const first = deferred<null>();
+ const repeat = deferred<null>();
+ client.emit('message', incoming('A', 'orig', { contact: first.promise }));
+ await flush();
+ editEvent(client, 'A', 'v1');
+ await flush();
+ client.emit('message', incoming('A', repeated, { contact: repeat.promise }));
+ await flush();
+ repeat.resolve(null);
+ await flush();
+ first.resolve(null);
+ await flush();
+
+ expect(rows).toEqual([expect.objectContaining({ waMessageId: 'A', body: 'v1', metadata: { edited: true } })]);
+ expect(events('message.received')).toEqual([expect.objectContaining({ body: 'v1', edited: true })]);
+ });
});
describe('changes kept while whatsapp-web.js processes a message (adapter level)', () => {
@@ -607,6 +630,23 @@ describe('changes kept while whatsapp-web.js processes a message (adapter level)
expect(arrivals).toEqual([undefined, undefined, undefined]);
});
+ it('hands a repeat delivery that carries an edit over with the text the first delivery arrived with', async () => {
+ const { client, bodies, arrivals } = wire();
+ const first = deferred<null>();
+ const repeat = deferred<null>();
+ client.emit('message', incoming('A', 'orig', { contact: first.promise }));
+ editEvent(client, 'A', 'v1');
+ client.emit('message', incoming('A', 'v1', { contact: repeat.promise }));
+
+ repeat.resolve(null);
+ await flush();
+ first.resolve(null);
+ await flush();
+
+ expect(bodies).toEqual(['v1', 'v1']);
+ expect(arrivals).toEqual(['orig', 'orig']);
+ });
+
it('hands over the text an own send arrived with beside the edit applied to it', async () => {
const download = deferred<undefined>();
const { client, bodies, arrivals } = wire({}, download.promise);
diff --git a/src/engine/adapters/wwebjs-message-events.ts b/src/engine/adapters/wwebjs-message-events.ts
index 9115503d6..333996d44 100644
--- a/src/engine/adapters/wwebjs-message-events.ts
+++ b/src/engine/adapters/wwebjs-message-events.ts
@@ -20,6 +20,8 @@ const MAX_CHANGES_KEPT_PER_MESSAGE = 64;
interface MessageInProcessing {
/** The text it arrived with, then that of the latest edit. */
text: string;
+ /** The text the first delivery arrived with: a repeat is built from the message as it is now, edited. */
+ arrived: string;
/** A delete for everyone reached it. */
deleted: boolean;
changes: Array<() => void>;
@@ -108,7 +110,7 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
if (!kept) {
// Past the bound a message is not tracked, and what reaches it is delivered at once, as it always was.
if (inProcessing.size >= MAX_MESSAGES_IN_PROCESSING) return NOT_KEPT;
- kept = { text: message.body, deleted: false, changes: [] };
+ kept = { text: message.body, arrived: message.body, deleted: false, changes: [] };
inProcessing.set(message.id, kept);
}
const mine = kept;
@@ -116,7 +118,7 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
handOver: handedOver => {
if (mine.deleted) return false;
// The session is told of the edits too, and compares them with the text the message arrived with.
- if (handedOver.body !== mine.text) handedOver.originalBody = handedOver.body;
+ if (mine.arrived !== mine.text) handedOver.originalBody = mine.arrived;
handedOver.body = mine.text;
return true;
},