mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-12 07:20:45 +00:00
Matrix: share room config normalization
This commit is contained in:
@@ -205,6 +205,34 @@ describe("matrix directory", () => {
|
||||
groupId: "!room:example.org",
|
||||
}),
|
||||
).toBe(false);
|
||||
|
||||
expect(
|
||||
matrixPlugin.groups!.resolveRequireMention!({
|
||||
cfg,
|
||||
accountId: "assistant",
|
||||
groupId: "matrix:room:!room:example.org",
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("matches prefixed Matrix aliases in group context", () => {
|
||||
const cfg = {
|
||||
channels: {
|
||||
matrix: {
|
||||
groups: {
|
||||
"#ops:example.org": { requireMention: false },
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as CoreConfig;
|
||||
|
||||
expect(
|
||||
matrixPlugin.groups!.resolveRequireMention!({
|
||||
cfg,
|
||||
groupId: "matrix:room:!room:example.org",
|
||||
groupChannel: "matrix:channel:#ops:example.org",
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("reports room access warnings against the active Matrix config path", () => {
|
||||
|
||||
@@ -1,30 +1,19 @@
|
||||
import type { ChannelGroupContext, GroupToolPolicyConfig } from "openclaw/plugin-sdk/matrix";
|
||||
import { resolveMatrixAccountConfig } from "./matrix/accounts.js";
|
||||
import { resolveMatrixRoomConfig } from "./matrix/monitor/rooms.js";
|
||||
import { normalizeMatrixResolvableTarget } from "./matrix/target-ids.js";
|
||||
import type { CoreConfig } from "./types.js";
|
||||
|
||||
function stripLeadingPrefixCaseInsensitive(value: string, prefix: string): string {
|
||||
return value.toLowerCase().startsWith(prefix.toLowerCase())
|
||||
? value.slice(prefix.length).trim()
|
||||
: value;
|
||||
}
|
||||
|
||||
function resolveMatrixRoomConfigForGroup(params: ChannelGroupContext) {
|
||||
const rawGroupId = params.groupId?.trim() ?? "";
|
||||
let roomId = rawGroupId;
|
||||
roomId = stripLeadingPrefixCaseInsensitive(roomId, "matrix:");
|
||||
roomId = stripLeadingPrefixCaseInsensitive(roomId, "channel:");
|
||||
roomId = stripLeadingPrefixCaseInsensitive(roomId, "room:");
|
||||
|
||||
const roomId = normalizeMatrixResolvableTarget(params.groupId?.trim() ?? "");
|
||||
const groupChannel = params.groupChannel?.trim() ?? "";
|
||||
const aliases = groupChannel ? [groupChannel] : [];
|
||||
const aliases = groupChannel ? [normalizeMatrixResolvableTarget(groupChannel)] : [];
|
||||
const cfg = params.cfg as CoreConfig;
|
||||
const matrixConfig = resolveMatrixAccountConfig({ cfg, accountId: params.accountId });
|
||||
return resolveMatrixRoomConfig({
|
||||
rooms: matrixConfig.groups ?? matrixConfig.rooms,
|
||||
roomId,
|
||||
aliases,
|
||||
name: groupChannel || undefined,
|
||||
}).config;
|
||||
}
|
||||
|
||||
|
||||
@@ -259,7 +259,6 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam
|
||||
rooms: roomsConfig,
|
||||
roomId,
|
||||
aliases: roomAliases,
|
||||
name: roomName,
|
||||
})
|
||||
: undefined;
|
||||
const roomConfig = roomConfigInfo?.config;
|
||||
|
||||
@@ -13,7 +13,6 @@ describe("resolveMatrixRoomConfig", () => {
|
||||
rooms,
|
||||
roomId: "!room:example.org",
|
||||
aliases: [],
|
||||
name: "Project Room",
|
||||
});
|
||||
expect(byId.allowed).toBe(true);
|
||||
expect(byId.matchKey).toBe("!room:example.org");
|
||||
@@ -22,7 +21,6 @@ describe("resolveMatrixRoomConfig", () => {
|
||||
rooms,
|
||||
roomId: "!other:example.org",
|
||||
aliases: ["#alias:example.org"],
|
||||
name: "Other Room",
|
||||
});
|
||||
expect(byAlias.allowed).toBe(true);
|
||||
expect(byAlias.matchKey).toBe("#alias:example.org");
|
||||
@@ -31,7 +29,6 @@ describe("resolveMatrixRoomConfig", () => {
|
||||
rooms: { "Project Room": { allow: true } },
|
||||
roomId: "!different:example.org",
|
||||
aliases: [],
|
||||
name: "Project Room",
|
||||
});
|
||||
expect(byName.allowed).toBe(false);
|
||||
expect(byName.config).toBeUndefined();
|
||||
|
||||
@@ -13,7 +13,6 @@ export function resolveMatrixRoomConfig(params: {
|
||||
rooms?: Record<string, MatrixRoomConfig>;
|
||||
roomId: string;
|
||||
aliases: string[];
|
||||
name?: string | null;
|
||||
}): MatrixRoomConfigResolved {
|
||||
const rooms = params.rooms ?? {};
|
||||
const keys = Object.keys(rooms);
|
||||
|
||||
Reference in New Issue
Block a user