diff --git a/src/web/inbound.test.ts b/src/web/inbound.test.ts index 1a31303cd..bac354d6b 100644 --- a/src/web/inbound.test.ts +++ b/src/web/inbound.test.ts @@ -75,6 +75,23 @@ describe("web inbound helpers", () => { expect(body).toBe(""); }); + it("trims and skips empty WhatsApp vcard phones", () => { + const body = extractText({ + contactMessage: { + vcard: [ + "BEGIN:VCARD", + "VERSION:3.0", + "FN:Ada Lovelace", + "TEL;TYPE=CELL: +15555550123 ", + "TEL;TYPE=HOME: ", + "TEL;TYPE=WORK:+15555550124", + "END:VCARD", + ].join("\n"), + }, + } as unknown as import("@whiskeysockets/baileys").proto.IMessage); + expect(body).toBe(""); + }); + it("extracts multiple WhatsApp contact cards", () => { const body = extractText({ contactsArrayMessage: { diff --git a/src/web/inbound.ts b/src/web/inbound.ts index 8fc094515..553c02a8f 100644 --- a/src/web/inbound.ts +++ b/src/web/inbound.ts @@ -801,8 +801,7 @@ function formatContactsPlaceholder(labels: string[], total: number): string { const suffix = total === 1 ? "contact" : "contacts"; return ``; } - const shown = cleaned.slice(0, 3); - const remaining = Math.max(total - shown.length, 0); + const { shown, remaining } = summarizeList(cleaned, total, 3); const suffix = remaining > 0 ? ` +${remaining} more` : ""; return ``; } @@ -822,10 +821,21 @@ function formatContactLabel( function formatPhoneList(phones?: string[]): string | undefined { const cleaned = phones?.map((phone) => phone.trim()).filter(Boolean) ?? []; if (cleaned.length === 0) return undefined; - const [primary, ...rest] = cleaned; + const { shown, remaining } = summarizeList(cleaned, cleaned.length, 1); + const [primary] = shown; if (!primary) return undefined; - if (rest.length === 0) return primary; - return `${primary} (+${rest.length} more)`; + if (remaining === 0) return primary; + return `${primary} (+${remaining} more)`; +} + +function summarizeList( + values: string[], + total: number, + maxShown: number, +): { shown: string[]; remaining: number } { + const shown = values.slice(0, maxShown); + const remaining = Math.max(total - shown.length, 0); + return { shown, remaining }; } export function extractLocationData(