mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 14:48:28 +00:00
refactor(channels): share account action gate resolution
This commit is contained in:
34
src/channels/plugins/account-action-gate.test.ts
Normal file
34
src/channels/plugins/account-action-gate.test.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { createAccountActionGate } from "./account-action-gate.js";
|
||||||
|
|
||||||
|
type TestActions = {
|
||||||
|
send?: boolean;
|
||||||
|
reactions?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
describe("createAccountActionGate", () => {
|
||||||
|
it("prefers account action values over base values", () => {
|
||||||
|
const gate = createAccountActionGate<TestActions>({
|
||||||
|
baseActions: { send: false, reactions: true },
|
||||||
|
accountActions: { send: true },
|
||||||
|
});
|
||||||
|
expect(gate("send")).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("falls back to base actions when account actions are unset", () => {
|
||||||
|
const gate = createAccountActionGate<TestActions>({
|
||||||
|
baseActions: { reactions: false },
|
||||||
|
accountActions: {},
|
||||||
|
});
|
||||||
|
expect(gate("reactions")).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses default value when neither account nor base defines the key", () => {
|
||||||
|
const gate = createAccountActionGate<TestActions>({
|
||||||
|
baseActions: {},
|
||||||
|
accountActions: {},
|
||||||
|
});
|
||||||
|
expect(gate("send", false)).toBe(false);
|
||||||
|
expect(gate("send")).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
21
src/channels/plugins/account-action-gate.ts
Normal file
21
src/channels/plugins/account-action-gate.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
export type ActionGate<T extends Record<string, boolean | undefined>> = (
|
||||||
|
key: keyof T,
|
||||||
|
defaultValue?: boolean,
|
||||||
|
) => boolean;
|
||||||
|
|
||||||
|
export function createAccountActionGate<T extends Record<string, boolean | undefined>>(params: {
|
||||||
|
baseActions?: T;
|
||||||
|
accountActions?: T;
|
||||||
|
}): ActionGate<T> {
|
||||||
|
return (key, defaultValue = true) => {
|
||||||
|
const accountValue = params.accountActions?.[key];
|
||||||
|
if (accountValue !== undefined) {
|
||||||
|
return accountValue;
|
||||||
|
}
|
||||||
|
const baseValue = params.baseActions?.[key];
|
||||||
|
if (baseValue !== undefined) {
|
||||||
|
return baseValue;
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { createAccountActionGate } from "../channels/plugins/account-action-gate.js";
|
||||||
import { createAccountListHelpers } from "../channels/plugins/account-helpers.js";
|
import { createAccountListHelpers } from "../channels/plugins/account-helpers.js";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
import type { DiscordAccountConfig, DiscordActionConfig } from "../config/types.js";
|
import type { DiscordAccountConfig, DiscordActionConfig } from "../config/types.js";
|
||||||
@@ -41,19 +42,10 @@ export function createDiscordActionGate(params: {
|
|||||||
accountId?: string | null;
|
accountId?: string | null;
|
||||||
}): (key: keyof DiscordActionConfig, defaultValue?: boolean) => boolean {
|
}): (key: keyof DiscordActionConfig, defaultValue?: boolean) => boolean {
|
||||||
const accountId = normalizeAccountId(params.accountId);
|
const accountId = normalizeAccountId(params.accountId);
|
||||||
const baseActions = params.cfg.channels?.discord?.actions;
|
return createAccountActionGate({
|
||||||
const accountActions = resolveAccountConfig(params.cfg, accountId)?.actions;
|
baseActions: params.cfg.channels?.discord?.actions,
|
||||||
return (key, defaultValue = true) => {
|
accountActions: resolveAccountConfig(params.cfg, accountId)?.actions,
|
||||||
const accountValue = accountActions?.[key];
|
});
|
||||||
if (accountValue !== undefined) {
|
|
||||||
return accountValue;
|
|
||||||
}
|
|
||||||
const baseValue = baseActions?.[key];
|
|
||||||
if (baseValue !== undefined) {
|
|
||||||
return baseValue;
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function resolveDiscordAccount(params: {
|
export function resolveDiscordAccount(params: {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { createAccountActionGate } from "../channels/plugins/account-action-gate.js";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
import type { TelegramAccountConfig, TelegramActionConfig } from "../config/types.js";
|
import type { TelegramAccountConfig, TelegramActionConfig } from "../config/types.js";
|
||||||
import { isTruthyEnvValue } from "../infra/env.js";
|
import { isTruthyEnvValue } from "../infra/env.js";
|
||||||
@@ -87,19 +88,10 @@ export function createTelegramActionGate(params: {
|
|||||||
accountId?: string | null;
|
accountId?: string | null;
|
||||||
}): (key: keyof TelegramActionConfig, defaultValue?: boolean) => boolean {
|
}): (key: keyof TelegramActionConfig, defaultValue?: boolean) => boolean {
|
||||||
const accountId = normalizeAccountId(params.accountId);
|
const accountId = normalizeAccountId(params.accountId);
|
||||||
const baseActions = params.cfg.channels?.telegram?.actions;
|
return createAccountActionGate({
|
||||||
const accountActions = resolveAccountConfig(params.cfg, accountId)?.actions;
|
baseActions: params.cfg.channels?.telegram?.actions,
|
||||||
return (key, defaultValue = true) => {
|
accountActions: resolveAccountConfig(params.cfg, accountId)?.actions,
|
||||||
const accountValue = accountActions?.[key];
|
});
|
||||||
if (accountValue !== undefined) {
|
|
||||||
return accountValue;
|
|
||||||
}
|
|
||||||
const baseValue = baseActions?.[key];
|
|
||||||
if (baseValue !== undefined) {
|
|
||||||
return baseValue;
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function resolveTelegramAccount(params: {
|
export function resolveTelegramAccount(params: {
|
||||||
|
|||||||
Reference in New Issue
Block a user