Files
community-rule/lib/server/magicLinkMail.ts
T

169 lines
5.9 KiB
TypeScript

import { ASSETS, getAssetPath } from "../assetUtils";
import { MAGIC_LINK_TTL_MINUTES } from "../privacyRetention";
import { mailLegalFooterHtml, mailLegalFooterUrls } from "./mailLegalFooter";
export const MAGIC_LINK_LOGO_PATH = getAssetPath(ASSETS.LOGO_MARK);
export type MagicLinkMailInput = {
verifyUrl: string;
origin: string;
};
export type MagicLinkMailContent = {
subject: string;
previewText: string;
text: string;
html: string;
};
function escapeHtml(value: string): string {
return value
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;");
}
function originHost(origin: string): string {
try {
return new URL(origin).host;
} catch {
return origin.replace(/^https?:\/\//, "").replace(/\/$/, "");
}
}
type BrandedVerifyMailCopy = {
verifyUrl: string;
origin: string;
subject: string;
previewText: string;
heading: string;
intro: string;
buttonLabel: string;
ignore: string;
why: string;
};
function buildBrandedVerifyMail(
copy: BrandedVerifyMailCopy,
): MagicLinkMailContent {
const origin = copy.origin.replace(/\/$/, "");
const { aboutUrl, privacyUrl } = mailLegalFooterUrls(origin);
const madeBy =
"CommunityRule is made by the Media Economies Design Lab at the University of Colorado Boulder.";
const logoUrl = `${origin}${MAGIC_LINK_LOGO_PATH}`;
const href = escapeHtml(copy.verifyUrl);
const text = [
copy.heading,
"",
copy.intro,
"",
`${copy.buttonLabel}: ${copy.verifyUrl}`,
"",
copy.ignore,
"",
madeBy,
...(privacyUrl ? [`Privacy ${privacyUrl}`] : []),
`About CommunityRule ${aboutUrl}`,
"",
copy.why,
].join("\n");
const html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>${escapeHtml(copy.subject)}</title>
</head>
<body style="margin:0;padding:0;background:#e4e4e4;">
<div style="display:none;max-height:0;overflow:hidden;mso-hide:all;">${escapeHtml(
copy.previewText,
)}</div>
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:#e4e4e4;">
<tr>
<td align="center">
<table role="presentation" width="600" cellpadding="0" cellspacing="0" style="width:600px;max-width:600px;background:#ffffff;">
<tr>
<td style="background:#000000;padding:24px 32px;">
<table role="presentation" cellpadding="0" cellspacing="0">
<tr>
<td style="font-family:'Bricolage Grotesque',Arial,sans-serif;font-size:22px;line-height:27px;color:#ffffff;padding-right:6px;">CommunityRule</td>
<td>
<img src="${escapeHtml(logoUrl)}" width="27" height="27" alt="" style="display:block;border:0;width:27px;height:27px;" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td style="padding:40px 32px 56px;">
<h1 style="margin:0 0 16px;font-family:'Bricolage Grotesque',Arial,sans-serif;font-size:36px;line-height:44px;font-weight:700;color:#000000;">${escapeHtml(copy.heading)}</h1>
<p style="margin:0 0 32px;font-family:Inter,Arial,sans-serif;font-size:18px;line-height:24px;color:#303030;">${escapeHtml(copy.intro)}</p>
<p style="margin:0 0 32px;">
<a href="${href}" style="display:inline-block;background:#141414;color:#ffffff;font-family:Inter,Arial,sans-serif;font-size:16px;line-height:20px;font-weight:500;text-decoration:none;border-radius:9999px;padding:12px 16px;">${escapeHtml(copy.buttonLabel)}</a>
</p>
<div style="height:1px;background:#e4e4e4;margin:0 0 32px;"></div>
<p style="margin:0 0 32px;font-family:Inter,Arial,sans-serif;font-size:16px;line-height:24px;color:#303030;">${escapeHtml(copy.ignore)}</p>
<div style="border-top:1px solid #e4e4e4;padding-top:24px;">
<p style="margin:0 0 12px;font-family:Inter,Arial,sans-serif;font-size:14px;line-height:20px;color:#464646;">${escapeHtml(madeBy)}</p>
<p style="margin:0;font-family:Inter,Arial,sans-serif;font-size:14px;line-height:20px;color:#464646;">
${mailLegalFooterHtml(origin)}
</p>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>`;
return {
subject: copy.subject,
previewText: copy.previewText,
text,
html,
};
}
export function buildMagicLinkMail(
input: MagicLinkMailInput,
): MagicLinkMailContent {
const minutes = MAGIC_LINK_TTL_MINUTES;
const origin = input.origin.replace(/\/$/, "");
return buildBrandedVerifyMail({
verifyUrl: input.verifyUrl,
origin,
subject: "Your login link for CommunityRule",
previewText: `It works once, for ${minutes} minutes.`,
heading: "Log in to CommunityRule",
intro: `Use the button to log in. It works once, for ${minutes} minutes.`,
buttonLabel: "Log in",
ignore:
"If you did not ask to log in, ignore this email. Nobody can log in without the link.",
why: `Sent because you asked to log in at ${originHost(origin)}.`,
});
}
export function buildEmailChangeMail(
input: MagicLinkMailInput,
): MagicLinkMailContent {
const minutes = MAGIC_LINK_TTL_MINUTES;
const origin = input.origin.replace(/\/$/, "");
return buildBrandedVerifyMail({
verifyUrl: input.verifyUrl,
origin,
subject: "Confirm your new CommunityRule email",
previewText: `It works once, for ${minutes} minutes.`,
heading: "Confirm your new email",
intro: `Use the button to confirm the new address on your CommunityRule account. It works once, for ${minutes} minutes.`,
buttonLabel: "Confirm email",
ignore:
"If you did not request this change, ignore this email. Your current login is unchanged until you confirm.",
why: `Sent because you asked to change the email on your CommunityRule account at ${originHost(origin)}.`,
});
}