Use constants for JID suffixes and improve prefix stripping
This commit is contained in:
parent
5eff33abe6
commit
44aa9c0ccc
@ -2,21 +2,21 @@ import { normalizeE164 } from "../utils.js";
|
|||||||
|
|
||||||
const WHATSAPP_USER_JID_RE = /^(\d+)(?::\d+)?@s\.whatsapp\.net$/i;
|
const WHATSAPP_USER_JID_RE = /^(\d+)(?::\d+)?@s\.whatsapp\.net$/i;
|
||||||
const WHATSAPP_LID_RE = /^(\d+)@lid$/i;
|
const WHATSAPP_LID_RE = /^(\d+)@lid$/i;
|
||||||
|
const WHATSAPP_GROUP_SUFFIX = "@g.us";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes "whatsapp:" prefixes from the string recursively and trims whitespace.
|
||||||
|
*/
|
||||||
function stripWhatsAppTargetPrefixes(value: string): string {
|
function stripWhatsAppTargetPrefixes(value: string): string {
|
||||||
let candidate = value.trim();
|
// More efficient than a loop: matches "whatsapp:" followed by optional spaces, one or more times.
|
||||||
for (;;) {
|
return value.trim().replace(/^(whatsapp:\s*)+/i, "").trim();
|
||||||
const before = candidate;
|
|
||||||
candidate = candidate.replace(/^whatsapp:/i, "").trim();
|
|
||||||
if (candidate === before) return candidate;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isWhatsAppGroupJid(value: string): boolean {
|
export function isWhatsAppGroupJid(value: string): boolean {
|
||||||
const candidate = stripWhatsAppTargetPrefixes(value);
|
const candidate = stripWhatsAppTargetPrefixes(value);
|
||||||
const lower = candidate.toLowerCase();
|
const lower = candidate.toLowerCase();
|
||||||
if (!lower.endsWith("@g.us")) return false;
|
if (!lower.endsWith(WHATSAPP_GROUP_SUFFIX)) return false;
|
||||||
const localPart = candidate.slice(0, candidate.length - "@g.us".length);
|
const localPart = candidate.slice(0, -WHATSAPP_GROUP_SUFFIX.length);
|
||||||
if (!localPart || localPart.includes("@")) return false;
|
if (!localPart || localPart.includes("@")) return false;
|
||||||
return /^[0-9]+(-[0-9]+)*$/.test(localPart);
|
return /^[0-9]+(-[0-9]+)*$/.test(localPart);
|
||||||
}
|
}
|
||||||
@ -30,7 +30,7 @@ export function isWhatsAppUserTarget(value: string): boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract the phone number from a WhatsApp user JID.
|
* Extract the phone number or ID from a WhatsApp user JID.
|
||||||
* "41796666864:0@s.whatsapp.net" -> "41796666864"
|
* "41796666864:0@s.whatsapp.net" -> "41796666864"
|
||||||
* "123456@lid" -> "123456"
|
* "123456@lid" -> "123456"
|
||||||
*/
|
*/
|
||||||
@ -45,20 +45,24 @@ function extractUserJidPhone(jid: string): string | null {
|
|||||||
export function normalizeWhatsAppTarget(value: string): string | null {
|
export function normalizeWhatsAppTarget(value: string): string | null {
|
||||||
const candidate = stripWhatsAppTargetPrefixes(value);
|
const candidate = stripWhatsAppTargetPrefixes(value);
|
||||||
if (!candidate) return null;
|
if (!candidate) return null;
|
||||||
|
|
||||||
if (isWhatsAppGroupJid(candidate)) {
|
if (isWhatsAppGroupJid(candidate)) {
|
||||||
const localPart = candidate.slice(0, candidate.length - "@g.us".length);
|
const localPart = candidate.slice(0, -WHATSAPP_GROUP_SUFFIX.length);
|
||||||
return `${localPart}@g.us`;
|
return `${localPart}${WHATSAPP_GROUP_SUFFIX}`;
|
||||||
}
|
}
|
||||||
// Handle user JIDs (e.g. "41796666864:0@s.whatsapp.net")
|
|
||||||
|
// Handle user JIDs (e.g. "41796666864:0@s.whatsapp.net" or "123@lid")
|
||||||
if (isWhatsAppUserTarget(candidate)) {
|
if (isWhatsAppUserTarget(candidate)) {
|
||||||
const phone = extractUserJidPhone(candidate);
|
const phone = extractUserJidPhone(candidate);
|
||||||
if (!phone) return null;
|
if (!phone) return null;
|
||||||
const normalized = normalizeE164(phone);
|
const normalized = normalizeE164(phone);
|
||||||
return normalized.length > 1 ? normalized : null;
|
return normalized.length > 1 ? normalized : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the caller passed a JID-ish string that we don't understand, fail fast.
|
// If the caller passed a JID-ish string that we don't understand, fail fast.
|
||||||
// Otherwise normalizeE164 would happily treat "group:120@g.us" as a phone number.
|
// Otherwise normalizeE164 would happily treat "group:120@g.us" as a phone number.
|
||||||
if (candidate.includes("@")) return null;
|
if (candidate.includes("@")) return null;
|
||||||
|
|
||||||
const normalized = normalizeE164(candidate);
|
const normalized = normalizeE164(candidate);
|
||||||
return normalized.length > 1 ? normalized : null;
|
return normalized.length > 1 ? normalized : null;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user