Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): hold a change to its row's rule from the arrival
Yudhi Armyndharisef33fb0d974b
diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.tsindex b3e9a9e1d..2668acff6 100644--- a/src/engine/adapters/baileys-events.ts+++ b/src/engine/adapters/baileys-events.ts@@ -475,11 +475,15 @@ export class BaileysEvents {// Learned before the chat id is canonicalized, as processInboundMessage does without a ticket.this.host.recordKeyLidMappings(msg.key);const remoteJid = msg.key.remoteJid!;+ const behindChat = (remoteJid.endsWith('@g.us') || remoteJid.endsWith('@broadcast')) && msg.key.participant;return admit({id: msg.key.id ?? '',chatId: this.host.toNeutralJid(baileysChatJid(remoteJid, msg.key.participant, msg.key.fromMe === true)),status: remoteJid === 'status@broadcast',needsRoom: this.plannedMediaDownload(this.host.getLoadedLib?.(), msg),+ fromMe: msg.key.fromMe === true,+ // As the mapper sets it, so a change that arrives before the emit is held to the same rule.+ ...(behindChat ? { author: this.host.toNeutralJid(msg.key.participant!) } : {}),});},error =>diff --git a/src/engine/adapters/inbound-hold.spec.ts b/src/engine/adapters/inbound-hold.spec.tsindex 24567b08f..902545167 100644--- a/src/engine/adapters/inbound-hold.spec.ts+++ b/src/engine/adapters/inbound-hold.spec.ts@@ -511,6 +511,27 @@ describe('Baileys inbound messages held for storage', () => {expect(h.announced()).toEqual(['H']);});+ // The session holds a change that reaches a message before its emit to the rule of the row it will+ // become, which needs the direction and the sender from the arrival on.+ it('names whose message it is in the arrival, and the sender behind a group message', async () => {+ const h = heldProjector();+ const admitInbound = jest.fn(h.callbacks.admitInbound);+ const { events } = build(4, { ...h.callbacks, admitInbound });+ const own = { ...text('B', '[email protected]'), key: { id: 'B', remoteJid: '[email protected]', fromMe: true } };+ const member = {+ ...text('C', '[email protected]'),+ key: { id: 'C', remoteJid: '[email protected]', fromMe: false, participant: '[email protected]' },+ };+ upsert(events, text('A', '[email protected]'), own as WAMessage, member as WAMessage);+ await flush();++ expect(admitInbound.mock.calls.map(([a]) => [a.id, a.fromMe, a.author])).toEqual([+ ['A', false, undefined],+ ['B', true, undefined],+ ['C', false, '[email protected]'],+ ]);+ });+it('still processes a message, and the rest of its batch, when its admission throws', async () => {const h = heldProjector();let calls = 0;@@ -958,6 +979,20 @@ describe('whatsapp-web.js inbound messages held for storage', () => {expect(h.inserted()).toEqual(['echo-image', 'echo-text']);});+ it('names whose message it is in the arrival', async () => {+ const h = heldProjector();+ const admitInbound = jest.fn(h.callbacks.admitInbound);+ const { client } = build({ ...h.callbacks, admitInbound });+ client.emit('message_create', ownEcho('mine', '[email protected]', undefined, 'hi'));+ client.emit('message', incoming('theirs', '[email protected]', undefined, 'hello'));+ await flush();++ expect(admitInbound.mock.calls.map(([a]) => [a.id, a.fromMe])).toEqual([+ ['mine', true],+ ['theirs', false],+ ]);+ });+it('settles the ticket of an own echo its handler never takes, so the chat is not held up', async () => {const h = heldProjector();// The handler ignores the ticket it is handed: only the adapter's own settle can release it.diff --git a/src/engine/adapters/wwebjs-message-events.ts b/src/engine/adapters/wwebjs-message-events.tsindex 96ae7fd34..6661350ee 100644--- a/src/engine/adapters/wwebjs-message-events.ts+++ b/src/engine/adapters/wwebjs-message-events.ts@@ -31,6 +31,8 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHochatId: base.chatId,status: Boolean(base.isStatusBroadcast),needsRoom: mayDownload && plannedInboundDownload(msg),+ fromMe: base.fromMe,+ ...(base.author ? { author: base.author } : {}),}),error =>host.logger.error(diff --git a/src/engine/interfaces/whatsapp-engine.interface.ts b/src/engine/interfaces/whatsapp-engine.interface.tsindex 49b09d451..dd7b69fc4 100644--- a/src/engine/interfaces/whatsapp-engine.interface.ts+++ b/src/engine/interfaces/whatsapp-engine.interface.ts@@ -864,6 +864,10 @@ export interface EngineEventCallbacks {chatId: string;needsRoom: boolean;status: boolean;+ /** Whether the account itself sent it. Left out where the engine cannot say before it maps the message. */+ fromMe?: boolean;+ /** The neutral id of the sender behind a group, status or list message, as `IncomingMessage.author`. */+ author?: string;}) => InboundTicket | undefined;onMessage?: (message: IncomingMessage, ticket?: InboundTicket) => void;/**diff --git a/src/modules/session/message-projector.inbound-hold.spec.ts b/src/modules/session/message-projector.inbound-hold.spec.tsindex d209676ac..d607d5ed1 100644--- a/src/modules/session/message-projector.inbound-hold.spec.ts+++ b/src/modules/session/message-projector.inbound-hold.spec.ts@@ -52,7 +52,13 @@ const message = (id: string, chatId: string, over: Partial<IncomingMessage> = {}...over,});-const arrival = (id: string, chatId: string, needsRoom = false, status = false) => ({ id, chatId, needsRoom, status });+const arrival = (id: string, chatId: string, needsRoom = false, status = false, fromMe?: boolean) => ({+ id,+ chatId,+ needsRoom,+ status,+ fromMe,+});describe('MessageProjector holding inbound messages (PostgreSQL)', () => {const env = { ...process.env };@@ -247,6 +253,83 @@ describe('MessageProjector holding inbound messages (PostgreSQL)', () => {expect(dispatched('message.received')[0]).toEqual(expect.objectContaining({ body: 'edited' }));});+ // The entry exists from the arrival, and a change that names another chat or the other party has to be+ // held to the rule of the row it will become from then on, not only once the message is emitted.+ describe('a change that reaches a message admitted on arrival', () => {+ // Phone ids, so that two of them are provably different chats.+ const CHAT = '[email protected]';+ const OTHER = '[email protected]';++ it('keeps an edit from another chat out of the message, and does not announce it', async () => {+ const t = admit(arrival('A', CHAT, false, false, false));+ projector.applyMessageEditQueued('s1', {+ messageId: 'A',+ body: 'FORGED',+ chatId: OTHER,+ fromMe: false,+ } as never);+ projector.handleInboundMessage('s1', engine, message('A', CHAT, { body: 'original' }), t);+ await flush();++ expect(rows[0]).toEqual(expect.objectContaining({ body: 'original' }));+ expect(dispatched('message.edited')).toEqual([]);+ expect(eventsGateway.emitMessageEdited).not.toHaveBeenCalled();+ });++ it('keeps an edit that claims to come from the account out of a message the counterparty sent', async () => {+ const t = admit(arrival('A', CHAT, false, false, false));+ projector.applyMessageEditQueued('s1', {+ messageId: 'A',+ body: 'FORGED',+ chatId: CHAT,+ fromMe: true,+ } as never);+ projector.handleInboundMessage('s1', engine, message('A', CHAT, { body: 'original' }), t);+ await flush();++ expect(rows[0]).toEqual(expect.objectContaining({ body: 'original' }));+ expect(dispatched('message.edited')).toEqual([]);+ });++ it('keeps a reaction aimed through another chat out of the message, and does not announce it', async () => {+ const t = admit(arrival('A', CHAT, false, false, false));+ projector.applyReactionQueued('s1', { messageId: 'A', chatId: OTHER, reaction: 'X', senderId: '[email protected]' });+ projector.handleInboundMessage('s1', engine, message('A', CHAT), t);+ await flush();++ expect((rows[0].metadata as Row | undefined)?.reactions).toBeUndefined();+ expect(eventsGateway.emitMessageReaction).not.toHaveBeenCalled();+ });++ it('still takes an edit from the same chat and sender', async () => {+ const t = admit(arrival('A', CHAT, false, false, false));+ projector.applyMessageEditQueued('s1', {+ messageId: 'A',+ body: 'edited',+ chatId: CHAT,+ fromMe: false,+ } as never);+ projector.handleInboundMessage('s1', engine, message('A', CHAT, { body: 'original' }), t);+ await flush();++ expect(rows[0]).toEqual(expect.objectContaining({ body: 'edited' }));+ });++ it('does not guess a direction when the engine did not say whose message it is', async () => {+ const t = admit(arrival('A', CHAT));+ projector.applyMessageEditQueued('s1', {+ messageId: 'A',+ body: 'edited',+ chatId: CHAT,+ fromMe: true,+ } as never);+ projector.handleInboundMessage('s1', engine, message('A', CHAT, { body: 'original', fromMe: true }), t);+ await flush();++ expect(rows[0]).toEqual(expect.objectContaining({ body: 'edited' }));+ });+ });+it('stores a reaction and an ack that arrived before the emit once the row is written', async () => {const t = admit(arrival('A', 'chat1'));projector.applyReactionQueued('s1', { messageId: 'A', chatId: 'chat1', reaction: 'ok', senderId: '[email protected]' });diff --git a/src/modules/session/message-projector.service.spec.ts b/src/modules/session/message-projector.service.spec.tsindex 1b900613e..4b6d44a0b 100644--- a/src/modules/session/message-projector.service.spec.ts+++ b/src/modules/session/message-projector.service.spec.ts@@ -843,6 +843,8 @@ describe('MessageProjector (inbound projection)', () => {'message.received',expect.objectContaining({ body: 'FORGED' }),);+ // Nor is it announced as an edit of a message the consumer has not been given yet.+ expect(webhookService.dispatch).not.toHaveBeenCalledWith(SESSION_ID, 'message.edited', expect.anything());});it('keeps an edit that claims to come from the account out of a message the counterparty sent', async () => {diff --git a/src/modules/session/message-projector.service.ts b/src/modules/session/message-projector.service.tsindex 39da03a16..b00c65d00 100644--- a/src/modules/session/message-projector.service.ts+++ b/src/modules/session/message-projector.service.ts@@ -92,6 +92,13 @@ type InboundMessageData = { [K in keyof IncomingMessage]: IncomingMessage[K] };const quoteOf = (m: Pick<IncomingMessage, 'chatId' | 'body'>) => ({ chatId: m.chatId, body: m.body });+/** Where a message lives and who wrote it: what a change aimed at it is held to (see mutationTargetsForeignRow). */+const refOf = (m: Pick<IncomingMessage, 'chatId' | 'fromMe' | 'author'>): StoredMessageRef => ({+ chatId: m.chatId,+ direction: m.fromMe ? MessageDirection.OUTGOING : MessageDirection.INCOMING,+ author: m.author,+});+/** A message not yet written, and the changes that reached it meanwhile (see inboundInFlight). */interface InFlightEntry {// What a quote of it may lend, as the hook chain last left it. Never the message itself: a repeat@@ -270,7 +277,7 @@ export class MessageProjector {admitInbound(id: string,engine: IWhatsAppEngine,- a: { id: string; chatId: string; needsRoom: boolean; status: boolean },+ a: { id: string; chatId: string; needsRoom: boolean; status: boolean; fromMe?: boolean; author?: string },): InboundTicket | undefined {if (!this.engines.isLive(id, engine)) return undefined;const key = a.status || !a.id ? null : `${id}:${a.id}`;@@ -278,6 +285,10 @@ export class MessageProjector {if (key) {entry = this.inboundInFlight.get(key) ?? entry;this.inboundInFlight.set(key, entry);+ // From the arrival, so a change that reaches the message while it downloads media is held to the rule+ // of the row it will become. Only when the engine says whose message it is: a guessed direction+ // would refuse the account's own change.+ if (a.fromMe !== undefined) entry.ref ??= refOf({ chatId: a.chatId, fromMe: a.fromMe, author: a.author });}entry.holders = (entry.holders ?? 0) + 1;const room = a.needsRoom ? this.room(id) : undefined;@@ -364,7 +375,7 @@ export class MessageProjector {// handler's rewrite.const inFlightKey = `${id}:${message.id}`;const inFlight = turn- ? Object.assign(turn.entry, { quote: quoteOf(messageData) })+ ? Object.assign(turn.entry, { quote: quoteOf(messageData), ref: refOf(messageData) }): this.trackInFlight(inFlightKey, messageData);const onFailure = (err: unknown): null => {@@ -437,12 +448,15 @@ 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 ref: StoredMessageRef = {- chatId: message.chatId,- direction: message.fromMe ? MessageDirection.OUTGOING : MessageDirection.INCOMING,- author: message.author,+ const inFlight = {+ quote: quoteOf(message),+ ref: refOf(message),+ revoked,+ revokeAnnounced,+ editedBody,+ ackStatus,+ reactions,};- const inFlight = { quote: quoteOf(message), ref, revoked, revokeAnnounced, editedBody, ackStatus, reactions };this.inboundInFlight.set(key, inFlight);return inFlight;}@@ -901,7 +915,7 @@ export class MessageProjector {// path, the entry follows the chain's rewrites, so a quote carries what the row will hold.const inFlightKey = `${id}:${message.id}`;const inFlight = turn- ? Object.assign(turn.entry, { quote: quoteOf(messageData) })+ ? Object.assign(turn.entry, { quote: quoteOf(messageData), ref: refOf(messageData) }): 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.@@ -1208,7 +1222,10 @@ 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 && !this.foreignToInFlight(inFlight, { kind: 'reaction', chatId: event.chatId })) {+ if (inFlight) {+ // The row it will become would drop the change, and no row exists yet to say so: it is neither+ // kept for the row nor announced.+ if (this.foreignToInFlight(inFlight, { kind: 'reaction', chatId: event.chatId })) return;(inFlight.reactions ??= {})[event.senderId] = event.reaction ?? '';}this.mutationProjector.applyReactionQueued(id, event);@@ -1217,15 +1234,10 @@ export class MessageProjector {/** 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 &&- !this.foreignToInFlight(inFlight, {- kind: 'edit',- chatId: message.chatId,- fromMe: message.fromMe,- author: message.author,- })- ) {+ if (inFlight) {+ // See applyReactionQueued: judged foreign, it is neither kept for the row nor announced.+ const source = { kind: 'edit', chatId: message.chatId, fromMe: message.fromMe, author: message.author } as const;+ if (this.foreignToInFlight(inFlight, source)) return;inFlight.editedBody = message.body;}this.mutationProjector.applyMessageEditQueued(id, message);