Merge pull request #624 from clawdbot/refactor/vcard-utils
refactor: extract vcard parsing helper
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
- WhatsApp: refactor vCard parsing helper and improve empty contact card summaries. (#624) — thanks @steipete
|
||||||
- Pairing: cap pending DM pairing requests at 3 per provider and avoid pairing replies for outbound DMs. — thanks @steipete
|
- Pairing: cap pending DM pairing requests at 3 per provider and avoid pairing replies for outbound DMs. — thanks @steipete
|
||||||
- macOS: replace relay smoke test with version check in packaging script. (#615) — thanks @YuriNachos
|
- macOS: replace relay smoke test with version check in packaging script. (#615) — thanks @YuriNachos
|
||||||
- macOS: avoid clearing Launch at Login during app initialization. (#607) — thanks @wes-davis
|
- macOS: avoid clearing Launch at Login during app initialization. (#607) — thanks @wes-davis
|
||||||
|
|||||||
@@ -74,6 +74,15 @@ describe("web inbound helpers", () => {
|
|||||||
expect(body).toBe("<contacts: Alice, Bob, Charlie +1 more>");
|
expect(body).toBe("<contacts: Alice, Bob, Charlie +1 more>");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("summarizes empty WhatsApp contact cards with a count", () => {
|
||||||
|
const body = extractText({
|
||||||
|
contactsArrayMessage: {
|
||||||
|
contacts: [{}, {}],
|
||||||
|
},
|
||||||
|
} as unknown as import("@whiskeysockets/baileys").proto.IMessage);
|
||||||
|
expect(body).toBe("<contacts: 2 contacts>");
|
||||||
|
});
|
||||||
|
|
||||||
it("unwraps view-once v2 extension messages", () => {
|
it("unwraps view-once v2 extension messages", () => {
|
||||||
const body = extractText({
|
const body = extractText({
|
||||||
viewOnceMessageV2Extension: {
|
viewOnceMessageV2Extension: {
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ import {
|
|||||||
getStatusCode,
|
getStatusCode,
|
||||||
waitForWaConnection,
|
waitForWaConnection,
|
||||||
} from "./session.js";
|
} from "./session.js";
|
||||||
|
import { parseVcard } from "./vcard.js";
|
||||||
|
|
||||||
export type WebListenerCloseReason = {
|
export type WebListenerCloseReason = {
|
||||||
status?: number;
|
status?: number;
|
||||||
@@ -789,49 +790,6 @@ function describeContact(input: {
|
|||||||
return { name, phone };
|
return { name, phone };
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseVcard(vcard?: string): { name?: string; phones: string[] } {
|
|
||||||
if (!vcard) return { phones: [] };
|
|
||||||
const lines = vcard.split(/\r?\n/);
|
|
||||||
let nameFromN: string | undefined;
|
|
||||||
let nameFromFn: string | undefined;
|
|
||||||
const phones: string[] = [];
|
|
||||||
for (const rawLine of lines) {
|
|
||||||
const line = rawLine.trim();
|
|
||||||
if (!line) continue;
|
|
||||||
const colonIndex = line.indexOf(":");
|
|
||||||
if (colonIndex === -1) continue;
|
|
||||||
const key = line.slice(0, colonIndex).toUpperCase();
|
|
||||||
const rawValue = line.slice(colonIndex + 1).trim();
|
|
||||||
if (!rawValue) continue;
|
|
||||||
const value = cleanVcardValue(rawValue);
|
|
||||||
if (!value) continue;
|
|
||||||
if (key === "FN" && !nameFromFn) {
|
|
||||||
nameFromFn = normalizeVcardName(value);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (key === "N" && !nameFromN) {
|
|
||||||
nameFromN = normalizeVcardName(value);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (key.startsWith("TEL") || key.includes(".TEL")) {
|
|
||||||
phones.push(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return { name: nameFromFn ?? nameFromN, phones };
|
|
||||||
}
|
|
||||||
|
|
||||||
function cleanVcardValue(value: string): string {
|
|
||||||
return value
|
|
||||||
.replace(/\\n/gi, " ")
|
|
||||||
.replace(/\\,/g, ",")
|
|
||||||
.replace(/\\;/g, ";")
|
|
||||||
.trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeVcardName(value: string): string {
|
|
||||||
return value.replace(/;/g, " ").replace(/\s+/g, " ").trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
function formatContactPlaceholder(name?: string, phone?: string): string {
|
function formatContactPlaceholder(name?: string, phone?: string): string {
|
||||||
const parts = [name, phone].filter((value): value is string =>
|
const parts = [name, phone].filter((value): value is string =>
|
||||||
Boolean(value),
|
Boolean(value),
|
||||||
@@ -842,7 +800,10 @@ function formatContactPlaceholder(name?: string, phone?: string): string {
|
|||||||
|
|
||||||
function formatContactsPlaceholder(labels: string[], total: number): string {
|
function formatContactsPlaceholder(labels: string[], total: number): string {
|
||||||
const cleaned = labels.map((label) => label.trim()).filter(Boolean);
|
const cleaned = labels.map((label) => label.trim()).filter(Boolean);
|
||||||
if (cleaned.length === 0) return "<contacts>";
|
if (cleaned.length === 0) {
|
||||||
|
const suffix = total === 1 ? "contact" : "contacts";
|
||||||
|
return `<contacts: ${total} ${suffix}>`;
|
||||||
|
}
|
||||||
const shown = cleaned.slice(0, 3);
|
const shown = cleaned.slice(0, 3);
|
||||||
const remaining = Math.max(total - shown.length, 0);
|
const remaining = Math.max(total - shown.length, 0);
|
||||||
const suffix = remaining > 0 ? ` +${remaining} more` : "";
|
const suffix = remaining > 0 ? ` +${remaining} more` : "";
|
||||||
|
|||||||
58
src/web/vcard.ts
Normal file
58
src/web/vcard.ts
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
type ParsedVcard = {
|
||||||
|
name?: string;
|
||||||
|
phones: string[];
|
||||||
|
};
|
||||||
|
|
||||||
|
const ALLOWED_VCARD_KEYS = new Set(["FN", "N", "TEL"]);
|
||||||
|
|
||||||
|
export function parseVcard(vcard?: string): ParsedVcard {
|
||||||
|
if (!vcard) return { phones: [] };
|
||||||
|
const lines = vcard.split(/\r?\n/);
|
||||||
|
let nameFromN: string | undefined;
|
||||||
|
let nameFromFn: string | undefined;
|
||||||
|
const phones: string[] = [];
|
||||||
|
for (const rawLine of lines) {
|
||||||
|
const line = rawLine.trim();
|
||||||
|
if (!line) continue;
|
||||||
|
const colonIndex = line.indexOf(":");
|
||||||
|
if (colonIndex === -1) continue;
|
||||||
|
const key = line.slice(0, colonIndex).toUpperCase();
|
||||||
|
const rawValue = line.slice(colonIndex + 1).trim();
|
||||||
|
if (!rawValue) continue;
|
||||||
|
const baseKey = normalizeVcardKey(key);
|
||||||
|
if (!baseKey || !ALLOWED_VCARD_KEYS.has(baseKey)) continue;
|
||||||
|
const value = cleanVcardValue(rawValue);
|
||||||
|
if (!value) continue;
|
||||||
|
if (baseKey === "FN" && !nameFromFn) {
|
||||||
|
nameFromFn = normalizeVcardName(value);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (baseKey === "N" && !nameFromN) {
|
||||||
|
nameFromN = normalizeVcardName(value);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (baseKey === "TEL") {
|
||||||
|
phones.push(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { name: nameFromFn ?? nameFromN, phones };
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizeVcardKey(key: string): string | undefined {
|
||||||
|
const [primary] = key.split(";");
|
||||||
|
if (!primary) return undefined;
|
||||||
|
const segments = primary.split(".");
|
||||||
|
return segments[segments.length - 1] || undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function cleanVcardValue(value: string): string {
|
||||||
|
return value
|
||||||
|
.replace(/\\n/gi, " ")
|
||||||
|
.replace(/\\,/g, ",")
|
||||||
|
.replace(/\\;/g, ";")
|
||||||
|
.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizeVcardName(value: string): string {
|
||||||
|
return value.replace(/;/g, " ").replace(/\s+/g, " ").trim();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user