Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

feat(engine): add chatKind() JID classifier

Yudhi Armyndharisc44700ceb69a
diff --git a/src/engine/identity/wa-id.spec.ts b/src/engine/identity/wa-id.spec.ts
index d734e22c2..459dbe90d 100644
--- a/src/engine/identity/wa-id.spec.ts
+++ b/src/engine/identity/wa-id.spec.ts
@@ -1,4 +1,4 @@
-import { isChannelJid, parseWaId, toNeutralJid, userPart } from './wa-id';
+import { chatKind, isChannelJid, parseWaId, toNeutralJid, userPart } from './wa-id';
describe('wa-id', () => {
describe('userPart', () => {
@@ -74,4 +74,21 @@ describe('wa-id', () => {
expect(toNeutralJid('weird-thing')).toBe('weird-thing');
});
});
+
+ describe('chatKind', () => {
+ it.each([
+ ['[email protected]', 'individual'],
+ ['[email protected]', 'individual'],
+ ['628111:[email protected]', 'individual'],
+ ['4707@lid', 'individual'],
+ ['[email protected]', 'group'],
+ ['abc@newsletter', 'channel'],
+ ['status@broadcast', 'status'],
+ ['12036@broadcast', 'broadcast'],
+ ['not-a-jid', 'unknown'],
+ ['', 'unknown'],
+ ])('classifies %s as %s', (jid, expected) => {
+ expect(chatKind(jid)).toBe(expected);
+ });
+ });
});
diff --git a/src/engine/identity/wa-id.ts b/src/engine/identity/wa-id.ts
index a13787069..22d846da4 100644
--- a/src/engine/identity/wa-id.ts
+++ b/src/engine/identity/wa-id.ts
@@ -110,3 +110,28 @@ export function toNeutralJid(jid: string, resolvePhone?: (jid: string) => string
export function isChannelJid(jid: string): boolean {
return parseWaId(jid).kind === 'newsletter';
}
+
+/** User-facing chat kind. The engine-neutral, consumer-visible vocabulary (never raw WaIdKind). */
+export type ChatKind = 'individual' | 'group' | 'channel' | 'status' | 'broadcast' | 'unknown';
+
+/**
+ * Map any WhatsApp JID to its user-facing chat kind. `lid` folds into `individual` (a lid is a
+ * privacy dialect of a user); `newsletter` surfaces as `channel`. Pure wrapper over {@link parseWaId}.
+ */
+export function chatKind(jid: string): ChatKind {
+ switch (parseWaId(jid).kind) {
+ case 'user':
+ case 'lid':
+ return 'individual';
+ case 'group':
+ return 'group';
+ case 'newsletter':
+ return 'channel';
+ case 'status':
+ return 'status';
+ case 'broadcast':
+ return 'broadcast';
+ default:
+ return 'unknown';
+ }
+}