mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-03 15:30:21 +00:00
refactor: share allow-from merge and sender-id checks
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { firstDefined, isSenderIdAllowed, mergeAllowFromSources } from "../channels/allow-from.js";
|
||||
import type { AllowlistMatch } from "../channels/allowlist-match.js";
|
||||
|
||||
export type NormalizedAllowFrom = {
|
||||
@@ -53,21 +54,7 @@ export const normalizeAllowFrom = (list?: Array<string | number>): NormalizedAll
|
||||
export const normalizeAllowFromWithStore = (params: {
|
||||
allowFrom?: Array<string | number>;
|
||||
storeAllowFrom?: string[];
|
||||
}): NormalizedAllowFrom => {
|
||||
const combined = [...(params.allowFrom ?? []), ...(params.storeAllowFrom ?? [])]
|
||||
.map((value) => String(value).trim())
|
||||
.filter(Boolean);
|
||||
return normalizeAllowFrom(combined);
|
||||
};
|
||||
|
||||
export const firstDefined = <T>(...values: Array<T | undefined>) => {
|
||||
for (const value of values) {
|
||||
if (typeof value !== "undefined") {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
}): NormalizedAllowFrom => normalizeAllowFrom(mergeAllowFromSources(params));
|
||||
|
||||
export const isSenderAllowed = (params: {
|
||||
allow: NormalizedAllowFrom;
|
||||
@@ -75,18 +62,11 @@ export const isSenderAllowed = (params: {
|
||||
senderUsername?: string;
|
||||
}) => {
|
||||
const { allow, senderId } = params;
|
||||
if (!allow.hasEntries) {
|
||||
return true;
|
||||
}
|
||||
if (allow.hasWildcard) {
|
||||
return true;
|
||||
}
|
||||
if (senderId && allow.entries.includes(senderId)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return isSenderIdAllowed(allow, senderId, true);
|
||||
};
|
||||
|
||||
export { firstDefined };
|
||||
|
||||
export const resolveSenderAllowMatch = (params: {
|
||||
allow: NormalizedAllowFrom;
|
||||
senderId?: string;
|
||||
|
||||
Reference in New Issue
Block a user