mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
refactor(auto-reply): dedupe allowlist path and name helpers
This commit is contained in:
@@ -29,6 +29,11 @@ import type { CommandHandler } from "./commands-types.js";
|
||||
type AllowlistScope = "dm" | "group" | "all";
|
||||
type AllowlistAction = "list" | "add" | "remove";
|
||||
type AllowlistTarget = "both" | "config" | "store";
|
||||
type ResolvedAllowlistName = {
|
||||
input: string;
|
||||
resolved: boolean;
|
||||
name?: string | null;
|
||||
};
|
||||
|
||||
type AllowlistCommand =
|
||||
| {
|
||||
@@ -249,6 +254,11 @@ function resolveChannelAllowFromPaths(
|
||||
channelId: ChannelId,
|
||||
scope: AllowlistScope,
|
||||
): string[] | null {
|
||||
const supportsGroupAllowlist =
|
||||
channelId === "telegram" ||
|
||||
channelId === "whatsapp" ||
|
||||
channelId === "signal" ||
|
||||
channelId === "imessage";
|
||||
if (scope === "all") {
|
||||
return null;
|
||||
}
|
||||
@@ -257,23 +267,13 @@ function resolveChannelAllowFromPaths(
|
||||
// Canonical DM allowlist location for Slack/Discord. Legacy: dm.allowFrom.
|
||||
return ["allowFrom"];
|
||||
}
|
||||
if (
|
||||
channelId === "telegram" ||
|
||||
channelId === "whatsapp" ||
|
||||
channelId === "signal" ||
|
||||
channelId === "imessage"
|
||||
) {
|
||||
if (supportsGroupAllowlist) {
|
||||
return ["allowFrom"];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (scope === "group") {
|
||||
if (
|
||||
channelId === "telegram" ||
|
||||
channelId === "whatsapp" ||
|
||||
channelId === "signal" ||
|
||||
channelId === "imessage"
|
||||
) {
|
||||
if (supportsGroupAllowlist) {
|
||||
return ["groupAllowFrom"];
|
||||
}
|
||||
return null;
|
||||
@@ -281,6 +281,16 @@ function resolveChannelAllowFromPaths(
|
||||
return null;
|
||||
}
|
||||
|
||||
function mapResolvedAllowlistNames(entries: ResolvedAllowlistName[]): Map<string, string> {
|
||||
const map = new Map<string, string>();
|
||||
for (const entry of entries) {
|
||||
if (entry.resolved && entry.name) {
|
||||
map.set(entry.input, entry.name);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
async function resolveSlackNames(params: {
|
||||
cfg: OpenClawConfig;
|
||||
accountId?: string | null;
|
||||
@@ -292,13 +302,7 @@ async function resolveSlackNames(params: {
|
||||
return new Map<string, string>();
|
||||
}
|
||||
const resolved = await resolveSlackUserAllowlist({ token, entries: params.entries });
|
||||
const map = new Map<string, string>();
|
||||
for (const entry of resolved) {
|
||||
if (entry.resolved && entry.name) {
|
||||
map.set(entry.input, entry.name);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
return mapResolvedAllowlistNames(resolved);
|
||||
}
|
||||
|
||||
async function resolveDiscordNames(params: {
|
||||
@@ -312,13 +316,7 @@ async function resolveDiscordNames(params: {
|
||||
return new Map<string, string>();
|
||||
}
|
||||
const resolved = await resolveDiscordUserAllowlist({ token, entries: params.entries });
|
||||
const map = new Map<string, string>();
|
||||
for (const entry of resolved) {
|
||||
if (entry.resolved && entry.name) {
|
||||
map.set(entry.input, entry.name);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
return mapResolvedAllowlistNames(resolved);
|
||||
}
|
||||
|
||||
export const handleAllowlistCommand: CommandHandler = async (params, allowTextCommands) => {
|
||||
|
||||
Reference in New Issue
Block a user