Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): hand no wwebjs repeat over after its message was deleted

Yudhi Armyndharis322674c8b077
diff --git a/src/engine/adapters/wwebjs-message-changes.spec.ts b/src/engine/adapters/wwebjs-message-changes.spec.ts
index 2abaedad9..07264ccf0 100644
--- a/src/engine/adapters/wwebjs-message-changes.spec.ts
+++ b/src/engine/adapters/wwebjs-message-changes.spec.ts
@@ -982,6 +982,41 @@ describe.each([
}
});
+ // Two deliveries of one message share what reaches it: one that ends first must not forget it for the other.
+ it('announces nothing of an own media send delivered twice after it was deleted between their hand-overs', async () => {
+ const first = download();
+ const second = download();
+ client.emit('message_create', incoming('S', 'orig', { page: first.page, fromMe: true }));
+ await flush();
+ client.emit('message_create', incoming('S', 'orig', { page: second.page, fromMe: true }));
+ await flush();
+ first.finish();
+ await flush();
+ revokeEvent(client, 'S', { fromMe: true });
+ await flush();
+ second.finish();
+ await flush();
+
+ expect(rows).toEqual([expect.objectContaining(deletedRow('S'))]);
+ expect(dispatchedAfterDeleted()).toEqual([]);
+ expect(hooked.filter(body => body === 'orig').length).toBeLessThanOrEqual(1);
+ });
+
+ it('hooks nothing of a message delivered twice after it was deleted between their hand-overs', async () => {
+ const first = deferred<null>();
+ client.emit('message', incoming('A', 'secret', { contact: first.promise }));
+ await flush();
+ client.emit('message', incoming('A', 'secret'));
+ await flush();
+ revokeEvent(client, 'A');
+ await flush();
+ first.resolve(null);
+ await flush();
+
+ expect(rows).toEqual([expect.objectContaining(deletedRow('A'))]);
+ expect(hooked.filter(body => body === 'secret').length).toBeLessThanOrEqual(1);
+ });
+
it('announces an own message stored again after its row was removed without the mark of that row', async () => {
client.emit('message_create', own('S', 'orig'));
await flush();
@@ -1449,6 +1484,46 @@ describe('changes kept while whatsapp-web.js processes a message (adapter level)
expect(arrivals).toEqual([undefined]);
});
+ it('delivers at once what reaches an id once one delivery of it ended, and hands the other over as it left it', async () => {
+ const { client, order, bodies, arrivals } = wire();
+ const first = deferred<null>();
+ client.emit('message', incoming('A', 'orig', { contact: first.promise }));
+ client.emit('message', incoming('A', 'orig'));
+ await flush();
+ editEvent(client, 'A', 'v1');
+ expect(order).toEqual(['message A', 'edit A v1']);
+
+ first.resolve(null);
+ await flush();
+
+ expect(order).toEqual(['message A', 'edit A v1', 'message A']);
+ expect(bodies).toEqual(['orig', 'v1']);
+ expect(arrivals).toEqual([undefined, 'orig']);
+ });
+
+ it.each([
+ ['the first', true],
+ ['the repeat', false],
+ ])(
+ 'hands over no delivery of an id deleted after another delivery of it ended, %s ending first',
+ async (_, firstEnds) => {
+ const { client, order, bodies } = wire();
+ const slow = deferred<null>();
+ client.emit('message', incoming('A', 'orig', firstEnds ? {} : { contact: slow.promise }));
+ client.emit('message', incoming('A', 'orig', firstEnds ? { contact: slow.promise } : {}));
+ await flush();
+ revokeEvent(client, 'A');
+ expect(order).toEqual(['message A', 'revoke A']);
+
+ slow.resolve(null);
+ await flush();
+ revokeEvent(client, 'A');
+
+ expect(order).toEqual(['message A', 'revoke A', 'revoke A']);
+ expect(bodies).toEqual(['orig']);
+ },
+ );
+
it('delivers the rest when one kept change fails', async () => {
const { client, order } = wire({
onMessageEdited: () => {
diff --git a/src/engine/adapters/wwebjs-message-events.ts b/src/engine/adapters/wwebjs-message-events.ts
index 165ed1e21..9a4cf04f5 100644
--- a/src/engine/adapters/wwebjs-message-events.ts
+++ b/src/engine/adapters/wwebjs-message-events.ts
@@ -27,6 +27,10 @@ interface MessageInProcessing {
edited: boolean;
/** A delete for everyone reached it. */
deleted: boolean;
+ /** Its deliveries still being processed: it is forgotten once the last of them ends. */
+ deliveries: number;
+ /** One of them ended: what reaches the message since goes at once, and is noted here for the others. */
+ delivered: boolean;
changes: Array<() => void>;
}
@@ -34,7 +38,7 @@ interface MessageInProcessing {
interface Processing {
/** Apply the latest edit to the copy about to be handed over. False when a delete reached it: it is not handed over. */
handOver: (message: IncomingMessage) => boolean;
- /** The processing ended, however it did: deliver the kept changes in arrival order and forget the message. */
+ /** The processing ended, however it did: deliver the kept changes in arrival order; the last to end forgets them. */
end: () => void;
}
@@ -104,8 +108,9 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
};
/**
- * Start keeping what reaches a message the session does not hold. `end` must run when the processing ends,
- * however it ends; it is safe to call twice. A repeat delivery of an id shares the record of the first.
+ * Start keeping what reaches a message the session does not hold. `end` must run once when the processing ends,
+ * however it ends. A repeat delivery of an id shares the record of the first, which is forgotten once the last of
+ * them ended.
*/
const keepChanges = (message: IncomingMessage, ticket: InboundTicket | undefined): Processing => {
if (ticket || !message.id || message.isStatusBroadcast) return NOT_KEPT;
@@ -119,10 +124,19 @@ 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, arrived: message.body, edited: false, deleted: false, changes: [] };
+ kept = {
+ text: message.body,
+ arrived: message.body,
+ edited: false,
+ deleted: false,
+ deliveries: 0,
+ delivered: false,
+ changes: [],
+ };
inProcessing.set(message.id, kept);
}
const mine = kept;
+ mine.deliveries++;
return {
handOver: handedOver => {
if (mine.deleted) return false;
@@ -134,7 +148,10 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
return true;
},
end: () => {
- if (inProcessing.get(message.id) === mine) inProcessing.delete(message.id);
+ mine.deliveries--;
+ // Another delivery of the id still processed is handed over as a later change leaves the message.
+ mine.delivered = true;
+ if (!mine.deliveries && inProcessing.get(message.id) === mine) inProcessing.delete(message.id);
deliverKept(mine);
},
};
@@ -156,7 +173,7 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
kept.edited = true;
}
if (change.deletes) kept.deleted = true;
- if (kept.changes.length < MAX_CHANGES_KEPT_PER_MESSAGE) {
+ if (!kept.delivered && kept.changes.length < MAX_CHANGES_KEPT_PER_MESSAGE) {
kept.changes.push(deliver);
return;
}