Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): hand a wwebjs message over as its early changes left it

Yudhi Armyndharisea33ee138e92
diff --git a/src/engine/adapters/wwebjs-message-changes.spec.ts b/src/engine/adapters/wwebjs-message-changes.spec.ts
index be9cf594d..e1b3a67dd 100644
--- a/src/engine/adapters/wwebjs-message-changes.spec.ts
+++ b/src/engine/adapters/wwebjs-message-changes.spec.ts
@@ -109,10 +109,13 @@ describe.each([
let rows: Row[];
let dispatch: jest.Mock;
let client: EventEmitter;
+ // The bodies the message:received and message:sent hooks were given.
+ let hooked: string[];
beforeEach(() => {
process.env.INBOUND_MEDIA_CONCURRENCY = '1';
rows = [];
+ hooked = [];
// The real projector over a stand-in `messages` table, wired as the database wires it: only PostgreSQL
// admits a message on arrival.
const repo = {
@@ -121,6 +124,12 @@ describe.each([
findOne: jest.fn(({ where }: { where: Row }) => Promise.resolve(rows.find(r => matches(r, where)) ?? null)),
create: jest.fn((x: Row) => ({ ...x })),
insert: jest.fn((row: Row) => {
+ // As the real table's unique index refuses a second row under an id.
+ if (rows.some(r => r.waMessageId === row.waMessageId)) {
+ return Promise.reject(
+ Object.assign(new Error('UNIQUE constraint failed'), { code: 'SQLITE_CONSTRAINT_UNIQUE' }),
+ );
+ }
rows.push({ ...row });
return Promise.resolve({ identifiers: [{ id: `pk-${String(row.waMessageId)}` }], generatedMaps: [] });
}),
@@ -141,7 +150,10 @@ describe.each([
new Proxy({}, { get: () => jest.fn() }) as unknown as EventsGateway,
{ dispatch } as unknown as WebhookService,
{
- execute: jest.fn((_event: string, data: unknown) => Promise.resolve({ continue: true, data })),
+ execute: jest.fn((event: string, data: unknown) => {
+ if (event === 'message:received' || event === 'message:sent') hooked.push(String((data as Row).body));
+ return Promise.resolve({ continue: true, data });
+ }),
} as unknown as HookManager,
{ ingest: jest.fn() } as unknown as StatusStoreService,
{ resolveSenderPhone: jest.fn().mockResolvedValue(null) } as unknown as SessionLidResolver,
@@ -188,7 +200,15 @@ describe.each([
return { page: jest.fn(() => page.promise), finish: () => page.resolve({ mimetype: 'image/png', data: 'QUJD' }) };
};
- it('stores a message deleted for everyone during its contact lookup cleared, and announces no content', async () => {
+ // What a message deleted for everyone while it was processed leaves behind. PostgreSQL held the message from its
+ // arrival and stores it cleared. SQLite is told of it only at the hand-over, which is skipped, as the Baileys
+ // adapter skips it: no row, no hook, no announcement.
+ const deletedRows = (id: string): unknown[] =>
+ type === 'postgres'
+ ? [expect.objectContaining({ waMessageId: id, body: '', type: 'revoked', metadata: null })]
+ : [];
+
+ it('leaves no content of a message deleted for everyone during its contact lookup, and announces none', async () => {
const contact = deferred<null>();
client.emit('message', incoming('A', 'secret', { contact: contact.promise }));
await flush();
@@ -197,12 +217,13 @@ describe.each([
contact.resolve(null);
await flush();
- expect(rows).toEqual([expect.objectContaining({ waMessageId: 'A', body: '', type: 'revoked', metadata: null })]);
+ expect(rows).toEqual(deletedRows('A'));
+ expect(hooked).toEqual([]);
expect(events('message.received')).toEqual([]);
expect(events('message.revoked')).toHaveLength(1);
});
- it('stores a message deleted for everyone during its media download cleared, and announces no content', async () => {
+ it('leaves no content of a message deleted for everyone during its media download, and announces none', async () => {
const media = download();
client.emit('message', incoming('M', 'caption', { page: media.page }));
await flush();
@@ -211,7 +232,8 @@ describe.each([
media.finish();
await flush();
- expect(rows).toEqual([expect.objectContaining({ waMessageId: 'M', body: '', type: 'revoked', metadata: null })]);
+ expect(rows).toEqual(deletedRows('M'));
+ expect(hooked).toEqual([]);
expect(events('message.received')).toEqual([]);
expect(events('message.revoked')).toHaveLength(1);
});
@@ -226,6 +248,7 @@ describe.each([
await flush();
expect(rows).toEqual([expect.objectContaining({ waMessageId: 'A', body: 'fixed', metadata: { edited: true } })]);
+ expect(hooked).toEqual(['fixed']);
expect(events('message.received').map(m => m.body)).toEqual(['fixed']);
expect(events('message.edited').map(m => m.body)).toEqual(['fixed']);
});
@@ -246,6 +269,7 @@ describe.each([
metadata: { media: expect.objectContaining({ mimetype: 'image/png' }) as unknown, edited: true },
}),
]);
+ expect(hooked).toEqual(['new cap']);
expect(events('message.received').map(m => m.body)).toEqual(['new cap']);
});
@@ -260,6 +284,7 @@ describe.each([
await flush();
expect(rows[0].body).toBe('v2');
+ expect(hooked).toEqual(['v2']);
expect(events('message.edited').map(m => m.body)).toEqual(['v1', 'v2']);
expect(events('message.received').map(m => m.body)).toEqual(['v2']);
});
@@ -309,7 +334,7 @@ describe.each([
]);
});
- it('stores a message edited and then deleted during its contact lookup cleared', async () => {
+ it('leaves no content of a message edited and then deleted during its contact lookup', async () => {
const contact = deferred<null>();
client.emit('message', incoming('A', 'orig', { contact: contact.promise }));
await flush();
@@ -319,11 +344,36 @@ describe.each([
contact.resolve(null);
await flush();
- expect(rows).toEqual([expect.objectContaining({ waMessageId: 'A', body: '', type: 'revoked', metadata: null })]);
+ expect(rows).toEqual(deletedRows('A'));
+ expect(hooked).toEqual([]);
expect(events('message.received')).toEqual([]);
});
- it('stores an own send deleted for everyone during its media download cleared, and announces no content', async () => {
+ it('leaves no content of an own send deleted for everyone during its media download, and announces none', async () => {
+ const media = download();
+ client.emit('message_create', incoming('S', 'sent', { page: media.page, fromMe: true }));
+ await flush();
+ revokeEvent(client, 'S', { fromMe: true });
+ await flush();
+ media.finish();
+ await flush();
+
+ expect(rows).toEqual(deletedRows('S'));
+ expect(hooked).toEqual([]);
+ expect(events('message.sent')).toEqual([]);
+ });
+
+ // The REST send stored its row before the echo came, so the delete has a row to clear on both wirings.
+ it('clears the row a REST send stored when its echo is deleted during its media download', async () => {
+ rows.push({
+ sessionId: 's1',
+ waMessageId: 'S',
+ chatId: CHAT,
+ direction: 'outgoing',
+ type: 'image',
+ body: 'sent',
+ metadata: { media: { mimetype: 'image/png', data: 'QUJD' } },
+ });
const media = download();
client.emit('message_create', incoming('S', 'sent', { page: media.page, fromMe: true }));
await flush();
@@ -333,6 +383,7 @@ describe.each([
await flush();
expect(rows).toEqual([expect.objectContaining({ waMessageId: 'S', body: '', type: 'revoked', metadata: null })]);
+ expect(hooked).toEqual([]);
expect(events('message.sent')).toEqual([]);
});
@@ -347,6 +398,7 @@ describe.each([
expect(rows).toEqual([expect.objectContaining({ waMessageId: 'S', body: 'sent again' })]);
expect((rows[0].metadata as Row).edited).toBe(true);
+ expect(hooked).toEqual(['sent again']);
expect(events('message.sent').map(m => m.body)).toEqual(['sent again']);
});
@@ -375,12 +427,16 @@ describe('changes kept while whatsapp-web.js processes a message (adapter level)
/** Calls of the engine callbacks in the order they were made, with the registration under test. */
const wire = (extra: Partial<EngineEventCallbacks> = {}, media: Promise<undefined> = Promise.resolve(undefined)) => {
const order: string[] = [];
+ // The body of each message handed over, in order.
+ const bodies: string[] = [];
const callbacks: EngineEventCallbacks = {
onMessage: (m: IncomingMessage) => {
order.push(`message ${m.id}`);
+ bodies.push(m.body);
},
onMessageCreate: (m: IncomingMessage) => {
order.push(`own ${m.id}`);
+ bodies.push(m.body);
},
onMessageEdited: (e: EditedMessage) => {
order.push(`edit ${e.messageId} ${e.body}`);
@@ -400,15 +456,32 @@ describe('changes kept while whatsapp-web.js processes a message (adapter level)
capInboundMediaFor: jest.fn(() => media),
} as unknown as WwebjsEngineHost;
registerWwebjsMessageEvents(client as unknown as Client, host);
- return { client, order, callbacks };
+ return { client, order, bodies, callbacks };
};
// The bounds of wwebjs-message-events.ts: changes kept per message, and messages tracked at once.
const PER_MESSAGE = 64;
const TRACKED = 5_000;
- it('delivers what reached a message right after it, in the order it arrived', async () => {
- const { client, order } = wire();
+ it('hands a message over with its latest edit, and delivers what reached it right after, in order', async () => {
+ const { client, order, bodies } = wire();
+ const contact = deferred<null>();
+ client.emit('message', incoming('A', 'orig', { contact: contact.promise }));
+ editEvent(client, 'A', 'fixed');
+ reactionEvent(client, 'A', 'πŸ‘');
+ editEvent(client, 'A', 'fixed twice');
+ await flush();
+ expect(order).toEqual([]);
+
+ contact.resolve(null);
+ await flush();
+
+ expect(order).toEqual(['message A', 'edit A fixed', 'react A πŸ‘', 'edit A fixed twice']);
+ expect(bodies).toEqual(['fixed twice']);
+ });
+
+ it('does not hand over a message deleted meanwhile, and delivers what reached it', async () => {
+ const { client, order, bodies } = wire();
const contact = deferred<null>();
client.emit('message', incoming('A', 'orig', { contact: contact.promise }));
editEvent(client, 'A', 'fixed');
@@ -420,7 +493,35 @@ describe('changes kept while whatsapp-web.js processes a message (adapter level)
contact.resolve(null);
await flush();
- expect(order).toEqual(['message A', 'edit A fixed', 'react A πŸ‘', 'revoke A']);
+ expect(order).toEqual(['edit A fixed', 'react A πŸ‘', 'revoke A']);
+ expect(bodies).toEqual([]);
+ });
+
+ it('does not hand over a message deleted after more changes than are kept', async () => {
+ const { client, order, bodies } = wire();
+ const contact = deferred<null>();
+ client.emit('message', incoming('A', 'orig', { contact: contact.promise }));
+ for (let i = 1; i <= PER_MESSAGE; i++) editEvent(client, 'A', `v${i}`);
+ revokeEvent(client, 'A');
+ expect(order).toHaveLength(PER_MESSAGE + 1);
+ expect(order.at(-1)).toBe('revoke A');
+
+ contact.resolve(null);
+ await flush();
+
+ expect(order).toHaveLength(PER_MESSAGE + 1);
+ expect(bodies).toEqual([]);
+ });
+
+ it('delivers at once a delete that reaches a message already handed over', async () => {
+ const { client, order } = wire();
+ const contact = deferred<null>();
+ client.emit('message', incoming('A', 'orig', { contact: contact.promise }));
+ contact.resolve(null);
+ await flush();
+ revokeEvent(client, 'A');
+
+ expect(order).toEqual(['message A', 'revoke A']);
});
it('delivers at once what reaches a message the session holds from its arrival', async () => {
@@ -430,7 +531,7 @@ describe('changes kept while whatsapp-web.js processes a message (adapter level)
drop: () => undefined,
revoked: false,
};
- const { client, order } = wire({ admitInbound: () => ticket });
+ const { client, order, bodies } = wire({ admitInbound: () => ticket });
const contact = deferred<null>();
client.emit('message', incoming('A', 'orig', { contact: contact.promise }));
editEvent(client, 'A', 'fixed');
@@ -440,7 +541,9 @@ describe('changes kept while whatsapp-web.js processes a message (adapter level)
contact.resolve(null);
await flush();
+ // The session applies them itself: the message goes over as it arrived.
expect(order).toEqual(['edit A fixed', 'revoke A', 'message A']);
+ expect(bodies).toEqual(['orig']);
});
it('delivers at once what reaches another message, and what reaches a message already handed over', async () => {
@@ -473,7 +576,7 @@ describe('changes kept while whatsapp-web.js processes a message (adapter level)
});
it('delivers an edit that repeats the text at once and keeps one that changes it', async () => {
- const { client, order } = wire();
+ const { client, order, bodies } = wire();
const contact = deferred<null>();
client.emit('message', incoming('A', 'orig', { contact: contact.promise }));
editEvent(client, 'A', 'orig');
@@ -486,6 +589,7 @@ describe('changes kept while whatsapp-web.js processes a message (adapter level)
await flush();
expect(order).toEqual(['edit A orig', 'edit A v1', 'message A', 'edit A v1', 'edit A orig']);
+ expect(bodies).toEqual(['orig']);
});
it('forgets the message when its processing fails, after delivering what it kept', async () => {
@@ -507,19 +611,34 @@ describe('changes kept while whatsapp-web.js processes a message (adapter level)
expect(order).toEqual(['edit A fixed', 'edit A later']);
});
- it('delivers what reached an own send right after it, once its media is fetched', async () => {
+ it('hands an own send over with its latest edit, and delivers what reached it right after', async () => {
const download = deferred<undefined>();
- const { client, order } = wire({}, download.promise);
+ const { client, order, bodies } = wire({}, download.promise);
client.emit('message_create', incoming('S', 'sent', { page: jest.fn(), fromMe: true }));
editEvent(client, 'S', 'sent again', { media: true, fromMe: true });
- revokeEvent(client, 'S', { fromMe: true });
await flush();
expect(order).toEqual([]);
download.resolve(undefined);
await flush();
- expect(order).toEqual(['own S', 'edit S sent again', 'revoke S']);
+ expect(order).toEqual(['own S', 'edit S sent again']);
+ expect(bodies).toEqual(['sent again']);
+ });
+
+ it('does not hand over an own send deleted meanwhile, and delivers what reached it', async () => {
+ const download = deferred<undefined>();
+ const { client, order, bodies } = wire({}, download.promise);
+ client.emit('message_create', incoming('S', 'sent', { page: jest.fn(), fromMe: true }));
+ editEvent(client, 'S', 'sent again', { media: true, fromMe: true });
+ revokeEvent(client, 'S', { fromMe: true });
+ await flush();
+
+ download.resolve(undefined);
+ await flush();
+
+ expect(order).toEqual(['edit S sent again', 'revoke S']);
+ expect(bodies).toEqual([]);
});
it('forgets an own send when handing it over fails, after delivering what it kept', async () => {
@@ -555,6 +674,24 @@ describe('changes kept while whatsapp-web.js processes a message (adapter level)
await flush();
});
+ it('hands over neither delivery of an id that was deleted meanwhile', async () => {
+ const { client, order, bodies } = wire();
+ const first = deferred<null>();
+ const second = deferred<null>();
+ client.emit('message', incoming('A', 'orig', { contact: first.promise }));
+ client.emit('message', incoming('A', 'orig', { contact: second.promise }));
+ revokeEvent(client, 'A');
+
+ second.resolve(null);
+ await flush();
+ first.resolve(null);
+ await flush();
+ revokeEvent(client, 'A');
+
+ expect(order).toEqual(['revoke A', 'revoke A']);
+ expect(bodies).toEqual([]);
+ });
+
it('shares what reached an id between two deliveries of it, and forgets the id after both', async () => {
const { client, order } = wire();
const first = deferred<null>();
@@ -584,12 +721,12 @@ describe('changes kept while whatsapp-web.js processes a message (adapter level)
const contact = deferred<null>();
client.emit('message', incoming('A', 'orig', { contact: contact.promise }));
editEvent(client, 'A', 'fixed');
- revokeEvent(client, 'A');
+ reactionEvent(client, 'A', 'πŸ‘');
contact.resolve(null);
await flush();
- expect(order).toEqual(['message A', 'revoke A']);
+ expect(order).toEqual(['message A', 'react A πŸ‘']);
});
it('keeps a bounded number of changes for one message, delivering the rest in order at once', async () => {
diff --git a/src/engine/adapters/wwebjs-message-events.ts b/src/engine/adapters/wwebjs-message-events.ts
index fc9f33244..0ced6e44c 100644
--- a/src/engine/adapters/wwebjs-message-events.ts
+++ b/src/engine/adapters/wwebjs-message-events.ts
@@ -16,12 +16,32 @@ import { type WwebjsEngineHost } from './wwebjs-host';
const MAX_MESSAGES_IN_PROCESSING = 5_000;
const MAX_CHANGES_KEPT_PER_MESSAGE = 64;
-/** A message this adapter is still processing: the text it carries so far, and the changes that reached it. */
+/** A message this adapter is still processing: what it carries so far, and the changes that reached it. */
interface MessageInProcessing {
+ /** The text it arrived with, then that of the latest edit. */
text: string;
+ /** A delete for everyone reached it. */
+ deleted: boolean;
changes: Array<() => void>;
}
+/** What the processing of one message does with the changes that reached it meanwhile. */
+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. */
+ end: () => void;
+}
+
+/** A change to a message that is processed: the text of an edit, or a delete for everyone. */
+interface Change {
+ editedText?: string;
+ deletes?: boolean;
+}
+
+/** The processing of a message whose changes are not kept: the session holds it, or it is not tracked. */
+const NOT_KEPT: Processing = { handOver: () => true, end: () => undefined };
+
/**
* Message-domain client events (message, message_create, ack, revoke, reaction, edit) extracted
* from the adapter's event wiring: pure mapping from wwebjs payloads to neutral events fired
@@ -61,9 +81,11 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
// A message reaches the session once its contact, media and quoted message were fetched. A session that holds
// each arrival (a ticket, PostgreSQL) keeps what reaches the message meanwhile for its row. Any other learns of
// the message only when it is handed over, so an edit or a delete for everyone that came first would find no
- // row to change, and the message would then be stored and announced as it was: a deleted one with its content.
- // A reaction would never reach the row. Those changes are kept here, by message id, and delivered right after
- // the message.
+ // row to change, and the message would then be hooked, stored and announced as it was: a deleted one with its
+ // content. A reaction would never reach the row. So the adapter keeps those changes here, by message id. The
+ // message is handed over with the latest edit applied, or not at all when it was deleted, as the Baileys adapter
+ // does, and the changes themselves are delivered right after it, so the session still announces them and marks
+ // the edit.
const inProcessing = new Map<string, MessageInProcessing>();
const deliverKept = (kept: MessageInProcessing): void => {
@@ -77,37 +99,45 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
};
/**
- * Start keeping what reaches a message the session does not hold. Returns the call that ends the processing,
- * however it ends: it delivers what was kept, in arrival order, and forgets the message. Safe to call twice.
+ * 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.
*/
- const keepChanges = (message: IncomingMessage, ticket: InboundTicket | undefined): (() => void) => {
- if (ticket || !message.id || message.isStatusBroadcast) return () => undefined;
+ const keepChanges = (message: IncomingMessage, ticket: InboundTicket | undefined): Processing => {
+ if (ticket || !message.id || message.isStatusBroadcast) return NOT_KEPT;
let kept = inProcessing.get(message.id);
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 () => undefined;
- kept = { text: message.body, changes: [] };
+ if (inProcessing.size >= MAX_MESSAGES_IN_PROCESSING) return NOT_KEPT;
+ kept = { text: message.body, deleted: false, changes: [] };
inProcessing.set(message.id, kept);
}
const mine = kept;
- return () => {
- if (inProcessing.get(message.id) === mine) inProcessing.delete(message.id);
- deliverKept(mine);
+ return {
+ handOver: handedOver => {
+ if (mine.deleted) return false;
+ handedOver.body = mine.text;
+ return true;
+ },
+ end: () => {
+ if (inProcessing.get(message.id) === mine) inProcessing.delete(message.id);
+ deliverKept(mine);
+ },
};
};
/**
* Deliver a change aimed at `messageId`: right after the message while it is still being processed, else at
- * once. An edit passes its text: one that repeats the text the message carries so far changes nothing, and goes
- * through at once as it always did. Past the bound the changes kept so far go first, so none overtakes another.
+ * once. An edit that repeats the text the message carries so far changes nothing, and goes through at once as
+ * it always did. Past the bound the changes kept so far go first, so none overtakes another.
*/
- const afterMessage = (messageId: string, deliver: () => void, editedText?: string): void => {
+ const afterMessage = (messageId: string, deliver: () => void, change: Change = {}): void => {
const kept = inProcessing.get(messageId);
- if (!kept || editedText === kept.text) {
+ if (!kept || change.editedText === kept.text) {
deliver();
return;
}
- if (editedText !== undefined) kept.text = editedText;
+ if (change.editedText !== undefined) kept.text = change.editedText;
+ if (change.deletes) kept.deleted = true;
if (kept.changes.length < MAX_CHANGES_KEPT_PER_MESSAGE) {
kept.changes.push(deliver);
return;
@@ -125,10 +155,10 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
return;
}
const ticket = admit(msg, base, true);
- const endProcessing = keepChanges(base, ticket);
- const done = handleIncoming(host, msg, base, ticket);
+ const processing = keepChanges(base, ticket);
+ const done = handleIncoming(host, msg, base, ticket, processing);
holdWhileProcessing(ticket, done);
- void done.then(endProcessing, endProcessing);
+ void done.then(processing.end, processing.end);
});
client.on('message_create', msg => {
@@ -141,7 +171,7 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
}
let ticket: InboundTicket | undefined;
- let endProcessing: (() => void) | undefined;
+ let processing = NOT_KEPT;
const done = (async () => {
let incomingMessage: IncomingMessage;
try {
@@ -154,7 +184,7 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
}
// Synchronous, as the first await comes after it.
ticket = admit(msg, incomingMessage, !incomingMessage.isStatusBroadcast);
- endProcessing = keepChanges(incomingMessage, ticket);
+ processing = keepChanges(incomingMessage, ticket);
// Enrich with the media payload through the same capped path the incoming handler uses β€”
// the base builder is sync and carries none, so a phone-sent image would otherwise persist
// and render as a bare πŸ“Ž marker even though the media is downloadable right here. Not for an own
@@ -170,6 +200,12 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
}
}
try {
+ if (!processing.handOver(incomingMessage)) {
+ host.logger.debug('Not handing over an own message deleted for everyone while it was processed', {
+ msgId: incomingMessage.id,
+ });
+ return;
+ }
// The ticket goes along only where one was issued, so elsewhere the call is unchanged.
if (ticket) host.getCallbacks().onMessageCreate?.(incomingMessage, ticket);
else host.getCallbacks().onMessageCreate?.(incomingMessage);
@@ -179,8 +215,8 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
})();
holdWhileProcessing(ticket, done);
void done.then(
- () => endProcessing?.(),
- () => endProcessing?.(),
+ () => processing.end(),
+ () => processing.end(),
);
});
@@ -237,7 +273,9 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
body: '',
timestamp: after.timestamp,
};
- afterMessage(payload.revokedId ?? payload.id, () => host.getCallbacks().onMessageRevoked?.(payload));
+ afterMessage(payload.revokedId ?? payload.id, () => host.getCallbacks().onMessageRevoked?.(payload), {
+ deletes: true,
+ });
} catch (error) {
host.logger.error('Error processing message_revoke_everyone', String(error));
}
@@ -285,7 +323,9 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
mentionedIds: message.mentionedIds,
});
const payload: EditedMessage = buildEditedMessage(base, Boolean(message.hasMedia));
- afterMessage(payload.messageId, () => host.getCallbacks().onMessageEdited?.(payload), payload.body);
+ afterMessage(payload.messageId, () => host.getCallbacks().onMessageEdited?.(payload), {
+ editedText: payload.body,
+ });
} catch (error) {
host.logger.error('Error processing message_edit', String(error));
}
@@ -294,13 +334,15 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
/**
* The `message` event body: enrich the incoming message (contact, location, media, quote, call) and
- * emit it with its ticket. Never rejects.
+ * emit it with its ticket, carrying the latest edit that reached it, or not at all once a delete for everyone did.
+ * Never rejects.
*/
async function handleIncoming(
host: WwebjsEngineHost,
msg: Message,
incomingMessage: IncomingMessage,
ticket: InboundTicket | undefined,
+ processing: Processing,
): Promise<void> {
// A held message (a ticket) keeps every later message of its chat waiting until it is emitted, so its
// page lookups get the download deadline instead of Puppeteer's protocol timeout, and go without.
@@ -373,6 +415,12 @@ async function handleIncoming(
const call = extractWwebjsCall(msg);
if (call) incomingMessage.call = call;
+ if (!processing.handOver(incomingMessage)) {
+ host.logger.debug('Not handing over a message deleted for everyone while it was processed', {
+ msgId: incomingMessage.id,
+ });
+ return;
+ }
if (ticket) host.getCallbacks().onMessage?.(incomingMessage, ticket);
else host.getCallbacks().onMessage?.(incomingMessage);
} catch (error) {