Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): read the spellings of one phone number as one chat

Yudhi Armyndharisdcfbbe1f7d6c
diff --git a/src/modules/session/message-history-projector.spec.ts b/src/modules/session/message-history-projector.spec.ts
index 3cd444c28..479eceecc 100644
--- a/src/modules/session/message-history-projector.spec.ts
+++ b/src/modules/session/message-history-projector.spec.ts
@@ -65,7 +65,7 @@ describe('persistHistoryMessages revokes', () => {
});
it('leaves a row of another chat alone', async () => {
- await stored('[email protected]');
+ await stored('[email protected]');
const cleared = await run([tombstone('[email protected]')]);
diff --git a/src/modules/session/message-mutation-guard.spec.ts b/src/modules/session/message-mutation-guard.spec.ts
index eb8d63e35..ee53fa368 100644
--- a/src/modules/session/message-mutation-guard.spec.ts
+++ b/src/modules/session/message-mutation-guard.spec.ts
@@ -30,10 +30,36 @@ describe('mutationTargetsForeignRow', () => {
it('refuses a change that names a message of another chat, whatever the kind', () => {
for (const kind of ['edit', 'revoke', 'reaction'] as const) {
- expect(mutationTargetsForeignRow({ chatId: '[email protected]', direction: IN }, { kind, chatId: direct })).toBe(
- 'chat',
- );
expect(mutationTargetsForeignRow({ chatId: group, direction: IN }, { kind, chatId: direct })).toBe('chat');
+ expect(mutationTargetsForeignRow({ chatId: direct, direction: IN }, { kind, chatId: group })).toBe('chat');
+ expect(
+ mutationTargetsForeignRow({ chatId: '[email protected]', direction: IN }, { kind, chatId: group }),
+ ).toBe('chat');
+ }
+ });
+
+ // A REST send keeps the number as its caller wrote it ('+', spaces, no mobile marker digit) while the
+ // engine reports the canonical one.
+ it('reads the spellings of one phone number as one chat', () => {
+ const spellings: Array<[string, string]> = [
+ ];
+ for (const [stored, reported] of spellings) {
+ expect(
+ mutationTargetsForeignRow({ chatId: stored, direction: OUT }, { kind: 'reaction', chatId: reported }),
+ ).toBeNull();
+ }
+ });
+
+ it('still calls two different phone numbers different chats', () => {
+ for (const stored of ['[email protected]', '[email protected]', '628222', '[email protected]']) {
+ expect(
+ mutationTargetsForeignRow({ chatId: stored, direction: IN }, { kind: 'reaction', chatId: '[email protected]' }),
+ ).toBe('chat');
}
});
@@ -62,12 +88,6 @@ describe('mutationTargetsForeignRow', () => {
).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',
);
diff --git a/src/modules/session/message-mutation-guard.ts b/src/modules/session/message-mutation-guard.ts
index 370199a3b..ceed3db51 100644
--- a/src/modules/session/message-mutation-guard.ts
+++ b/src/modules/session/message-mutation-guard.ts
@@ -18,6 +18,19 @@ export interface MutationSource {
author?: string;
}
+/**
+ * What two spellings of one phone number share: its digits, without a '+', spaces or dashes, and without
+ * the mobile marker digit that the id WhatsApp reports for a Brazilian, Mexican or Argentine number
+ * drops, which a REST caller usually types (a send stores the chat id as its caller wrote it).
+ */
+function phoneKey(local: string): string {
+ return local
+ .replace(/\D/g, '')
+ .replace(/^55(\d{2})9(\d{8})$/, '55$1$2')
+ .replace(/^521(\d{10})$/, '52$1')
+ .replace(/^549(\d+)$/, '54$1');
+}
+
/**
* 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.
@@ -25,16 +38,16 @@ export interface MutationSource {
*/
function entityOf(jid: string): { kind: string; id: string } | null {
const parsed = parseWaId(jid);
+ if (parsed.kind === 'user') return { kind: 'user', id: phoneKey(parsed.userPart) };
if (parsed.kind !== 'unknown') return { kind: parsed.kind, id: parsed.userPart };
- return !jid.includes('@') && /^\d+$/.test(parsed.userPart) ? { kind: 'user', id: parsed.userPart } : null;
+ return !jid.includes('@') && /^\+?\d[\d \-]*$/.test(jid.trim()) ? { kind: 'user', id: phoneKey(jid) } : null;
}
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.
+ * Whether two ids provably name different chats or people, compared by the entity they name rather than
+ * by dialect or spelling. 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;
diff --git a/src/modules/session/message-projector.service.spec.ts b/src/modules/session/message-projector.service.spec.ts
index b901724ef..ebdfaebf3 100644
--- a/src/modules/session/message-projector.service.spec.ts
+++ b/src/modules/session/message-projector.service.spec.ts
@@ -203,7 +203,7 @@ describe('MessageProjector', () => {
});
it('drops a reaction that names a message of another chat', async () => {
- messageRepository.findOne.mockResolvedValue({ chatId: '[email protected]', direction: 'incoming' });
+ messageRepository.findOne.mockResolvedValue({ chatId: '[email protected]', direction: 'incoming' });
projector.applyReactionQueued('s1', { messageId: 'M1', chatId: '[email protected]', senderId: '[email protected]', reaction: '👍' });
await settle();
@@ -367,7 +367,7 @@ describe('MessageProjector', () => {
});
it('drops a revoke that names a message of another chat', async () => {
- row({ chatId: '[email protected]' });
+ row({ chatId: '[email protected]' });
projector.handleMessageRevoked('s1', engine, revoke, { fromMe: false });
await settle();