refactor(onboarding): share allowlist merge helpers

This commit is contained in:
Peter Steinberger
2026-02-16 22:50:41 +00:00
parent 1dfacd4dd1
commit ff7a735115
3 changed files with 15 additions and 20 deletions

View File

@@ -17,23 +17,14 @@ import { resolveDiscordUserAllowlist } from "../../../discord/resolve-users.js";
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../../../routing/session-key.js";
import { formatDocsLink } from "../../../terminal/links.js";
import { promptChannelAccessConfig } from "./channel-access.js";
import { promptAccountId } from "./helpers.js";
function addDiscordWildcardAllowFrom(allowFrom?: string[] | null): string[] {
const next = (allowFrom ?? []).map((entry) => entry.trim()).filter(Boolean);
if (!next.includes("*")) {
next.push("*");
}
return next;
}
import { addWildcardAllowFrom, mergeAllowFromEntries, promptAccountId } from "./helpers.js";
const channel = "discord" as const;
function setDiscordDmPolicy(cfg: OpenClawConfig, dmPolicy: DmPolicy) {
const existingAllowFrom =
cfg.channels?.discord?.allowFrom ?? cfg.channels?.discord?.dm?.allowFrom;
const allowFrom =
dmPolicy === "open" ? addDiscordWildcardAllowFrom(existingAllowFrom) : undefined;
const allowFrom = dmPolicy === "open" ? addWildcardAllowFrom(existingAllowFrom) : undefined;
return {
...cfg,
channels: {
@@ -221,9 +212,7 @@ async function promptDiscordAllowFrom(params: {
);
continue;
}
const unique = [...new Set([...existing.map((v) => String(v).trim()), ...ids])].filter(
Boolean,
);
const unique = mergeAllowFromEntries(existing, ids);
return setDiscordAllowFrom(params.cfg, unique);
}
@@ -244,7 +233,7 @@ async function promptDiscordAllowFrom(params: {
continue;
}
const ids = results.map((res) => res.id as string);
const unique = [...new Set([...existing.map((v) => String(v).trim()).filter(Boolean), ...ids])];
const unique = mergeAllowFromEntries(existing, ids);
return setDiscordAllowFrom(params.cfg, unique);
}
}

View File

@@ -14,3 +14,11 @@ export function addWildcardAllowFrom(
}
return next;
}
export function mergeAllowFromEntries(
current: Array<string | number> | null | undefined,
additions: Array<string | number>,
): string[] {
const merged = [...(current ?? []), ...additions].map((v) => String(v).trim()).filter(Boolean);
return [...new Set(merged)];
}

View File

@@ -12,7 +12,7 @@ import { resolveSlackChannelAllowlist } from "../../../slack/resolve-channels.js
import { resolveSlackUserAllowlist } from "../../../slack/resolve-users.js";
import { formatDocsLink } from "../../../terminal/links.js";
import { promptChannelAccessConfig } from "./channel-access.js";
import { addWildcardAllowFrom, promptAccountId } from "./helpers.js";
import { addWildcardAllowFrom, mergeAllowFromEntries, promptAccountId } from "./helpers.js";
const channel = "slack" as const;
@@ -280,9 +280,7 @@ async function promptSlackAllowFrom(params: {
);
continue;
}
const unique = [...new Set([...existing.map((v) => String(v).trim()), ...ids])].filter(
Boolean,
);
const unique = mergeAllowFromEntries(existing, ids);
return setSlackAllowFrom(params.cfg, unique);
}
@@ -303,7 +301,7 @@ async function promptSlackAllowFrom(params: {
continue;
}
const ids = results.map((res) => res.id as string);
const unique = [...new Set([...existing.map((v) => String(v).trim()).filter(Boolean), ...ids])];
const unique = mergeAllowFromEntries(existing, ids);
return setSlackAllowFrom(params.cfg, unique);
}
}