mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 22:44:36 +00:00
refactor(channels): share allowlist config patch helper
This commit is contained in:
@@ -63,6 +63,31 @@ export function resolveAllowlistIdAdditions<T extends AllowlistUserResolutionLik
|
|||||||
return additions;
|
return additions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function patchAllowlistUsersInConfigEntries<
|
||||||
|
T extends AllowlistUserResolutionLike,
|
||||||
|
TEntries extends Record<string, unknown>,
|
||||||
|
>(params: { entries: TEntries; resolvedMap: Map<string, T> }): TEntries {
|
||||||
|
const nextEntries: Record<string, unknown> = { ...params.entries };
|
||||||
|
for (const [entryKey, entryConfig] of Object.entries(params.entries)) {
|
||||||
|
if (!entryConfig || typeof entryConfig !== "object") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const users = (entryConfig as { users?: Array<string | number> }).users;
|
||||||
|
if (!Array.isArray(users) || users.length === 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const additions = resolveAllowlistIdAdditions({
|
||||||
|
existing: users,
|
||||||
|
resolvedMap: params.resolvedMap,
|
||||||
|
});
|
||||||
|
nextEntries[entryKey] = {
|
||||||
|
...entryConfig,
|
||||||
|
users: mergeAllowlist({ existing: users, additions }),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return nextEntries as TEntries;
|
||||||
|
}
|
||||||
|
|
||||||
export function summarizeMapping(
|
export function summarizeMapping(
|
||||||
label: string,
|
label: string,
|
||||||
mapping: string[],
|
mapping: string[],
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import {
|
|||||||
buildAllowlistResolutionSummary,
|
buildAllowlistResolutionSummary,
|
||||||
mergeAllowlist,
|
mergeAllowlist,
|
||||||
resolveAllowlistIdAdditions,
|
resolveAllowlistIdAdditions,
|
||||||
|
patchAllowlistUsersInConfigEntries,
|
||||||
summarizeMapping,
|
summarizeMapping,
|
||||||
} from "../../channels/allowlists/resolve-utils.js";
|
} from "../../channels/allowlists/resolve-utils.js";
|
||||||
import {
|
import {
|
||||||
@@ -352,25 +353,10 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) {
|
|||||||
}
|
}
|
||||||
const channels = (guildConfig as { channels?: Record<string, unknown> }).channels ?? {};
|
const channels = (guildConfig as { channels?: Record<string, unknown> }).channels ?? {};
|
||||||
if (channels && typeof channels === "object") {
|
if (channels && typeof channels === "object") {
|
||||||
const nextChannels: Record<string, unknown> = { ...channels };
|
nextGuild.channels = patchAllowlistUsersInConfigEntries({
|
||||||
for (const [channelKey, channelConfig] of Object.entries(channels)) {
|
entries: channels,
|
||||||
if (!channelConfig || typeof channelConfig !== "object") {
|
resolvedMap,
|
||||||
continue;
|
});
|
||||||
}
|
|
||||||
const channelUsers = (channelConfig as { users?: Array<string | number> }).users;
|
|
||||||
if (!Array.isArray(channelUsers) || channelUsers.length === 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const additions = resolveAllowlistIdAdditions({
|
|
||||||
existing: channelUsers,
|
|
||||||
resolvedMap,
|
|
||||||
});
|
|
||||||
nextChannels[channelKey] = {
|
|
||||||
...channelConfig,
|
|
||||||
users: mergeAllowlist({ existing: channelUsers, additions }),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
nextGuild.channels = nextChannels;
|
|
||||||
}
|
}
|
||||||
nextGuilds[guildKey] = nextGuild;
|
nextGuilds[guildKey] = nextGuild;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import { DEFAULT_GROUP_HISTORY_LIMIT } from "../../auto-reply/reply/history.js";
|
|||||||
import {
|
import {
|
||||||
buildAllowlistResolutionSummary,
|
buildAllowlistResolutionSummary,
|
||||||
mergeAllowlist,
|
mergeAllowlist,
|
||||||
resolveAllowlistIdAdditions,
|
patchAllowlistUsersInConfigEntries,
|
||||||
summarizeMapping,
|
summarizeMapping,
|
||||||
} from "../../channels/allowlists/resolve-utils.js";
|
} from "../../channels/allowlists/resolve-utils.js";
|
||||||
import { loadConfig } from "../../config/config.js";
|
import { loadConfig } from "../../config/config.js";
|
||||||
@@ -330,24 +330,10 @@ export async function monitorSlackProvider(opts: MonitorSlackOpts = {}) {
|
|||||||
const { resolvedMap, mapping, unresolved } =
|
const { resolvedMap, mapping, unresolved } =
|
||||||
buildAllowlistResolutionSummary(resolvedUsers);
|
buildAllowlistResolutionSummary(resolvedUsers);
|
||||||
|
|
||||||
const nextChannels = { ...channelsConfig };
|
const nextChannels = patchAllowlistUsersInConfigEntries({
|
||||||
for (const [channelKey, channelConfig] of Object.entries(channelsConfig)) {
|
entries: channelsConfig,
|
||||||
if (!channelConfig || typeof channelConfig !== "object") {
|
resolvedMap,
|
||||||
continue;
|
});
|
||||||
}
|
|
||||||
const channelUsers = (channelConfig as { users?: Array<string | number> }).users;
|
|
||||||
if (!Array.isArray(channelUsers) || channelUsers.length === 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const additions = resolveAllowlistIdAdditions({
|
|
||||||
existing: channelUsers,
|
|
||||||
resolvedMap,
|
|
||||||
});
|
|
||||||
nextChannels[channelKey] = {
|
|
||||||
...channelConfig,
|
|
||||||
users: mergeAllowlist({ existing: channelUsers, additions }),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
channelsConfig = nextChannels;
|
channelsConfig = nextChannels;
|
||||||
ctx.channelsConfig = nextChannels;
|
ctx.channelsConfig = nextChannels;
|
||||||
summarizeMapping("slack channel users", mapping, unresolved, runtime);
|
summarizeMapping("slack channel users", mapping, unresolved, runtime);
|
||||||
|
|||||||
Reference in New Issue
Block a user