fix(web): handle self-chat mode

This commit is contained in:
Peter Steinberger
2025-12-20 19:31:51 +01:00
parent c38aeb1081
commit 929a10e33d
5 changed files with 178 additions and 20 deletions

View File

@@ -31,6 +31,28 @@ export function normalizeE164(number: string): string {
return `+${digits}`;
}
/**
* "Self-chat mode" heuristic (single phone): the gateway is logged in as the owner's own WhatsApp account,
* and `inbound.allowFrom` includes that same number. Used to avoid side-effects that make no sense when the
* "bot" and the human are the same WhatsApp identity (e.g. auto read receipts, @mention JID triggers).
*/
export function isSelfChatMode(
selfE164: string | null | undefined,
allowFrom?: Array<string | number> | null,
): boolean {
if (!selfE164) return false;
if (!Array.isArray(allowFrom) || allowFrom.length === 0) return false;
const normalizedSelf = normalizeE164(selfE164);
return allowFrom.some((n) => {
if (n === "*") return false;
try {
return normalizeE164(String(n)) === normalizedSelf;
} catch {
return false;
}
});
}
export function toWhatsappJid(number: string): string {
const e164 = normalizeE164(number);
const digits = e164.replace(/\D/g, "");