Feishu: honor group/dm prefixes in target parsing

This commit is contained in:
liuxiaopai-ai
2026-03-02 18:48:36 +08:00
committed by Peter Steinberger
parent 12be9a08fe
commit 07b419a0e7
2 changed files with 34 additions and 1 deletions

View File

@@ -13,6 +13,10 @@ describe("resolveReceiveIdType", () => {
it("defaults unprefixed IDs to user_id", () => {
expect(resolveReceiveIdType("u_123")).toBe("user_id");
});
it("treats explicit group targets as chat_id", () => {
expect(resolveReceiveIdType("group:oc_123")).toBe("chat_id");
});
});
describe("normalizeFeishuTarget", () => {
@@ -25,9 +29,17 @@ describe("normalizeFeishuTarget", () => {
expect(normalizeFeishuTarget("feishu:chat:oc_123")).toBe("oc_123");
});
it("strips provider and group prefixes", () => {
expect(normalizeFeishuTarget("feishu:group:oc_123")).toBe("oc_123");
});
it("accepts provider-prefixed raw ids", () => {
expect(normalizeFeishuTarget("feishu:ou_123")).toBe("ou_123");
});
it("strips provider and dm prefixes", () => {
expect(normalizeFeishuTarget("lark:dm:ou_123")).toBe("ou_123");
});
});
describe("looksLikeFeishuId", () => {
@@ -38,4 +50,8 @@ describe("looksLikeFeishuId", () => {
it("accepts provider-prefixed chat targets", () => {
expect(looksLikeFeishuId("lark:chat:oc_123")).toBe(true);
});
it("accepts provider-prefixed group targets", () => {
expect(looksLikeFeishuId("feishu:group:oc_123")).toBe(true);
});
});

View File

@@ -33,9 +33,15 @@ export function normalizeFeishuTarget(raw: string): string | null {
if (lowered.startsWith("chat:")) {
return withoutProvider.slice("chat:".length).trim() || null;
}
if (lowered.startsWith("group:")) {
return withoutProvider.slice("group:".length).trim() || null;
}
if (lowered.startsWith("user:")) {
return withoutProvider.slice("user:".length).trim() || null;
}
if (lowered.startsWith("dm:")) {
return withoutProvider.slice("dm:".length).trim() || null;
}
if (lowered.startsWith("open_id:")) {
return withoutProvider.slice("open_id:".length).trim() || null;
}
@@ -56,6 +62,17 @@ export function formatFeishuTarget(id: string, type?: FeishuIdType): string {
export function resolveReceiveIdType(id: string): "chat_id" | "open_id" | "user_id" {
const trimmed = id.trim();
const lowered = trimmed.toLowerCase();
if (lowered.startsWith("chat:") || lowered.startsWith("group:")) {
return "chat_id";
}
if (lowered.startsWith("open_id:")) {
return "open_id";
}
if (lowered.startsWith("user:") || lowered.startsWith("dm:")) {
const normalized = trimmed.replace(/^(user|dm):/i, "").trim();
return normalized.startsWith(OPEN_ID_PREFIX) ? "open_id" : "user_id";
}
if (trimmed.startsWith(CHAT_ID_PREFIX)) {
return "chat_id";
}
@@ -70,7 +87,7 @@ export function looksLikeFeishuId(raw: string): boolean {
if (!trimmed) {
return false;
}
if (/^(chat|user|open_id):/i.test(trimmed)) {
if (/^(chat|group|user|dm|open_id):/i.test(trimmed)) {
return true;
}
if (trimmed.startsWith(CHAT_ID_PREFIX)) {