Give finished Rules a print path, folded launch counts, and a two-day follow-up.
Export is PDF, RULE.md, and Print on US Letter, with every shared term, no member names, and a takeaway that points at Ask an organizer. Finished Rules keep type and place so the team can read folded counts without a database, Umami can sit beside the app with no cookies, and the person who holds the pen gets working PDF and RULE.md links 48 hours later.
This commit is contained in:
1 parent
0efc907633
commit
d9c475a967
74 files changed
+2401
-630
No files matched your search
@@ -12,6 +12,10 @@ import {
|
||||
stakeholderInviteReplyTo,
|
||||
type RuleStakeholderInviteMailInput,
|
||||
} from "./ruleStakeholderInviteMail";
|
||||
import {
|
||||
buildRuleFollowUpMail,
|
||||
type RuleFollowUpMailInput,
|
||||
} from "./ruleFollowUpMail";
|
||||
|
||||
function escapeHtml(value: string): string {
|
||||
return value
|
||||
@@ -115,6 +119,20 @@ export async function sendRuleStakeholderInviteEmail(
|
||||
});
|
||||
}
|
||||
|
||||
/** 48-hour print follow-up to the address that holds the pen. */
|
||||
export async function sendRuleFollowUpEmail(
|
||||
input: RuleFollowUpMailInput,
|
||||
): Promise<void> {
|
||||
const { subject, text, html } = buildRuleFollowUpMail(input);
|
||||
await sendHtmlMail({
|
||||
to: input.to,
|
||||
subject,
|
||||
text,
|
||||
html,
|
||||
devLog: `[dev] Rule follow-up mail for ${input.to}`,
|
||||
});
|
||||
}
|
||||
|
||||
/** Notify support/organizers when a visitor submits the Ask an organizer form. */
|
||||
export async function sendOrganizerInquiryNotification(params: {
|
||||
/** Destination inbox (e.g. from ORGANIZER_INQUIRY_TO). */
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { publishedRuleAnalyticsFromDocument } from "../create/publishedRuleAnalytics";
|
||||
|
||||
export function publishedRuleAnalyticsColumns(document: unknown): {
|
||||
communityLocation: string | null;
|
||||
orgType: string | null;
|
||||
} {
|
||||
return publishedRuleAnalyticsFromDocument(document);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { createHmac, timingSafeEqual } from "crypto";
|
||||
|
||||
export function ruleExportToken(ruleId: string, pepper: string): string {
|
||||
return createHmac("sha256", pepper)
|
||||
.update(`rule-export:${ruleId}`)
|
||||
.digest("base64url");
|
||||
}
|
||||
|
||||
export function ruleExportTokenMatches(
|
||||
ruleId: string,
|
||||
token: string,
|
||||
pepper: string,
|
||||
): boolean {
|
||||
if (token.length === 0) return false;
|
||||
const expected = Buffer.from(ruleExportToken(ruleId, pepper));
|
||||
const actual = Buffer.from(token);
|
||||
if (expected.length !== actual.length) return false;
|
||||
return timingSafeEqual(expected, actual);
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
import type { PrismaClient } from "@prisma/client";
|
||||
import { logger } from "../logger";
|
||||
import { originFromHost } from "../siteMetadata";
|
||||
import { ruleExportMarkdownUrl, ruleExportPdfUrl } from "../sharePaths";
|
||||
import { getSessionPepper } from "./env";
|
||||
import { sendRuleFollowUpEmail } from "./mail";
|
||||
import { ruleExportToken } from "./ruleExportToken";
|
||||
|
||||
export const RULE_FOLLOW_UP_AFTER_MS = 48 * 60 * 60 * 1000;
|
||||
export const RULE_FOLLOW_UP_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
|
||||
|
||||
export type RuleFollowUpResult = {
|
||||
sent: number;
|
||||
failed: number;
|
||||
skipped: number;
|
||||
};
|
||||
|
||||
export async function sendDueRuleFollowUpEmails(
|
||||
db: PrismaClient,
|
||||
now = new Date(),
|
||||
): Promise<RuleFollowUpResult> {
|
||||
const result: RuleFollowUpResult = { sent: 0, failed: 0, skipped: 0 };
|
||||
|
||||
let pepper: string;
|
||||
try {
|
||||
pepper = getSessionPepper();
|
||||
} catch (err) {
|
||||
logger.error({
|
||||
scope: "ruleFollowUp",
|
||||
message: err instanceof Error ? err.message : "SESSION_SECRET missing",
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
const origin = originFromHost(null);
|
||||
const dueBy = new Date(now.getTime() - RULE_FOLLOW_UP_AFTER_MS);
|
||||
const notBefore = new Date(now.getTime() - RULE_FOLLOW_UP_MAX_AGE_MS);
|
||||
|
||||
const rows = await db.publishedRule.findMany({
|
||||
where: {
|
||||
followUpEmailSentAt: null,
|
||||
userId: { not: null },
|
||||
createdAt: { lte: dueBy, gte: notBefore },
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
user: { select: { email: true } },
|
||||
},
|
||||
});
|
||||
|
||||
for (const row of rows) {
|
||||
const email = row.user?.email?.trim().toLowerCase() ?? "";
|
||||
if (!email.includes("@")) {
|
||||
result.skipped += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
const token = ruleExportToken(row.id, pepper);
|
||||
try {
|
||||
await sendRuleFollowUpEmail({
|
||||
to: email,
|
||||
origin,
|
||||
communityName: row.title,
|
||||
pdfUrl: ruleExportPdfUrl(origin, row.id, token),
|
||||
markdownUrl: ruleExportMarkdownUrl(origin, row.id, token),
|
||||
});
|
||||
await db.publishedRule.update({
|
||||
where: { id: row.id },
|
||||
data: { followUpEmailSentAt: now },
|
||||
});
|
||||
result.sent += 1;
|
||||
} catch (err) {
|
||||
result.failed += 1;
|
||||
logger.error({
|
||||
scope: "ruleFollowUp",
|
||||
message: err instanceof Error ? err.message : "send failed",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
logger.info({
|
||||
scope: "ruleFollowUp",
|
||||
sent: result.sent,
|
||||
failed: result.failed,
|
||||
skipped: result.skipped,
|
||||
});
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
import { ASSETS, getAssetPath } from "../assetUtils";
|
||||
import { communityPossessive } from "../create/ruleInviteCardMeta";
|
||||
import { RULE_MD_FILENAME } from "../create/ruleExportModel";
|
||||
|
||||
export const RULE_FOLLOW_UP_LOGO_PATH = getAssetPath(ASSETS.LOGO_MARK);
|
||||
|
||||
export type RuleFollowUpMailInput = {
|
||||
to: string;
|
||||
origin: string;
|
||||
communityName: string;
|
||||
pdfUrl: string;
|
||||
markdownUrl: string;
|
||||
};
|
||||
|
||||
export type RuleFollowUpMailContent = {
|
||||
subject: string;
|
||||
previewText: string;
|
||||
text: string;
|
||||
html: string;
|
||||
};
|
||||
|
||||
function escapeHtml(value: string): string {
|
||||
return value
|
||||
.replace(/&/g, "&")
|
||||
.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 buildRuleFollowUpMail(
|
||||
input: RuleFollowUpMailInput,
|
||||
): RuleFollowUpMailContent {
|
||||
const communityName = input.communityName.trim() || "A community";
|
||||
const possessive = communityPossessive(communityName);
|
||||
const origin = input.origin.replace(/\/$/, "");
|
||||
const kitUrl = `${origin}/kit`;
|
||||
const host = originHost(origin);
|
||||
const subject = `${possessive} Rule, printed`;
|
||||
const previewText = `Here is ${possessive} Rule as a PDF, ready to print.`;
|
||||
const intro = `Here is ${possessive} Rule as a PDF, ready to print or drop into your onboarding doc.`;
|
||||
const putIt =
|
||||
"Put it where members will see it, on the wall where you meet or at the top of the doc new members read.";
|
||||
const fixNow =
|
||||
"If anything in it already feels wrong, that is normal, and it is easier to fix now than in a year.";
|
||||
const officeHours = `Bring it to office hours if you want a second read. No signup, and the next one is listed at ${host}/kit.`;
|
||||
const signOff = "CommunityRule";
|
||||
const why = `Sent because you finished a Rule at ${host}.`;
|
||||
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}${RULE_FOLLOW_UP_LOGO_PATH}`;
|
||||
|
||||
const text = [
|
||||
"Your Rule is ready to print",
|
||||
"",
|
||||
intro,
|
||||
"",
|
||||
"Download the PDF",
|
||||
input.pdfUrl,
|
||||
"",
|
||||
`Or download ${RULE_MD_FILENAME} for a wiki or a repository.`,
|
||||
input.markdownUrl,
|
||||
"",
|
||||
putIt,
|
||||
"",
|
||||
fixNow,
|
||||
"",
|
||||
officeHours,
|
||||
kitUrl,
|
||||
"",
|
||||
signOff,
|
||||
"",
|
||||
why,
|
||||
"",
|
||||
madeBy,
|
||||
`Privacy ${privacyUrl}`,
|
||||
].join("\n");
|
||||
|
||||
const html = followUpHtml({
|
||||
subject,
|
||||
previewText,
|
||||
intro,
|
||||
pdfUrl: input.pdfUrl,
|
||||
markdownUrl: input.markdownUrl,
|
||||
putIt,
|
||||
fixNow,
|
||||
officeHours,
|
||||
kitUrl,
|
||||
signOff,
|
||||
why,
|
||||
madeBy,
|
||||
privacyUrl,
|
||||
aboutUrl,
|
||||
logoUrl,
|
||||
kitHostPath: `${host}/kit`,
|
||||
});
|
||||
|
||||
return { subject, previewText, text, html };
|
||||
}
|
||||
|
||||
function followUpHtml(opts: {
|
||||
subject: string;
|
||||
previewText: string;
|
||||
intro: string;
|
||||
pdfUrl: string;
|
||||
markdownUrl: string;
|
||||
putIt: string;
|
||||
fixNow: string;
|
||||
officeHours: string;
|
||||
kitUrl: string;
|
||||
signOff: string;
|
||||
why: string;
|
||||
madeBy: string;
|
||||
privacyUrl: string;
|
||||
aboutUrl: string;
|
||||
logoUrl: string;
|
||||
kitHostPath: string;
|
||||
}): string {
|
||||
const pdfHref = escapeHtml(opts.pdfUrl);
|
||||
const mdHref = escapeHtml(opts.markdownUrl);
|
||||
const kitHref = escapeHtml(opts.kitUrl);
|
||||
const kitHostPath = escapeHtml(opts.kitHostPath);
|
||||
const officeLead = "Bring it to office hours if you want a second read. No signup, and the next one is listed at ";
|
||||
const officeHtml = opts.officeHours.startsWith(officeLead)
|
||||
? `${escapeHtml(officeLead)}<a href="${kitHref}" style="color:#000000;text-decoration:underline;">${kitHostPath}</a>.`
|
||||
: escapeHtml(opts.officeHours);
|
||||
|
||||
return `<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
||||
<title>${escapeHtml(opts.subject)}</title>
|
||||
</head>
|
||||
<body style="margin:0;padding:0;background:#e4e4e4;">
|
||||
<div style="display:none;max-height:0;overflow:hidden;mso-hide:all;">${escapeHtml(
|
||||
opts.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(opts.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;">Your Rule is ready to print</h1>
|
||||
<p style="margin:0 0 32px;font-family:Inter,Arial,sans-serif;font-size:18px;line-height:24px;color:#303030;">${escapeHtml(opts.intro)}</p>
|
||||
<p style="margin:0 0 16px;">
|
||||
<a href="${pdfHref}" 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;">Download the PDF</a>
|
||||
</p>
|
||||
<p style="margin:0 0 32px;font-family:Inter,Arial,sans-serif;font-size:14px;line-height:20px;color:#464646;">
|
||||
Or download <a href="${mdHref}" style="color:#000000;text-decoration:underline;">${RULE_MD_FILENAME}</a> for a wiki or a repository.
|
||||
</p>
|
||||
<div style="height:1px;background:#e4e4e4;margin:0 0 32px;"></div>
|
||||
<p style="margin:0 0 20px;font-family:Inter,Arial,sans-serif;font-size:16px;line-height:24px;color:#303030;">${escapeHtml(opts.putIt)}</p>
|
||||
<p style="margin:0 0 20px;font-family:Inter,Arial,sans-serif;font-size:16px;line-height:24px;color:#303030;">${escapeHtml(opts.fixNow)}</p>
|
||||
<p style="margin:0 0 20px;font-family:Inter,Arial,sans-serif;font-size:16px;line-height:24px;color:#303030;">${officeHtml}</p>
|
||||
<p style="margin:0 0 32px;font-family:Inter,Arial,sans-serif;font-size:16px;line-height:24px;color:#000000;">${escapeHtml(opts.signOff)}</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(opts.why)}</p>
|
||||
<p style="margin:0 0 12px;font-family:Inter,Arial,sans-serif;font-size:14px;line-height:20px;color:#464646;">${escapeHtml(opts.madeBy)}</p>
|
||||
<p style="margin:0;font-family:Inter,Arial,sans-serif;font-size:14px;line-height:20px;color:#464646;">
|
||||
<a href="${escapeHtml(opts.privacyUrl)}" style="color:#464646;text-decoration:underline;">Privacy</a>
|
||||
|
||||
<a href="${escapeHtml(opts.aboutUrl)}" style="color:#464646;text-decoration:underline;">About CommunityRule</a>
|
||||
</p>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
export const RULE_COUNT_FOLD_THRESHOLD = 5;
|
||||
|
||||
export type RuleCountRow = {
|
||||
orgType: string | null;
|
||||
place: string | null;
|
||||
};
|
||||
|
||||
export type RuleCountCell = {
|
||||
orgType: string | null;
|
||||
place: string | null;
|
||||
count: number;
|
||||
folded: boolean;
|
||||
};
|
||||
|
||||
export type RuleLaunchCounts = {
|
||||
finished: number;
|
||||
cells: RuleCountCell[];
|
||||
};
|
||||
|
||||
function normalizePlace(raw: string | null): string {
|
||||
return (raw ?? "").trim().replace(/\s+/g, " ").toLowerCase();
|
||||
}
|
||||
|
||||
function cellKey(orgType: string | null, placeKey: string): string {
|
||||
return `${orgType ?? ""}\u0000${placeKey}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finished-Rule counts by group type and place. Any cell under five is folded
|
||||
* so a single group cannot be picked out.
|
||||
*/
|
||||
export function aggregateRuleLaunchCounts(rows: RuleCountRow[]): RuleLaunchCounts {
|
||||
const grouped = new Map<
|
||||
string,
|
||||
{ orgType: string | null; place: string | null; count: number }
|
||||
>();
|
||||
for (const row of rows) {
|
||||
const orgType = row.orgType && row.orgType.trim().length > 0 ? row.orgType : null;
|
||||
const placeRaw = row.place && row.place.trim().length > 0 ? row.place.trim() : null;
|
||||
const key = cellKey(orgType, normalizePlace(placeRaw));
|
||||
const existing = grouped.get(key);
|
||||
if (existing) {
|
||||
existing.count += 1;
|
||||
} else {
|
||||
grouped.set(key, { orgType, place: placeRaw, count: 1 });
|
||||
}
|
||||
}
|
||||
|
||||
const shown: RuleCountCell[] = [];
|
||||
let foldedCount = 0;
|
||||
for (const cell of grouped.values()) {
|
||||
if (cell.count < RULE_COUNT_FOLD_THRESHOLD) {
|
||||
foldedCount += cell.count;
|
||||
continue;
|
||||
}
|
||||
shown.push({ ...cell, folded: false });
|
||||
}
|
||||
shown.sort((a, b) => b.count - a.count || (a.orgType ?? "").localeCompare(b.orgType ?? ""));
|
||||
if (foldedCount > 0) {
|
||||
shown.push({
|
||||
orgType: null,
|
||||
place: null,
|
||||
count: foldedCount,
|
||||
folded: true,
|
||||
});
|
||||
}
|
||||
return { finished: rows.length, cells: shown };
|
||||
}
|
||||
Reference in new issue
Block a user