Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): mark a held edit on the row it lands on
Yudhi Armyndharisc0d298e4e540
diff --git a/src/modules/session/message-edited-marker.spec.ts b/src/modules/session/message-edited-marker.spec.tsindex 24e0c4102..145b773c9 100644--- a/src/modules/session/message-edited-marker.spec.ts+++ b/src/modules/session/message-edited-marker.spec.ts@@ -1,11 +1,22 @@import { DataSource, type Repository } from 'typeorm';import { Message, MessageDirection } from '../message/entities/message.entity';import { MessageMutationProjector } from './message-mutation-projector';+import { MessageProjector } from './message-projector.service';+import { EngineRegistry } from '../../engine/engine-registry.service';import { KeyedMutationQueue } from '../../common/utils/keyed-mutation-queue';+import { deferred } from './inbound-room';+import type { Session } from './entities/session.entity';import type { EventsGateway } from '../events/events.gateway';import type { WebhookService } from '../webhook/webhook.service';+import type { HookManager } from '../../core/hooks';+import type { StatusStoreService } from '../status-store/status-store.service';+import type { SessionLidResolver } from './session-lid-resolver.service';import type { LoggerService } from '../../common/services/logger.service';-import type { EditedMessage } from '../../engine/interfaces/whatsapp-engine.interface';+import type {+ EditedMessage,+ IncomingMessage,+ IWhatsAppEngine,+} from '../../engine/interfaces/whatsapp-engine.interface';describe('persisted edited message marker', () => {let db: DataSource;@@ -101,3 +112,145 @@ describe('persisted edited message marker', () => {expect(emit).not.toHaveBeenCalled();});});++// An edit can reach a message before its row is written: its insert is out, or an earlier message of the+// chat holds the commit turn. The edit then lands on the row once it exists, and leaves the same marker.+describe('edited marker for a message whose row is not written yet', () => {+ const CHAT = '[email protected]';+ let db: DataSource;+ let repository: Repository<Message>;+ let projector: MessageProjector;+ let engine: IWhatsAppEngine;+ // The insert of a row waits for the promise held under its message id.+ let stalled: Map<string, Promise<void>>;++ const flush = async (): Promise<void> => {+ for (let i = 0; i < 25; i++) await new Promise(resolve => setImmediate(resolve));+ };+ const incoming = (id: string, over: Partial<IncomingMessage> = {}): IncomingMessage => ({+ id,+ chatId: CHAT,+ from: CHAT,+ to: '[email protected]',+ body: 'original',+ type: 'text',+ timestamp: 1_700_000_000,+ fromMe: false,+ isGroup: false,+ kind: 'individual',+ ...over,+ });+ const editOf = (messageId: string, body: string): EditedMessage =>+ ({ messageId, chatId: CHAT, fromMe: false, body }) as EditedMessage;+ const stored = (waMessageId: string): Promise<Message> => repository.findOneByOrFail({ waMessageId });++ beforeEach(async () => {+ db = new DataSource({ type: 'better-sqlite3', database: ':memory:', entities: [Message], synchronize: true });+ await db.initialize();+ repository = db.getRepository(Message);+ stalled = new Map();+ const insert = repository.insert.bind(repository);+ jest.spyOn(repository, 'insert').mockImplementation(async row => {+ await stalled.get((row as { waMessageId: string }).waMessageId);+ return insert(row);+ });+ const engines = new EngineRegistry();+ engine = { tag: 'one' } as unknown as IWhatsAppEngine;+ engines.set('s1', engine);+ projector = new MessageProjector(+ 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,+ {+ execute: jest.fn((_event: string, data: unknown) => 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,+ );+ });++ afterEach(async () => {+ await db.destroy();+ });++ 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);+ projector.handleInboundMessage('s1', engine, incoming('A', { quotedMessage: { id: 'Q', body: 'quoted' } }));+ await flush();+ expect(await repository.count()).toBe(0);++ projector.applyMessageEditQueued('s1', editOf('A', 'edited'));+ projector.applyReactionQueued('s1', { messageId: 'A', chatId: CHAT, senderId: CHAT, reaction: 'π' });+ await flush();+ insert.resolve();+ await flush();++ const row = await stored('A');+ expect(row.body).toBe('edited');+ expect(row.metadata).toEqual({+ quotedMessage: { id: 'Q', body: 'quoted' },+ reactions: { [CHAT]: 'π' },+ edited: true,+ });+ });++ it('marks a message edited while it waits behind an earlier message of its chat', async () => {+ const insert = deferred<void>();+ stalled.set('A', insert.promise);+ projector.handleInboundMessage('s1', engine, incoming('A'));+ projector.handleInboundMessage('s1', engine, incoming('B'));+ await flush();++ projector.applyMessageEditQueued('s1', editOf('B', 'edited'));+ await flush();+ insert.resolve();+ await flush();++ expect(await stored('B')).toEqual(expect.objectContaining({ body: 'edited', metadata: { edited: true } }));+ expect((await stored('A')).metadata).toBeNull();+ });++ it('marks a send echoed behind an earlier message when a REST edit reaches it first', async () => {+ const insert = deferred<void>();+ stalled.set('A', insert.promise);+ projector.handleInboundMessage('s1', engine, incoming('A'));+ projector.handleOwnSendEcho('s1', engine, incoming('S', { fromMe: true, body: 'sent' }));+ await flush();++ await projector.recordOutboundMessageEdit('s1', 'S', 'rest edit', { kind: 'edit', chatId: CHAT, fromMe: true });+ insert.resolve();+ await flush();++ expect(await stored('S')).toEqual(+ expect.objectContaining({ body: 'rest edit', direction: MessageDirection.OUTGOING, metadata: { edited: true } }),+ );+ });++ it('leaves a message nobody edited unmarked', async () => {+ const insert = deferred<void>();+ stalled.set('A', insert.promise);+ projector.handleInboundMessage('s1', engine, incoming('A'));+ await flush();+ insert.resolve();+ await flush();++ expect((await stored('A')).metadata).toBeNull();+ });++ it('does not mark a message deleted after the edit while it was not written yet', async () => {+ const insert = deferred<void>();+ stalled.set('A', insert.promise);+ projector.handleInboundMessage('s1', engine, incoming('A'));+ await flush();++ projector.applyMessageEditQueued('s1', editOf('A', 'edited'));+ await projector.recordRevoke('s1', 'A');+ insert.resolve();+ await flush();++ expect(await stored('A')).toEqual(expect.objectContaining({ body: '', type: 'revoked', metadata: null }));+ });+});diff --git a/src/modules/session/message-mutation-projector.ts b/src/modules/session/message-mutation-projector.tsindex fd3ccd914..e609fe13f 100644--- a/src/modules/session/message-mutation-projector.ts+++ b/src/modules/session/message-mutation-projector.ts@@ -162,7 +162,7 @@ export class MessageMutationProjector {}/** Keep the marker alongside existing reactions, media and quote metadata. */- private async markMessageEdited(sessionId: string, waMessageId: string): Promise<void> {+ async markMessageEdited(sessionId: string, waMessageId: string): Promise<void> {await updateMessageMetadata(this.messageRepository, { sessionId, waMessageId }, metadata => ({...metadata,edited: true,diff --git a/src/modules/session/message-projector.inbound-hold.spec.ts b/src/modules/session/message-projector.inbound-hold.spec.tsindex 81389ccd1..4d28ccec7 100644--- a/src/modules/session/message-projector.inbound-hold.spec.ts+++ b/src/modules/session/message-projector.inbound-hold.spec.ts@@ -258,10 +258,51 @@ describe('MessageProjector holding inbound messages (PostgreSQL)', () => {const calls = hookManager.execute.mock.calls as Array<[string, IncomingMessage]>;const hooked = calls.find(c => c[0] === 'message:received')?.[1];expect(hooked?.body).toBe('edited');- expect(rows[0]).toEqual(expect.objectContaining({ body: 'edited' }));+ expect(rows[0]).toEqual(expect.objectContaining({ body: 'edited', metadata: { edited: true } }));+ // The row `message:persisted` hands out is the one the edit left, marker included.+ expect(persistedHooks()[0]).toEqual(expect.objectContaining({ body: 'edited', metadata: { edited: true } }));expect(dispatched('message.received')[0]).toEqual(expect.objectContaining({ body: 'edited' }));});+ it('marks a message edited while its insert is out, where nothing holds the arrival (SQLite)', async () => {+ build('better-sqlite3');+ const stall = deferred<void>();+ insertGate = () => stall.promise;+ projector.handleInboundMessage('s1', engine, message('A', 'chat1', { body: 'original' }));+ await flush();+ projector.applyMessageEditQueued('s1', { messageId: 'A', body: 'edited' } as never);+ await flush();+ stall.resolve();+ await flush();++ expect(rows).toEqual([expect.objectContaining({ body: 'edited', metadata: { edited: true } })]);+ });++ // A REST edit of a send the account's phone composed reaches the echo while it waits for its row.+ describe.each([+ ['PostgreSQL', 'postgres'],+ ['SQLite', 'better-sqlite3'],+ ])('an own-send echo that a REST edit reaches before its row is written, on %s', (_name, type) => {+ const CHAT = '[email protected]';++ it('stores the row with the edit and its marker', async () => {+ build(type);+ const hook = deferred<void>();+ hookManager.execute.mockImplementationOnce(async (_event: string, data: unknown) => {+ await hook.promise;+ return { continue: true, data };+ });+ const ticket = type === 'postgres' ? admit(arrival('A', CHAT, false, false, true)) : undefined;+ projector.handleOwnSendEcho('s1', engine, message('A', CHAT, { fromMe: true, body: 'sent' }), ticket);+ await flush();+ await projector.recordOutboundMessageEdit('s1', 'A', 'rest edit', { kind: 'edit', chatId: CHAT, fromMe: true });+ hook.resolve();+ await flush();++ expect(rows).toEqual([expect.objectContaining({ body: 'rest edit', metadata: { edited: true } })]);+ });+ });+// 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', () => {@@ -836,7 +877,9 @@ describe('MessageProjector holding inbound messages (PostgreSQL)', () => {await flush();expect(rows.find(r => r.waMessageId === 'A')).toEqual(expect.objectContaining({ body: '', type: 'revoked' }));- expect(rows.find(r => r.waMessageId === 'B')).toEqual(expect.objectContaining({ body: 'rest edit' }));+ expect(rows.find(r => r.waMessageId === 'B')).toEqual(+ expect.objectContaining({ body: 'rest edit', metadata: { edited: true } }),+ );// A delete for me: the message still exists for its sender, so it is announced as revoked.expect(dispatched('message.received').map(m => [m.id, m.type, m.body])).toEqual([['A', 'revoked', ''],diff --git a/src/modules/session/message-projector.service.ts b/src/modules/session/message-projector.service.tsindex d0e945b27..269c9fa61 100644--- a/src/modules/session/message-projector.service.ts+++ b/src/modules/session/message-projector.service.ts@@ -743,7 +743,10 @@ export class MessageProjector {if (!this.holdsInbound) return false;const entry = dbMessage.waMessageId ? this.inboundInFlight.get(`${id}:${dbMessage.waMessageId}`) : undefined;if (entry?.revoked) Object.assign(dbMessage, REVOKED_ROW_PATCH);- else if (entry?.editedBody !== undefined) dbMessage.body = entry.editedBody;+ else if (entry?.editedBody !== undefined) {+ dbMessage.body = entry.editedBody;+ dbMessage.metadata = { ...dbMessage.metadata, edited: true };+ }return entry?.revoked === true;}@@ -860,10 +863,11 @@ export class MessageProjector {): Promise<void> {if (pending.revoked) return this.revokeRow(id, waMessageId, !insertedCleared);try {- if (pending.editedBody !== undefined) {- await this.messageRepository.update({ sessionId: id, waMessageId }, { body: pending.editedBody });- }+ const edited = pending.editedBody !== undefined;+ if (edited) await this.messageRepository.update({ sessionId: id, waMessageId }, { body: pending.editedBody });if (pending.reactions) await this.storeReactions(id, waMessageId, pending.reactions);+ // The marker an edit of a stored row leaves, written last so a lost metadata race costs it alone.+ if (edited) await this.mutationProjector.markMessageEdited(id, waMessageId);} catch (err) {this.logger.error(`Failed to apply changes to message ${waMessageId}`, String(err));}