mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 07:11:25 +00:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
@@ -6,8 +6,12 @@ export type SessionSendPolicyDecision = "allow" | "deny";
|
||||
|
||||
export function normalizeSendPolicy(raw?: string | null): SessionSendPolicyDecision | undefined {
|
||||
const value = raw?.trim().toLowerCase();
|
||||
if (value === "allow") return "allow";
|
||||
if (value === "deny") return "deny";
|
||||
if (value === "allow") {
|
||||
return "allow";
|
||||
}
|
||||
if (value === "deny") {
|
||||
return "deny";
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -17,7 +21,9 @@ function normalizeMatchValue(raw?: string | null) {
|
||||
}
|
||||
|
||||
function deriveChannelFromKey(key?: string) {
|
||||
if (!key) return undefined;
|
||||
if (!key) {
|
||||
return undefined;
|
||||
}
|
||||
const parts = key.split(":").filter(Boolean);
|
||||
if (parts.length >= 3 && (parts[1] === "group" || parts[1] === "channel")) {
|
||||
return normalizeMatchValue(parts[0]);
|
||||
@@ -26,9 +32,15 @@ function deriveChannelFromKey(key?: string) {
|
||||
}
|
||||
|
||||
function deriveChatTypeFromKey(key?: string): SessionChatType | undefined {
|
||||
if (!key) return undefined;
|
||||
if (key.includes(":group:")) return "group";
|
||||
if (key.includes(":channel:")) return "channel";
|
||||
if (!key) {
|
||||
return undefined;
|
||||
}
|
||||
if (key.includes(":group:")) {
|
||||
return "group";
|
||||
}
|
||||
if (key.includes(":channel:")) {
|
||||
return "channel";
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -40,10 +52,14 @@ export function resolveSendPolicy(params: {
|
||||
chatType?: SessionChatType;
|
||||
}): SessionSendPolicyDecision {
|
||||
const override = normalizeSendPolicy(params.entry?.sendPolicy);
|
||||
if (override) return override;
|
||||
if (override) {
|
||||
return override;
|
||||
}
|
||||
|
||||
const policy = params.cfg.session?.sendPolicy;
|
||||
if (!policy) return "allow";
|
||||
if (!policy) {
|
||||
return "allow";
|
||||
}
|
||||
|
||||
const channel =
|
||||
normalizeMatchValue(params.channel) ??
|
||||
@@ -57,21 +73,33 @@ export function resolveSendPolicy(params: {
|
||||
|
||||
let allowedMatch = false;
|
||||
for (const rule of policy.rules ?? []) {
|
||||
if (!rule) continue;
|
||||
if (!rule) {
|
||||
continue;
|
||||
}
|
||||
const action = normalizeSendPolicy(rule.action) ?? "allow";
|
||||
const match = rule.match ?? {};
|
||||
const matchChannel = normalizeMatchValue(match.channel);
|
||||
const matchChatType = normalizeChatType(match.chatType);
|
||||
const matchPrefix = normalizeMatchValue(match.keyPrefix);
|
||||
|
||||
if (matchChannel && matchChannel !== channel) continue;
|
||||
if (matchChatType && matchChatType !== chatType) continue;
|
||||
if (matchPrefix && !sessionKey.startsWith(matchPrefix)) continue;
|
||||
if (action === "deny") return "deny";
|
||||
if (matchChannel && matchChannel !== channel) {
|
||||
continue;
|
||||
}
|
||||
if (matchChatType && matchChatType !== chatType) {
|
||||
continue;
|
||||
}
|
||||
if (matchPrefix && !sessionKey.startsWith(matchPrefix)) {
|
||||
continue;
|
||||
}
|
||||
if (action === "deny") {
|
||||
return "deny";
|
||||
}
|
||||
allowedMatch = true;
|
||||
}
|
||||
|
||||
if (allowedMatch) return "allow";
|
||||
if (allowedMatch) {
|
||||
return "allow";
|
||||
}
|
||||
|
||||
const fallback = normalizeSendPolicy(policy.default);
|
||||
return fallback ?? "allow";
|
||||
|
||||
Reference in New Issue
Block a user