mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 05:50:43 +00:00
refactor: generalize route target parsing
This commit is contained in:
@@ -7,63 +7,63 @@ afterEach(() => {
|
||||
__testing.setDepsForTest();
|
||||
});
|
||||
|
||||
describe("resolveAnnounceOrigin telegram forum topics", () => {
|
||||
it("preserves stored forum topic thread ids when requester origin omits one for the same chat", () => {
|
||||
describe("resolveAnnounceOrigin threaded route targets", () => {
|
||||
it("preserves stored thread ids when requester origin omits one for the same chat", () => {
|
||||
expect(
|
||||
resolveAnnounceOrigin(
|
||||
{
|
||||
lastChannel: "telegram",
|
||||
lastTo: "telegram:-1001234567890:topic:99",
|
||||
lastChannel: "topicchat",
|
||||
lastTo: "topicchat:room-a:topic:99",
|
||||
lastThreadId: 99,
|
||||
},
|
||||
{
|
||||
channel: "telegram",
|
||||
to: "telegram:-1001234567890",
|
||||
channel: "topicchat",
|
||||
to: "topicchat:room-a",
|
||||
},
|
||||
),
|
||||
).toEqual({
|
||||
channel: "telegram",
|
||||
to: "telegram:-1001234567890",
|
||||
channel: "topicchat",
|
||||
to: "topicchat:room-a",
|
||||
threadId: 99,
|
||||
});
|
||||
});
|
||||
|
||||
it("preserves stored forum topic thread ids for legacy group-prefixed requester targets", () => {
|
||||
it("preserves stored thread ids for group-prefixed requester targets", () => {
|
||||
expect(
|
||||
resolveAnnounceOrigin(
|
||||
{
|
||||
lastChannel: "telegram",
|
||||
lastTo: "telegram:-1001234567890:topic:99",
|
||||
lastChannel: "topicchat",
|
||||
lastTo: "topicchat:room-a:topic:99",
|
||||
lastThreadId: 99,
|
||||
},
|
||||
{
|
||||
channel: "telegram",
|
||||
to: "group:-1001234567890",
|
||||
channel: "topicchat",
|
||||
to: "group:room-a",
|
||||
},
|
||||
),
|
||||
).toEqual({
|
||||
channel: "telegram",
|
||||
to: "group:-1001234567890",
|
||||
channel: "topicchat",
|
||||
to: "group:room-a",
|
||||
threadId: 99,
|
||||
});
|
||||
});
|
||||
|
||||
it("still strips stale thread ids when the stored telegram route points at a different chat", () => {
|
||||
it("still strips stale thread ids when the stored route points at a different chat", () => {
|
||||
expect(
|
||||
resolveAnnounceOrigin(
|
||||
{
|
||||
lastChannel: "telegram",
|
||||
lastTo: "telegram:-1009999999999:topic:99",
|
||||
lastChannel: "topicchat",
|
||||
lastTo: "topicchat:room-b:topic:99",
|
||||
lastThreadId: 99,
|
||||
},
|
||||
{
|
||||
channel: "telegram",
|
||||
to: "telegram:-1001234567890",
|
||||
channel: "topicchat",
|
||||
to: "topicchat:room-a",
|
||||
},
|
||||
),
|
||||
).toEqual({
|
||||
channel: "telegram",
|
||||
to: "telegram:-1001234567890",
|
||||
channel: "topicchat",
|
||||
to: "topicchat:room-a",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { resolveComparableTargetForLoadedChannel } from "../channels/plugins/target-parsing-loaded.js";
|
||||
import {
|
||||
normalizeOptionalLowercaseString,
|
||||
normalizeOptionalString,
|
||||
@@ -93,20 +94,31 @@ function deliveryContextFromSession(entry?: DeliveryContextSource): DeliveryCont
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeTelegramAnnounceTarget(target: string | undefined): string | undefined {
|
||||
const trimmed = target?.trim();
|
||||
if (!trimmed) {
|
||||
function stripThreadRouteSuffix(target: string): string {
|
||||
return /^(.*):topic:[^:]+$/u.exec(target)?.[1] ?? target;
|
||||
}
|
||||
|
||||
function normalizeAnnounceRouteTarget(context?: DeliveryContext): string | undefined {
|
||||
const rawTo = normalizeOptionalString(context?.to);
|
||||
if (!rawTo) {
|
||||
return undefined;
|
||||
}
|
||||
if (trimmed.startsWith("group:")) {
|
||||
return `telegram:${trimmed.slice("group:".length)}`;
|
||||
const channel = normalizeOptionalLowercaseString(context?.channel);
|
||||
const parsed = channel
|
||||
? resolveComparableTargetForLoadedChannel({
|
||||
channel,
|
||||
rawTarget: rawTo,
|
||||
fallbackThreadId: context?.threadId,
|
||||
})
|
||||
: null;
|
||||
let route = stripThreadRouteSuffix(parsed?.to ?? rawTo);
|
||||
if (channel && route.toLowerCase().startsWith(`${channel}:`)) {
|
||||
route = route.slice(channel.length + 1);
|
||||
}
|
||||
if (!trimmed.startsWith("telegram:")) {
|
||||
return undefined;
|
||||
if (route.startsWith("group:") || route.startsWith("channel:")) {
|
||||
route = route.slice(route.indexOf(":") + 1);
|
||||
}
|
||||
const raw = trimmed.slice("telegram:".length);
|
||||
const topicMatch = /^(.*):topic:[^:]+$/u.exec(raw);
|
||||
return `telegram:${topicMatch?.[1] ?? raw}`;
|
||||
return route || undefined;
|
||||
}
|
||||
|
||||
function shouldStripThreadFromAnnounceEntry(
|
||||
@@ -120,16 +132,8 @@ function shouldStripThreadFromAnnounceEntry(
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
const requesterChannel = normalizeOptionalLowercaseString(normalizedRequester.channel);
|
||||
if (requesterChannel === "telegram") {
|
||||
const requesterTarget = normalizeTelegramAnnounceTarget(normalizedRequester.to);
|
||||
const entryTarget = normalizeTelegramAnnounceTarget(normalizedEntry?.to);
|
||||
if (requesterTarget && entryTarget) {
|
||||
return requesterTarget !== entryTarget;
|
||||
}
|
||||
}
|
||||
const requesterTarget = normalizeOptionalString(normalizedRequester.to);
|
||||
const entryTarget = normalizeOptionalString(normalizedEntry?.to);
|
||||
const requesterTarget = normalizeAnnounceRouteTarget(normalizedRequester);
|
||||
const entryTarget = normalizeAnnounceRouteTarget(normalizedEntry);
|
||||
if (requesterTarget && entryTarget) {
|
||||
return requesterTarget !== entryTarget;
|
||||
}
|
||||
|
||||
@@ -14,14 +14,5 @@ export function extractSimpleExplicitGroupId(raw: string | undefined | null): st
|
||||
const joined = parts.slice(1).join(":");
|
||||
return joined.replace(/:topic:.*$/, "") || undefined;
|
||||
}
|
||||
if (parts.length >= 2 && parts[0] === "whatsapp") {
|
||||
const joined = parts
|
||||
.slice(1)
|
||||
.join(":")
|
||||
.replace(/:topic:.*$/, "");
|
||||
if (/@g\.us$/i.test(joined)) {
|
||||
return joined || undefined;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -9,22 +9,22 @@ describe("extractSimpleExplicitGroupId", () => {
|
||||
expect(extractSimpleExplicitGroupId(" ")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("extracts group ID from telegram group format", () => {
|
||||
expect(extractSimpleExplicitGroupId("telegram:group:-1003776849159")).toBe("-1003776849159");
|
||||
it("extracts group ID from provider group format", () => {
|
||||
expect(extractSimpleExplicitGroupId("chat:group:-1003776849159")).toBe("-1003776849159");
|
||||
});
|
||||
|
||||
it("extracts group ID from telegram forum topic format, stripping topic suffix", () => {
|
||||
expect(extractSimpleExplicitGroupId("telegram:group:-1003776849159:topic:1264")).toBe(
|
||||
it("extracts group ID from provider topic format, stripping topic suffix", () => {
|
||||
expect(extractSimpleExplicitGroupId("chat:group:-1003776849159:topic:1264")).toBe(
|
||||
"-1003776849159",
|
||||
);
|
||||
});
|
||||
|
||||
it("extracts group ID from channel format", () => {
|
||||
expect(extractSimpleExplicitGroupId("telegram:channel:-1001234567890")).toBe("-1001234567890");
|
||||
expect(extractSimpleExplicitGroupId("chat:channel:-1001234567890")).toBe("-1001234567890");
|
||||
});
|
||||
|
||||
it("extracts group ID from channel format with topic", () => {
|
||||
expect(extractSimpleExplicitGroupId("telegram:channel:-1001234567890:topic:42")).toBe(
|
||||
expect(extractSimpleExplicitGroupId("chat:channel:-1001234567890:topic:42")).toBe(
|
||||
"-1001234567890",
|
||||
);
|
||||
});
|
||||
@@ -37,12 +37,6 @@ describe("extractSimpleExplicitGroupId", () => {
|
||||
expect(extractSimpleExplicitGroupId("group:-1003776849159:topic:999")).toBe("-1003776849159");
|
||||
});
|
||||
|
||||
it("extracts WhatsApp group ID", () => {
|
||||
expect(extractSimpleExplicitGroupId("whatsapp:120363123456789@g.us")).toBe(
|
||||
"120363123456789@g.us",
|
||||
);
|
||||
});
|
||||
|
||||
it("returns undefined for unrecognized formats", () => {
|
||||
expect(extractSimpleExplicitGroupId("user:12345")).toBeUndefined();
|
||||
expect(extractSimpleExplicitGroupId("just-a-string")).toBeUndefined();
|
||||
|
||||
Reference in New Issue
Block a user