Matrix: use account-scoped auto-join settings

This commit is contained in:
Gustavo Madeira Santana
2026-03-11 21:53:24 +00:00
parent 238ff2def9
commit 0e71120160
5 changed files with 49 additions and 34 deletions

View File

@@ -6,7 +6,9 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import { setMatrixRuntime } from "../../runtime.js";
import { maybeMigrateLegacyStorage, resolveMatrixStoragePaths } from "./storage.js";
const maybeCreateMatrixMigrationSnapshotMock = vi.hoisted(() => vi.fn(async () => undefined));
const maybeCreateMatrixMigrationSnapshotMock = vi.hoisted(() =>
vi.fn(async (_params: unknown) => undefined),
);
vi.mock("openclaw/plugin-sdk/matrix", async (importOriginal) => {
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/matrix")>();

View File

@@ -1,7 +1,7 @@
import type { PluginRuntime } from "openclaw/plugin-sdk/matrix";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { setMatrixRuntime } from "../../runtime.js";
import type { CoreConfig } from "../../types.js";
import type { MatrixConfig } from "../../types.js";
import { registerMatrixAutoJoin } from "./auto-join.js";
type InviteHandler = (roomId: string, inviteEvent: unknown) => Promise<void>;
@@ -39,17 +39,13 @@ describe("registerMatrixAutoJoin", () => {
it("joins all invites when autoJoin=always", async () => {
const { client, getInviteHandler, joinRoom } = createClientStub();
const cfg: CoreConfig = {
channels: {
matrix: {
autoJoin: "always",
},
},
const accountConfig: MatrixConfig = {
autoJoin: "always",
};
registerMatrixAutoJoin({
client,
cfg,
accountConfig,
runtime: {
log: vi.fn(),
error: vi.fn(),
@@ -69,18 +65,14 @@ describe("registerMatrixAutoJoin", () => {
alias: "#other:example.org",
alt_aliases: ["#else:example.org"],
});
const cfg: CoreConfig = {
channels: {
matrix: {
autoJoin: "allowlist",
autoJoinAllowlist: ["#allowed:example.org"],
},
},
const accountConfig: MatrixConfig = {
autoJoin: "allowlist",
autoJoinAllowlist: ["#allowed:example.org"],
};
registerMatrixAutoJoin({
client,
cfg,
accountConfig,
runtime: {
log: vi.fn(),
error: vi.fn(),
@@ -100,18 +92,40 @@ describe("registerMatrixAutoJoin", () => {
alias: "#allowed:example.org",
alt_aliases: ["#backup:example.org"],
});
const cfg: CoreConfig = {
channels: {
matrix: {
autoJoin: "allowlist",
autoJoinAllowlist: [" #allowed:example.org "],
},
},
const accountConfig: MatrixConfig = {
autoJoin: "allowlist",
autoJoinAllowlist: [" #allowed:example.org "],
};
registerMatrixAutoJoin({
client,
cfg,
accountConfig,
runtime: {
log: vi.fn(),
error: vi.fn(),
} as unknown as import("openclaw/plugin-sdk/matrix").RuntimeEnv,
});
const inviteHandler = getInviteHandler();
expect(inviteHandler).toBeTruthy();
await inviteHandler!("!room:example.org", {});
expect(joinRoom).toHaveBeenCalledWith("!room:example.org");
});
it("uses account-scoped auto-join settings for non-default accounts", async () => {
const { client, getInviteHandler, joinRoom, getRoomStateEvent } = createClientStub();
getRoomStateEvent.mockResolvedValue({
alias: "#ops-allowed:example.org",
alt_aliases: [],
});
registerMatrixAutoJoin({
client,
accountConfig: {
autoJoin: "allowlist",
autoJoinAllowlist: ["#ops-allowed:example.org"],
},
runtime: {
log: vi.fn(),
error: vi.fn(),

View File

@@ -1,14 +1,14 @@
import type { RuntimeEnv } from "openclaw/plugin-sdk/matrix";
import { getMatrixRuntime } from "../../runtime.js";
import type { CoreConfig } from "../../types.js";
import type { MatrixConfig } from "../../types.js";
import type { MatrixClient } from "../sdk.js";
export function registerMatrixAutoJoin(params: {
client: MatrixClient;
cfg: CoreConfig;
accountConfig: Pick<MatrixConfig, "autoJoin" | "autoJoinAllowlist">;
runtime: RuntimeEnv;
}) {
const { client, cfg, runtime } = params;
const { client, accountConfig, runtime } = params;
const core = getMatrixRuntime();
const logVerbose = (message: string) => {
if (!core.logging.shouldLogVerbose()) {
@@ -16,11 +16,9 @@ export function registerMatrixAutoJoin(params: {
}
runtime.log?.(message);
};
const autoJoin = cfg.channels?.["matrix"]?.autoJoin ?? "always";
const autoJoin = accountConfig.autoJoin ?? "always";
const autoJoinAllowlist = new Set(
(cfg.channels?.["matrix"]?.autoJoinAllowlist ?? [])
.map((entry) => String(entry).trim())
.filter(Boolean),
(accountConfig.autoJoinAllowlist ?? []).map((entry) => String(entry).trim()).filter(Boolean),
);
if (autoJoin === "off") {

View File

@@ -164,7 +164,7 @@ export async function monitorMatrixProvider(opts: MonitorMatrixOpts = {}): Promi
const startupMs = Date.now();
const startupGraceMs = 0;
const directTracker = createDirectRoomTracker(client, { log: logVerboseMessage });
registerMatrixAutoJoin({ client, cfg, runtime });
registerMatrixAutoJoin({ client, accountConfig, runtime });
const warnedEncryptedRooms = new Set<string>();
const warnedCryptoMissingRooms = new Set<string>();

View File

@@ -76,7 +76,8 @@ async function readDatabaseRecords(params: {
return;
}
db.close();
resolve(keys.map((key, index) => ({ key, value: values[index] })));
const resolvedValues = values;
resolve(keys.map((key, index) => ({ key, value: resolvedValues[index] })));
};
keysReq.onsuccess = () => {