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, ">")
.replace(/"/g, """);
}
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 = `
${escapeHtml(copy.subject)}
${escapeHtml(
copy.previewText,
)}
| CommunityRule |
|
|
${escapeHtml(copy.heading)}
${escapeHtml(copy.intro)}
${escapeHtml(copy.buttonLabel)}
${escapeHtml(copy.ignore)}
${escapeHtml(madeBy)}
${mailLegalFooterHtml(origin)}
|
|
`;
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)}.`,
});
}