Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): let an echo's in-flight copy follow message:sent rewrites

Yudhi Armyndharisa1a793fff313
diff --git a/src/modules/session/message-projector.service.spec.ts b/src/modules/session/message-projector.service.spec.ts
index 0ec7f520d..8411be757 100644
--- a/src/modules/session/message-projector.service.spec.ts
+++ b/src/modules/session/message-projector.service.spec.ts
@@ -1200,6 +1200,24 @@ describe('MessageProjector (inbound projection)', () => {
});
describe('handleOwnSendEcho', () => {
+ it('lends a quote the body its message:sent chain rewrote, until the row is written', async () => {
+ const engine = makeEngine();
+ engines.set(SESSION_ID, engine);
+ hookManager.execute.mockImplementationOnce(
+ (_event: string, data: IncomingMessage, options: { accept: (d: unknown) => boolean }) => {
+ const rewritten = { ...data, body: '[redacted]' };
+ options.accept(rewritten);
+ return Promise.resolve({ continue: true, data: rewritten });
+ },
+ );
+ messageRepository.insert.mockImplementationOnce(() => new Promise(() => undefined));
+
+ projector.handleOwnSendEcho(SESSION_ID, engine, makeIncoming({ fromMe: true }));
+ await new Promise(resolve => setImmediate(resolve));
+
+ expect(projector.inFlightInbound(SESSION_ID, 'wamid.1')).toMatchObject({ body: '[redacted]' });
+ });
+
it('still persists and dispatches the send when a message:sent hook returns null', async () => {
const engine = makeEngine();
engines.set(SESSION_ID, engine);
diff --git a/src/modules/session/message-projector.service.ts b/src/modules/session/message-projector.service.ts
index 848582b97..a0c4ead2a 100644
--- a/src/modules/session/message-projector.service.ts
+++ b/src/modules/session/message-projector.service.ts
@@ -628,20 +628,25 @@ export class MessageProjector {
this.logger.error(`onMessageCreate handler failed for ${id}`, String(err));
return null;
};
+ // The commit may wait behind a slow earlier message of the chat; a revoke, edit or ack of this
+ // send landing meanwhile is recorded here and applied once the row is written. Like the inbound
+ // path, the entry follows the chain's rewrites, so a quote carries what the row will hold.
+ const inFlightKey = `${id}:${message.id}`;
+ const inFlight = this.trackInFlight(inFlightKey, messageData);
// Execute hook for message sent - plugins can modify or stop processing. Like the inbound path,
// the hook chain runs concurrently and the commit waits its turn on the chat's queue.
const prepared = this.hookManager
.execute('message:sent', messageData, {
sessionId: id,
source: 'Engine',
- accept: isMessagePayload,
+ accept: data => {
+ if (!isMessagePayload(data)) return false;
+ inFlight.message = data;
+ return true;
+ },
})
.then(({ data }) => this.messageOrEngineCopy(id, 'message:sent', data, message))
.catch(onFailure);
- // The commit may wait behind a slow earlier message of the chat; a revoke, edit or ack of this
- // send landing meanwhile is recorded here and applied once the row is written.
- const inFlightKey = `${id}:${message.id}`;
- const inFlight = this.trackInFlight(inFlightKey, messageData);
this.chatCommits.enqueue(this.chatCommitKey(id, message), async () => {
try {
const finalMessage = await prepared;