mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 04:30:42 +00:00
refactor: reuse shared core account lookups
This commit is contained in:
@@ -1,18 +1,17 @@
|
||||
import type { OpenClawConfig } from "../../config/config.js";
|
||||
import { resolveAccountEntry } from "../../routing/account-lookup.js";
|
||||
import { DEFAULT_ACCOUNT_ID } from "../../routing/session-key.js";
|
||||
import { normalizeAccountId } from "../../routing/session-key.js";
|
||||
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../../routing/session-key.js";
|
||||
import { isInternalMessageChannel } from "../../utils/message-channel.js";
|
||||
import type { ChannelId } from "./types.js";
|
||||
|
||||
type ChannelConfigWithAccounts = {
|
||||
type AccountConfigWithWrites = {
|
||||
configWrites?: boolean;
|
||||
accounts?: Record<string, { configWrites?: boolean }>;
|
||||
};
|
||||
|
||||
function resolveAccountConfig(accounts: ChannelConfigWithAccounts["accounts"], accountId: string) {
|
||||
return resolveAccountEntry(accounts, accountId);
|
||||
}
|
||||
type ChannelConfigWithAccounts = {
|
||||
configWrites?: boolean;
|
||||
accounts?: Record<string, AccountConfigWithWrites>;
|
||||
};
|
||||
|
||||
export type ConfigWriteScope = {
|
||||
channelId?: ChannelId | null;
|
||||
@@ -38,16 +37,11 @@ export function resolveChannelConfigWrites(params: {
|
||||
channelId?: ChannelId | null;
|
||||
accountId?: string | null;
|
||||
}): boolean {
|
||||
if (!params.channelId) {
|
||||
return true;
|
||||
}
|
||||
const channels = params.cfg.channels as Record<string, ChannelConfigWithAccounts> | undefined;
|
||||
const channelConfig = channels?.[params.channelId];
|
||||
const channelConfig = resolveChannelConfig(params.cfg, params.channelId);
|
||||
if (!channelConfig) {
|
||||
return true;
|
||||
}
|
||||
const accountId = normalizeAccountId(params.accountId);
|
||||
const accountConfig = resolveAccountConfig(channelConfig.accounts, accountId);
|
||||
const accountConfig = resolveChannelAccountConfig(channelConfig, params.accountId);
|
||||
const value = accountConfig?.configWrites ?? channelConfig.configWrites;
|
||||
return value !== false;
|
||||
}
|
||||
@@ -181,3 +175,20 @@ function listConfigWriteTargetScopes(target?: ConfigWriteTarget): ConfigWriteSco
|
||||
}
|
||||
return [target.scope];
|
||||
}
|
||||
|
||||
function resolveChannelConfig(
|
||||
cfg: OpenClawConfig,
|
||||
channelId?: ChannelId | null,
|
||||
): ChannelConfigWithAccounts | undefined {
|
||||
if (!channelId) {
|
||||
return undefined;
|
||||
}
|
||||
return (cfg.channels as Record<string, ChannelConfigWithAccounts> | undefined)?.[channelId];
|
||||
}
|
||||
|
||||
function resolveChannelAccountConfig(
|
||||
channelConfig: ChannelConfigWithAccounts,
|
||||
accountId?: string | null,
|
||||
): AccountConfigWithWrites | undefined {
|
||||
return resolveAccountEntry(channelConfig.accounts, normalizeAccountId(accountId));
|
||||
}
|
||||
|
||||
@@ -505,6 +505,12 @@ describe("resolveChannelConfigWrites", () => {
|
||||
const cfg = makeSlackConfigWritesCfg("Work");
|
||||
expect(resolveChannelConfigWrites({ cfg, channelId: "slack", accountId: "work" })).toBe(false);
|
||||
});
|
||||
|
||||
it("ignores account ids when the channel is missing", () => {
|
||||
expect(resolveChannelConfigWrites({ cfg: {}, channelId: "slack", accountId: "work" })).toBe(
|
||||
true,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("authorizeConfigWrite", () => {
|
||||
|
||||
Reference in New Issue
Block a user