mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 22:18:27 +00:00
refactor(src): split oversized modules
This commit is contained in:
141
src/slack/monitor/channel-config.ts
Normal file
141
src/slack/monitor/channel-config.ts
Normal file
@@ -0,0 +1,141 @@
|
||||
import type { SlackReactionNotificationMode } from "../../config/config.js";
|
||||
import type { SlackMessageEvent } from "../types.js";
|
||||
import {
|
||||
allowListMatches,
|
||||
normalizeAllowListLower,
|
||||
normalizeSlackSlug,
|
||||
} from "./allow-list.js";
|
||||
|
||||
export type SlackChannelConfigResolved = {
|
||||
allowed: boolean;
|
||||
requireMention: boolean;
|
||||
allowBots?: boolean;
|
||||
users?: Array<string | number>;
|
||||
skills?: string[];
|
||||
systemPrompt?: string;
|
||||
};
|
||||
|
||||
function firstDefined<T>(...values: Array<T | undefined>) {
|
||||
for (const value of values) {
|
||||
if (typeof value !== "undefined") return value;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function shouldEmitSlackReactionNotification(params: {
|
||||
mode: SlackReactionNotificationMode | undefined;
|
||||
botId?: string | null;
|
||||
messageAuthorId?: string | null;
|
||||
userId: string;
|
||||
userName?: string | null;
|
||||
allowlist?: Array<string | number> | null;
|
||||
}) {
|
||||
const { mode, botId, messageAuthorId, userId, userName, allowlist } = params;
|
||||
const effectiveMode = mode ?? "own";
|
||||
if (effectiveMode === "off") return false;
|
||||
if (effectiveMode === "own") {
|
||||
if (!botId || !messageAuthorId) return false;
|
||||
return messageAuthorId === botId;
|
||||
}
|
||||
if (effectiveMode === "allowlist") {
|
||||
if (!Array.isArray(allowlist) || allowlist.length === 0) return false;
|
||||
const users = normalizeAllowListLower(allowlist);
|
||||
return allowListMatches({
|
||||
allowList: users,
|
||||
id: userId,
|
||||
name: userName ?? undefined,
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export function resolveSlackChannelLabel(params: {
|
||||
channelId?: string;
|
||||
channelName?: string;
|
||||
}) {
|
||||
const channelName = params.channelName?.trim();
|
||||
if (channelName) {
|
||||
const slug = normalizeSlackSlug(channelName);
|
||||
return `#${slug || channelName}`;
|
||||
}
|
||||
const channelId = params.channelId?.trim();
|
||||
return channelId ? `#${channelId}` : "unknown channel";
|
||||
}
|
||||
|
||||
export function resolveSlackChannelConfig(params: {
|
||||
channelId: string;
|
||||
channelName?: string;
|
||||
channels?: Record<
|
||||
string,
|
||||
{
|
||||
enabled?: boolean;
|
||||
allow?: boolean;
|
||||
requireMention?: boolean;
|
||||
allowBots?: boolean;
|
||||
users?: Array<string | number>;
|
||||
skills?: string[];
|
||||
systemPrompt?: string;
|
||||
}
|
||||
>;
|
||||
}): SlackChannelConfigResolved | null {
|
||||
const { channelId, channelName, channels } = params;
|
||||
const entries = channels ?? {};
|
||||
const keys = Object.keys(entries);
|
||||
const normalizedName = channelName ? normalizeSlackSlug(channelName) : "";
|
||||
const directName = channelName ? channelName.trim() : "";
|
||||
const candidates = [
|
||||
channelId,
|
||||
channelName ? `#${directName}` : "",
|
||||
directName,
|
||||
normalizedName,
|
||||
].filter(Boolean);
|
||||
|
||||
let matched:
|
||||
| {
|
||||
enabled?: boolean;
|
||||
allow?: boolean;
|
||||
requireMention?: boolean;
|
||||
allowBots?: boolean;
|
||||
users?: Array<string | number>;
|
||||
skills?: string[];
|
||||
systemPrompt?: string;
|
||||
}
|
||||
| undefined;
|
||||
for (const candidate of candidates) {
|
||||
if (candidate && entries[candidate]) {
|
||||
matched = entries[candidate];
|
||||
break;
|
||||
}
|
||||
}
|
||||
const fallback = entries["*"];
|
||||
|
||||
if (keys.length === 0) {
|
||||
return { allowed: true, requireMention: true };
|
||||
}
|
||||
if (!matched && !fallback) {
|
||||
return { allowed: false, requireMention: true };
|
||||
}
|
||||
|
||||
const resolved = matched ?? fallback ?? {};
|
||||
const allowed =
|
||||
firstDefined(
|
||||
resolved.enabled,
|
||||
resolved.allow,
|
||||
fallback?.enabled,
|
||||
fallback?.allow,
|
||||
true,
|
||||
) ?? true;
|
||||
const requireMention =
|
||||
firstDefined(resolved.requireMention, fallback?.requireMention, true) ??
|
||||
true;
|
||||
const allowBots = firstDefined(resolved.allowBots, fallback?.allowBots);
|
||||
const users = firstDefined(resolved.users, fallback?.users);
|
||||
const skills = firstDefined(resolved.skills, fallback?.skills);
|
||||
const systemPrompt = firstDefined(
|
||||
resolved.systemPrompt,
|
||||
fallback?.systemPrompt,
|
||||
);
|
||||
return { allowed, requireMention, allowBots, users, skills, systemPrompt };
|
||||
}
|
||||
|
||||
export type { SlackMessageEvent };
|
||||
Reference in New Issue
Block a user