mirror of https://github.com/openclaw/openclaw.git
Matrix: centralize account config helpers
This commit is contained in:
parent
314a478fc7
commit
9be74a5cff
|
|
@ -15,6 +15,16 @@ export function resolveMatrixAccountsMap(
|
|||
return accounts;
|
||||
}
|
||||
|
||||
export function listNormalizedMatrixAccountIds(cfg: CoreConfig): string[] {
|
||||
return [
|
||||
...new Set(
|
||||
Object.keys(resolveMatrixAccountsMap(cfg))
|
||||
.filter(Boolean)
|
||||
.map((accountId) => normalizeAccountId(accountId)),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
export function findMatrixAccountConfig(
|
||||
cfg: CoreConfig,
|
||||
accountId: string,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { CoreConfig } from "../types.js";
|
||||
import { resolveMatrixAccount } from "./accounts.js";
|
||||
import {
|
||||
listMatrixAccountIds,
|
||||
resolveDefaultMatrixAccountId,
|
||||
resolveMatrixAccount,
|
||||
} from "./accounts.js";
|
||||
|
||||
vi.mock("./credentials.js", () => ({
|
||||
loadMatrixCredentials: () => null,
|
||||
|
|
@ -79,4 +83,31 @@ describe("resolveMatrixAccount", () => {
|
|||
const account = resolveMatrixAccount({ cfg });
|
||||
expect(account.configured).toBe(true);
|
||||
});
|
||||
|
||||
it("normalizes and de-duplicates configured account ids", () => {
|
||||
const cfg: CoreConfig = {
|
||||
channels: {
|
||||
matrix: {
|
||||
defaultAccount: "Main Bot",
|
||||
accounts: {
|
||||
"Main Bot": {
|
||||
homeserver: "https://matrix.example.org",
|
||||
accessToken: "main-token",
|
||||
},
|
||||
"main-bot": {
|
||||
homeserver: "https://matrix.example.org",
|
||||
accessToken: "duplicate-token",
|
||||
},
|
||||
OPS: {
|
||||
homeserver: "https://matrix.example.org",
|
||||
accessToken: "ops-token",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
expect(listMatrixAccountIds(cfg)).toEqual(["main-bot", "ops"]);
|
||||
expect(resolveDefaultMatrixAccountId(cfg)).toBe("main-bot");
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import { hasConfiguredSecretInput } from "../secret-input.js";
|
|||
import type { CoreConfig, MatrixConfig } from "../types.js";
|
||||
import {
|
||||
findMatrixAccountConfig,
|
||||
resolveMatrixAccountsMap,
|
||||
listNormalizedMatrixAccountIds,
|
||||
resolveMatrixBaseConfig,
|
||||
} from "./account-config.js";
|
||||
import { resolveMatrixConfigForAccount } from "./client.js";
|
||||
|
|
@ -39,23 +39,8 @@ export type ResolvedMatrixAccount = {
|
|||
config: MatrixConfig;
|
||||
};
|
||||
|
||||
function listConfiguredAccountIds(cfg: CoreConfig): string[] {
|
||||
const accounts = resolveMatrixAccountsMap(cfg);
|
||||
if (Object.keys(accounts).length === 0) {
|
||||
return [];
|
||||
}
|
||||
// Normalize and de-duplicate keys so listing and resolution use the same semantics
|
||||
return [
|
||||
...new Set(
|
||||
Object.keys(accounts)
|
||||
.filter(Boolean)
|
||||
.map((id) => normalizeAccountId(id)),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
export function listMatrixAccountIds(cfg: CoreConfig): string[] {
|
||||
const ids = listConfiguredAccountIds(cfg);
|
||||
const ids = listNormalizedMatrixAccountIds(cfg);
|
||||
if (ids.length === 0) {
|
||||
// Fall back to default if no accounts configured (legacy top-level config)
|
||||
return [DEFAULT_ACCOUNT_ID];
|
||||
|
|
@ -75,10 +60,6 @@ export function resolveDefaultMatrixAccountId(cfg: CoreConfig): string {
|
|||
return ids[0] ?? DEFAULT_ACCOUNT_ID;
|
||||
}
|
||||
|
||||
function resolveAccountConfig(cfg: CoreConfig, accountId: string): MatrixConfig | undefined {
|
||||
return findMatrixAccountConfig(cfg, accountId);
|
||||
}
|
||||
|
||||
export function resolveMatrixAccount(params: {
|
||||
cfg: CoreConfig;
|
||||
accountId?: string | null;
|
||||
|
|
@ -120,7 +101,7 @@ export function resolveMatrixAccountConfig(params: {
|
|||
}): MatrixConfig {
|
||||
const accountId = normalizeAccountId(params.accountId);
|
||||
const matrixBase = resolveMatrixBaseConfig(params.cfg);
|
||||
const accountConfig = resolveAccountConfig(params.cfg, accountId);
|
||||
const accountConfig = findMatrixAccountConfig(params.cfg, accountId);
|
||||
if (!accountConfig) {
|
||||
return matrixBase;
|
||||
}
|
||||
|
|
@ -128,9 +109,3 @@ export function resolveMatrixAccountConfig(params: {
|
|||
// groupPolicy and blockStreaming inherit when not overridden.
|
||||
return mergeAccountConfig(matrixBase, accountConfig);
|
||||
}
|
||||
|
||||
export function listEnabledMatrixAccounts(cfg: CoreConfig): ResolvedMatrixAccount[] {
|
||||
return listMatrixAccountIds(cfg)
|
||||
.map((accountId) => resolveMatrixAccount({ cfg, accountId }))
|
||||
.filter((account) => account.enabled);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import { normalizeResolvedSecretInputString } from "../../secret-input.js";
|
|||
import type { CoreConfig } from "../../types.js";
|
||||
import {
|
||||
findMatrixAccountConfig,
|
||||
resolveMatrixAccountsMap,
|
||||
listNormalizedMatrixAccountIds,
|
||||
resolveMatrixBaseConfig,
|
||||
} from "../account-config.js";
|
||||
import { MatrixClient } from "../sdk.js";
|
||||
|
|
@ -238,17 +238,6 @@ export function resolveMatrixConfigForAccount(
|
|||
};
|
||||
}
|
||||
|
||||
function listNormalizedMatrixAccountIds(cfg: CoreConfig): string[] {
|
||||
const accounts = resolveMatrixAccountsMap(cfg);
|
||||
return [
|
||||
...new Set(
|
||||
Object.keys(accounts)
|
||||
.filter(Boolean)
|
||||
.map((accountId) => normalizeAccountId(accountId)),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
function hasMatrixAuthInputs(config: MatrixResolvedConfig): boolean {
|
||||
return Boolean(config.homeserver && (config.accessToken || (config.userId && config.password)));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue