Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): hold an in-flight edit or reaction to the row's rules
Yudhi Armyndharisb8d6580c0a8f
diff --git a/src/modules/session/message-projector.service.spec.ts b/src/modules/session/message-projector.service.spec.tsindex ebdfaebf3..1b900613e 100644--- a/src/modules/session/message-projector.service.spec.ts+++ b/src/modules/session/message-projector.service.spec.ts@@ -821,6 +821,108 @@ describe('MessageProjector (inbound projection)', () => {expect(webhookService.dispatch).toHaveBeenCalledWith(SESSION_ID, 'message.edited', expect.anything());});+ it('keeps an edit from another chat out of the message while its row is not written', async () => {+ const engine = makeEngine();+ engines.set(SESSION_ID, engine);+ await received(engine);++ projector.applyMessageEditQueued(SESSION_ID, {+ messageId: 'wamid.1',+ body: 'FORGED',+ chatId: '[email protected]',+ fromMe: false,+ } as never);+ await drain();+ releaseHook();+ await drain();++ expect(messageRepository.insert).toHaveBeenCalledTimes(1);+ expect(updatesAfterInsert()).toEqual([]);+ expect(webhookService.dispatch).not.toHaveBeenCalledWith(+ SESSION_ID,+ 'message.received',+ expect.objectContaining({ body: 'FORGED' }),+ );+ });++ it('keeps an edit that claims to come from the account out of a message the counterparty sent', async () => {+ const engine = makeEngine();+ engines.set(SESSION_ID, engine);+ await received(engine);++ projector.applyMessageEditQueued(SESSION_ID, {+ messageId: 'wamid.1',+ body: 'FORGED',+ chatId: '[email protected]',+ fromMe: true,+ } as never);+ await drain();+ releaseHook();+ await drain();++ expect(updatesAfterInsert()).toEqual([]);+ });++ it("keeps another member's edit out of a group message while its row is not written", async () => {+ const engine = makeEngine();+ engines.set(SESSION_ID, engine);+ const group = '[email protected]';+ const gate = new Promise<void>(resolve => (releaseHook = resolve));+ hookManager.execute.mockImplementationOnce(async (_event: string, data: unknown) => {+ await gate;+ return { continue: true, data };+ });+ projector.handleInboundMessage(+ SESSION_ID,+ engine,+ makeIncoming({ chatId: group, from: group, isGroup: true, kind: 'group', author: '39900313301142@lid' }),+ );+ await new Promise(resolve => setImmediate(resolve));++ projector.applyMessageEditQueued(SESSION_ID, {+ messageId: 'wamid.1',+ body: 'genuine edit',+ chatId: group,+ fromMe: false,+ author: '39900313301142@lid',+ } as never);+ // The latest recorded edit is the one written, so a forged one that was kept would win.+ projector.applyMessageEditQueued(SESSION_ID, {+ messageId: 'wamid.1',+ body: 'FORGED',+ chatId: group,+ fromMe: false,+ author: '195700218425410@lid',+ } as never);+ await drain();+ releaseHook();+ await drain();++ expect(updatesAfterInsert()).toEqual([[where, { body: 'genuine edit' }]]);+ });++ it('keeps a reaction from another chat out of the message while its row is not written', async () => {+ const engine = makeEngine();+ engines.set(SESSION_ID, engine);+ await received(engine);++ const storeReactions = jest.spyOn(+ projector as unknown as { storeReactions: () => Promise<void> },+ 'storeReactions',+ );+ projector.applyReactionQueued(SESSION_ID, {+ messageId: 'wamid.1',+ chatId: '[email protected]',+ reaction: 'X',+ senderId: '[email protected]',+ });+ await drain();+ releaseHook();+ await drain();++ expect(storeReactions).not.toHaveBeenCalled();+ });+it('lets a revoke win over an edit', 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.tsindex 03c149ebc..39da03a16 100644--- a/src/modules/session/message-projector.service.ts+++ b/src/modules/session/message-projector.service.ts@@ -10,6 +10,7 @@ import { KeyedMutationQueue } from '../../common/utils/keyed-mutation-queue';import { SessionLidResolver } from './session-lid-resolver.service';import { buildMessageMetadata, REVOKED_ROW_PATCH, storableWaMessageId } from './message-row.mapper';import { MessageMutationProjector } from './message-mutation-projector';+import { mutationTargetsForeignRow, type MutationSource, type StoredMessageRef } from './message-mutation-guard';import { updateMessageMetadata } from '../message/message-metadata';import { persistHistoryMessages } from './message-history-projector';import { isTransientDbError, isUniqueViolation } from '../../common/utils/db-errors';@@ -97,6 +98,9 @@ interface InFlightEntry {// delivery shares the entry, and its payload must not outlive the slot that accounts for it.// Unset while the message is admitted but not yet emitted (see admitInbound).quote?: Pick<IncomingMessage, 'chatId' | 'body'>;+ // Where the message lives and who wrote it, so a change recorded for it meanwhile is held to the same+ // chat, direction and author rule as the row it will become (see mutationTargetsForeignRow).+ ref?: StoredMessageRef;revoked?: boolean;// Set when the revoke came from the engine, so message.revoked went out for it.revokeAnnounced?: boolean;@@ -433,7 +437,12 @@ export class MessageProjector {/** Record a message as not yet written; a re-fire of the same id keeps the changes recorded so far. */private trackInFlight(key: string, message: InboundMessageData) {const { revoked, revokeAnnounced, editedBody, ackStatus, reactions } = this.inboundInFlight.get(key) ?? {};- const inFlight = { quote: quoteOf(message), revoked, revokeAnnounced, editedBody, ackStatus, reactions };+ const ref: StoredMessageRef = {+ chatId: message.chatId,+ direction: message.fromMe ? MessageDirection.OUTGOING : MessageDirection.INCOMING,+ author: message.author,+ };+ const inFlight = { quote: quoteOf(message), ref, revoked, revokeAnnounced, editedBody, ackStatus, reactions };this.inboundInFlight.set(key, inFlight);return inFlight;}@@ -1199,17 +1208,46 @@ export class MessageProjector {applyReactionQueued(id: string, event: ReactionEvent): void {// A message not written yet keeps the reaction for its row; the live event still goes out now.const inFlight = event.messageId ? this.inboundInFlight.get(`${id}:${event.messageId}`) : undefined;- if (inFlight) (inFlight.reactions ??= {})[event.senderId] = event.reaction ?? '';+ if (inFlight && !this.foreignToInFlight(inFlight, { kind: 'reaction', chatId: event.chatId })) {+ (inFlight.reactions ??= {})[event.senderId] = event.reaction ?? '';+ }this.mutationProjector.applyReactionQueued(id, event);}/** Edit apply, queued on the per-message mutation chain — see MessageMutationProjector. */applyMessageEditQueued(id: string, message: EditedMessage): void {const inFlight = this.inboundInFlight.get(`${id}:${message.messageId}`);- if (inFlight) inFlight.editedBody = message.body;+ if (+ inFlight &&+ !this.foreignToInFlight(inFlight, {+ kind: 'edit',+ chatId: message.chatId,+ fromMe: message.fromMe,+ author: message.author,+ })+ ) {+ inFlight.editedBody = message.body;+ }this.mutationProjector.applyMessageEditQueued(id, message);}+ /**+ * Whether a change names a message of another chat, or one it may not change, judged against the+ * message still waiting for its row. Recording it for the row would carry it into `message.received`+ * and the insert, past the check the row's own update applies.+ */+ private foreignToInFlight(inFlight: InFlightEntry, source: MutationSource): boolean {+ if (!inFlight.ref || !source.chatId) return false;+ const foreign = mutationTargetsForeignRow(inFlight.ref, source);+ if (foreign) {+ this.logger.warn('Not keeping a change aimed at a message of another chat or sender', {+ reason: foreign,+ kind: source.kind,+ });+ }+ return foreign !== null;+ }+/** Queue a message-scoped mutation. A failed operation is isolated so later events still run. */enqueueMessageMutation(id: string, messageId: string, work: () => Promise<void>): void {this.mutationProjector.enqueueMessageMutation(id, messageId, work);