pages.ts

14.7 KB · raw

1import type { Context, Hono } from "hono";2import { saveByPrefix } from "../mirror/queries.ts";3import { type Html, html } from "../pages/html.ts";4import {5	cacheFor,6	formatDate,7	notFound,8	openPublicRepo,9	type PageContext,10	type PublicRepo,11	page,12	relativeTime,13} from "../pages/layout.ts";14import { visibleReleases } from "../releases/releases.ts";15import { notify } from "../server/db.ts";16import { checkStamp, FILINGS_PER_HOUR, formStamp, ipHash, MAX_CHARS } from "./spam.ts";17import {18	addEntry,19	fileIssue,20	filingsSince,21	findIssue,22	hashToken,23	ISSUE_TYPES,24	type Issue,25	type IssueType,26	isBlocked,27	issueCounts,28	listIssues,29	threadEntries,30} from "./store.ts";3132type Fields = Record<string, string>;33type Notice = { error?: string; draft?: string };3435const TOKEN = /^[0-9a-f]{64}$/;36const FORM_FIELDS = ["title", "body", "type", "version", "name", "email", "website", "stamp"];37const unixNow = () => Math.floor(Date.now() / 1000);38// The same words whatever the reason, so a blocked address learns nothing more.39const BLOCKED = "This address can't post here.";4041/** §23: the list, the filing form and the thread with its follow-up links. */42export function registerIssuePages(app: Hono, ctx: PageContext): void {43	app.get("/:repo/issues", (c) => {44		const repo = openPublicRepo(ctx, c.req.param("repo"));45		if (!repo) return notFound(c, ctx);46		const status = c.req.query("status") === "closed" ? "closed" : "open";47		cacheFor(c, false);48		return c.html(listPage(ctx, repo, status, c.req.query("q") ?? "", c.req.query("type") ?? ""));49	});5051	app.get("/:repo/issues/new", (c) => {52		const repo = openPublicRepo(ctx, c.req.param("repo"));53		if (!repo) return notFound(c, ctx);54		const version = visibleReleases(ctx.kepr, repo.mirror, repo.settings.name)[0]?.version ?? "";55		return formPage(c, ctx, repo, { type: "bug", version }, "", 200);56	});5758	app.post("/:repo/issues/new", async (c) => {59		const repo = openPublicRepo(ctx, c.req.param("repo"));60		if (!repo) return notFound(c, ctx);61		if (repo.settings.issuesPaused) return formPage(c, ctx, repo, {}, "", 403);62		const fields = await readFields(c, FORM_FIELDS);63		const now = unixNow();64		const refuse = (message: string, status: 400 | 403 | 429) =>65			formPage(c, ctx, repo, fields, message, status);66		// A bot that fills the hidden field learns nothing from the answer.67		if (fields.website !== "") return refuse("This issue could not be filed.", 400);68		const stamp = checkStamp(ctx.kepr, c, fields.stamp ?? "", now);69		if (stamp === "invalid") return refuse("This form expired. Please send it again.", 403);70		if (stamp === "too-fast") return refuse("That was quick. Please send it again.", 400);71		const title = fields.title?.trim() ?? "";72		const body = fields.body?.trim() ?? "";73		if (title === "" || body === "") return refuse("A title and what happened are needed.", 400);74		if (!ISSUE_TYPES.includes(fields.type as IssueType)) return refuse("Pick a type.", 400);75		if (Object.values(fields).some((value) => value.length > MAX_CHARS)) {76			return refuse(77				`Please keep each field under ${MAX_CHARS.toLocaleString("en-US")} characters.`,78				400,79			);80		}81		const hash = ipHash(ctx.kepr, c);82		if (isBlocked(ctx.kepr, hash)) return refuse(BLOCKED, 403);83		if (filingsSince(ctx.kepr, hash, now - 3600) >= FILINGS_PER_HOUR) {84			return refuse("Too many issues from here this hour. Please try again later.", 429);85		}86		const name = repo.settings.name;87		const filed = fileIssue(ctx.kepr, {88			repo: name,89			title,90			body,91			type: fields.type as IssueType,92			version: fields.version?.trim() ?? "",93			reporterName: fields.name?.trim() ?? "",94			contactEmail: fields.email?.trim() ?? "",95			ipHash: hash,96			filedAt: now,97		});98		notify(99			ctx.kepr,100			"issue_filed",101			`New issue ${name} #${filed.number}: ${title}`,102			`/admin/issues/${name}/${filed.number}`,103		);104		const link = `https://${ctx.domain}/${name}/issues/${filed.number}?token=${filed.token}`;105		privatePage(c);106		return c.html(107			page(ctx, {108				title: `issue #${filed.number} filed · ${name}`,109				description: "",110				path: c.req.path,111				body: html`${crumbs(name)}112<h1 class="file-page-title">Filed as113<a href="/${name}/issues/${filed.number}">#${filed.number}</a></h1>114<p>This is your private link for adding to the issue later. It is shown only this once,115so keep it; anyone who has it can post as you.</p>116<p class="issue-link mono"><a href="${link}">${link}</a></p>`,117			}),118		);119	});120121	app.get("/:repo/issues/:n{[0-9]{1,9}}", (c) => {122		const repo = openPublicRepo(ctx, c.req.param("repo"));123		const issue = repo && findIssue(ctx.kepr, repo.settings.name, Number(c.req.param("n")));124		if (!repo || !issue) return notFound(c, ctx);125		const token = c.req.query("token");126		if (token === undefined) {127			cacheFor(c, false);128			return c.html(threadPage(c, ctx, repo, issue, null, {}));129		}130		privatePage(c);131		return c.html(threadPage(c, ctx, repo, issue, token, {}));132	});133134	app.post("/:repo/issues/:n{[0-9]{1,9}}", async (c) => {135		const repo = openPublicRepo(ctx, c.req.param("repo"));136		const issue = repo && findIssue(ctx.kepr, repo.settings.name, Number(c.req.param("n")));137		if (!repo || !issue) return notFound(c, ctx);138		const { token = "", body = "", stamp = "" } = await readFields(c, ["token", "body", "stamp"]);139		privatePage(c);140		const refuse = (error: string, status: 400 | 403) =>141			c.html(threadPage(c, ctx, repo, issue, token, { error, draft: body }), status);142		if (!ownsIssue(issue, token))143			return refuse("This follow-up link does not match the issue.", 403);144		const check = checkStamp(ctx.kepr, c, stamp, unixNow());145		if (check === "invalid") return refuse("This form expired. Please send it again.", 403);146		if (check === "too-fast") return refuse("That was quick. Please send it again.", 400);147		if (isBlocked(ctx.kepr, ipHash(ctx.kepr, c))) return refuse(BLOCKED, 403);148		const text = body.trim();149		if (text === "") return refuse("Write something to add.", 400);150		if (text.length > MAX_CHARS) {151			return refuse(`Please keep it under ${MAX_CHARS.toLocaleString("en-US")} characters.`, 400);152		}153		const entry = addEntry(ctx.kepr, issue.id, "reporter", text, unixNow());154		notify(155			ctx.kepr,156			"issue_followup",157			`Follow-up on ${repo.settings.name} #${issue.number}: ${issue.title}`,158			`/admin/issues/${repo.settings.name}/${issue.number}`,159		);160		return c.redirect(161			`/${repo.settings.name}/issues/${issue.number}?token=${token}#entry-${entry}`,162			303,163		);164	});165}166167async function readFields(c: Context, names: string[]): Promise<Fields> {168	const form = await c.req.parseBody();169	const fields: Fields = {};170	for (const name of names) {171		const value = form[name];172		fields[name] = typeof value === "string" ? value : "";173	}174	return fields;175}176177function ownsIssue(issue: Issue, token: string): boolean {178	return TOKEN.test(token) && issue.token_hash === hashToken(token);179}180181// A page carrying a token or a form stamp is never cached, and its URL never leaks as a referrer.182function privatePage(c: Context): void {183	c.header("Cache-Control", "no-store");184	c.header("Referrer-Policy", "no-referrer");185}186187function crumbs(repo: string): Html {188	return html`<p class="breadcrumb mono">189<a href="/${repo}">${repo}</a> / <a href="/${repo}/issues">issues</a></p>`;190}191192function listPage(193	ctx: PageContext,194	repo: PublicRepo,195	status: "open" | "closed",196	query: string,197	type: string,198): string {199	const name = repo.settings.name;200	const counts = issueCounts(ctx.kepr, name);201	const filter = ISSUE_TYPES.includes(type as IssueType) ? type : "";202	const issues = listIssues(ctx.kepr, name, status).filter(203		(issue) =>204			(!filter || issue.type === filter) &&205			issue.title.toLowerCase().includes(query.trim().toLowerCase()),206	);207	const tab = (which: "open" | "closed") =>208		html`<a class="issues-tab${which === status ? " active" : ""}"209			href="/${name}/issues${which === "closed" ? "?status=closed" : ""}">210${which} <span class="muted">${counts[which]}</span></a>`;211	return page(ctx, {212		title: `issues · ${name}`,213		description: `Issues filed against ${name}.`,214		path: `/${name}/issues`,215		body: html`<p class="breadcrumb mono"><a href="/${name}">${name}</a></p>216<h1 class="file-page-title">issues</h1>217<p class="issues-bar mono">${tab("open")}${tab("closed")}218${!repo.settings.issuesPaused && html`<a class="issues-new" href="/${name}/issues/new">report an issue →</a>`}</p>219<form class="issues-filter" method="get" action="/${name}/issues">220<input type="hidden" name="status" value="${status}">221<input class="issues-input" name="q" type="search" aria-label="search issue titles"222placeholder="search issues" value="${query}">223<select class="issues-input" name="type" aria-label="issue type">224<option value="">all types</option>225${ISSUE_TYPES.map(226	(value) =>227		html`<option value="${value}"${filter === value ? html` selected` : ""}>${value}</option>`,228)}229</select><button class="issues-submit" type="submit">filter</button></form>230<section class="repo-section">231${232	issues.length > 0233		? issues.map(234				(issue) =>235					html`<a class="issue-row" href="/${name}/issues/${issue.number}">236<span class="issue-symbol issue-symbol--${issue.status}">237${issue.status === "open" ? "◉" : "◈"}</span>238<span class="issue-title">${issue.title}</span>239${/* this line stays whole: splitting it changes the text tests/issues.test.ts asserts */ ""}<span class="issue-meta mono">#${issue.number} · ${issue.type}${issue.version && ` · v${issue.version}`} · ${relativeTime(issue.filed_at)}</span>240<span class="issue-reporter mono">${issue.reporter_name || "anonymous"}</span>241</a>`,242			)243		: html`<p class="muted mono save-empty">${query || filter ? "no matching issues" : `no ${status} issues`}</p>`244}245</section>`,246	});247}248249function formPage(250	c: Context,251	ctx: PageContext,252	repo: PublicRepo,253	values: Fields,254	error: string,255	status: 200 | 400 | 403 | 429,256): Response {257	const name = repo.settings.name;258	privatePage(c);259	const typeOption = (type: string) =>260		html`<option value="${type}"${values.type === type ? html` selected` : ""}>${type}</option>`;261	const body = repo.settings.issuesPaused262		? html`${crumbs(name)}263<h1 class="file-page-title">report an issue</h1>264<p>${name} is not taking new issues right now.</p>265<p><a href="/${name}/issues">see the existing issues →</a></p>`266		: html`${crumbs(name)}267<h1 class="file-page-title">report an issue</h1>268${error && html`<p class="issues-error mono" role="alert">${error}</p>`}269<form class="issues-form" method="post" action="/${name}/issues/new">270<input type="hidden" name="stamp" value="${formStamp(ctx.kepr, c, unixNow())}">271<label class="issues-hp" aria-hidden="true">leave this empty272<input name="website" tabindex="-1" autocomplete="off"></label>273<label class="issues-label" for="issue-title">title</label>274<input class="issues-input" id="issue-title" name="title" required maxlength="${MAX_CHARS}" value="${values.title ?? ""}">275<label class="issues-label" for="issue-body">what happened</label>276<textarea class="issues-textarea" id="issue-body" name="body" rows="8" required277maxlength="${MAX_CHARS}">${values.body ?? ""}</textarea>278<label class="issues-label" for="issue-type">type</label>279<select class="issues-input" id="issue-type" name="type">${ISSUE_TYPES.map(typeOption)}</select>280<label class="issues-label" for="issue-version">version you're on</label>281<input class="issues-input" id="issue-version" name="version" maxlength="200" value="${values.version ?? ""}">282<label class="issues-label" for="issue-name">name to show283<span class="muted">(optional)</span></label>284<input class="issues-input" id="issue-name" name="name" maxlength="200" value="${values.name ?? ""}">285<label class="issues-label" for="issue-email">contact email286<span class="muted">(optional, only the operator sees it)</span></label>287<input class="issues-input" id="issue-email" name="email" type="email" maxlength="320" value="${values.email ?? ""}">288<p class="issues-actions"><button class="issues-submit" type="submit">file issue</button>289<a class="issues-cancel mono" href="/${name}/issues">cancel</a></p>290</form>`;291	return c.html(292		page(ctx, {293			title: `report an issue · ${name}`,294			description: `File an issue against ${name}.`,295			path: `/${name}/issues/new`,296			body,297		}),298		status,299	);300}301302function threadPage(303	c: Context,304	ctx: PageContext,305	repo: PublicRepo,306	issue: Issue,307	token: string | null,308	notice: Notice,309): string {310	const name = repo.settings.name;311	const reporter = issue.reporter_name || "anonymous";312	const post = (id: string, operator: boolean, at: number, text: string) =>313		html`<article class="issue-post${operator ? " issue-post--operator" : ""}" id="${id}">314<p class="issue-post-head mono">${315			operator316				? html`<span class="issue-badge">operator</span>`317				: html`<span class="issue-author">${reporter}</span>`318		}<span class="history-when">${formatDate(at)}</span></p>319<div class="issue-text">${text}</div>320</article>`;321	const owner = token !== null && ownsIssue(issue, token);322	return page(ctx, {323		title: `#${issue.number} ${issue.title} · ${name}`,324		description: issue.title,325		path: `/${name}/issues/${issue.number}`,326		body: html`${crumbs(name)}327<h1 class="file-page-title">${issue.title} <span class="muted">#${issue.number}</span></h1>328<p class="issue-meta mono">329<span class="issue-symbol issue-symbol--${issue.status}">330${issue.status === "open" ? "◉ open" : "◈ closed"}</span>331${issue.type}${issue.version && ` · v${issue.version}`} · filed ${relativeTime(issue.filed_at)}332${issue.status === "closed" && fixedIn(repo, issue)}</p>333${post("issue", false, issue.filed_at, issue.body)}334${threadEntries(ctx.kepr, issue.id).map((entry) =>335	post(`entry-${entry.id}`, entry.author === "operator", entry.created_at, entry.body),336)}337${notice.error && html`<p class="issues-error mono" role="alert">${notice.error}</p>`}338${339	token !== null &&340	!owner &&341	!notice.error &&342	html`<p class="issues-error mono">This follow-up link does not match the issue.</p>`343}344${345	owner &&346	html`<form class="issues-form" method="post" action="/${name}/issues/${issue.number}">347<p class="muted">This is your private follow-up link; anyone who has it can post as you.</p>348<input type="hidden" name="token" value="${token}">349<input type="hidden" name="stamp" value="${formStamp(ctx.kepr, c, unixNow())}">350<label class="issues-label" for="follow-up">add to this issue</label>351<textarea class="issues-textarea" id="follow-up" name="body" rows="5" required352maxlength="${MAX_CHARS}">${notice.draft ?? ""}</textarea>353<button class="issues-submit" type="submit">post</button>354</form>`355}`,356	});357}358359function fixedIn(repo: PublicRepo, issue: Issue): Html | "" {360	if (issue.fixed_in === "") return "";361	const save = saveByPrefix(repo.mirror, issue.fixed_in);362	// Only a save with a page of its own is linked; a checkpoint or an unknown ID stays text.363	const id =364		save && !save.is_checkpoint365			? html`<a class="history-id" href="/${repo.settings.name}/saves/${save.id}">${save.id}</a>`366			: html`<span class="history-id">${issue.fixed_in}</span>`;367	return html` · fixed in ${id}`;368}