mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-12 07:20:45 +00:00
Matrix: use account-scoped auto-join settings
This commit is contained in:
@@ -6,7 +6,9 @@ import { afterEach, describe, expect, it, vi } from "vitest";
|
|||||||
import { setMatrixRuntime } from "../../runtime.js";
|
import { setMatrixRuntime } from "../../runtime.js";
|
||||||
import { maybeMigrateLegacyStorage, resolveMatrixStoragePaths } from "./storage.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) => {
|
vi.mock("openclaw/plugin-sdk/matrix", async (importOriginal) => {
|
||||||
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/matrix")>();
|
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/matrix")>();
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { PluginRuntime } from "openclaw/plugin-sdk/matrix";
|
import type { PluginRuntime } from "openclaw/plugin-sdk/matrix";
|
||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import { setMatrixRuntime } from "../../runtime.js";
|
import { setMatrixRuntime } from "../../runtime.js";
|
||||||
import type { CoreConfig } from "../../types.js";
|
import type { MatrixConfig } from "../../types.js";
|
||||||
import { registerMatrixAutoJoin } from "./auto-join.js";
|
import { registerMatrixAutoJoin } from "./auto-join.js";
|
||||||
|
|
||||||
type InviteHandler = (roomId: string, inviteEvent: unknown) => Promise<void>;
|
type InviteHandler = (roomId: string, inviteEvent: unknown) => Promise<void>;
|
||||||
@@ -39,17 +39,13 @@ describe("registerMatrixAutoJoin", () => {
|
|||||||
|
|
||||||
it("joins all invites when autoJoin=always", async () => {
|
it("joins all invites when autoJoin=always", async () => {
|
||||||
const { client, getInviteHandler, joinRoom } = createClientStub();
|
const { client, getInviteHandler, joinRoom } = createClientStub();
|
||||||
const cfg: CoreConfig = {
|
const accountConfig: MatrixConfig = {
|
||||||
channels: {
|
autoJoin: "always",
|
||||||
matrix: {
|
|
||||||
autoJoin: "always",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
registerMatrixAutoJoin({
|
registerMatrixAutoJoin({
|
||||||
client,
|
client,
|
||||||
cfg,
|
accountConfig,
|
||||||
runtime: {
|
runtime: {
|
||||||
log: vi.fn(),
|
log: vi.fn(),
|
||||||
error: vi.fn(),
|
error: vi.fn(),
|
||||||
@@ -69,18 +65,14 @@ describe("registerMatrixAutoJoin", () => {
|
|||||||
alias: "#other:example.org",
|
alias: "#other:example.org",
|
||||||
alt_aliases: ["#else:example.org"],
|
alt_aliases: ["#else:example.org"],
|
||||||
});
|
});
|
||||||
const cfg: CoreConfig = {
|
const accountConfig: MatrixConfig = {
|
||||||
channels: {
|
autoJoin: "allowlist",
|
||||||
matrix: {
|
autoJoinAllowlist: ["#allowed:example.org"],
|
||||||
autoJoin: "allowlist",
|
|
||||||
autoJoinAllowlist: ["#allowed:example.org"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
registerMatrixAutoJoin({
|
registerMatrixAutoJoin({
|
||||||
client,
|
client,
|
||||||
cfg,
|
accountConfig,
|
||||||
runtime: {
|
runtime: {
|
||||||
log: vi.fn(),
|
log: vi.fn(),
|
||||||
error: vi.fn(),
|
error: vi.fn(),
|
||||||
@@ -100,18 +92,40 @@ describe("registerMatrixAutoJoin", () => {
|
|||||||
alias: "#allowed:example.org",
|
alias: "#allowed:example.org",
|
||||||
alt_aliases: ["#backup:example.org"],
|
alt_aliases: ["#backup:example.org"],
|
||||||
});
|
});
|
||||||
const cfg: CoreConfig = {
|
const accountConfig: MatrixConfig = {
|
||||||
channels: {
|
autoJoin: "allowlist",
|
||||||
matrix: {
|
autoJoinAllowlist: [" #allowed:example.org "],
|
||||||
autoJoin: "allowlist",
|
|
||||||
autoJoinAllowlist: [" #allowed:example.org "],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
registerMatrixAutoJoin({
|
registerMatrixAutoJoin({
|
||||||
client,
|
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: {
|
runtime: {
|
||||||
log: vi.fn(),
|
log: vi.fn(),
|
||||||
error: vi.fn(),
|
error: vi.fn(),
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
import type { RuntimeEnv } from "openclaw/plugin-sdk/matrix";
|
import type { RuntimeEnv } from "openclaw/plugin-sdk/matrix";
|
||||||
import { getMatrixRuntime } from "../../runtime.js";
|
import { getMatrixRuntime } from "../../runtime.js";
|
||||||
import type { CoreConfig } from "../../types.js";
|
import type { MatrixConfig } from "../../types.js";
|
||||||
import type { MatrixClient } from "../sdk.js";
|
import type { MatrixClient } from "../sdk.js";
|
||||||
|
|
||||||
export function registerMatrixAutoJoin(params: {
|
export function registerMatrixAutoJoin(params: {
|
||||||
client: MatrixClient;
|
client: MatrixClient;
|
||||||
cfg: CoreConfig;
|
accountConfig: Pick<MatrixConfig, "autoJoin" | "autoJoinAllowlist">;
|
||||||
runtime: RuntimeEnv;
|
runtime: RuntimeEnv;
|
||||||
}) {
|
}) {
|
||||||
const { client, cfg, runtime } = params;
|
const { client, accountConfig, runtime } = params;
|
||||||
const core = getMatrixRuntime();
|
const core = getMatrixRuntime();
|
||||||
const logVerbose = (message: string) => {
|
const logVerbose = (message: string) => {
|
||||||
if (!core.logging.shouldLogVerbose()) {
|
if (!core.logging.shouldLogVerbose()) {
|
||||||
@@ -16,11 +16,9 @@ export function registerMatrixAutoJoin(params: {
|
|||||||
}
|
}
|
||||||
runtime.log?.(message);
|
runtime.log?.(message);
|
||||||
};
|
};
|
||||||
const autoJoin = cfg.channels?.["matrix"]?.autoJoin ?? "always";
|
const autoJoin = accountConfig.autoJoin ?? "always";
|
||||||
const autoJoinAllowlist = new Set(
|
const autoJoinAllowlist = new Set(
|
||||||
(cfg.channels?.["matrix"]?.autoJoinAllowlist ?? [])
|
(accountConfig.autoJoinAllowlist ?? []).map((entry) => String(entry).trim()).filter(Boolean),
|
||||||
.map((entry) => String(entry).trim())
|
|
||||||
.filter(Boolean),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (autoJoin === "off") {
|
if (autoJoin === "off") {
|
||||||
|
|||||||
@@ -164,7 +164,7 @@ export async function monitorMatrixProvider(opts: MonitorMatrixOpts = {}): Promi
|
|||||||
const startupMs = Date.now();
|
const startupMs = Date.now();
|
||||||
const startupGraceMs = 0;
|
const startupGraceMs = 0;
|
||||||
const directTracker = createDirectRoomTracker(client, { log: logVerboseMessage });
|
const directTracker = createDirectRoomTracker(client, { log: logVerboseMessage });
|
||||||
registerMatrixAutoJoin({ client, cfg, runtime });
|
registerMatrixAutoJoin({ client, accountConfig, runtime });
|
||||||
const warnedEncryptedRooms = new Set<string>();
|
const warnedEncryptedRooms = new Set<string>();
|
||||||
const warnedCryptoMissingRooms = new Set<string>();
|
const warnedCryptoMissingRooms = new Set<string>();
|
||||||
|
|
||||||
|
|||||||
@@ -76,7 +76,8 @@ async function readDatabaseRecords(params: {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
db.close();
|
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 = () => {
|
keysReq.onsuccess = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user