From 7218ec6d0444a830d62a4d5cb2928ebaca5cdc8c Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Mon, 9 Mar 2026 03:40:11 -0400 Subject: [PATCH] Matrix: centralize account config helpers --- .../matrix/src/matrix/account-config.ts | 10 ++++++ extensions/matrix/src/matrix/accounts.test.ts | 33 ++++++++++++++++++- extensions/matrix/src/matrix/accounts.ts | 31 ++--------------- extensions/matrix/src/matrix/client/config.ts | 13 +------- 4 files changed, 46 insertions(+), 41 deletions(-) diff --git a/extensions/matrix/src/matrix/account-config.ts b/extensions/matrix/src/matrix/account-config.ts index acc9a339c22..8b288989f1f 100644 --- a/extensions/matrix/src/matrix/account-config.ts +++ b/extensions/matrix/src/matrix/account-config.ts @@ -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, diff --git a/extensions/matrix/src/matrix/accounts.test.ts b/extensions/matrix/src/matrix/accounts.test.ts index d453684756c..6d7e5605727 100644 --- a/extensions/matrix/src/matrix/accounts.test.ts +++ b/extensions/matrix/src/matrix/accounts.test.ts @@ -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"); + }); }); diff --git a/extensions/matrix/src/matrix/accounts.ts b/extensions/matrix/src/matrix/accounts.ts index cd9d29bccb9..7bf315e3cce 100644 --- a/extensions/matrix/src/matrix/accounts.ts +++ b/extensions/matrix/src/matrix/accounts.ts @@ -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); -} diff --git a/extensions/matrix/src/matrix/client/config.ts b/extensions/matrix/src/matrix/client/config.ts index c23f8bc5e0d..f39e6995eb7 100644 --- a/extensions/matrix/src/matrix/client/config.ts +++ b/extensions/matrix/src/matrix/client/config.ts @@ -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))); }