store.ts

11.8 KB · raw

1import type { Database } from "bun:sqlite";23export const ISSUE_TYPES = ["bug", "idea", "question"] as const;4export type IssueType = (typeof ISSUE_TYPES)[number];56export type Issue = {7	id: number;8	number: number;9	title: string;10	body: string;11	type: IssueType;12	version: string;13	reporter_name: string;14	filed_at: number;15	status: "open" | "closed";16	fixed_in: string;17};1819/** A row of the public list: the issue and its visible replies. */20export type ListedIssue = Issue & { replies: number };2122/** One reply. "reporter" is anyone from the public, under the name they chose ('' is anonymous). */23export type Entry = {24	id: number;25	author: "reporter" | "operator";26	name: string;27	body: string;28	created_at: number;29	hidden: number;30	reply_to: number | null;31};3233export type NewEntry = {34	author: Entry["author"];35	body: string;36	at: number;37	name?: string;38	ipHash?: string;39	replyTo?: number | null;40};4142export type NewIssue = {43	repo: string;44	title: string;45	body: string;46	type: IssueType;47	version: string;48	reporterName: string;49	contactEmail: string;50	ipHash: string;51	filedAt: number;52};5354// Everything the public may see; the contact email and IP hash stay out of every public read.55const PUBLIC_COLUMNS =56	"id, number, title, body, type, version, reporter_name, filed_at, status, fixed_in";5758/** Files an issue under the repo's next number (§23). */59export function fileIssue(db: Database, issue: NewIssue): { id: number; number: number } {60	// Immediate: it reads the next number before writing, and the sync's thread writes kepr.db too.61	return db62		.transaction(() => {63			const row = db64				.query<{ next: number }, [string]>(65					`SELECT COALESCE(MAX(number), 0) + 1 AS next66FROM issues67WHERE repo = ?`,68				)69				.get(issue.repo) as { next: number };70			const { lastInsertRowid } = db71				.query(72					`INSERT INTO issues (73repo, number, title, body, type, version, reporter_name, contact_email, filed_at, ip_hash74) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,75				)76				.run(77					issue.repo,78					row.next,79					issue.title,80					issue.body,81					issue.type,82					issue.version,83					issue.reporterName,84					issue.contactEmail,85					issue.filedAt,86					issue.ipHash,87				);88			return { id: Number(lastInsertRowid), number: row.next };89		})90		.immediate();91}9293/** Issues filed from one hashed IP since a time, hidden ones included: spam stays counted. */94export function filingsSince(db: Database, ipHash: string, since: number): number {95	const row = db96		.query<{ n: number }, [string, number]>(97			"SELECT COUNT(*) AS n FROM issues WHERE ip_hash = ? AND filed_at > ?",98		)99		.get(ipHash, since);100	return row?.n ?? 0;101}102103/** Replies posted from one hashed IP since a time, hidden ones included. */104export function repliesSince(db: Database, ipHash: string, since: number): number {105	const row = db106		.query<{ n: number }, [string, number]>(107			"SELECT COUNT(*) AS n FROM issue_entries WHERE ip_hash = ? AND created_at > ?",108		)109		.get(ipHash, since);110	return row?.n ?? 0;111}112113/** A repo's visible issues with one status, newest first, each counting its visible replies. */114export function listIssues(db: Database, repo: string, status: "open" | "closed"): ListedIssue[] {115	return db116		.query<ListedIssue, [string, string]>(117			`SELECT ${PUBLIC_COLUMNS}, (118	SELECT COUNT(*) FROM issue_entries e WHERE e.issue_id = issues.id AND e.hidden = 0119) AS replies120FROM issues121WHERE repo = ? AND status = ? AND hidden = 0122ORDER BY number DESC`,123		)124		.all(repo, status);125}126127export function issueCounts(db: Database, repo: string): { open: number; closed: number } {128	const rows = db129		.query<{ status: "open" | "closed"; n: number }, [string]>(130			`SELECT status, COUNT(*) AS n131FROM issues132WHERE repo = ? AND hidden = 0133GROUP BY status`,134		)135		.all(repo);136	const count = (status: string) => rows.find((row) => row.status === status)?.n ?? 0;137	return { open: count("open"), closed: count("closed") };138}139140/** A visible issue by its number; a hidden one is as missing as an unknown one. */141export function findIssue(db: Database, repo: string, number: number): Issue | null {142	return db143		.query<Issue, [string, number]>(144			`SELECT ${PUBLIC_COLUMNS}145FROM issues146WHERE repo = ? AND number = ? AND hidden = 0`,147		)148		.get(repo, number);149}150151/** An issue's visible replies, oldest first. */152export function threadEntries(db: Database, issueId: number): Entry[] {153	return operatorEntries(db, issueId).filter((entry) => !entry.hidden);154}155156/** Every reply in an issue's thread, hidden ones included, for the console. */157export function operatorEntries(db: Database, issueId: number): Entry[] {158	return db159		.query<Entry, [number]>(160			`SELECT id, author, name, body, created_at, hidden, reply_to161FROM issue_entries162WHERE issue_id = ?163ORDER BY id`,164		)165		.all(issueId);166}167168/** Hides or shows one entry of one issue; false when the issue has no such entry. */169export function setEntryHidden(170	db: Database,171	issueId: number,172	entryId: number,173	hidden: boolean,174): boolean {175	return (176		db177			.query("UPDATE issue_entries SET hidden = ? WHERE id = ? AND issue_id = ?")178			.run(hidden ? 1 : 0, entryId, issueId).changes === 1179	);180}181182export function addEntry(db: Database, issueId: number, entry: NewEntry): number {183	return Number(184		db185			.query(186				`INSERT INTO issue_entries (issue_id, author, name, body, created_at, ip_hash, reply_to)187VALUES (?, ?, ?, ?, ?, ?, ?)`,188			)189			.run(190				issueId,191				entry.author,192				entry.name ?? "",193				entry.body,194				entry.at,195				entry.ipHash ?? "",196				entry.replyTo ?? null,197			).lastInsertRowid,198	);199}200201/** Issues anyone can see, across every public repo: the profile's count (§21.2). */202export function publicIssueCount(db: Database): number {203	return (204		db205			.query<{ n: number }, []>(206				`SELECT COUNT(*) AS n207FROM issues i208JOIN repos r ON r.name = i.repo209WHERE r.public = 1 AND i.hidden = 0`,210			)211			.get()?.n ?? 0212	);213}214215/** An issue as the operator sees it (§24): hidden or not, with the reporter's contact detail. */216export type OperatorIssue = Issue & {217	repo: string;218	hidden: number;219	contact_email: string;220	/** 1 when the reporter spoke last: the filing, or their newest visible follow-up (§24). */221	awaiting: number;222};223export type OperatorFilter = "open" | "awaiting" | "closed" | "hidden";224225const REPORTER_SPOKE_LAST = `COALESCE((226	SELECT author FROM issue_entries e227	WHERE e.issue_id = issues.id AND e.hidden = 0228	ORDER BY e.id DESC229	LIMIT 1230), 'reporter') = 'reporter'`;231const OPERATOR_COLUMNS = `${PUBLIC_COLUMNS}, repo, hidden, contact_email,232${REPORTER_SPOKE_LAST} AS awaiting`;233/** Each console tab's test, so a single read of every issue answers every tab. */234export const operatorMatch: Record<OperatorFilter, (issue: OperatorIssue) => boolean> = {235	open: (issue) => !issue.hidden && issue.status === "open",236	awaiting: (issue) => !issue.hidden && issue.status === "open" && issue.awaiting === 1,237	closed: (issue) => !issue.hidden && issue.status === "closed",238	hidden: (issue) => issue.hidden === 1,239};240241/** Every repo's issues, private repos' included, newest first; no filter reads them all at once. */242export function operatorIssues(db: Database, filter?: OperatorFilter): OperatorIssue[] {243	const issues = db244		.query<OperatorIssue, []>(245			`SELECT ${OPERATOR_COLUMNS}246FROM issues247ORDER BY filed_at DESC, id DESC`,248		)249		.all();250	return filter ? issues.filter(operatorMatch[filter]) : issues;251}252253export function operatorIssue(db: Database, repo: string, number: number): OperatorIssue | null {254	return db255		.query<OperatorIssue, [string, number]>(256			`SELECT ${OPERATOR_COLUMNS}257FROM issues258WHERE repo = ? AND number = ?`,259		)260		.get(repo, number);261}262263/** Closes an issue, optionally "fixed in" a save (§23); reopening clears both. */264export function setClosed(db: Database, id: number, closedAt: number | null, fixedIn = ""): void {265	db.query("UPDATE issues SET status = ?, closed_at = ?, fixed_in = ? WHERE id = ?").run(266		closedAt === null ? "open" : "closed",267		closedAt,268		fixedIn,269		id,270	);271}272273export function setHidden(db: Database, id: number, hidden: boolean): void {274	db.query("UPDATE issues SET hidden = ? WHERE id = ?").run(hidden ? 1 : 0, id);275}276277export function setType(db: Database, id: number, type: IssueType): void {278	db.query("UPDATE issues SET type = ? WHERE id = ?").run(type, id);279}280281// A list of issue ids bound as one JSON parameter, so the statement stays fixed whatever its length.282const IDS = "id IN (SELECT value FROM json_each(?))";283284/** How many of these ids are issues; the console refuses a bulk action naming one that isn't. */285export function countIssues(db: Database, ids: number[]): number {286	return (287		db288			.query<{ n: number }, [string]>(`SELECT COUNT(*) AS n FROM issues WHERE ${IDS}`)289			.get(JSON.stringify(ids))?.n ?? 0290	);291}292293/**294 * Closes or reopens many issues at once (§24). Only those not already so change, so a closed295 * issue keeps its time and "fixed in" save; those that changed are returned.296 */297export function setManyClosed(298	db: Database,299	ids: number[],300	closedAt: number | null,301): Pick<OperatorIssue, "id" | "repo" | "number" | "title" | "hidden">[] {302	return db303		.query<304			Pick<OperatorIssue, "id" | "repo" | "number" | "title" | "hidden">,305			[string, number | null, string]306		>(307			`UPDATE issues308		SET status = ?1, closed_at = ?2, fixed_in = ''309		WHERE id IN (SELECT value FROM json_each(?3)) AND status != ?1310		RETURNING id, repo, number, title, hidden`,311		)312		.all(closedAt === null ? "open" : "closed", closedAt, JSON.stringify(ids));313}314315/** Each reply of an issue that came with an address, and whether that address is blocked. */316export function replyAddresses(db: Database, issueId: number): { id: number; blocked: number }[] {317	return db318		.query<{ id: number; blocked: number }, [number]>(319			`SELECT e.id, b.id IS NOT NULL AS blocked320FROM issue_entries e321LEFT JOIN blocked_reporters b ON b.ip_hash = e.ip_hash322WHERE e.issue_id = ? AND e.ip_hash != ''`,323		)324		.all(issueId);325}326327export function setManyHidden(db: Database, ids: number[], hidden: boolean): void {328	db.query(`UPDATE issues SET hidden = ? WHERE ${IDS}`).run(hidden ? 1 : 0, JSON.stringify(ids));329}330331export type BlockedReporter = { id: number; repo: string; number: number; blocked_at: number };332333/**334 * Blocks the address an issue was filed from, or one of its replies was posted from (§23); the335 * posts themselves are untouched. Returns the block's id. A post with no address (imported from336 * Kepr 2, or the operator's) blocks nothing and returns null.337 */338export function blockReporter(339	db: Database,340	issueId: number,341	now: number,342	entryId: number | null = null,343): number | null {344	return (345		db346			.query<{ id: number }, [number | null, number, number | null, number]>(347				`INSERT INTO blocked_reporters (ip_hash, repo, number, blocked_at)348				SELECT hash, repo, number, blocked_at FROM (349					SELECT CASE WHEN ?1 IS NULL THEN i.ip_hash ELSE e.ip_hash END AS hash,350					i.repo, i.number, ?2 AS blocked_at351					FROM issues i352					LEFT JOIN issue_entries e ON e.id = ?3 AND e.issue_id = i.id353					WHERE i.id = ?4354				)355				WHERE hash != ''356				ON CONFLICT (ip_hash) DO UPDATE SET ip_hash = excluded.ip_hash357				RETURNING id`,358			)359			.get(entryId, now, entryId, issueId)?.id ?? null360	);361}362363/** Lifts a block; false when there was none with that id. */364export function unblockReporter(db: Database, id: number): boolean {365	return db.query("DELETE FROM blocked_reporters WHERE id = ?").run(id).changes > 0;366}367368export function isBlocked(db: Database, ipHash: string): boolean {369	return db.query("SELECT 1 FROM blocked_reporters WHERE ip_hash = ?").get(ipHash) !== null;370}371372/** Whether the address an issue came from is blocked. */373export function reporterBlocked(db: Database, issueId: number): boolean {374	return (375		db376			.query(377				`SELECT 1378				FROM blocked_reporters b379				JOIN issues i ON i.ip_hash = b.ip_hash380				WHERE i.id = ?`,381			)382			.get(issueId) !== null383	);384}385386export function blockedReporters(db: Database): BlockedReporter[] {387	return db388		.query<BlockedReporter, []>(389			"SELECT id, repo, number, blocked_at FROM blocked_reporters ORDER BY blocked_at DESC, id DESC",390		)391		.all();392}