fix(whatsapp): prefer lid mention resolution

This commit is contained in:
Val Alexander
2026-05-02 23:24:07 -05:00
parent f9a3596803
commit bb1df9e681
2 changed files with 40 additions and 3 deletions

View File

@@ -51,6 +51,37 @@ describe("resolveWhatsAppOutboundMentions", () => {
});
});
it("prefers explicit LID metadata over a phone JID id", () => {
expect(
resolveWhatsAppOutboundMentions({
chatJid: "120363000000000000@g.us",
text: "ping @15551234567 and @277038292303944",
participants: [
{
id: "15551234567@s.whatsapp.net",
lid: "277038292303944@lid",
},
],
}),
).toEqual({
text: "ping @277038292303944 and @277038292303944",
mentionedJids: ["277038292303944@lid"],
});
});
it("uses bare digit tokens for LIDs before phone numbers when participant keys collide", () => {
expect(
resolveWhatsAppOutboundMentions({
chatJid: "120363000000000000@g.us",
text: "ping @277038292303944 and @+277038292303944",
participants: [{ id: "277038292303944@s.whatsapp.net" }, { id: "277038292303944@lid" }],
}),
).toEqual({
text: "ping @277038292303944 and @+277038292303944",
mentionedJids: ["277038292303944@lid", "277038292303944@s.whatsapp.net"],
});
});
it("applies LID rewrites by match position while skipping code spans", () => {
expect(
resolveWhatsAppOutboundMentions({

View File

@@ -128,9 +128,13 @@ function participantValues(participant: WhatsAppOutboundMentionParticipant): {
function chooseMentionJid(participant: WhatsAppOutboundMentionParticipant): string | null {
const values = participantValues(participant);
const idJid = normalizeKnownUserJid(values.id ?? "");
const lidJid = normalizeKnownUserJid(values.lid ?? "");
return (
normalizeKnownUserJid(values.id ?? "") ??
normalizeKnownUserJid(values.lid ?? "") ??
(idJid && isLidJid(idJid) ? idJid : null) ??
(lidJid && isLidJid(lidJid) ? lidJid : null) ??
idJid ??
lidJid ??
normalizeKnownUserJid(values.phoneNumber ?? "") ??
normalizeKnownUserJid(values.e164 ?? "")
);
@@ -210,7 +214,9 @@ export function resolveWhatsAppOutboundMentions(params: {
}
const token = match[0];
const digits = match[1].replace(/\D/g, "");
const target = byPhone.get(digits) ?? byLid.get(digits);
const target = token.startsWith("@+")
? (byPhone.get(digits) ?? byLid.get(digits))
: (byLid.get(digits) ?? byPhone.get(digits));
if (!target) {
continue;
}