mirror of
https://github.com/juherr/kill-the-news.git
synced 2026-06-20 22:03:48 +00:00
7bf0f71f86
Replace the history-driven lib/ + utils/ split with DDD layers: - domain/: aggregate, repositories, value objects, pure parsers/format - application/: feed-service, email-processor, feed-fetcher, stats - infrastructure/: logging, auth, KV/R2 adapters, HTTP, framework glue Pure file relocation; imports updated mechanically. Behaviour unchanged. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
import PostalMime from "postal-mime";
|
|
import { Env } from "../types";
|
|
import { processEmail, RawAttachment } from "../application/email-processor";
|
|
import { normalizeCid } from "../infrastructure/html-processor";
|
|
import { logger } from "./logger";
|
|
|
|
export async function handleCloudflareEmail(
|
|
message: ForwardableEmailMessage,
|
|
env: Env,
|
|
ctx: ExecutionContext,
|
|
): Promise<void> {
|
|
try {
|
|
const email = await PostalMime.parse(message.raw);
|
|
|
|
const fromAddress = email.from?.address ?? message.from;
|
|
const from =
|
|
email.from?.name && email.from.address
|
|
? `${email.from.name} <${email.from.address}>`
|
|
: fromAddress;
|
|
|
|
const headers: Record<string, string> = {};
|
|
for (const h of email.headers) {
|
|
headers[h.key] = h.value;
|
|
}
|
|
|
|
const rawAttachments: RawAttachment[] = (email.attachments ?? [])
|
|
.filter((a) => a.content instanceof ArrayBuffer)
|
|
.map((a) => ({
|
|
filename: a.filename || "attachment",
|
|
contentType: a.mimeType || "application/octet-stream",
|
|
content: a.content as ArrayBuffer,
|
|
contentId: normalizeCid(a.contentId),
|
|
}));
|
|
|
|
const result = await processEmail(
|
|
{
|
|
toAddress: message.to,
|
|
from,
|
|
senders: [message.from],
|
|
subject: email.subject ?? "(no subject)",
|
|
content: email.html ?? email.text ?? "",
|
|
receivedAt: email.date ? new Date(email.date).getTime() : Date.now(),
|
|
headers,
|
|
attachments: rawAttachments,
|
|
},
|
|
env,
|
|
ctx,
|
|
);
|
|
if (!result.ok) {
|
|
logger.warn("Inbound email rejected", {
|
|
to: message.to,
|
|
reason: result.reason,
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error("Error processing Cloudflare email:", error);
|
|
}
|
|
}
|