mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
refactor(allowlists): share user entry collection
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildAllowlistResolutionSummary } from "./resolve-utils.js";
|
||||
import {
|
||||
addAllowlistUserEntriesFromConfigEntry,
|
||||
buildAllowlistResolutionSummary,
|
||||
} from "./resolve-utils.js";
|
||||
|
||||
describe("buildAllowlistResolutionSummary", () => {
|
||||
it("returns mapping, additions, and unresolved (including missing ids)", () => {
|
||||
@@ -23,3 +26,17 @@ describe("buildAllowlistResolutionSummary", () => {
|
||||
expect(result.mapping).toEqual(["a→1 (note)"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("addAllowlistUserEntriesFromConfigEntry", () => {
|
||||
it("adds trimmed users and skips '*' and blanks", () => {
|
||||
const target = new Set<string>();
|
||||
addAllowlistUserEntriesFromConfigEntry(target, { users: [" a ", "*", "", "b"] });
|
||||
expect(Array.from(target).toSorted()).toEqual(["a", "b"]);
|
||||
});
|
||||
|
||||
it("ignores non-objects", () => {
|
||||
const target = new Set<string>(["a"]);
|
||||
addAllowlistUserEntriesFromConfigEntry(target, null);
|
||||
expect(Array.from(target)).toEqual(["a"]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -96,6 +96,22 @@ export function patchAllowlistUsersInConfigEntries<
|
||||
return nextEntries as TEntries;
|
||||
}
|
||||
|
||||
export function addAllowlistUserEntriesFromConfigEntry(target: Set<string>, entry: unknown): void {
|
||||
if (!entry || typeof entry !== "object") {
|
||||
return;
|
||||
}
|
||||
const users = (entry as { users?: Array<string | number> }).users;
|
||||
if (!Array.isArray(users)) {
|
||||
return;
|
||||
}
|
||||
for (const value of users) {
|
||||
const trimmed = String(value).trim();
|
||||
if (trimmed && trimmed !== "*") {
|
||||
target.add(trimmed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function summarizeMapping(
|
||||
label: string,
|
||||
mapping: string[],
|
||||
|
||||
@@ -8,6 +8,7 @@ import { resolveTextChunkLimit } from "../../auto-reply/chunk.js";
|
||||
import { listNativeCommandSpecsForConfig } from "../../auto-reply/commands-registry.js";
|
||||
import { listSkillCommandsForAgents } from "../../auto-reply/skill-commands.js";
|
||||
import {
|
||||
addAllowlistUserEntriesFromConfigEntry,
|
||||
buildAllowlistResolutionSummary,
|
||||
mergeAllowlist,
|
||||
resolveAllowlistIdAdditions,
|
||||
@@ -294,30 +295,10 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) {
|
||||
if (!guild || typeof guild !== "object") {
|
||||
continue;
|
||||
}
|
||||
const users = (guild as { users?: Array<string | number> }).users;
|
||||
if (Array.isArray(users)) {
|
||||
for (const entry of users) {
|
||||
const trimmed = String(entry).trim();
|
||||
if (trimmed && trimmed !== "*") {
|
||||
userEntries.add(trimmed);
|
||||
}
|
||||
}
|
||||
}
|
||||
addAllowlistUserEntriesFromConfigEntry(userEntries, guild);
|
||||
const channels = (guild as { channels?: Record<string, unknown> }).channels ?? {};
|
||||
for (const channel of Object.values(channels)) {
|
||||
if (!channel || typeof channel !== "object") {
|
||||
continue;
|
||||
}
|
||||
const channelUsers = (channel as { users?: Array<string | number> }).users;
|
||||
if (!Array.isArray(channelUsers)) {
|
||||
continue;
|
||||
}
|
||||
for (const entry of channelUsers) {
|
||||
const trimmed = String(entry).trim();
|
||||
if (trimmed && trimmed !== "*") {
|
||||
userEntries.add(trimmed);
|
||||
}
|
||||
}
|
||||
addAllowlistUserEntriesFromConfigEntry(userEntries, channel);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { MonitorSlackOpts } from "./types.js";
|
||||
import { resolveTextChunkLimit } from "../../auto-reply/chunk.js";
|
||||
import { DEFAULT_GROUP_HISTORY_LIMIT } from "../../auto-reply/reply/history.js";
|
||||
import {
|
||||
addAllowlistUserEntriesFromConfigEntry,
|
||||
buildAllowlistResolutionSummary,
|
||||
mergeAllowlist,
|
||||
patchAllowlistUsersInConfigEntries,
|
||||
@@ -305,19 +306,7 @@ export async function monitorSlackProvider(opts: MonitorSlackOpts = {}) {
|
||||
if (channelsConfig && Object.keys(channelsConfig).length > 0) {
|
||||
const userEntries = new Set<string>();
|
||||
for (const channel of Object.values(channelsConfig)) {
|
||||
if (!channel || typeof channel !== "object") {
|
||||
continue;
|
||||
}
|
||||
const channelUsers = (channel as { users?: Array<string | number> }).users;
|
||||
if (!Array.isArray(channelUsers)) {
|
||||
continue;
|
||||
}
|
||||
for (const entry of channelUsers) {
|
||||
const trimmed = String(entry).trim();
|
||||
if (trimmed && trimmed !== "*") {
|
||||
userEntries.add(trimmed);
|
||||
}
|
||||
}
|
||||
addAllowlistUserEntriesFromConfigEntry(userEntries, channel);
|
||||
}
|
||||
|
||||
if (userEntries.size > 0) {
|
||||
|
||||
Reference in New Issue
Block a user