refactor(domain): add FeedId, EmailAddress and Domain value objects

Encapsulate the email/domain/feed-id parsing that was scattered as ad-hoc
regexes and split("@") calls into three small immutable value objects under
src/domain/value-objects/. EmailParser.extractFeedId and generateFeedId now
delegate to FeedId; the sender policy, favicon domain extraction and the admin
SenderField parse through EmailAddress/Domain.

Left as-is on purpose: forwardemail's multi-address free-text extraction and the
admin allow/block list normaliser, which operate on mixed email-or-domain input
that the single-address value objects would reject.

Behaviour-preserving; adds unit tests for each value object.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Julien Herr
2026-05-24 00:05:46 +02:00
parent 8f036cf223
commit c65aabe7f4
11 changed files with 198 additions and 35 deletions
+21 -12
View File
@@ -1,4 +1,6 @@
import { Env, FeedConfig, FeedMetadata, EmailMetadata } from "../types";
import { EmailAddress } from "./value-objects/email-address";
import { Domain } from "./value-objects/domain";
const HOUR_MS = 3_600_000;
@@ -41,29 +43,36 @@ function normalizeEmail(value: string): string {
type SenderMatch = "blocked" | "allowed" | "neutral";
function toDomains(entries: string[]): Domain[] {
return entries
.map((e) => Domain.parse(e))
.filter((d): d is Domain => d !== null);
}
function evaluateSender(
sender: string,
allowedSenders: string[],
blockedSenders: string[],
): SenderMatch {
const normalized = normalizeEmail(sender);
const domain = normalized.split("@")[1] || "";
const normalizeDomain = (e: string) => (e.startsWith("@") ? e.slice(1) : e);
const parsed = EmailAddress.parse(sender);
const normalized = parsed ? parsed.normalized : normalizeEmail(sender);
const senderDomain = parsed?.domain ?? null;
const exactBlocked = blockedSenders.filter((e) => e.includes("@"));
const exactAllowed = allowedSenders.filter((e) => e.includes("@"));
const domainBlocked = blockedSenders
.filter((e) => !e.includes("@"))
.map(normalizeDomain);
const domainAllowed = allowedSenders
.filter((e) => !e.includes("@"))
.map(normalizeDomain);
const domainBlocked = toDomains(
blockedSenders.filter((e) => !e.includes("@")),
);
const domainAllowed = toDomains(
allowedSenders.filter((e) => !e.includes("@")),
);
if (exactBlocked.includes(normalized)) return "blocked";
if (exactAllowed.includes(normalized)) return "allowed";
if (domain && domainBlocked.includes(domain)) return "blocked";
if (domain && domainAllowed.includes(domain)) return "allowed";
if (senderDomain && domainBlocked.some((d) => d.matches(senderDomain)))
return "blocked";
if (senderDomain && domainAllowed.some((d) => d.matches(senderDomain)))
return "allowed";
return "neutral";
}