refactor: share allow-from merge and sender-id checks

This commit is contained in:
Peter Steinberger
2026-02-19 14:14:02 +00:00
parent ba538c98c7
commit ffd4e85873
4 changed files with 114 additions and 48 deletions

View File

@@ -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;