Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): compare a change's chat with the stored row by identity
Yudhi Armyndharisd0d0b900b817
diff --git a/src/modules/session/message-mutation-guard.spec.ts b/src/modules/session/message-mutation-guard.spec.tsindex 8df78512c..eb8d63e35 100644--- a/src/modules/session/message-mutation-guard.spec.ts+++ b/src/modules/session/message-mutation-guard.spec.ts@@ -52,6 +52,27 @@ describe('mutationTargetsForeignRow', () => {).toBe('chat');});+ // The send routes store the chat id as the caller wrote it, in either dialect the docs give, while the+ // engines report the neutral one.+ it('reads the id a REST send stored in any dialect of the chat as the same chat', () => {+ for (const stored of ['[email protected]', '628111', '628111:[email protected]', '[email protected]']) {+ for (const kind of ['reaction', 'edit', 'revoke'] as const) {+ expect(+ mutationTargetsForeignRow({ chatId: stored, direction: OUT }, { kind, chatId: direct, fromMe: true }),+ ).toBeNull();+ }+ }+ expect(+ mutationTargetsForeignRow(+ { chatId: '[email protected]', direction: OUT },+ { kind: 'reaction', chatId: direct },+ ),+ ).toBe('chat');+ expect(mutationTargetsForeignRow({ chatId: '628111', direction: OUT }, { kind: 'reaction', chatId: group })).toBe(+ 'chat',+ );+ });+it('lets any participant react, and an admin delete a message the account sent', () => {expect(mutationTargetsForeignRow({ chatId: group, direction: OUT }, { kind: 'reaction', chatId: group, fromMe: false }),diff --git a/src/modules/session/message-mutation-guard.ts b/src/modules/session/message-mutation-guard.tsindex af2bb0cb9..370199a3b 100644--- a/src/modules/session/message-mutation-guard.ts+++ b/src/modules/session/message-mutation-guard.ts@@ -1,4 +1,5 @@import { MessageDirection } from '../message/entities/message.entity';+import { parseWaId } from '../../engine/identity/wa-id';/** What a stored row says about whose message it is and where it lives. */export interface StoredMessageRef {@@ -17,13 +18,31 @@ export interface MutationSource {author?: string;}-const isLid = (jid: string): boolean => jid.endsWith('@lid');-const isPhone = (jid: string): boolean => jid.endsWith('@c.us');+/**+ * The entity an id names, whatever its dialect: a phone (`@c.us`, `@s.whatsapp.net`, hosted, a `:device`+ * form or a bare number, which is how the send routes may have stored it), a lid, a group or a list.+ * Null for an id this cannot place, which proves nothing.+ */+function entityOf(jid: string): { kind: string; id: string } | null {+ const parsed = parseWaId(jid);+ if (parsed.kind !== 'unknown') return { kind: parsed.kind, id: parsed.userPart };+ return !jid.includes('@') && /^\d+$/.test(parsed.userPart) ? { kind: 'user', id: parsed.userPart } : null;+}-/** Whether two neutral ids provably name different chats or people. A lid against a phone number may be one. */-function provablyDifferent(a?: string | null, b?: string | null): boolean {+const isPerson = (kind: string): boolean => kind === 'user' || kind === 'lid';++/**+ * Whether two ids provably name different chats or people. The engines report the neutral dialect but a+ * REST send stores the chat id as the caller wrote it, so ids are compared by the entity they name. A lid+ * against a phone number may be one person.+ */+export function provablyDifferent(a?: string | null, b?: string | null): boolean {if (!a || !b || a === b) return false;- return !((isLid(a) && isPhone(b)) || (isLid(b) && isPhone(a)));+ const left = entityOf(a);+ const right = entityOf(b);+ if (!left || !right) return false;+ if (left.kind === right.kind) return left.id !== right.id;+ return !(isPerson(left.kind) && isPerson(right.kind));}/**diff --git a/src/modules/session/message-projector.service.spec.ts b/src/modules/session/message-projector.service.spec.tsindex 241472fff..b901724ef 100644--- a/src/modules/session/message-projector.service.spec.ts+++ b/src/modules/session/message-projector.service.spec.ts@@ -187,6 +187,21 @@ describe('MessageProjector', () => {expect(messageRepository.update).not.toHaveBeenCalled();});+ it('applies a reaction to a message a REST send stored under the other dialect of the chat id', async () => {+ messageRepository.findOne.mockResolvedValue({ chatId: '[email protected]', direction: 'outgoing' });++ projector.applyReactionQueued('s1', {+ messageId: 'M1',+ chatId: '[email protected]',+ senderId: '[email protected]',+ reaction: '👍',+ });+ await settle();+ await settle();++ expect(eventsGateway.emitMessageReaction).toHaveBeenCalledTimes(1);+ });+it('drops a reaction that names a message of another chat', async () => {messageRepository.findOne.mockResolvedValue({ chatId: '[email protected]', direction: 'incoming' });@@ -342,6 +357,15 @@ describe('MessageProjector', () => {expect(eventsGateway.emitMessageRevoked).toHaveBeenCalledTimes(1);});+ it('applies the account deleting, from its phone, a message a REST send stored under the other dialect', async () => {+ row({ chatId: '[email protected]', direction: 'outgoing' });++ projector.handleMessageRevoked('s1', engine, revoke, { fromMe: true });+ await settle();++ expect(eventsGateway.emitMessageRevoked).toHaveBeenCalledTimes(1);+ });+it('drops a revoke that names a message of another chat', async () => {row({ chatId: '[email protected]' });