mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 07:31:24 +00:00
refactor(channels): share allowlist user resolve helpers
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
import type { RuntimeEnv } from "../../runtime.js";
|
import type { RuntimeEnv } from "../../runtime.js";
|
||||||
|
|
||||||
|
export type AllowlistUserResolutionLike = {
|
||||||
|
input: string;
|
||||||
|
resolved: boolean;
|
||||||
|
id?: string;
|
||||||
|
};
|
||||||
|
|
||||||
export function mergeAllowlist(params: {
|
export function mergeAllowlist(params: {
|
||||||
existing?: Array<string | number>;
|
existing?: Array<string | number>;
|
||||||
additions: string[];
|
additions: string[];
|
||||||
@@ -27,6 +33,36 @@ export function mergeAllowlist(params: {
|
|||||||
return merged;
|
return merged;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function buildAllowlistResolutionSummary<T extends AllowlistUserResolutionLike>(
|
||||||
|
resolvedUsers: T[],
|
||||||
|
): {
|
||||||
|
resolvedMap: Map<string, T>;
|
||||||
|
mapping: string[];
|
||||||
|
unresolved: string[];
|
||||||
|
} {
|
||||||
|
const resolvedMap = new Map(resolvedUsers.map((entry) => [entry.input, entry]));
|
||||||
|
const mapping = resolvedUsers
|
||||||
|
.filter((entry) => entry.resolved && entry.id)
|
||||||
|
.map((entry) => `${entry.input}→${entry.id}`);
|
||||||
|
const unresolved = resolvedUsers.filter((entry) => !entry.resolved).map((entry) => entry.input);
|
||||||
|
return { resolvedMap, mapping, unresolved };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveAllowlistIdAdditions<T extends AllowlistUserResolutionLike>(params: {
|
||||||
|
existing: Array<string | number>;
|
||||||
|
resolvedMap: Map<string, T>;
|
||||||
|
}): string[] {
|
||||||
|
const additions: string[] = [];
|
||||||
|
for (const entry of params.existing) {
|
||||||
|
const trimmed = String(entry).trim();
|
||||||
|
const resolved = params.resolvedMap.get(trimmed);
|
||||||
|
if (resolved?.resolved && resolved.id) {
|
||||||
|
additions.push(resolved.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return additions;
|
||||||
|
}
|
||||||
|
|
||||||
export function summarizeMapping(
|
export function summarizeMapping(
|
||||||
label: string,
|
label: string,
|
||||||
mapping: string[],
|
mapping: string[],
|
||||||
|
|||||||
@@ -7,7 +7,12 @@ import type { OpenClawConfig, ReplyToMode } from "../../config/config.js";
|
|||||||
import { resolveTextChunkLimit } from "../../auto-reply/chunk.js";
|
import { resolveTextChunkLimit } from "../../auto-reply/chunk.js";
|
||||||
import { listNativeCommandSpecsForConfig } from "../../auto-reply/commands-registry.js";
|
import { listNativeCommandSpecsForConfig } from "../../auto-reply/commands-registry.js";
|
||||||
import { listSkillCommandsForAgents } from "../../auto-reply/skill-commands.js";
|
import { listSkillCommandsForAgents } from "../../auto-reply/skill-commands.js";
|
||||||
import { mergeAllowlist, summarizeMapping } from "../../channels/allowlists/resolve-utils.js";
|
import {
|
||||||
|
buildAllowlistResolutionSummary,
|
||||||
|
mergeAllowlist,
|
||||||
|
resolveAllowlistIdAdditions,
|
||||||
|
summarizeMapping,
|
||||||
|
} from "../../channels/allowlists/resolve-utils.js";
|
||||||
import {
|
import {
|
||||||
isNativeCommandsExplicitlyDisabled,
|
isNativeCommandsExplicitlyDisabled,
|
||||||
resolveNativeCommandsEnabled,
|
resolveNativeCommandsEnabled,
|
||||||
@@ -331,13 +336,8 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) {
|
|||||||
token,
|
token,
|
||||||
entries: Array.from(userEntries),
|
entries: Array.from(userEntries),
|
||||||
});
|
});
|
||||||
const resolvedMap = new Map(resolvedUsers.map((entry) => [entry.input, entry]));
|
const { resolvedMap, mapping, unresolved } =
|
||||||
const mapping = resolvedUsers
|
buildAllowlistResolutionSummary(resolvedUsers);
|
||||||
.filter((entry) => entry.resolved && entry.id)
|
|
||||||
.map((entry) => `${entry.input}→${entry.id}`);
|
|
||||||
const unresolved = resolvedUsers
|
|
||||||
.filter((entry) => !entry.resolved)
|
|
||||||
.map((entry) => entry.input);
|
|
||||||
|
|
||||||
const nextGuilds = { ...guildEntries };
|
const nextGuilds = { ...guildEntries };
|
||||||
for (const [guildKey, guildConfig] of Object.entries(guildEntries ?? {})) {
|
for (const [guildKey, guildConfig] of Object.entries(guildEntries ?? {})) {
|
||||||
@@ -347,14 +347,7 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) {
|
|||||||
const nextGuild = { ...guildConfig } as Record<string, unknown>;
|
const nextGuild = { ...guildConfig } as Record<string, unknown>;
|
||||||
const users = (guildConfig as { users?: Array<string | number> }).users;
|
const users = (guildConfig as { users?: Array<string | number> }).users;
|
||||||
if (Array.isArray(users) && users.length > 0) {
|
if (Array.isArray(users) && users.length > 0) {
|
||||||
const additions: string[] = [];
|
const additions = resolveAllowlistIdAdditions({ existing: users, resolvedMap });
|
||||||
for (const entry of users) {
|
|
||||||
const trimmed = String(entry).trim();
|
|
||||||
const resolved = resolvedMap.get(trimmed);
|
|
||||||
if (resolved?.resolved && resolved.id) {
|
|
||||||
additions.push(resolved.id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
nextGuild.users = mergeAllowlist({ existing: users, additions });
|
nextGuild.users = mergeAllowlist({ existing: users, additions });
|
||||||
}
|
}
|
||||||
const channels = (guildConfig as { channels?: Record<string, unknown> }).channels ?? {};
|
const channels = (guildConfig as { channels?: Record<string, unknown> }).channels ?? {};
|
||||||
@@ -368,14 +361,10 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) {
|
|||||||
if (!Array.isArray(channelUsers) || channelUsers.length === 0) {
|
if (!Array.isArray(channelUsers) || channelUsers.length === 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const additions: string[] = [];
|
const additions = resolveAllowlistIdAdditions({
|
||||||
for (const entry of channelUsers) {
|
existing: channelUsers,
|
||||||
const trimmed = String(entry).trim();
|
resolvedMap,
|
||||||
const resolved = resolvedMap.get(trimmed);
|
});
|
||||||
if (resolved?.resolved && resolved.id) {
|
|
||||||
additions.push(resolved.id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
nextChannels[channelKey] = {
|
nextChannels[channelKey] = {
|
||||||
...channelConfig,
|
...channelConfig,
|
||||||
users: mergeAllowlist({ existing: channelUsers, additions }),
|
users: mergeAllowlist({ existing: channelUsers, additions }),
|
||||||
|
|||||||
@@ -4,7 +4,12 @@ import type { SessionScope } from "../../config/sessions.js";
|
|||||||
import type { MonitorSlackOpts } from "./types.js";
|
import type { MonitorSlackOpts } from "./types.js";
|
||||||
import { resolveTextChunkLimit } from "../../auto-reply/chunk.js";
|
import { resolveTextChunkLimit } from "../../auto-reply/chunk.js";
|
||||||
import { DEFAULT_GROUP_HISTORY_LIMIT } from "../../auto-reply/reply/history.js";
|
import { DEFAULT_GROUP_HISTORY_LIMIT } from "../../auto-reply/reply/history.js";
|
||||||
import { mergeAllowlist, summarizeMapping } from "../../channels/allowlists/resolve-utils.js";
|
import {
|
||||||
|
buildAllowlistResolutionSummary,
|
||||||
|
mergeAllowlist,
|
||||||
|
resolveAllowlistIdAdditions,
|
||||||
|
summarizeMapping,
|
||||||
|
} from "../../channels/allowlists/resolve-utils.js";
|
||||||
import { loadConfig } from "../../config/config.js";
|
import { loadConfig } from "../../config/config.js";
|
||||||
import { warn } from "../../globals.js";
|
import { warn } from "../../globals.js";
|
||||||
import { installRequestBodyLimitGuard } from "../../infra/http-body.js";
|
import { installRequestBodyLimitGuard } from "../../infra/http-body.js";
|
||||||
@@ -322,13 +327,8 @@ export async function monitorSlackProvider(opts: MonitorSlackOpts = {}) {
|
|||||||
token: resolveToken,
|
token: resolveToken,
|
||||||
entries: Array.from(userEntries),
|
entries: Array.from(userEntries),
|
||||||
});
|
});
|
||||||
const resolvedMap = new Map(resolvedUsers.map((entry) => [entry.input, entry]));
|
const { resolvedMap, mapping, unresolved } =
|
||||||
const mapping = resolvedUsers
|
buildAllowlistResolutionSummary(resolvedUsers);
|
||||||
.filter((entry) => entry.resolved && entry.id)
|
|
||||||
.map((entry) => `${entry.input}→${entry.id}`);
|
|
||||||
const unresolved = resolvedUsers
|
|
||||||
.filter((entry) => !entry.resolved)
|
|
||||||
.map((entry) => entry.input);
|
|
||||||
|
|
||||||
const nextChannels = { ...channelsConfig };
|
const nextChannels = { ...channelsConfig };
|
||||||
for (const [channelKey, channelConfig] of Object.entries(channelsConfig)) {
|
for (const [channelKey, channelConfig] of Object.entries(channelsConfig)) {
|
||||||
@@ -339,14 +339,10 @@ export async function monitorSlackProvider(opts: MonitorSlackOpts = {}) {
|
|||||||
if (!Array.isArray(channelUsers) || channelUsers.length === 0) {
|
if (!Array.isArray(channelUsers) || channelUsers.length === 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const additions: string[] = [];
|
const additions = resolveAllowlistIdAdditions({
|
||||||
for (const entry of channelUsers) {
|
existing: channelUsers,
|
||||||
const trimmed = String(entry).trim();
|
resolvedMap,
|
||||||
const resolved = resolvedMap.get(trimmed);
|
});
|
||||||
if (resolved?.resolved && resolved.id) {
|
|
||||||
additions.push(resolved.id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
nextChannels[channelKey] = {
|
nextChannels[channelKey] = {
|
||||||
...channelConfig,
|
...channelConfig,
|
||||||
users: mergeAllowlist({ existing: channelUsers, additions }),
|
users: mergeAllowlist({ existing: channelUsers, additions }),
|
||||||
|
|||||||
Reference in New Issue
Block a user