Refine workspaces and event publishing; harden uploads and email delivery
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import { renderAlbumReadyEmail, emailBrowserPreview } from "./index";
|
||||
|
||||
test("gallery email escapes content and includes a plain-text alternative", () => {
|
||||
const message = renderAlbumReadyEmail({ to: "test@example.test", eventTitle: '<script>alert("x")</script>', galleryUrl: "https://manyangles.test/e/demo" });
|
||||
expect(message.html).not.toContain("<script>");
|
||||
expect(message.html).toContain("<script>");
|
||||
expect(message.html).toContain('role="presentation"');
|
||||
expect(message.html).toContain("Manyangles");
|
||||
expect(message.text).toContain("https://manyangles.test/e/demo");
|
||||
expect(message.html).toContain("Arial,Helvetica,sans-serif");
|
||||
expect(message.html).toContain("cid:manyangles-mark-v1");
|
||||
expect(message.attachments[0]?.contentId).toBe("manyangles-mark-v1");
|
||||
expect(Buffer.from(message.attachments[0]!.content, "base64").subarray(1, 4).toString()).toBe("PNG");
|
||||
expect(emailBrowserPreview(message.html)).toContain("data:image/png;base64,");
|
||||
});
|
||||
+40
-15
@@ -1,6 +1,8 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import nodemailer from "nodemailer";
|
||||
import { Resend } from "resend";
|
||||
import { EMAIL_LOGO_CID, emailLogoAttachment } from "./logo";
|
||||
export { emailBrowserPreview } from "./logo";
|
||||
|
||||
const PRIMARY = "#8b5a4a";
|
||||
|
||||
@@ -16,25 +18,33 @@ function referenceHash(value: string) {
|
||||
return createHash("sha256").update(value).digest("hex").slice(0, 16);
|
||||
}
|
||||
|
||||
function chrome(bodyHtml: string) {
|
||||
export function chrome(bodyHtml: string) {
|
||||
return `<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<link href="https://fonts.googleapis.com/css2?family=Funnel+Display:wght@500;600;700&family=Geologica:wght@400;600;700&display=swap" rel="stylesheet">
|
||||
<style>h1,h2 {font-family:'Funnel Display',Arial,Helvetica,sans-serif;line-height:1.15;letter-spacing:-.035em} @media(max-width:480px){body{padding:12px 6px!important}}</style>
|
||||
</head>
|
||||
<body style="margin:0;padding:24px 12px;background:#f6f1ea">
|
||||
<div style="max-width:560px;margin:0 auto;color:#2c2416;background:#fff;font-family:Georgia,serif">
|
||||
<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="max-width:640px;margin:0 auto;color:#292524;background:#fff;font-family:'Geologica',Arial,Helvetica,sans-serif"><tr><td>
|
||||
<div style="padding:20px 24px;border:1px solid #e8dfd2;border-top:6px solid ${PRIMARY}">
|
||||
<strong style="font-size:22px;letter-spacing:.04em">Manyangles</strong>
|
||||
<table role="presentation" cellspacing="0" cellpadding="0"><tr>
|
||||
<td style="vertical-align:middle;padding-right:8px"><img src="cid:${EMAIL_LOGO_CID}" width="32" height="32" alt="" style="display:block;border:0;width:32px;height:32px"></td>
|
||||
<td style="vertical-align:middle"><strong style="font-size:26px;line-height:32px;letter-spacing:-.04em;color:${PRIMARY}">Manyangles</strong></td>
|
||||
</tr></table>
|
||||
<p style="margin:8px 0 0;font-size:11px;font-weight:700;letter-spacing:.12em;text-transform:uppercase;color:#78716c">Every angle. One shared album.</p>
|
||||
</div>
|
||||
<div style="padding:32px;border:1px solid #e8dfd2;border-top:0">
|
||||
<div style="padding:32px;border:1px solid #e8dfd2;border-top:0;font-size:16px;line-height:1.65">
|
||||
${bodyHtml}
|
||||
</div>
|
||||
<div style="padding:18px 24px;text-align:center;background:#faf7f2;border:1px solid #e8dfd2;border-top:0">
|
||||
<strong style="font-size:18px;color:${PRIMARY}">Manyangles</strong>
|
||||
<p style="margin:8px 0;font-size:11px;letter-spacing:.08em;text-transform:uppercase;color:#7a6e5e">Keep the moment.</p>
|
||||
<p style="margin:0;font-size:11px;color:#7a6e5e">Hadlock Technologies LLC</p>
|
||||
</div>
|
||||
</div>
|
||||
</td></tr></table>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
@@ -63,15 +73,21 @@ export function getEmailReadiness() {
|
||||
};
|
||||
}
|
||||
|
||||
async function sendEmail(input: {
|
||||
export async function sendEmail(input: {
|
||||
to: string;
|
||||
subject: string;
|
||||
html: string;
|
||||
text: string;
|
||||
referenceId: string;
|
||||
}) {
|
||||
const provider = process.env.EMAIL_PROVIDER ?? "resend";
|
||||
const from =
|
||||
from?: string;
|
||||
attachments?: { filename: string; content: string; contentId: string }[];
|
||||
}, options?: { provider?: string; idempotencyKey?: string }) {
|
||||
const attachments = input.attachments ?? (input.html.includes(`cid:${EMAIL_LOGO_CID}`) ? [emailLogoAttachment] : []);
|
||||
const provider = options?.provider ?? process.env.EMAIL_PROVIDER ?? "resend";
|
||||
if (options?.provider && provider !== (process.env.EMAIL_PROVIDER ?? "resend")) throw new Error("Queued email provider differs from configured provider");
|
||||
if (!["mailpit", "smtp", "resend"].includes(provider)) throw new Error("Unsupported email provider");
|
||||
if (provider === "mailpit" && process.env.NODE_ENV === "production") throw new Error("Mailpit is development-only");
|
||||
const from = input.from ??
|
||||
process.env.EMAIL_FROM ??
|
||||
process.env.RESEND_FROM ??
|
||||
"Manyangles <photos@manyangles.test>";
|
||||
@@ -87,6 +103,8 @@ async function sendEmail(input: {
|
||||
host: process.env.SMTP_HOST ?? "127.0.0.1",
|
||||
port: Number(process.env.SMTP_PORT ?? 1025),
|
||||
secure: process.env.SMTP_SECURE === "true",
|
||||
connectionTimeout: 15_000,
|
||||
socketTimeout: 30_000,
|
||||
});
|
||||
const result = await transporter.sendMail({
|
||||
from,
|
||||
@@ -95,14 +113,14 @@ async function sendEmail(input: {
|
||||
html: input.html,
|
||||
text: input.text,
|
||||
headers: { "X-Entity-Ref-ID": input.referenceId },
|
||||
attachments: attachments.map((attachment) => ({ filename: attachment.filename, content: Buffer.from(attachment.content, "base64"), cid: attachment.contentId, contentType: "image/png", contentDisposition: "inline" as const })),
|
||||
});
|
||||
return { id: result.messageId };
|
||||
}
|
||||
|
||||
const apiKey = process.env.RESEND_API_KEY;
|
||||
if (!apiKey) {
|
||||
console.info(`[email:demo] ${input.subject} -> ${input.to}`);
|
||||
return { id: `demo-${input.referenceId}` };
|
||||
throw new Error("RESEND_API_KEY is required; use Mailpit for local email");
|
||||
}
|
||||
|
||||
const resend = new Resend(apiKey);
|
||||
@@ -113,7 +131,8 @@ async function sendEmail(input: {
|
||||
html: input.html,
|
||||
text: input.text,
|
||||
headers: { "X-Entity-Ref-ID": input.referenceId },
|
||||
});
|
||||
attachments,
|
||||
}, { idempotencyKey: options?.idempotencyKey ?? `${input.referenceId}-${referenceHash(input.to.toLowerCase())}` });
|
||||
if (error) throw new Error(error.message);
|
||||
return { id: data?.id ?? input.referenceId };
|
||||
}
|
||||
@@ -192,12 +211,14 @@ export async function sendStaffInviteEmail(message: {
|
||||
});
|
||||
}
|
||||
|
||||
export async function sendAlbumReadyEmail(message: {
|
||||
export function renderAlbumReadyEmail(message: {
|
||||
to: string;
|
||||
eventTitle: string;
|
||||
galleryUrl: string;
|
||||
}) {
|
||||
return sendEmail({
|
||||
return {
|
||||
from: process.env.EMAIL_FROM ?? process.env.RESEND_FROM ?? "Manyangles <photos@manyangles.test>",
|
||||
attachments: [emailLogoAttachment],
|
||||
to: message.to,
|
||||
subject: `The gallery for ${message.eventTitle} is ready`,
|
||||
html: chrome(`
|
||||
@@ -212,5 +233,9 @@ export async function sendAlbumReadyEmail(message: {
|
||||
`),
|
||||
text: `The gallery for ${message.eventTitle} is ready: ${message.galleryUrl}`,
|
||||
referenceId: `album-ready-${referenceHash(message.galleryUrl)}`,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export async function sendAlbumReadyEmail(message: { to: string; eventTitle: string; galleryUrl: string }) {
|
||||
return sendEmail(renderAlbumReadyEmail(message));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
// 3x PNG rendering of BrandMark. Keep v1 immutable for queued email retries.
|
||||
export const EMAIL_LOGO_CID = "manyangles-mark-v1";
|
||||
export const EMAIL_LOGO_BASE64 = "iVB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mC3lx7yzWsxhFZAXJO/LbRPF96Lp14C6sDqKgKMLg4dpI3mk+H6DmrE6goC0Cg4KZCuEn+GvgOr1FnUUfkBah3KOMCIMx3n83wLGtQKOQ5o1uS9sqykK8so5VE1y7WwJf2qZGTjGAv7Ste8+J6bSXdKMsaDFLEHzN+bWW+oJJNiVESbbhMZg2CxW2xqzQjrXdWXBh9sEajoP7iZvbkhiBfaAeEPRfQF5I0l5aR76cbZXX2XWvT5sOjumcMiLNsG1wbSOF0CE5TgIGp10GaIRQOXNT2d8Rk4dDIep0B18j19YOhGgbi91Udf8CBJjAsSOg4ZVVLIuKfMHHi8wIlf0gEvjrsxqmof8RHwEpmCl7iJxv1M1bL4/EzjQkwIEIUeoKMmAAUb2qc/OGgRoc6MKciYoIEHDZm8zW/sLHCg59QN4OwRDgmBbwd+GzH740mFF5CyxY6a0e1Og2VhbbtJA83MCfLyCXZyKcXl7K76uA7Yl20pEznjSteWLOwsLCwsLCwsLBgkcD/A+2/fsplol4AAAAASUVORK5CYII=";
|
||||
export const emailLogoAttachment = { filename: "manyangles-logo.png", content: EMAIL_LOGO_BASE64, contentId: EMAIL_LOGO_CID };
|
||||
export function emailBrowserPreview(html: string) {
|
||||
return html.replaceAll(`cid:${EMAIL_LOGO_CID}`, `data:image/png;base64,${EMAIL_LOGO_BASE64}`);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { and, eq, sql } from "drizzle-orm";
|
||||
import { auditEvents, emailDeliveries, events, getDb, guests } from "@album/database";
|
||||
import { sendEmail } from "./index";
|
||||
|
||||
export async function processEmailDelivery() {
|
||||
const db = getDb();
|
||||
const provider = process.env.EMAIL_PROVIDER ?? "resend";
|
||||
await db.execute(sql`UPDATE email_deliveries SET status = CASE WHEN provider = 'resend' AND first_attempt_at > now() - interval '23 hours' AND attempts < 5 THEN 'pending' ELSE 'review' END,
|
||||
last_error = 'Worker interrupted; delivery needs reconciliation', updated_at = now()
|
||||
WHERE provider = ${provider} AND status = 'sending' AND updated_at < now() - interval '5 minutes'`);
|
||||
const rows = await db.execute<{ id: string; event_id: string }>(sql`UPDATE email_deliveries SET status = 'sending', attempts = attempts + 1,
|
||||
first_attempt_at = coalesce(first_attempt_at, now()), updated_at = now()
|
||||
WHERE id = (SELECT id FROM email_deliveries WHERE provider = ${provider} AND status = 'pending' AND next_attempt_at <= now() ORDER BY created_at FOR UPDATE SKIP LOCKED LIMIT 1)
|
||||
RETURNING id, event_id`);
|
||||
const job = rows[0];
|
||||
if (!job) return false;
|
||||
const scope = and(eq(emailDeliveries.id, job.id), eq(emailDeliveries.eventId, job.event_id));
|
||||
const [delivery] = await db.select().from(emailDeliveries).where(scope);
|
||||
if (!delivery) return true;
|
||||
try {
|
||||
if (delivery.attempts > 1 && (!delivery.firstAttemptAt || Date.now() - delivery.firstAttemptAt.getTime() >= 23 * 3600000)) {
|
||||
await db.update(emailDeliveries).set({ status: "review", lastError: "Idempotency window expired", updatedAt: new Date() }).where(scope);
|
||||
return true;
|
||||
}
|
||||
const [event] = await db.select().from(events).where(eq(events.id, job.event_id));
|
||||
const now = new Date();
|
||||
const visible = event && (event.status !== "draft" || (event.publishAt && event.publishAt <= now)) && (!event.publishAt || event.publishAt <= now) && event.galleryPolicy !== "never" && (!event.galleryVisibleAt || event.galleryVisibleAt <= now) && (event.galleryPolicy === "automatic" || event.galleryReleasedAt || event.galleryVisibleAt) && delivery.payload.text.endsWith(`/e/${event.slug}`);
|
||||
const optedIn = await db.select({ id: guests.id }).from(guests).where(and(eq(guests.eventId, job.event_id), eq(guests.notifyWhenReady, true), sql`lower(${guests.email}) = ${delivery.recipient}`)).limit(1);
|
||||
if (!visible || !optedIn.length) {
|
||||
await db.update(emailDeliveries).set({ status: "review", lastError: "Gallery visibility or recipient consent changed", updatedAt: now }).where(scope);
|
||||
return true;
|
||||
}
|
||||
const result = await sendEmail(delivery.payload, { provider: delivery.provider, idempotencyKey: `gallery-ready/${delivery.id}` });
|
||||
await db.transaction(async (tx) => {
|
||||
await tx.update(emailDeliveries).set({ status: "sent", providerId: result.id, lastError: null, updatedAt: new Date() }).where(scope);
|
||||
await tx.update(guests).set({ notifiedAt: new Date(), updatedAt: new Date() }).where(and(eq(guests.eventId, job.event_id), eq(guests.notifyWhenReady, true), sql`lower(${guests.email}) = ${delivery.recipient}`));
|
||||
await tx.insert(auditEvents).values({ eventId: job.event_id, action: "guest.email.sent", subjectType: "email", subjectId: delivery.id, metadata: { providerId: result.id } });
|
||||
});
|
||||
} catch {
|
||||
const retry = delivery.provider === "resend" && delivery.attempts < 5 && delivery.firstAttemptAt && Date.now() - delivery.firstAttemptAt.getTime() < 23 * 3600000;
|
||||
await db.update(emailDeliveries).set({ status: retry ? "pending" : "review", nextAttemptAt: new Date(Date.now() + Math.min(3600, 30 * 2 ** delivery.attempts) * 1000), lastError: "Delivery could not be confirmed; no recipient data logged", updatedAt: new Date() }).where(scope);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import { createHmac } from "node:crypto";
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import { emailDeliveries, emailWebhookEvents, events, getDb, groups } from "@album/database";
|
||||
import { emailDeliveryOutcome, recordEmailWebhook, verifyEmailWebhook } from "./webhooks";
|
||||
|
||||
const key = Buffer.from("local-test-signing-secret-not-production");
|
||||
const secret = `whsec_${key.toString("base64")}`;
|
||||
function signed(type = "email.delivered", timestamp = Math.floor(Date.now() / 1000)) {
|
||||
const id = `test_${crypto.randomUUID()}`;
|
||||
const payload = JSON.stringify({ type, created_at: new Date().toISOString(), data: { email_id: crypto.randomUUID() } });
|
||||
const signature = createHmac("sha256", key).update(`${id}.${timestamp}.${payload}`).digest("base64");
|
||||
return { payload, headers: new Headers({ "svix-id": id, "svix-timestamp": `${timestamp}`, "svix-signature": `v1,${signature}` }) };
|
||||
}
|
||||
test("webhooks require an authentic, fresh, unmodified signature", () => {
|
||||
const input = signed();
|
||||
expect(verifyEmailWebhook(input.payload, input.headers, secret)?.outcome).toBe("delivered");
|
||||
expect(() => verifyEmailWebhook(input.payload + " ", input.headers, secret)).toThrow();
|
||||
expect(() => verifyEmailWebhook(input.payload, new Headers(), secret)).toThrow();
|
||||
const stale = signed("email.delivered", 1);
|
||||
expect(() => verifyEmailWebhook(stale.payload, stale.headers, secret)).toThrow();
|
||||
const ignored = signed("email.opened");
|
||||
expect(verifyEmailWebhook(ignored.payload, ignored.headers, secret)).toBeNull();
|
||||
});
|
||||
|
||||
test.skipIf(process.env.WEBHOOK_INTEGRATION !== "1")("early, duplicate and reordered callbacks preserve terminal outcome and event scope", async () => {
|
||||
if (!["localhost", "127.0.0.1"].includes(new URL(process.env.DATABASE_URL!).hostname)) throw new Error("Local database required");
|
||||
const db = getDb();
|
||||
const [group] = await db.select({ id: groups.id }).from(groups).limit(1);
|
||||
const input = signed("email.bounced");
|
||||
const callback = verifyEmailWebhook(input.payload, input.headers, secret)!;
|
||||
const ids = [callback.id, `${callback.id}-delivered`];
|
||||
const [event] = await db.insert(events).values({ groupId: group!.id, title: "Webhook test", slug: `webhook-${crypto.randomUUID()}` }).returning();
|
||||
try {
|
||||
await Promise.all([recordEmailWebhook(callback), recordEmailWebhook(callback)]);
|
||||
expect(await db.select().from(emailWebhookEvents).where(eq(emailWebhookEvents.id, callback.id))).toHaveLength(1);
|
||||
const [delivery] = await db.insert(emailDeliveries).values({ eventId: event!.id, recipient: "webhook@manyangles.test", provider: "resend", payload: { to: "webhook@manyangles.test", from: "test@manyangles.test", subject: "Test", html: "", text: "", referenceId: "test" } }).returning();
|
||||
const scope = and(eq(emailDeliveries.eventId, event!.id), eq(emailDeliveries.id, delivery!.id));
|
||||
expect((await db.select({ outcome: emailDeliveryOutcome }).from(emailDeliveries).where(scope))[0]?.outcome).toBeNull();
|
||||
await db.update(emailDeliveries).set({ providerId: callback.providerId }).where(scope);
|
||||
await recordEmailWebhook({ ...callback, id: ids[1]!, outcome: "delivered", occurredAt: new Date(Date.now() + 1000) });
|
||||
expect((await db.select({ outcome: emailDeliveryOutcome }).from(emailDeliveries).where(scope))[0]?.outcome).toBe("bounced");
|
||||
expect(await db.select({ outcome: emailDeliveryOutcome }).from(emailDeliveries).where(eq(emailDeliveries.eventId, crypto.randomUUID()))).toHaveLength(0);
|
||||
await db.update(emailDeliveries).set({ provider: "mailpit" }).where(scope);
|
||||
expect((await db.select({ outcome: emailDeliveryOutcome }).from(emailDeliveries).where(scope))[0]?.outcome).toBeNull();
|
||||
} finally {
|
||||
await db.delete(events).where(eq(events.id, event!.id));
|
||||
await db.delete(emailWebhookEvents).where(inArray(emailWebhookEvents.id, ids));
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import { Resend } from "resend";
|
||||
import { sql } from "drizzle-orm";
|
||||
import { emailWebhookEvents, getDb } from "@album/database";
|
||||
|
||||
const outcomes = new Set(["sent", "delivery_delayed", "delivered", "failed", "bounced", "complained"]);
|
||||
|
||||
export function verifyEmailWebhook(payload: string, headers: Headers, secret: string) {
|
||||
const event = new Resend("webhook-verification-only").webhooks.verify({
|
||||
payload,
|
||||
headers: { id: headers.get("svix-id") ?? "", timestamp: headers.get("svix-timestamp") ?? "", signature: headers.get("svix-signature") ?? "" },
|
||||
webhookSecret: secret,
|
||||
});
|
||||
const outcome = event.type.replace(/^email\./, "");
|
||||
if (!event.type.startsWith("email.") || !outcomes.has(outcome)) return null;
|
||||
const occurredAt = new Date(event.created_at);
|
||||
if (!("email_id" in event.data) || typeof event.data.email_id !== "string" || !event.data.email_id || !Number.isFinite(occurredAt.getTime())) throw new Error("Invalid event");
|
||||
return { id: headers.get("svix-id")!, providerId: event.data.email_id, outcome, occurredAt };
|
||||
}
|
||||
|
||||
export async function recordEmailWebhook(event: NonNullable<ReturnType<typeof verifyEmailWebhook>>) {
|
||||
await getDb().insert(emailWebhookEvents).values(event).onConflictDoNothing();
|
||||
}
|
||||
|
||||
// Read through the inbox so early and duplicate callbacks need no reconciliation
|
||||
// job. Negative terminal outcomes win even if delivered/sent arrives afterward.
|
||||
// Only evaluate this expression inside an authorized, event-scoped delivery query.
|
||||
export const emailDeliveryOutcome = sql<string | null>`(SELECT outcome FROM email_webhook_events
|
||||
WHERE provider_id = "email_deliveries"."provider_id" AND "email_deliveries"."provider" = 'resend'
|
||||
ORDER BY CASE outcome WHEN 'complained' THEN 6 WHEN 'bounced' THEN 5 WHEN 'failed' THEN 4
|
||||
WHEN 'delivered' THEN 3 WHEN 'delivery_delayed' THEN 2 ELSE 1 END DESC,
|
||||
occurred_at DESC, id DESC LIMIT 1)`;
|
||||
Reference in New Issue
Block a user