Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
feat(identity): classify and qualify participant ids
Yudhi Armyndharisab41039a8710
diff --git a/src/engine/identity/wa-id.spec.ts b/src/engine/identity/wa-id.spec.tsindex 459dbe90d..b97a0f2d5 100644--- a/src/engine/identity/wa-id.spec.ts+++ b/src/engine/identity/wa-id.spec.ts@@ -1,4 +1,12 @@-import { chatKind, isChannelJid, parseWaId, toNeutralJid, userPart } from './wa-id';+import {+ chatKind,+ isAddressableParticipant,+ isChannelJid,+ parseWaId,+ toNeutralJid,+ toParticipantWid,+ userPart,+} from './wa-id';describe('wa-id', () => {describe('userPart', () => {@@ -91,4 +99,42 @@ describe('wa-id', () => {expect(chatKind(jid)).toBe(expected);});});++ describe('isAddressableParticipant', () => {+ it.each([+ ['628123456789', true],+ ['[email protected]', true],+ ['[email protected]', true],+ ['12345678901234567890@lid', true],+ ['628123456789:[email protected]', true],+ [' [email protected] ', true],+ ['NOT A USER', false],+ ['', false],+ ['1234', false],+ ['[email protected]', false],+ ['status@broadcast', false],+ ['abc@newsletter', false],+ ['abc@weird', false],+ ])('classifies %s as %s', (value, expected) => {+ expect(isAddressableParticipant(value)).toBe(expected);+ });+ });++ describe('toParticipantWid', () => {+ it('qualifies a bare number to the c.us dialect', () => {+ expect(toParticipantWid('628123456789')).toBe('[email protected]');+ });++ it('leaves an already-qualified id untouched', () => {+ expect(toParticipantWid('[email protected]')).toBe('[email protected]');+ expect(toParticipantWid('12345678901234567890@lid')).toBe('12345678901234567890@lid');+ expect(toParticipantWid('[email protected]')).toBe('[email protected]');+ });++ it('does not append a second domain to a string that already carries one', () => {+ // The difference from the old `p.includes('@')` test: an unrecognised domain must pass+ // through rather than become `abc@[email protected]`.+ expect(toParticipantWid('abc@weird')).toBe('abc@weird');+ });+ });});diff --git a/src/engine/identity/wa-id.ts b/src/engine/identity/wa-id.tsindex 22d846da4..0afe59733 100644--- a/src/engine/identity/wa-id.ts+++ b/src/engine/identity/wa-id.ts@@ -70,6 +70,38 @@ export function parseWaId(jid: string): ParsedWaId {return { kind, userPart: local, device, raw };}+/** A digits-only id, e.g. `628123456789` — accepted for convenience and qualified by {@link toParticipantWid}. */+const BARE_NUMBER = /^\d{5,}$/;++/**+ * Whether a string can address an individual participant of a group.+ *+ * A participant names a person, so only the individual dialects qualify: `<phone>@c.us` (and its+ * raw-protocol twin `@s.whatsapp.net`), `<lid>@lid`, or a bare phone number. A group id is rejected —+ * a group cannot be a member of a group — as is anything else.+ *+ * Nothing validated the shape before, so free text reached whatsapp-web.js's page-side `createWid`,+ * which threw a minified `t: t` and left the caller with an undiagnosable 500 (#1220). The same+ * failure was diagnosed once already for message mentions (#1068, see is-mention-wid.validator.ts).+ */+export function isAddressableParticipant(value: string): boolean {+ const trimmed = value.trim();+ const { kind } = parseWaId(trimmed);+ return kind === 'user' || kind === 'lid' || (kind === 'unknown' && BARE_NUMBER.test(trimmed));+}++/**+ * Qualify a bare number to the neutral `@c.us` dialect. Anything else passes through verbatim.+ *+ * Deliberately keyed on the bare-number shape rather than on the absence of `@`: the old test+ * (`p.includes('@') ? p : p + '@c.us'`) agrees for every valid input, but this form cannot turn an+ * unrecognised domain into a double-qualified `abc@[email protected]`.+ */+export function toParticipantWid(value: string): string {+ const trimmed = value.trim();+ return BARE_NUMBER.test(trimmed) ? `${trimmed}@c.us` : trimmed;+}+/*** Reduce any WhatsApp JID to the neutral dialect (see the module contract above). `resolvePhone` maps a* lid to its phone user-part when the engine knows the mapping; an unresolvable lid is kept as