mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 06:01:23 +00:00
refactor(allowlists): share user entry collection
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { buildAllowlistResolutionSummary } from "./resolve-utils.js";
|
import {
|
||||||
|
addAllowlistUserEntriesFromConfigEntry,
|
||||||
|
buildAllowlistResolutionSummary,
|
||||||
|
} from "./resolve-utils.js";
|
||||||
|
|
||||||
describe("buildAllowlistResolutionSummary", () => {
|
describe("buildAllowlistResolutionSummary", () => {
|
||||||
it("returns mapping, additions, and unresolved (including missing ids)", () => {
|
it("returns mapping, additions, and unresolved (including missing ids)", () => {
|
||||||
@@ -23,3 +26,17 @@ describe("buildAllowlistResolutionSummary", () => {
|
|||||||
expect(result.mapping).toEqual(["a→1 (note)"]);
|
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;
|
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(
|
export function summarizeMapping(
|
||||||
label: string,
|
label: string,
|
||||||
mapping: string[],
|
mapping: string[],
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ 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 {
|
import {
|
||||||
|
addAllowlistUserEntriesFromConfigEntry,
|
||||||
buildAllowlistResolutionSummary,
|
buildAllowlistResolutionSummary,
|
||||||
mergeAllowlist,
|
mergeAllowlist,
|
||||||
resolveAllowlistIdAdditions,
|
resolveAllowlistIdAdditions,
|
||||||
@@ -294,30 +295,10 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) {
|
|||||||
if (!guild || typeof guild !== "object") {
|
if (!guild || typeof guild !== "object") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const users = (guild as { users?: Array<string | number> }).users;
|
addAllowlistUserEntriesFromConfigEntry(userEntries, guild);
|
||||||
if (Array.isArray(users)) {
|
|
||||||
for (const entry of users) {
|
|
||||||
const trimmed = String(entry).trim();
|
|
||||||
if (trimmed && trimmed !== "*") {
|
|
||||||
userEntries.add(trimmed);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const channels = (guild as { channels?: Record<string, unknown> }).channels ?? {};
|
const channels = (guild as { channels?: Record<string, unknown> }).channels ?? {};
|
||||||
for (const channel of Object.values(channels)) {
|
for (const channel of Object.values(channels)) {
|
||||||
if (!channel || typeof channel !== "object") {
|
addAllowlistUserEntriesFromConfigEntry(userEntries, channel);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ 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 {
|
import {
|
||||||
|
addAllowlistUserEntriesFromConfigEntry,
|
||||||
buildAllowlistResolutionSummary,
|
buildAllowlistResolutionSummary,
|
||||||
mergeAllowlist,
|
mergeAllowlist,
|
||||||
patchAllowlistUsersInConfigEntries,
|
patchAllowlistUsersInConfigEntries,
|
||||||
@@ -305,19 +306,7 @@ export async function monitorSlackProvider(opts: MonitorSlackOpts = {}) {
|
|||||||
if (channelsConfig && Object.keys(channelsConfig).length > 0) {
|
if (channelsConfig && Object.keys(channelsConfig).length > 0) {
|
||||||
const userEntries = new Set<string>();
|
const userEntries = new Set<string>();
|
||||||
for (const channel of Object.values(channelsConfig)) {
|
for (const channel of Object.values(channelsConfig)) {
|
||||||
if (!channel || typeof channel !== "object") {
|
addAllowlistUserEntriesFromConfigEntry(userEntries, channel);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userEntries.size > 0) {
|
if (userEntries.size > 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user