mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 22:28:27 +00:00
fix(imessage): normalize messaging targets (#1708)
Co-authored-by: Aaron Ng <1653630+aaronn@users.noreply.github.com>
This commit is contained in:
15
src/channels/plugins/normalize/imessage.test.ts
Normal file
15
src/channels/plugins/normalize/imessage.test.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { normalizeIMessageMessagingTarget } from "./imessage.js";
|
||||
|
||||
describe("imessage target normalization", () => {
|
||||
it("preserves service prefixes for handles", () => {
|
||||
expect(normalizeIMessageMessagingTarget("sms:+1 (555) 222-3333")).toBe("sms:+15552223333");
|
||||
});
|
||||
|
||||
it("drops service prefixes for chat targets", () => {
|
||||
expect(normalizeIMessageMessagingTarget("sms:chat_id:123")).toBe("chat_id:123");
|
||||
expect(normalizeIMessageMessagingTarget("imessage:CHAT_GUID:abc")).toBe("chat_guid:abc");
|
||||
expect(normalizeIMessageMessagingTarget("auto:ChatIdentifier:foo")).toBe("chat_identifier:foo");
|
||||
});
|
||||
});
|
||||
35
src/channels/plugins/normalize/imessage.ts
Normal file
35
src/channels/plugins/normalize/imessage.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { normalizeIMessageHandle } from "../../../imessage/targets.js";
|
||||
|
||||
// Service prefixes that indicate explicit delivery method; must be preserved during normalization
|
||||
const SERVICE_PREFIXES = ["imessage:", "sms:", "auto:"] as const;
|
||||
const CHAT_TARGET_PREFIX_RE =
|
||||
/^(chat_id:|chatid:|chat:|chat_guid:|chatguid:|guid:|chat_identifier:|chatidentifier:|chatident:)/i;
|
||||
|
||||
export function normalizeIMessageMessagingTarget(raw: string): string | undefined {
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) return undefined;
|
||||
|
||||
// Preserve service prefix if present (e.g., "sms:+1555" → "sms:+15551234567")
|
||||
const lower = trimmed.toLowerCase();
|
||||
for (const prefix of SERVICE_PREFIXES) {
|
||||
if (lower.startsWith(prefix)) {
|
||||
const remainder = trimmed.slice(prefix.length).trim();
|
||||
const normalizedHandle = normalizeIMessageHandle(remainder);
|
||||
if (!normalizedHandle) return undefined;
|
||||
if (CHAT_TARGET_PREFIX_RE.test(normalizedHandle)) return normalizedHandle;
|
||||
return `${prefix}${normalizedHandle}`;
|
||||
}
|
||||
}
|
||||
|
||||
const normalized = normalizeIMessageHandle(trimmed);
|
||||
return normalized || undefined;
|
||||
}
|
||||
|
||||
export function looksLikeIMessageTargetId(raw: string): boolean {
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) return false;
|
||||
if (/^(imessage:|sms:|auto:)/i.test(trimmed)) return true;
|
||||
if (CHAT_TARGET_PREFIX_RE.test(trimmed)) return true;
|
||||
if (trimmed.includes("@")) return true;
|
||||
return /^\+?\d{3,}$/.test(trimmed);
|
||||
}
|
||||
Reference in New Issue
Block a user