fix: honor telegram default account setup policy

This commit is contained in:
Tak Hoffman 2026-04-03 12:43:46 -05:00
parent 379c329f81
commit 638e831bca
No known key found for this signature in database
2 changed files with 36 additions and 6 deletions

View File

@ -191,6 +191,34 @@ describe("telegramSetupWizard.dmPolicy", () => {
});
});
it("uses configured defaultAccount for omitted DM policy account context", () => {
const cfg = {
channels: {
telegram: {
defaultAccount: "alerts",
dmPolicy: "disabled",
allowFrom: ["123"],
accounts: {
alerts: {
dmPolicy: "allowlist",
botToken: "tok",
},
},
},
},
};
expect(telegramSetupWizard.dmPolicy?.getCurrent(cfg)).toBe("allowlist");
expect(telegramSetupWizard.dmPolicy?.resolveConfigKeys?.(cfg)).toEqual({
policyKey: "channels.telegram.accounts.alerts.dmPolicy",
allowFromKey: "channels.telegram.accounts.alerts.allowFrom",
});
const next = telegramSetupWizard.dmPolicy?.setPolicy(cfg, "open");
expect(next?.channels?.telegram?.dmPolicy).toBe("disabled");
expect(next?.channels?.telegram?.accounts?.alerts?.dmPolicy).toBe("open");
});
it('writes open policy state to the named account and preserves inherited allowFrom with "*"', () => {
const next = telegramSetupWizard.dmPolicy?.setPolicy(
{

View File

@ -16,6 +16,7 @@ import { inspectTelegramAccount } from "./account-inspect.js";
import {
listTelegramAccountIds,
mergeTelegramAccountConfig,
resolveDefaultTelegramAccountId,
resolveTelegramAccount,
} from "./accounts.js";
import {
@ -82,20 +83,21 @@ const dmPolicy: ChannelSetupDmPolicy = {
channel,
policyKey: "channels.telegram.dmPolicy",
allowFromKey: "channels.telegram.allowFrom",
resolveConfigKeys: (_cfg, accountId) =>
accountId && accountId !== DEFAULT_ACCOUNT_ID
resolveConfigKeys: (cfg, accountId) =>
(accountId ?? resolveDefaultTelegramAccountId(cfg)) !== DEFAULT_ACCOUNT_ID
? {
policyKey: `channels.telegram.accounts.${accountId}.dmPolicy`,
allowFromKey: `channels.telegram.accounts.${accountId}.allowFrom`,
policyKey: `channels.telegram.accounts.${accountId ?? resolveDefaultTelegramAccountId(cfg)}.dmPolicy`,
allowFromKey: `channels.telegram.accounts.${accountId ?? resolveDefaultTelegramAccountId(cfg)}.allowFrom`,
}
: {
policyKey: "channels.telegram.dmPolicy",
allowFromKey: "channels.telegram.allowFrom",
},
getCurrent: (cfg, accountId) =>
mergeTelegramAccountConfig(cfg, accountId ?? DEFAULT_ACCOUNT_ID).dmPolicy ?? "pairing",
mergeTelegramAccountConfig(cfg, accountId ?? resolveDefaultTelegramAccountId(cfg)).dmPolicy ??
"pairing",
setPolicy: (cfg, policy, accountId) => {
const resolvedAccountId = accountId ?? DEFAULT_ACCOUNT_ID;
const resolvedAccountId = accountId ?? resolveDefaultTelegramAccountId(cfg);
const merged = mergeTelegramAccountConfig(cfg, resolvedAccountId);
return patchChannelConfigForAccount({
cfg,