mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-30 07:52:41 +00:00
refactor(channels): share allowlist resolution summary
This commit is contained in:
25
src/channels/allowlists/resolve-utils.test.ts
Normal file
25
src/channels/allowlists/resolve-utils.test.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildAllowlistResolutionSummary } from "./resolve-utils.js";
|
||||
|
||||
describe("buildAllowlistResolutionSummary", () => {
|
||||
it("returns mapping, additions, and unresolved (including missing ids)", () => {
|
||||
const resolvedUsers = [
|
||||
{ input: "a", resolved: true, id: "1" },
|
||||
{ input: "b", resolved: false },
|
||||
{ input: "c", resolved: true },
|
||||
];
|
||||
const result = buildAllowlistResolutionSummary(resolvedUsers);
|
||||
expect(result.mapping).toEqual(["a→1"]);
|
||||
expect(result.additions).toEqual(["1"]);
|
||||
expect(result.unresolved).toEqual(["b", "c"]);
|
||||
});
|
||||
|
||||
it("supports custom resolved formatting", () => {
|
||||
const resolvedUsers = [{ input: "a", resolved: true, id: "1", note: "x" }];
|
||||
const result = buildAllowlistResolutionSummary(resolvedUsers, {
|
||||
formatResolved: (entry) =>
|
||||
`${entry.input}→${entry.id}${(entry as { note?: string }).note ? " (note)" : ""}`,
|
||||
});
|
||||
expect(result.mapping).toEqual(["a→1 (note)"]);
|
||||
});
|
||||
});
|
||||
@@ -35,17 +35,25 @@ export function mergeAllowlist(params: {
|
||||
|
||||
export function buildAllowlistResolutionSummary<T extends AllowlistUserResolutionLike>(
|
||||
resolvedUsers: T[],
|
||||
opts?: { formatResolved?: (entry: T) => string },
|
||||
): {
|
||||
resolvedMap: Map<string, T>;
|
||||
mapping: string[];
|
||||
unresolved: string[];
|
||||
additions: 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 };
|
||||
const resolvedOk = (entry: T) => Boolean(entry.resolved && entry.id);
|
||||
const formatResolved = opts?.formatResolved ?? ((entry: T) => `${entry.input}→${entry.id}`);
|
||||
const mapping = resolvedUsers.filter(resolvedOk).map(formatResolved);
|
||||
const additions = resolvedUsers
|
||||
.filter(resolvedOk)
|
||||
.map((entry) => entry.id)
|
||||
.filter((entry): entry is string => Boolean(entry));
|
||||
const unresolved = resolvedUsers
|
||||
.filter((entry) => !resolvedOk(entry))
|
||||
.map((entry) => entry.input);
|
||||
return { resolvedMap, mapping, unresolved, additions };
|
||||
}
|
||||
|
||||
export function resolveAllowlistIdAdditions<T extends AllowlistUserResolutionLike>(params: {
|
||||
|
||||
@@ -278,17 +278,7 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) {
|
||||
token,
|
||||
entries: allowEntries.map((entry) => String(entry)),
|
||||
});
|
||||
const mapping: string[] = [];
|
||||
const unresolved: string[] = [];
|
||||
const additions: string[] = [];
|
||||
for (const entry of resolvedUsers) {
|
||||
if (entry.resolved && entry.id) {
|
||||
mapping.push(`${entry.input}→${entry.id}`);
|
||||
additions.push(entry.id);
|
||||
} else {
|
||||
unresolved.push(entry.input);
|
||||
}
|
||||
}
|
||||
const { mapping, unresolved, additions } = buildAllowlistResolutionSummary(resolvedUsers);
|
||||
allowFrom = mergeAllowlist({ existing: allowFrom, additions });
|
||||
summarizeMapping("discord users", mapping, unresolved, runtime);
|
||||
} catch (err) {
|
||||
|
||||
@@ -283,18 +283,17 @@ export async function monitorSlackProvider(opts: MonitorSlackOpts = {}) {
|
||||
token: resolveToken,
|
||||
entries: allowEntries.map((entry) => String(entry)),
|
||||
});
|
||||
const mapping: string[] = [];
|
||||
const unresolved: string[] = [];
|
||||
const additions: string[] = [];
|
||||
for (const entry of resolvedUsers) {
|
||||
if (entry.resolved && entry.id) {
|
||||
const note = entry.note ? ` (${entry.note})` : "";
|
||||
mapping.push(`${entry.input}→${entry.id}${note}`);
|
||||
additions.push(entry.id);
|
||||
} else {
|
||||
unresolved.push(entry.input);
|
||||
}
|
||||
}
|
||||
const { mapping, unresolved, additions } = buildAllowlistResolutionSummary(
|
||||
resolvedUsers,
|
||||
{
|
||||
formatResolved: (entry) => {
|
||||
const note = (entry as { note?: string }).note
|
||||
? ` (${(entry as { note?: string }).note})`
|
||||
: "";
|
||||
return `${entry.input}→${entry.id}${note}`;
|
||||
},
|
||||
},
|
||||
);
|
||||
allowFrom = mergeAllowlist({ existing: allowFrom, additions });
|
||||
ctx.allowFrom = normalizeAllowList(allowFrom);
|
||||
summarizeMapping("slack users", mapping, unresolved, runtime);
|
||||
|
||||
Reference in New Issue
Block a user