Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): tell the session what an edited message arrived with

Yudhi Armyndharis4b67ccebb34c
diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.ts
index a934ed7a0..b3cdf3534 100644
--- a/src/engine/adapters/baileys-events.ts
+++ b/src/engine/adapters/baileys-events.ts
@@ -865,6 +865,9 @@ export class BaileysEvents {
toStore = JSON.parse(JSON.stringify(msg, b.BufferJSON.replacer), b.BufferJSON.reviver) as WAMessage;
const content = b.normalizeMessageContent(toStore.message ?? undefined);
if (content) setBaileysText(content, editedBody);
+ // The session is told of the edit too, with a ticket at once and without one right after the emit, and
+ // compares it with the text the message arrived with.
+ if (editedBody !== incoming.body) incoming.originalBody = incoming.body;
incoming.body = editedBody;
}
if (opts.generation === this.storeGeneration) {
diff --git a/src/engine/adapters/inbound-hold.spec.ts b/src/engine/adapters/inbound-hold.spec.ts
index 72b251bdd..1a69d30c3 100644
--- a/src/engine/adapters/inbound-hold.spec.ts
+++ b/src/engine/adapters/inbound-hold.spec.ts
@@ -1239,6 +1239,32 @@ describe('Baileys inbound messages held for storage', () => {
expect(rowsWhenEdited).toEqual([0]);
});
+ // The message is handed over with the edit applied, and the edit reaches the session too, so the session is told
+ // the text the message arrived with to compare the edit with.
+ it.each([
+ ['an edit', 'fixed caption', 'caption'],
+ ['an edit that repeats the text', 'caption', undefined],
+ ])('hands the message over with %s and the text it arrived with', async (_, body, originalBody) => {
+ const h = heldProjector({ unique: true, sqlite });
+ const handedOver: Array<Pick<IncomingMessage, 'body' | 'originalBody'>> = [];
+ const { events } = build(4, {
+ ...h.callbacks,
+ onMessage: (m, t) => (
+ handedOver.push({ body: m.body, originalBody: m.originalBody }),
+ h.callbacks.onMessage!(m, t)
+ ),
+ });
+ const release = download();
+ upsert(events, media('M', CHAT, { caption: 'caption' }));
+ await flush();
+ upsert(events, edit('E', 'M', body));
+ await flush();
+ release();
+ await flush();
+
+ expect(handedOver).toEqual([{ body, originalBody }]);
+ });
+
it(
sqlite
? 'announces them once the message has been handed on'
diff --git a/src/engine/adapters/wwebjs-message-changes.spec.ts b/src/engine/adapters/wwebjs-message-changes.spec.ts
index 51a30f7dd..34ecf7362 100644
--- a/src/engine/adapters/wwebjs-message-changes.spec.ts
+++ b/src/engine/adapters/wwebjs-message-changes.spec.ts
@@ -427,16 +427,19 @@ 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.
+ // The body of each message handed over, in order, and the text it arrived with where an edit was applied.
const bodies: string[] = [];
+ const arrivals: Array<string | undefined> = [];
const callbacks: EngineEventCallbacks = {
onMessage: (m: IncomingMessage) => {
order.push(`message ${m.id}`);
bodies.push(m.body);
+ arrivals.push(m.originalBody);
},
onMessageCreate: (m: IncomingMessage) => {
order.push(`own ${m.id}`);
bodies.push(m.body);
+ arrivals.push(m.originalBody);
},
onMessageEdited: (e: EditedMessage) => {
order.push(`edit ${e.messageId} ${e.body}`);
@@ -456,7 +459,7 @@ 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, bodies, callbacks };
+ return { client, order, bodies, arrivals, callbacks };
};
// The bounds of wwebjs-message-events.ts: changes kept per message, and messages tracked at once.
@@ -480,6 +483,53 @@ describe('changes kept while whatsapp-web.js processes a message (adapter level)
expect(bodies).toEqual(['fixed twice']);
});
+ it('hands over the text a message arrived with beside the edit applied to it', async () => {
+ const { client, bodies, arrivals } = wire();
+ const contact = deferred<null>();
+ client.emit('message', incoming('A', 'orig', { contact: contact.promise }));
+ editEvent(client, 'A', 'v1');
+ editEvent(client, 'A', 'v2');
+
+ contact.resolve(null);
+ await flush();
+
+ expect(bodies).toEqual(['v2']);
+ expect(arrivals).toEqual(['orig']);
+ });
+
+ it('hands over no arrival text when no edit was applied', async () => {
+ const { client, arrivals } = wire();
+ const plain = deferred<null>();
+ const reacted = deferred<null>();
+ const restored = deferred<null>();
+ client.emit('message', incoming('A', 'orig', { contact: plain.promise }));
+ client.emit('message', incoming('B', 'orig', { contact: reacted.promise }));
+ client.emit('message', incoming('C', 'orig', { contact: restored.promise }));
+ reactionEvent(client, 'B', '👍');
+ editEvent(client, 'C', 'v1');
+ editEvent(client, 'C', 'orig');
+
+ plain.resolve(null);
+ reacted.resolve(null);
+ restored.resolve(null);
+ await flush();
+
+ expect(arrivals).toEqual([undefined, undefined, undefined]);
+ });
+
+ 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);
+ client.emit('message_create', incoming('S', 'sent', { page: jest.fn(), fromMe: true }));
+ editEvent(client, 'S', 'sent again', { media: true, fromMe: true });
+
+ download.resolve(undefined);
+ await flush();
+
+ expect(bodies).toEqual(['sent again']);
+ expect(arrivals).toEqual(['sent']);
+ });
+
it('does not hand over a message deleted meanwhile, and delivers what reached it', async () => {
const { client, order, bodies } = wire();
const contact = deferred<null>();
diff --git a/src/engine/adapters/wwebjs-message-events.ts b/src/engine/adapters/wwebjs-message-events.ts
index 0ced6e44c..568c57ae3 100644
--- a/src/engine/adapters/wwebjs-message-events.ts
+++ b/src/engine/adapters/wwebjs-message-events.ts
@@ -115,6 +115,8 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
return {
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;
handedOver.body = mine.text;
return true;
},
diff --git a/src/engine/interfaces/whatsapp-engine.interface.ts b/src/engine/interfaces/whatsapp-engine.interface.ts
index 49fd01eac..806d5ba71 100644
--- a/src/engine/interfaces/whatsapp-engine.interface.ts
+++ b/src/engine/interfaces/whatsapp-engine.interface.ts
@@ -103,6 +103,12 @@ export interface IncomingMessage {
timestamp: number;
fromMe: boolean;
isGroup: boolean;
+ /**
+ * Set by an engine that applied an edit to `body` before it handed the message over: the text the message
+ * arrived with. The edits that reached the message meanwhile reach the session too, and are compared with
+ * this text. The session removes the field before the message is hooked, stored or announced.
+ */
+ originalBody?: string;
/** User-facing chat kind of the conversation this message belongs to (derived from `chatId`). */
kind: ChatKind;
/**
diff --git a/src/modules/session/message-edited-marker.spec.ts b/src/modules/session/message-edited-marker.spec.ts
index 6143ad1c3..a2159b36f 100644
--- a/src/modules/session/message-edited-marker.spec.ts
+++ b/src/modules/session/message-edited-marker.spec.ts
@@ -144,6 +144,10 @@ describe('edited marker for a message whose row is not written yet', () => {
let engine: IWhatsAppEngine;
// The insert of a row waits for the promise held under its message id.
let stalled: Map<string, Promise<void>>;
+ // What the message hooks were given, and what webhooks and the socket were told, in order.
+ let hooked: Array<{ event: string; data: Record<string, unknown> }>;
+ let announced: Array<{ event: string; payload: Record<string, unknown> }>;
+ let hookManager: { execute: jest.Mock };
const flush = async (): Promise<void> => {
for (let i = 0; i < 25; i++) await new Promise(resolve => setImmediate(resolve));
@@ -170,6 +174,14 @@ describe('edited marker for a message whose row is not written yet', () => {
await db.initialize();
repository = db.getRepository(Message);
stalled = new Map();
+ hooked = [];
+ announced = [];
+ hookManager = {
+ execute: jest.fn((event: string, data: unknown) => {
+ hooked.push({ event, data: data as Record<string, unknown> });
+ return Promise.resolve({ continue: true, data });
+ }),
+ };
const insert = repository.insert.bind(repository);
jest.spyOn(repository, 'insert').mockImplementation(async row => {
await stalled.get((row as { waMessageId: string }).waMessageId);
@@ -182,11 +194,23 @@ describe('edited marker for a message whose row is not written yet', () => {
repository,
{ update: jest.fn().mockResolvedValue({ affected: 1 }) } as unknown as Repository<Session>,
engines,
- new Proxy({}, { get: () => jest.fn() }) as unknown as EventsGateway,
- { dispatch: jest.fn().mockResolvedValue(undefined) } as unknown as WebhookService,
+ new Proxy(
+ {},
+ {
+ get:
+ (_target, name) =>
+ (_id: string, payload: Record<string, unknown>): void => {
+ announced.push({ event: `socket ${String(name)}`, payload });
+ },
+ },
+ ) as unknown as EventsGateway,
{
- execute: jest.fn((_event: string, data: unknown) => Promise.resolve({ continue: true, data })),
- } as unknown as HookManager,
+ dispatch: jest.fn((_id: string, event: string, payload: Record<string, unknown>) => {
+ announced.push({ event, payload });
+ return Promise.resolve();
+ }),
+ } as unknown as WebhookService,
+ hookManager as unknown as HookManager,
{ ingest: jest.fn() } as unknown as StatusStoreService,
{ resolveSenderPhone: jest.fn().mockResolvedValue(null) } as unknown as SessionLidResolver,
);
@@ -196,6 +220,40 @@ describe('edited marker for a message whose row is not written yet', () => {
await db.destroy();
});
+ // What an engine that applied an edit before the hand-over tells the session stays between them.
+ it('removes the text a message arrived with before it is hooked, stored or announced', async () => {
+ projector.handleInboundMessage('s1', engine, incoming('A', { body: 'fixed', originalBody: 'original' }));
+ projector.handleOwnSendEcho('s1', engine, incoming('S', { fromMe: true, body: 'fixed', originalBody: 'original' }));
+ await flush();
+
+ const seen = [
+ ...hooked.filter(h => /^message:(received|sent)$/.test(h.event)).map(h => h.data),
+ ...announced.map(a => a.payload),
+ ];
+ expect(seen.length).toBeGreaterThanOrEqual(4);
+ for (const payload of seen) expect(payload).not.toHaveProperty('originalBody');
+ expect(announced.find(a => a.event === 'message.received')?.payload).toEqual(
+ expect.objectContaining({ body: 'fixed' }),
+ );
+ expect((await stored('A')).body).toBe('fixed');
+ expect((await stored('S')).body).toBe('fixed');
+ });
+
+ // A hook that hands back something that is not a message leaves the session with the engine's own copy.
+ it('removes the text a message arrived with from the copy it falls back to when a hook returns no message', async () => {
+ hookManager.execute.mockImplementation(() => Promise.resolve({ continue: true, data: null }));
+ projector.handleInboundMessage('s1', engine, incoming('A', { body: 'fixed', originalBody: 'original' }));
+ projector.handleOwnSendEcho('s1', engine, incoming('S', { fromMe: true, body: 'fixed', originalBody: 'original' }));
+ await flush();
+
+ const sent = announced.filter(a => a.event === 'message.received' || a.event === 'message.sent');
+ expect(sent.map(a => a.event).sort()).toEqual(['message.received', 'message.sent']);
+ for (const { payload } of sent) {
+ expect(payload).toEqual(expect.objectContaining({ body: 'fixed' }));
+ expect(payload).not.toHaveProperty('originalBody');
+ }
+ });
+
it('marks a message edited while its insert is out, beside the metadata it was stored with', async () => {
const insert = deferred<void>();
stalled.set('A', insert.promise);
diff --git a/src/modules/session/message-projector.service.ts b/src/modules/session/message-projector.service.ts
index 269c9fa61..e0e46ec4d 100644
--- a/src/modules/session/message-projector.service.ts
+++ b/src/modules/session/message-projector.service.ts
@@ -404,6 +404,8 @@ export class MessageProjector {
void this.sessionRepository.update(id, { lastActiveAt: new Date() }).catch(() => undefined);
// Convert IncomingMessage to plain object for dispatch
const messageData = { ...message };
+ // What an engine says of the edit it applied is for the session alone: no hook, row or consumer sees it.
+ delete messageData.originalBody;
// Tracks what the chain's current copy lends a quote, so one taken mid-chain carries an earlier
// handler's rewrite.
const inFlightKey = `${id}:${message.id}`;
@@ -553,7 +555,9 @@ export class MessageProjector {
received: candidate === null ? 'null' : typeof candidate,
action: 'hook_message_discarded',
});
- return { ...message };
+ const copy = { ...message };
+ delete copy.originalBody;
+ return copy;
}
/** `isStatusBroadcast` arm of {@link handleInboundMessage}: ingest into the status store, not the message pipeline. */
@@ -1042,6 +1046,7 @@ export class MessageProjector {
// Update last active timestamp
void this.sessionRepository.update(id, { lastActiveAt: new Date() }).catch(() => undefined);
const messageData = { ...message };
+ delete messageData.originalBody;
const onFailure = (err: unknown): null => {
this.logger.error(`onMessageCreate handler failed for ${id}`, String(err));