Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): announce a reaction whose stored copy failed

Yudhi Armyndharis908823b052a7
diff --git a/src/modules/session/message-mutation-projector.spec.ts b/src/modules/session/message-mutation-projector.spec.ts
new file mode 100644
index 000000000..c69a467f1
--- /dev/null
+++ b/src/modules/session/message-mutation-projector.spec.ts
@@ -0,0 +1,66 @@
+import { DataSource } from 'typeorm';
+import { Message } from '../message/entities/message.entity';
+import { KeyedMutationQueue } from '../../common/utils/keyed-mutation-queue';
+import type { LoggerService } from '../../common/services/logger.service';
+import type { EventsGateway } from '../events/events.gateway';
+import type { WebhookService } from '../webhook/webhook.service';
+import { MessageMutationProjector } from './message-mutation-projector';
+
+const settle = async (): Promise<void> => {
+ for (let n = 0; n < 5; n++) await new Promise(resolve => setImmediate(resolve));
+};
+
+// The stored copy of a reaction is best-effort; the notification is not (see applyReaction).
+describe('a reaction whose stored copy cannot be written', () => {
+ let db: DataSource;
+ let emitMessageReaction: jest.Mock;
+ let dispatch: jest.Mock;
+ let projector: MessageMutationProjector;
+ let logger: { error: jest.Mock; warn: jest.Mock };
+
+ beforeEach(async () => {
+ db = await new DataSource({
+ type: 'better-sqlite3',
+ database: ':memory:',
+ entities: [Message],
+ synchronize: true,
+ }).initialize();
+ const repository = db.getRepository(Message);
+ await repository.insert({
+ sessionId: 's1',
+ waMessageId: 'WA1',
+ chatId: '[email protected]',
+ direction: 'incoming' as never,
+ body: 'hello',
+ type: 'text',
+ timestamp: 1_700_000_000,
+ });
+ jest.spyOn(repository, 'update').mockRejectedValue(new Error('database is locked'));
+ emitMessageReaction = jest.fn();
+ dispatch = jest.fn().mockResolvedValue(undefined);
+ logger = { error: jest.fn(), warn: jest.fn() };
+ projector = new MessageMutationProjector(
+ repository,
+ { emitMessageReaction } as unknown as EventsGateway,
+ { dispatch } as unknown as WebhookService,
+ new KeyedMutationQueue(),
+ logger as unknown as LoggerService,
+ );
+ });
+
+ afterEach(async () => {
+ await db.destroy();
+ });
+
+ it('is still announced, without the full list of reactions it could not compute', async () => {
+ const event = { messageId: 'WA1', chatId: '[email protected]', senderId: '[email protected]', reaction: 'πŸ‘' };
+ projector.applyReactionQueued('s1', event);
+ await settle();
+
+ expect(logger.error).toHaveBeenCalledTimes(1);
+ expect(emitMessageReaction).toHaveBeenCalledWith('s1', event);
+ expect(dispatch).toHaveBeenCalledWith('s1', 'message.reaction', event);
+ });
+});
diff --git a/src/modules/session/message-mutation-projector.ts b/src/modules/session/message-mutation-projector.ts
index 1d8022cfc..351a25950 100644
--- a/src/modules/session/message-mutation-projector.ts
+++ b/src/modules/session/message-mutation-projector.ts
@@ -107,6 +107,7 @@ export class MessageMutationProjector {
// ephemeral one under STORE_EPHEMERAL_MESSAGES=false, or one that arrived before the session
// went live) β€” but the notification is not: `message.reaction` is a declared webhook event and
// the dashboard stream is the point of it, the same contract handleMessageRevoked states.
+ // A write that fails is the same case: it is logged, and the reaction goes out without the full list.
const metadata = await updateMessageMetadata(
this.messageRepository,
{ sessionId: id, waMessageId: event.messageId },
@@ -116,7 +117,10 @@ export class MessageMutationProjector {
else reactions[event.senderId] = event.reaction;
return { ...current, reactions };
},
- );
+ ).catch((err: unknown) => {
+ this.logger.error(`Failed to update message reaction: ${event.messageId}`, String(err));
+ return null;
+ });
const reactions = metadata?.reactions as Record<string, string> | undefined;
// `reactions` is the post-apply snapshot of EVERY reaction on the message and consumers REPLACE