import { ASSETS, getAssetPath } from "../assetUtils";
import { MAGIC_LINK_TTL_MINUTES } from "../privacyRetention";
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(/\/$/, "");
}
}
export function buildMagicLinkMail(
input: MagicLinkMailInput,
): MagicLinkMailContent {
const origin = input.origin.replace(/\/$/, "");
const minutes = MAGIC_LINK_TTL_MINUTES;
const subject = "Your login link for CommunityRule";
const previewText = `It works once, for ${minutes} minutes.`;
const heading = "Log in to CommunityRule";
const intro = `Use the button to log in. It works once, for ${minutes} minutes.`;
const ignore =
"If you did not ask to log in, ignore this email. Nobody can log in without the link.";
const madeBy =
"CommunityRule is made by the Media Economies Design Lab at the University of Colorado Boulder.";
const privacyUrl = `${origin}/privacy`;
const aboutUrl = `${origin}/about`;
const logoUrl = `${origin}${MAGIC_LINK_LOGO_PATH}`;
const host = originHost(origin);
const text = [
heading,
"",
intro,
"",
`Log in: ${input.verifyUrl}`,
"",
ignore,
"",
madeBy,
`Privacy ${privacyUrl}`,
`About CommunityRule ${aboutUrl}`,
"",
`Sent because you asked to log in at ${host}.`,
].join("\n");
const href = escapeHtml(input.verifyUrl);
const html = `
${escapeHtml(subject)}
${escapeHtml(
previewText,
)}
| CommunityRule |
|
|
${escapeHtml(heading)}
${escapeHtml(intro)}
Log in
${escapeHtml(ignore)}
|
|
`;
return { subject, previewText, text, html };
}