From 54fc9ecf7d558e08e0cebaa6ca8e9e9dfc6987e9 Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Mon, 9 Mar 2026 03:55:10 -0400 Subject: [PATCH] Matrix: remove unused internal helpers --- .../matrix/src/matrix/account-config.ts | 4 +- .../matrix/src/matrix/actions/client.ts | 8 +- .../src/matrix/monitor/access-policy.ts | 126 ------------------ extensions/matrix/src/matrix/send/client.ts | 7 +- 4 files changed, 4 insertions(+), 141 deletions(-) delete mode 100644 extensions/matrix/src/matrix/monitor/access-policy.ts diff --git a/extensions/matrix/src/matrix/account-config.ts b/extensions/matrix/src/matrix/account-config.ts index 8b288989f1f..140dfb11d53 100644 --- a/extensions/matrix/src/matrix/account-config.ts +++ b/extensions/matrix/src/matrix/account-config.ts @@ -5,9 +5,7 @@ export function resolveMatrixBaseConfig(cfg: CoreConfig): MatrixConfig { return cfg.channels?.matrix ?? {}; } -export function resolveMatrixAccountsMap( - cfg: CoreConfig, -): Readonly> { +function resolveMatrixAccountsMap(cfg: CoreConfig): Readonly> { const accounts = resolveMatrixBaseConfig(cfg).accounts; if (!accounts || typeof accounts !== "object") { return {}; diff --git a/extensions/matrix/src/matrix/actions/client.ts b/extensions/matrix/src/matrix/actions/client.ts index dcb133e781c..cf11c494b8d 100644 --- a/extensions/matrix/src/matrix/actions/client.ts +++ b/extensions/matrix/src/matrix/actions/client.ts @@ -1,10 +1,6 @@ -import { ensureMatrixNodeRuntime, resolveRuntimeMatrixClient } from "../client-bootstrap.js"; +import { resolveRuntimeMatrixClient } from "../client-bootstrap.js"; import type { MatrixActionClient, MatrixActionClientOpts } from "./types.js"; -export function ensureNodeRuntime() { - ensureMatrixNodeRuntime(); -} - async function ensureActionClientReadiness( client: MatrixActionClient["client"], readiness: MatrixActionClientOpts["readiness"], @@ -34,7 +30,7 @@ export async function resolveActionClient( }); } -export type MatrixActionClientStopMode = "stop" | "persist"; +type MatrixActionClientStopMode = "stop" | "persist"; export async function stopActionClient( resolved: MatrixActionClient, diff --git a/extensions/matrix/src/matrix/monitor/access-policy.ts b/extensions/matrix/src/matrix/monitor/access-policy.ts deleted file mode 100644 index cace7070fd6..00000000000 --- a/extensions/matrix/src/matrix/monitor/access-policy.ts +++ /dev/null @@ -1,126 +0,0 @@ -import { - formatAllowlistMatchMeta, - issuePairingChallenge, - readStoreAllowFromForDmPolicy, - resolveDmGroupAccessWithLists, - resolveSenderScopedGroupPolicy, -} from "openclaw/plugin-sdk/matrix"; -import { - normalizeMatrixAllowList, - resolveMatrixAllowListMatch, - resolveMatrixAllowListMatches, -} from "./allowlist.js"; - -type MatrixDmPolicy = "open" | "pairing" | "allowlist" | "disabled"; -type MatrixGroupPolicy = "open" | "allowlist" | "disabled"; - -export async function resolveMatrixAccessState(params: { - isDirectMessage: boolean; - resolvedAccountId: string; - dmPolicy: MatrixDmPolicy; - groupPolicy: MatrixGroupPolicy; - allowFrom: string[]; - groupAllowFrom: Array; - senderId: string; - readStoreForDmPolicy: (provider: string, accountId: string) => Promise; -}) { - const storeAllowFrom = params.isDirectMessage - ? await readStoreAllowFromForDmPolicy({ - provider: "matrix", - accountId: params.resolvedAccountId, - dmPolicy: params.dmPolicy, - readStore: params.readStoreForDmPolicy, - }) - : []; - const normalizedGroupAllowFrom = normalizeMatrixAllowList(params.groupAllowFrom); - const senderGroupPolicy = resolveSenderScopedGroupPolicy({ - groupPolicy: params.groupPolicy, - groupAllowFrom: normalizedGroupAllowFrom, - }); - const access = resolveDmGroupAccessWithLists({ - isGroup: !params.isDirectMessage, - dmPolicy: params.dmPolicy, - groupPolicy: senderGroupPolicy, - allowFrom: params.allowFrom, - groupAllowFrom: normalizedGroupAllowFrom, - storeAllowFrom, - groupAllowFromFallbackToAllowFrom: false, - isSenderAllowed: (allowFrom) => - resolveMatrixAllowListMatches({ - allowList: normalizeMatrixAllowList(allowFrom), - userId: params.senderId, - }), - }); - const effectiveAllowFrom = normalizeMatrixAllowList(access.effectiveAllowFrom); - const effectiveGroupAllowFrom = normalizeMatrixAllowList(access.effectiveGroupAllowFrom); - return { - access, - effectiveAllowFrom, - effectiveGroupAllowFrom, - groupAllowConfigured: effectiveGroupAllowFrom.length > 0, - }; -} - -export async function enforceMatrixDirectMessageAccess(params: { - dmEnabled: boolean; - dmPolicy: MatrixDmPolicy; - accessDecision: "allow" | "block" | "pairing"; - senderId: string; - senderName: string; - effectiveAllowFrom: string[]; - upsertPairingRequest: (input: { - id: string; - meta?: Record; - }) => Promise<{ - code: string; - created: boolean; - }>; - sendPairingReply: (text: string) => Promise; - logVerboseMessage: (message: string) => void; -}): Promise { - if (!params.dmEnabled) { - return false; - } - if (params.accessDecision === "allow") { - return true; - } - const allowMatch = resolveMatrixAllowListMatch({ - allowList: params.effectiveAllowFrom, - userId: params.senderId, - }); - const allowMatchMeta = formatAllowlistMatchMeta(allowMatch); - if (params.accessDecision === "pairing") { - await issuePairingChallenge({ - channel: "matrix", - senderId: params.senderId, - senderIdLine: `Matrix user id: ${params.senderId}`, - meta: { name: params.senderName }, - upsertPairingRequest: params.upsertPairingRequest, - buildReplyText: ({ code }) => - [ - "OpenClaw: access not configured.", - "", - `Pairing code: ${code}`, - "", - "Ask the bot owner to approve with:", - "openclaw pairing approve matrix ", - ].join("\n"), - sendPairingReply: params.sendPairingReply, - onCreated: () => { - params.logVerboseMessage( - `matrix pairing request sender=${params.senderId} name=${params.senderName ?? "unknown"} (${allowMatchMeta})`, - ); - }, - onReplyError: (err) => { - params.logVerboseMessage( - `matrix pairing reply failed for ${params.senderId}: ${String(err)}`, - ); - }, - }); - return false; - } - params.logVerboseMessage( - `matrix: blocked dm sender ${params.senderId} (dmPolicy=${params.dmPolicy}, ${allowMatchMeta})`, - ); - return false; -} diff --git a/extensions/matrix/src/matrix/send/client.ts b/extensions/matrix/src/matrix/send/client.ts index 2a886e9b969..680ba3f6c1d 100644 --- a/extensions/matrix/src/matrix/send/client.ts +++ b/extensions/matrix/src/matrix/send/client.ts @@ -2,7 +2,6 @@ import { getMatrixRuntime } from "../../runtime.js"; import type { CoreConfig } from "../../types.js"; import { resolveMatrixAccountConfig } from "../accounts.js"; import { - ensureMatrixNodeRuntime, resolveRuntimeMatrixClient, type ResolvedRuntimeMatrixClient, } from "../client-bootstrap.js"; @@ -10,10 +9,6 @@ import type { MatrixClient } from "../sdk.js"; const getCore = () => getMatrixRuntime(); -export function ensureNodeRuntime() { - ensureMatrixNodeRuntime(); -} - export function resolveMediaMaxBytes(accountId?: string | null): number | undefined { const cfg = getCore().config.loadConfig() as CoreConfig; const matrixCfg = resolveMatrixAccountConfig({ cfg, accountId }); @@ -41,7 +36,7 @@ export async function resolveMatrixClient(opts: { }); } -export function stopResolvedMatrixClient(resolved: ResolvedRuntimeMatrixClient): void { +function stopResolvedMatrixClient(resolved: ResolvedRuntimeMatrixClient): void { if (resolved.stopOnDone) { resolved.client.stop(); }