{"file":"server/src/issues/store.ts","version":318,"total":1693,"from":1,"to":1693,"lines":[{"n":1,"text":"// Issues for the open server (abso-core issue 17): one store per project, in SQLite, holding current state:","atom":"a1450","author":"trials","version":151},{"n":2,"text":"// issues, comments, labels, asks with their answers and threads, claims, the versions out for each issue,","atom":"a1450","author":"trials","version":151},{"n":3,"text":"// drafts, and what each reader's news has shown. Ported from the platform's issues Worker, without its","atom":"a1450","author":"trials","version":151},{"n":4,"text":"// workflow and release stores, prose authorship records, attachments, copies and meaning search.","atom":"a1450","author":"trials","version":151},{"n":5,"text":"//","atom":"a1450","author":"trials","version":151},{"n":6,"text":"// Status comes from the work and nobody sets it: Done when a publish closed it (or someone marked it done),","atom":"a1450","author":"trials","version":151},{"n":7,"text":"// Blocked while an ask that isn't a review is open on it (or names it in `blocks`) or an issue it is blocked","atom":"a1450","author":"trials","version":151},{"n":8,"text":"// by is not done, In review while a review ask is open on it, In progress while a start note names it, and","atom":"a1450","author":"trials","version":151},{"n":9,"text":"// Open otherwise. An answer is kept in its writer's own words on the ask it answers.","atom":"a1450","author":"trials","version":151},{"n":10,"text":"//","atom":"a1450","author":"trials","version":151},{"n":11,"text":"// Watchers: who opened an issue, its Agent and any agent that claims it, its reviewer, whoever was named in","atom":"a1450","author":"trials","version":151},{"n":12,"text":"// Notify, and whoever comments. An edit tells them unless its writer says not to.","atom":"a1450","author":"trials","version":151},{"n":13,"text":"","atom":"a1450","author":"trials","version":151},{"n":14,"text":"import type {","atom":"a1450","author":"trials","version":151},{"n":15,"text":"\tActor,","atom":"a1450","author":"trials","version":151},{"n":16,"text":"\tAsk,","atom":"a1450","author":"trials","version":151},{"n":17,"text":"\tAskReply,","atom":"a1450","author":"trials","version":151},{"n":18,"text":"\tComment,","atom":"a1450","author":"trials","version":151},{"n":19,"text":"\tDraft,","atom":"a1450","author":"trials","version":151},{"n":20,"text":"\tDraftBody,","atom":"a1450","author":"trials","version":151},{"n":21,"text":"\tEdit,","atom":"a1450","author":"trials","version":151},{"n":22,"text":"\tIssuePage,","atom":"a1450","author":"trials","version":151},{"n":23,"text":"\tIssueRow,","atom":"a1450","author":"trials","version":151},{"n":24,"text":"\tLines,","atom":"a1450","author":"trials","version":151},{"n":25,"text":"\tNews,","atom":"a1450","author":"trials","version":151},{"n":26,"text":"\tProseAuthorship,","atom":"a1450","author":"trials","version":151},{"n":27,"text":"\tStatus,","atom":"a1450","author":"trials","version":151},{"n":28,"text":"} from \"../../../client/src/issues-wire.ts\";","atom":"a1450","author":"trials","version":151},{"n":29,"text":"import { unseenIn } from \"../../../worker/project/unseen.ts\";","atom":"a1450","author":"trials","version":151},{"n":30,"text":"","atom":"a1450","author":"trials","version":151},{"n":31,"text":"export type {","atom":"a1450","author":"trials","version":151},{"n":32,"text":"\tActor,","atom":"a1450","author":"trials","version":151},{"n":33,"text":"\tAsk,","atom":"a1450","author":"trials","version":151},{"n":34,"text":"\tClaim,","atom":"a1450","author":"trials","version":151},{"n":35,"text":"\tComment,","atom":"a1450","author":"trials","version":151},{"n":36,"text":"\tDraft,","atom":"a1450","author":"trials","version":151},{"n":37,"text":"\tDraftBody,","atom":"a1450","author":"trials","version":151},{"n":38,"text":"\tEdit,","atom":"a1450","author":"trials","version":151},{"n":39,"text":"\tIssuePage,","atom":"a1450","author":"trials","version":151},{"n":40,"text":"\tIssueRow,","atom":"a1450","author":"trials","version":151},{"n":41,"text":"\tLines,","atom":"a1450","author":"trials","version":151},{"n":42,"text":"\tNews,","atom":"a1450","author":"trials","version":151},{"n":43,"text":"\tStatus,","atom":"a1450","author":"trials","version":151},{"n":44,"text":"} from \"../../../client/src/issues-wire.ts\";","atom":"a1450","author":"trials","version":151},{"n":45,"text":"","atom":"a1450","author":"trials","version":151},{"n":46,"text":"/** The SQL a Durable Object's storage gives (ctx.storage.sql), and node:sqlite in tests. */","atom":"a1450","author":"trials","version":151},{"n":47,"text":"export interface Sql {","atom":"a1450","author":"trials","version":151},{"n":48,"text":"\texec(query: string, ...bindings: unknown[]): { toArray(): unknown[] };","atom":"a1450","author":"trials","version":151},{"n":49,"text":"}","atom":"a1450","author":"trials","version":151},{"n":50,"text":"","atom":"a1450","author":"trials","version":151},{"n":51,"text":"/** A plain sentence for the person or agent; the route answers with it and `status`. */","atom":"a1450","author":"trials","version":151},{"n":52,"text":"export class IssuesError extends Error {","atom":"a1450","author":"trials","version":151},{"n":53,"text":"\treadonly status: number;","atom":"a1450","author":"trials","version":151},{"n":54,"text":"\t/** Seconds until a refused write may be tried again (429), for Retry-After. */","atom":"a1450","author":"trials","version":151},{"n":55,"text":"\treadonly retryAfter: number | undefined;","atom":"a1450","author":"trials","version":151},{"n":56,"text":"\tconstructor(message: string, status = 400, retryAfter?: number) {","atom":"a1450","author":"trials","version":151},{"n":57,"text":"\t\tsuper(message);","atom":"a1450","author":"trials","version":151},{"n":58,"text":"\t\tthis.status = status;","atom":"a1450","author":"trials","version":151},{"n":59,"text":"\t\tthis.retryAfter = retryAfter;","atom":"a1450","author":"trials","version":151},{"n":60,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":61,"text":"}","atom":"a1450","author":"trials","version":151},{"n":62,"text":"","atom":"a1450","author":"trials","version":151},{"n":63,"text":"/**","atom":"a1450","author":"trials","version":151},{"n":64,"text":" * A label: 1-40 characters of lower-case letters, digits, and - _ . : (so","atom":"a1450","author":"trials","version":151},{"n":65,"text":" * \"friction\", \"good-first-issue\", \"area:client\"). Anything else is refused,","atom":"a1450","author":"trials","version":151},{"n":66,"text":" * upper case included, rather than changed: the client and the","atom":"a1500","author":"trials","version":151},{"n":67,"text":" * site lower-case what's typed before sending.","atom":"a1450","author":"trials","version":151},{"n":68,"text":" */","atom":"a1450","author":"trials","version":151},{"n":69,"text":"export const LABEL = /^[a-z0-9][a-z0-9_.:-]{0,39}$/;","atom":"a1450","author":"trials","version":151},{"n":70,"text":"export const LABELS_PER_ISSUE = 20;","atom":"a1450","author":"trials","version":151},{"n":71,"text":"/** Distinct labels a project may hold, since GET labels lists every one. */","atom":"a1500","author":"trials","version":151},{"n":72,"text":"export const LABELS_PER_PROJECT = 200;","atom":"a1450","author":"trials","version":151},{"n":73,"text":"","atom":"a1450","author":"trials","version":151},{"n":74,"text":"/** Writes per writer (agent or person:<id>), since each can carry news. */","atom":"a1500","author":"trials","version":151},{"n":75,"text":"export const WRITES_PER_MINUTE = 100;","atom":"a1450","author":"trials","version":151},{"n":76,"text":"export const WRITES_PER_DAY = 1000;","atom":"a1450","author":"trials","version":151},{"n":77,"text":"","atom":"a1450","author":"trials","version":151},{"n":78,"text":"/** At most this many @names in one comment are told. */","atom":"a1500","author":"trials","version":151},{"n":79,"text":"export const MENTIONS_MAX = 20;","atom":"a1450","author":"trials","version":151},{"n":80,"text":"","atom":"a1450","author":"trials","version":151},{"n":81,"text":"/**","atom":"a1450","author":"trials","version":151},{"n":82,"text":" * The agents a text names as @name, in order, each once: an @ at the start or after anything","atom":"a1500","author":"trials","version":151},{"n":83,"text":" * but a name character, so an email address isn't one. A sentence's full stop or dash after the name isn't","atom":"a1450","author":"trials","version":151},{"n":84,"text":" * part of it.","atom":"a1450","author":"trials","version":151},{"n":85,"text":" */","atom":"a1450","author":"trials","version":151},{"n":86,"text":"export function mentionsIn(text: string): string[] {","atom":"a1450","author":"trials","version":151},{"n":87,"text":"\tconst names = new Set<string>();","atom":"a1450","author":"trials","version":151},{"n":88,"text":"\tfor (const m of text.matchAll(/(?:^|[^A-Za-z0-9._@-])@([A-Za-z0-9][A-Za-z0-9._-]{0,63})/g)) {","atom":"a1450","author":"trials","version":151},{"n":89,"text":"\t\tnames.add(m[1]!.replace(/[._-]+$/, \"\"));","atom":"a1450","author":"trials","version":151},{"n":90,"text":"\t\tif (names.size === MENTIONS_MAX) break;","atom":"a1450","author":"trials","version":151},{"n":91,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":92,"text":"\treturn [...names];","atom":"a1450","author":"trials","version":151},{"n":93,"text":"}","atom":"a1450","author":"trials","version":151},{"n":94,"text":"","atom":"a1450","author":"trials","version":151},{"n":95,"text":"const LIMITS = {","atom":"a1450","author":"trials","version":151},{"n":96,"text":"\ttitle: 200,","atom":"a1450","author":"trials","version":151},{"n":97,"text":"\tdescription: 20_000,","atom":"a1450","author":"trials","version":151},{"n":98,"text":"\ttext: 4_000,","atom":"a1450","author":"trials","version":151},{"n":99,"text":"\topen: 10_000,","atom":"a1450","author":"trials","version":151},{"n":100,"text":"\toptions: 10,","atom":"a1450","author":"trials","version":151},{"n":101,"text":"\tblocks: 50,","atom":"a1450","author":"trials","version":151},{"n":102,"text":"\tnames: 50,","atom":"a1450","author":"trials","version":151},{"n":103,"text":"\tdrafts: 100,","atom":"a1450","author":"trials","version":151},{"n":104,"text":"\tdraft: 30_000,","atom":"a1450","author":"trials","version":151},{"n":105,"text":"};","atom":"a1450","author":"trials","version":151},{"n":106,"text":"","atom":"a1450","author":"trials","version":151},{"n":107,"text":"/**","atom":"a1450","author":"trials","version":151},{"n":108,"text":" * The tables the change count watches: what issue reads show. Not drafts (one person's), writes (the rate log),","atom":"a1450","author":"trials","version":151},{"n":109,"text":" * search (kept from issues and comments) or meta. Fixed, since SCHEMA makes three triggers for each: a later","atom":"a1450","author":"trials","version":151},{"n":110,"text":" * table appends its own three to SCHEMA instead.","atom":"a1450","author":"trials","version":151},{"n":111,"text":" */","atom":"a1450","author":"trials","version":151},{"n":112,"text":"const COUNTED_TABLES = [","atom":"a1450","author":"trials","version":151},{"n":113,"text":"\t\"issues\",","atom":"a1450","author":"trials","version":151},{"n":114,"text":"\t\"comments\",","atom":"a1450","author":"trials","version":151},{"n":115,"text":"\t\"asks\",","atom":"a1450","author":"trials","version":151},{"n":116,"text":"\t\"claims\",","atom":"a1450","author":"trials","version":151},{"n":117,"text":"\t\"versions\",","atom":"a1450","author":"trials","version":151},{"n":118,"text":"\t\"watchers\",","atom":"a1450","author":"trials","version":151},{"n":119,"text":"\t\"blocked_by\",","atom":"a1450","author":"trials","version":151},{"n":120,"text":"\t\"edits\",","atom":"a1450","author":"trials","version":151},{"n":121,"text":"\t\"labels\",","atom":"a1450","author":"trials","version":151},{"n":122,"text":"\t\"ask_replies\",","atom":"a1451","author":"trials","version":151},{"n":123,"text":"] as const;","atom":"a1450","author":"trials","version":151},{"n":124,"text":"","atom":"a1450","author":"trials","version":151},{"n":125,"text":"/**","atom":"a1450","author":"trials","version":151},{"n":126,"text":" * The tables, as add-only migrations: the IssueProject object runs each once, in order (index.ts), so an","atom":"a1451","author":"trials","version":151},{"n":127,"text":" * older Worker still reads a newer database. Append; never edit or reorder.","atom":"a1451","author":"trials","version":151},{"n":128,"text":" */","atom":"a1450","author":"trials","version":151},{"n":129,"text":"export const SCHEMA: readonly string[] = [","atom":"a1451","author":"trials","version":151},{"n":130,"text":"\t`CREATE TABLE IF NOT EXISTS issues (id INTEGER PRIMARY KEY, parent INTEGER, title TEXT NOT NULL, description TEXT NOT NULL DEFAULT '',","atom":"a1451","author":"trials","version":151},{"n":131,"text":"\t\topened_by TEXT NOT NULL, opened_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, lines TEXT,","atom":"a1451","author":"trials","version":151},{"n":132,"text":"\t\tclosed_how TEXT, closed_by TEXT, closed_version INTEGER, closed_at INTEGER, agent TEXT)`,","atom":"a1451","author":"trials","version":151},{"n":133,"text":"\t\"CREATE TABLE IF NOT EXISTS comments (id INTEGER PRIMARY KEY, issue INTEGER NOT NULL, by TEXT NOT NULL, kind TEXT NOT NULL, text TEXT NOT NULL, at INTEGER NOT NULL)\",","atom":"a1451","author":"trials","version":151},{"n":134,"text":"\t`CREATE TABLE IF NOT EXISTS asks (id INTEGER PRIMARY KEY, issue INTEGER, by TEXT NOT NULL, of TEXT NOT NULL, kind TEXT NOT NULL, question TEXT NOT NULL,","atom":"a1451","author":"trials","version":151},{"n":135,"text":"\t\treview INTEGER NOT NULL, due TEXT, recommendation TEXT, options TEXT NOT NULL, blocks TEXT NOT NULL, at INTEGER NOT NULL,","atom":"a1451","author":"trials","version":151},{"n":136,"text":"\t\tanswer_by TEXT, answer_words TEXT, answer_at INTEGER, seen_at INTEGER, outcome TEXT, ack_at INTEGER, ack_note TEXT, waiting_on TEXT)`,","atom":"a1451","author":"trials","version":151},{"n":137,"text":"\t\"CREATE TABLE IF NOT EXISTS claims (issue INTEGER NOT NULL, agent TEXT NOT NULL, change INTEGER NOT NULL, since INTEGER NOT NULL, PRIMARY KEY (issue, agent))\",","atom":"a1451","author":"trials","version":151},{"n":138,"text":"\t\"CREATE TABLE IF NOT EXISTS versions (issue INTEGER NOT NULL, version INTEGER NOT NULL, agent TEXT NOT NULL, at INTEGER NOT NULL, PRIMARY KEY (issue, version))\",","atom":"a1451","author":"trials","version":151},{"n":139,"text":"\t\"CREATE TABLE IF NOT EXISTS watchers (issue INTEGER NOT NULL, who TEXT NOT NULL, since INTEGER NOT NULL, PRIMARY KEY (issue, who))\",","atom":"a1451","author":"trials","version":151},{"n":140,"text":"\t\"CREATE TABLE IF NOT EXISTS blocked_by (issue INTEGER NOT NULL, blocker INTEGER NOT NULL, PRIMARY KEY (issue, blocker))\",","atom":"a1451","author":"trials","version":151},{"n":141,"text":"\t\"CREATE TABLE IF NOT EXISTS edits (id INTEGER PRIMARY KEY, issue INTEGER NOT NULL, by TEXT NOT NULL, changes TEXT NOT NULL, notified TEXT NOT NULL, at INTEGER NOT NULL)\",","atom":"a1451","author":"trials","version":151},{"n":142,"text":"\t\"CREATE TABLE IF NOT EXISTS drafts (id INTEGER PRIMARY KEY, owner TEXT NOT NULL, body TEXT NOT NULL, saved_at INTEGER NOT NULL)\",","atom":"a1451","author":"trials","version":151},{"n":143,"text":"\t// Each writer's writes of the last day, for the limit.","atom":"a1451","author":"trials","version":151},{"n":144,"text":"\t\"CREATE TABLE IF NOT EXISTS writes (who TEXT NOT NULL, at INTEGER NOT NULL)\",","atom":"a1451","author":"trials","version":151},{"n":145,"text":"\t\"CREATE TABLE IF NOT EXISTS labels (issue INTEGER NOT NULL, label TEXT NOT NULL, by TEXT NOT NULL, at INTEGER NOT NULL, PRIMARY KEY (issue, label))\",","atom":"a1451","author":"trials","version":151},{"n":146,"text":"\t// Ask threads: replies from either side while the ask is open or after. An ask's waiting_on is null while it","atom":"a1451","author":"trials","version":151},{"n":147,"text":"\t// waits on the one asked, and the asker's name after the one asked replies with a question.","atom":"a1451","author":"trials","version":151},{"n":148,"text":"\t\"CREATE TABLE IF NOT EXISTS ask_replies (id INTEGER PRIMARY KEY, ask INTEGER NOT NULL, by TEXT NOT NULL, text TEXT NOT NULL, question INTEGER NOT NULL DEFAULT 0, at INTEGER NOT NULL)\",","atom":"a1451","author":"trials","version":151},{"n":149,"text":"\t// A reader's news: read up to when. Not counted, since no issue read shows it.","atom":"a1451","author":"trials","version":151},{"n":150,"text":"\t\"CREATE TABLE IF NOT EXISTS news_read (who TEXT PRIMARY KEY, at INTEGER NOT NULL)\",","atom":"a1451","author":"trials","version":151},{"n":151,"text":"\t\"CREATE INDEX IF NOT EXISTS comments_issue ON comments (issue)\",","atom":"a1451","author":"trials","version":151},{"n":152,"text":"\t\"CREATE INDEX IF NOT EXISTS comments_at ON comments (at)\",","atom":"a1451","author":"trials","version":151},{"n":153,"text":"\t\"CREATE INDEX IF NOT EXISTS asks_issue ON asks (issue)\",","atom":"a1451","author":"trials","version":151},{"n":154,"text":"\t\"CREATE INDEX IF NOT EXISTS asks_open ON asks (answer_at)\",","atom":"a1451","author":"trials","version":151},{"n":155,"text":"\t\"CREATE INDEX IF NOT EXISTS issues_parent ON issues (parent)\",","atom":"a1451","author":"trials","version":151},{"n":156,"text":"\t\"CREATE INDEX IF NOT EXISTS issues_updated ON issues (updated_at)\",","atom":"a1451","author":"trials","version":151},{"n":157,"text":"\t\"CREATE INDEX IF NOT EXISTS edits_issue ON edits (issue)\",","atom":"a1451","author":"trials","version":151},{"n":158,"text":"\t\"CREATE INDEX IF NOT EXISTS drafts_owner ON drafts (owner)\",","atom":"a1451","author":"trials","version":151},{"n":159,"text":"\t\"CREATE INDEX IF NOT EXISTS writes_who ON writes (who, at)\",","atom":"a1451","author":"trials","version":151},{"n":160,"text":"\t\"CREATE INDEX IF NOT EXISTS labels_label ON labels (label)\",","atom":"a1451","author":"trials","version":151},{"n":161,"text":"\t\"CREATE INDEX IF NOT EXISTS ask_replies_ask ON ask_replies (ask)\",","atom":"a1451","author":"trials","version":151},{"n":162,"text":"\t\"CREATE INDEX IF NOT EXISTS ask_replies_at ON ask_replies (at)\",","atom":"a1451","author":"trials","version":151},{"n":163,"text":"\t// Search: every title, description and comment in a full-text index, kept in step by triggers.","atom":"a1451","author":"trials","version":151},{"n":164,"text":"\t\"CREATE VIRTUAL TABLE IF NOT EXISTS search USING fts5(issue UNINDEXED, comment UNINDEXED, field UNINDEXED, text, tokenize = 'porter unicode61')\",","atom":"a1451","author":"trials","version":151},{"n":165,"text":"\t`CREATE TRIGGER IF NOT EXISTS search_issue_in AFTER INSERT ON issues BEGIN","atom":"a1451","author":"trials","version":151},{"n":166,"text":"\t\tINSERT INTO search (issue, comment, field, text) VALUES (new.id, 0, 'title', new.title);","atom":"a1451","author":"trials","version":151},{"n":167,"text":"\t\tINSERT INTO search (issue, comment, field, text) SELECT new.id, 0, 'description', new.description WHERE new.description <> '';","atom":"a1451","author":"trials","version":151},{"n":168,"text":"\tEND`,","atom":"a1451","author":"trials","version":151},{"n":169,"text":"\t`CREATE TRIGGER IF NOT EXISTS search_issue_edit AFTER UPDATE OF title, description ON issues BEGIN","atom":"a1451","author":"trials","version":151},{"n":170,"text":"\t\tDELETE FROM search WHERE issue = old.id AND comment = 0;","atom":"a1451","author":"trials","version":151},{"n":171,"text":"\t\tINSERT INTO search (issue, comment, field, text) VALUES (new.id, 0, 'title', new.title);","atom":"a1451","author":"trials","version":151},{"n":172,"text":"\t\tINSERT INTO search (issue, comment, field, text) SELECT new.id, 0, 'description', new.description WHERE new.description <> '';","atom":"a1451","author":"trials","version":151},{"n":173,"text":"\tEND`,","atom":"a1451","author":"trials","version":151},{"n":174,"text":"\t\"CREATE TRIGGER IF NOT EXISTS search_issue_out AFTER DELETE ON issues BEGIN DELETE FROM search WHERE issue = old.id; END\",","atom":"a1451","author":"trials","version":151},{"n":175,"text":"\t\"CREATE TRIGGER IF NOT EXISTS search_comment_in AFTER INSERT ON comments BEGIN INSERT INTO search (issue, comment, field, text) VALUES (new.issue, new.id, 'comment', new.text); END\",","atom":"a1451","author":"trials","version":151},{"n":176,"text":"\t`CREATE TRIGGER IF NOT EXISTS search_comment_edit AFTER UPDATE OF text ON comments BEGIN","atom":"a1451","author":"trials","version":151},{"n":177,"text":"\t\tDELETE FROM search WHERE comment = old.id;","atom":"a1451","author":"trials","version":151},{"n":178,"text":"\t\tINSERT INTO search (issue, comment, field, text) VALUES (new.issue, new.id, 'comment', new.text);","atom":"a1451","author":"trials","version":151},{"n":179,"text":"\tEND`,","atom":"a1451","author":"trials","version":151},{"n":180,"text":"\t\"CREATE TRIGGER IF NOT EXISTS search_comment_out AFTER DELETE ON comments BEGIN DELETE FROM search WHERE comment = old.id; END\",","atom":"a1451","author":"trials","version":151},{"n":181,"text":"\t// The change count: one number that only grows, bumped by triggers on every table an issue read shows, so","atom":"a1451","author":"trials","version":151},{"n":182,"text":"\t// every write path moves it, the seen marks waiting writes included. A reader can cache issue reads by it.","atom":"a1451","author":"trials","version":151},{"n":183,"text":"\t\"CREATE TABLE IF NOT EXISTS change_count (id INTEGER PRIMARY KEY CHECK (id = 1), n INTEGER NOT NULL)\",","atom":"a1451","author":"trials","version":151},{"n":184,"text":"\t\"INSERT OR IGNORE INTO change_count (id, n) VALUES (1, 0)\",","atom":"a1451","author":"trials","version":151},{"n":185,"text":"\t...COUNTED_TABLES.flatMap((t) =>","atom":"a1451","author":"trials","version":151},{"n":186,"text":"\t\t([\"INSERT\", \"UPDATE\", \"DELETE\"] as const).map(","atom":"a1451","author":"trials","version":151},{"n":187,"text":"\t\t\t(op) =>","atom":"a1451","author":"trials","version":151},{"n":188,"text":"\t\t\t\t`CREATE TRIGGER IF NOT EXISTS changed_${t}_${op.toLowerCase()} AFTER ${op} ON ${t} BEGIN UPDATE change_count SET n = n + 1 WHERE id = 1; END`,","atom":"a1451","author":"trials","version":151},{"n":189,"text":"\t\t),","atom":"a1451","author":"trials","version":151},{"n":190,"text":"\t),","atom":"a1451","author":"trials","version":151},{"n":191,"text":"];","atom":"a1451","author":"trials","version":151},{"n":192,"text":"","atom":"a1451","author":"trials","version":151},{"n":193,"text":"/** How many news items one project gives a person's news, before the merge across projects. */","atom":"a1500","author":"trials","version":151},{"n":194,"text":"export const NEWS_MAX = 50;","atom":"a1450","author":"trials","version":151},{"n":195,"text":"/** How much of a comment, answer or reply a news item carries. */","atom":"a1450","author":"trials","version":151},{"n":196,"text":"const NEWS_TEXT_MAX = 280;","atom":"a1450","author":"trials","version":151},{"n":197,"text":"","atom":"a1450","author":"trials","version":151},{"n":198,"text":"/**","atom":"a1450","author":"trials","version":151},{"n":199,"text":" * One thing for a person's news: an answer to their ask, a reply on an ask they're in, an","atom":"a1500","author":"trials","version":151},{"n":200,"text":" * @mention of them, or a comment, edit or close on an issue they watch, by someone else.","atom":"a1450","author":"trials","version":151},{"n":201,"text":" */","atom":"a1450","author":"trials","version":151},{"n":202,"text":"export interface NewsItem {","atom":"a1450","author":"trials","version":151},{"n":203,"text":"\tkind: \"answer\" | \"reply\" | \"mention\" | \"comment\" | \"edit\" | \"closed\";","atom":"a1450","author":"trials","version":151},{"n":204,"text":"\tissue: number | null;","atom":"a1450","author":"trials","version":151},{"n":205,"text":"\ttitle: string | null;","atom":"a1450","author":"trials","version":151},{"n":206,"text":"\task?: number;","atom":"a1450","author":"trials","version":151},{"n":207,"text":"\tby: string;","atom":"a1450","author":"trials","version":151},{"n":208,"text":"\ttext: string;","atom":"a1450","author":"trials","version":151},{"n":209,"text":"\tat: number;","atom":"a1450","author":"trials","version":151},{"n":210,"text":"\tread: boolean;","atom":"a1450","author":"trials","version":151},{"n":211,"text":"}","atom":"a1450","author":"trials","version":151},{"n":212,"text":"","atom":"a1450","author":"trials","version":151},{"n":213,"text":"/** One issue a search found: where the best match is, a snippet around it, and how well it matched. */","atom":"a1500","author":"trials","version":151},{"n":214,"text":"export interface SearchHit {","atom":"a1450","author":"trials","version":151},{"n":215,"text":"\tissue: { id: number; title: string; status: Status; updated: number; labels: string[] };","atom":"a1450","author":"trials","version":151},{"n":216,"text":"\t/** Where the best match is: the title, the description, or a comment (with its id). */","atom":"a1450","author":"trials","version":151},{"n":217,"text":"\twhere: \"title\" | \"description\" | \"comment\";","atom":"a1450","author":"trials","version":151},{"n":218,"text":"\tcomment?: number;","atom":"a1450","author":"trials","version":151},{"n":219,"text":"\t/** A few words around the match, with each matched span's [start, end) in `marks`. */","atom":"a1450","author":"trials","version":151},{"n":220,"text":"\tsnippet: string;","atom":"a1450","author":"trials","version":151},{"n":221,"text":"\tmarks: number[][];","atom":"a1450","author":"trials","version":151},{"n":222,"text":"\t/** FTS5's bm25, made positive: higher matches better. */","atom":"a1450","author":"trials","version":151},{"n":223,"text":"\tscore: number;","atom":"a1450","author":"trials","version":151},{"n":224,"text":"}","atom":"a1450","author":"trials","version":151},{"n":225,"text":"","atom":"a1500","author":"trials","version":151},{"n":226,"text":"/** A search hit as the client reads it: the issue's title and the matched text, each with who wrote it. */","atom":"a1500","author":"trials","version":151},{"n":227,"text":"export type AuthoredSearchHit = Omit<SearchHit, \"issue\"> & {","atom":"a1500","author":"trials","version":151},{"n":228,"text":"\tissue: SearchHit[\"issue\"] & { authorship: { title: ProseAuthorship } };","atom":"a1500","author":"trials","version":151},{"n":229,"text":"\tmatchedAuthorship: ProseAuthorship;","atom":"a1500","author":"trials","version":151},{"n":230,"text":"};","atom":"a1500","author":"trials","version":151},{"n":231,"text":"","atom":"a1500","author":"trials","version":151},{"n":232,"text":"type StoredAskReply = Omit<AskReply, \"authorship\">;","atom":"a1500","author":"trials","version":151},{"n":233,"text":"type StoredAsk = Omit<Ask, \"authorship\" | \"answer\" | \"ack\" | \"replies\"> & {","atom":"a1500","author":"trials","version":151},{"n":234,"text":"\tanswer: { by: string; words: string; at: number } | null;","atom":"a1500","author":"trials","version":151},{"n":235,"text":"\tack?: { at: number; note: string };","atom":"a1500","author":"trials","version":151},{"n":236,"text":"\treplies: StoredAskReply[];","atom":"a1500","author":"trials","version":151},{"n":237,"text":"};","atom":"a1500","author":"trials","version":151},{"n":238,"text":"","atom":"a1500","author":"trials","version":151},{"n":239,"text":"const UNKNOWN: ProseAuthorship = { kind: \"unknown\", actor: null, provenance: \"unknown\", authorField: null };","atom":"a1500","author":"trials","version":151},{"n":240,"text":"","atom":"a1500","author":"trials","version":151},{"n":241,"text":"/**","atom":"a1450","author":"trials","version":151},{"n":242,"text":" * Who wrote a text, from the column that names its writer: an agent, or a person (person:<id>). This server","atom":"a1500","author":"trials","version":151},{"n":243,"text":" * keeps no separate authorship records, so every text is its writer's own (\"native\").","atom":"a1500","author":"trials","version":151},{"n":244,"text":" */","atom":"a1500","author":"trials","version":151},{"n":245,"text":"function authored(by: unknown, authorField: string): ProseAuthorship {","atom":"a1500","author":"trials","version":151},{"n":246,"text":"\tif (typeof by !== \"string\" || by === \"\") return UNKNOWN;","atom":"a1500","author":"trials","version":151},{"n":247,"text":"\treturn { kind: by.startsWith(\"person:\") ? \"human\" : \"agent\", actor: by, provenance: \"native\", authorField };","atom":"a1500","author":"trials","version":151},{"n":248,"text":"}","atom":"a1500","author":"trials","version":151},{"n":249,"text":"","atom":"a1500","author":"trials","version":151},{"n":250,"text":"/**","atom":"a1500","author":"trials","version":151},{"n":251,"text":" * A person's words as an FTS5 query: each word quoted, so nothing they type","atom":"a1450","author":"trials","version":151},{"n":252,"text":" * is query syntax, and the last one a prefix, so a search box finds as they","atom":"a1450","author":"trials","version":151},{"n":253,"text":" * type. Undefined when there's no word in it.","atom":"a1450","author":"trials","version":151},{"n":254,"text":" */","atom":"a1450","author":"trials","version":151},{"n":255,"text":"export function searchQuery(q: string): string | undefined {","atom":"a1450","author":"trials","version":151},{"n":256,"text":"\tconst words = q.match(/[\\p{L}\\p{N}_]+/gu)?.slice(0, 12);","atom":"a1450","author":"trials","version":151},{"n":257,"text":"\tif (!words || words.length === 0) return undefined;","atom":"a1450","author":"trials","version":151},{"n":258,"text":"\treturn words.map((w, i) => `\"${w}\"${i === words.length - 1 ? \"*\" : \"\"}`).join(\" \");","atom":"a1450","author":"trials","version":151},{"n":259,"text":"}","atom":"a1450","author":"trials","version":151},{"n":260,"text":"","atom":"a1500","author":"trials","version":151},{"n":261,"text":"type Row = Record<string, unknown>;","atom":"a1450","author":"trials","version":151},{"n":262,"text":"","atom":"a1450","author":"trials","version":151},{"n":263,"text":"/** How an actor is named in the store and on the pages: the agent's name, or person:<id>. */","atom":"a1450","author":"trials","version":151},{"n":264,"text":"export function who(actor: Actor): string {","atom":"a1450","author":"trials","version":151},{"n":265,"text":"\treturn \"agent\" in actor ? actor.agent : `person:${actor.person}`;","atom":"a1450","author":"trials","version":151},{"n":266,"text":"}","atom":"a1450","author":"trials","version":151},{"n":267,"text":"","atom":"a1450","author":"trials","version":151},{"n":268,"text":"/**","atom":"a1450","author":"trials","version":151},{"n":269,"text":" * Words that reach agents on pages and in news: within `max`, with nothing a reader can't see (the engine's","atom":"a1500","author":"trials","version":151},{"n":270,"text":" * own list, worker/project/unseen.ts), and on one line where `oneLine`.","atom":"a1500","author":"trials","version":151},{"n":271,"text":" */","atom":"a1450","author":"trials","version":151},{"n":272,"text":"function text(value: unknown, what: string, max: number, required = true, oneLine = false): string {","atom":"a1450","author":"trials","version":151},{"n":273,"text":"\tif (value === undefined || value === null || value === \"\") {","atom":"a1450","author":"trials","version":151},{"n":274,"text":"\t\tif (required) throw new IssuesError(`${what} is missing`);","atom":"a1450","author":"trials","version":151},{"n":275,"text":"\t\treturn \"\";","atom":"a1450","author":"trials","version":151},{"n":276,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":277,"text":"\tif (typeof value !== \"string\") throw new IssuesError(`${what} is text`);","atom":"a1450","author":"trials","version":151},{"n":278,"text":"\tconst t = value.trim();","atom":"a1450","author":"trials","version":151},{"n":279,"text":"\tif (required && t === \"\") throw new IssuesError(`${what} is empty`);","atom":"a1450","author":"trials","version":151},{"n":280,"text":"\tif (t.length > max) throw new IssuesError(`${what} is ${t.length} characters; the most is ${max}`);","atom":"a1450","author":"trials","version":151},{"n":281,"text":"\tconst hidden = unseenIn(t);","atom":"a1500","author":"trials","version":151},{"n":282,"text":"\tif (hidden !== null)","atom":"a1450","author":"trials","version":151},{"n":283,"text":"\t\tthrow new IssuesError(`${what} holds ${hidden}, which a reader of the page can't see; take it out`);","atom":"a1450","author":"trials","version":151},{"n":284,"text":"\tif (oneLine && /[\\n\\t]/.test(t)) throw new IssuesError(`${what} is one line`);","atom":"a1450","author":"trials","version":151},{"n":285,"text":"\treturn t;","atom":"a1450","author":"trials","version":151},{"n":286,"text":"}","atom":"a1450","author":"trials","version":151},{"n":287,"text":"","atom":"a1450","author":"trials","version":151},{"n":288,"text":"function issueNumber(value: unknown, what = \"an issue number\"): number {","atom":"a1450","author":"trials","version":151},{"n":289,"text":"\tconst n = Number(value);","atom":"a1450","author":"trials","version":151},{"n":290,"text":"\tif (!Number.isInteger(n) || n < 1) throw new IssuesError(`${what} is a whole number from 1`);","atom":"a1450","author":"trials","version":151},{"n":291,"text":"\treturn n;","atom":"a1450","author":"trials","version":151},{"n":292,"text":"}","atom":"a1450","author":"trials","version":151},{"n":293,"text":"","atom":"a1450","author":"trials","version":151},{"n":294,"text":"/** A list of names (agents, or person:<id>), each once. */","atom":"a1450","author":"trials","version":151},{"n":295,"text":"function names(value: unknown, what: string): string[] {","atom":"a1450","author":"trials","version":151},{"n":296,"text":"\tif (value === undefined || value === null) return [];","atom":"a1450","author":"trials","version":151},{"n":297,"text":"\tif (!Array.isArray(value) || value.length > LIMITS.names)","atom":"a1450","author":"trials","version":151},{"n":298,"text":"\t\tthrow new IssuesError(`${what} is a list of up to ${LIMITS.names} names`);","atom":"a1450","author":"trials","version":151},{"n":299,"text":"\treturn [...new Set(value.map((v, i) => text(v, `${what}'s name ${i + 1}`, 100, true, true)))];","atom":"a1450","author":"trials","version":151},{"n":300,"text":"}","atom":"a1450","author":"trials","version":151},{"n":301,"text":"","atom":"a1450","author":"trials","version":151},{"n":302,"text":"/** Labels, checked as given (nothing is changed but surrounding spaces), each once, sorted. */","atom":"a1450","author":"trials","version":151},{"n":303,"text":"function labelsOf(value: unknown, what = \"labels\"): string[] {","atom":"a1450","author":"trials","version":151},{"n":304,"text":"\tif (value === undefined || value === null) return [];","atom":"a1450","author":"trials","version":151},{"n":305,"text":"\tif (!Array.isArray(value) || value.length > LABELS_PER_ISSUE)","atom":"a1450","author":"trials","version":151},{"n":306,"text":"\t\tthrow new IssuesError(`${what} is a list of up to ${LABELS_PER_ISSUE} labels`);","atom":"a1450","author":"trials","version":151},{"n":307,"text":"\tconst out = new Set<string>();","atom":"a1450","author":"trials","version":151},{"n":308,"text":"\tfor (const v of value) {","atom":"a1450","author":"trials","version":151},{"n":309,"text":"\t\tconst l = typeof v === \"string\" ? v.trim() : \"\";","atom":"a1450","author":"trials","version":151},{"n":310,"text":"\t\tif (!LABEL.test(l))","atom":"a1450","author":"trials","version":151},{"n":311,"text":"\t\t\tthrow new IssuesError(","atom":"a1450","author":"trials","version":151},{"n":312,"text":"\t\t\t\t`${JSON.stringify(v)} isn't a label: 1-40 lower-case letters, digits, and - _ . : (like friction or area:client)`,","atom":"a1450","author":"trials","version":151},{"n":313,"text":"\t\t\t);","atom":"a1450","author":"trials","version":151},{"n":314,"text":"\t\tout.add(l);","atom":"a1450","author":"trials","version":151},{"n":315,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":316,"text":"\treturn [...out].sort();","atom":"a1450","author":"trials","version":151},{"n":317,"text":"}","atom":"a1450","author":"trials","version":151},{"n":318,"text":"","atom":"a1450","author":"trials","version":151},{"n":319,"text":"/** A name, or null for none: \"\" and null clear it. */","atom":"a1450","author":"trials","version":151},{"n":320,"text":"function nameOrNone(value: unknown, what: string): string | null {","atom":"a1450","author":"trials","version":151},{"n":321,"text":"\treturn value === null || value === \"\" ? null : text(value, what, 100, true, true);","atom":"a1450","author":"trials","version":151},{"n":322,"text":"}","atom":"a1450","author":"trials","version":151},{"n":323,"text":"","atom":"a1450","author":"trials","version":151},{"n":324,"text":"function lines(value: unknown): Lines | null {","atom":"a1450","author":"trials","version":151},{"n":325,"text":"\tif (value === undefined || value === null) return null;","atom":"a1450","author":"trials","version":151},{"n":326,"text":"\tconst l = value as Partial<Lines>;","atom":"a1450","author":"trials","version":151},{"n":327,"text":"\tif (","atom":"a1450","author":"trials","version":151},{"n":328,"text":"\t\ttypeof l.file !== \"string\" ||","atom":"a1450","author":"trials","version":151},{"n":329,"text":"\t\tl.file === \"\" ||","atom":"a1450","author":"trials","version":151},{"n":330,"text":"\t\t!Number.isInteger(l.from) ||","atom":"a1450","author":"trials","version":151},{"n":331,"text":"\t\t!Number.isInteger(l.to) ||","atom":"a1450","author":"trials","version":151},{"n":332,"text":"\t\t!Number.isInteger(l.version) ||","atom":"a1450","author":"trials","version":151},{"n":333,"text":"\t\tl.from! < 1 ||","atom":"a1450","author":"trials","version":151},{"n":334,"text":"\t\tl.to! < l.from! ||","atom":"a1450","author":"trials","version":151},{"n":335,"text":"\t\tl.version! < 0","atom":"a1450","author":"trials","version":151},{"n":336,"text":"\t) {","atom":"a1450","author":"trials","version":151},{"n":337,"text":"\t\tthrow new IssuesError(\"lines are {file, from, to, version}: lines from-to of a file at a version\");","atom":"a1450","author":"trials","version":151},{"n":338,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":339,"text":"\treturn { file: text(l.file, \"the lines' file\", 1000, true, true), from: l.from!, to: l.to!, version: l.version! };","atom":"a1450","author":"trials","version":151},{"n":340,"text":"}","atom":"a1450","author":"trials","version":151},{"n":341,"text":"","atom":"a1450","author":"trials","version":151},{"n":342,"text":"const unique = (xs: readonly string[]): string[] => [...new Set(xs)];","atom":"a1450","author":"trials","version":151},{"n":343,"text":"","atom":"a1450","author":"trials","version":151},{"n":344,"text":"export class IssuesStore {","atom":"a1500","author":"trials","version":151},{"n":345,"text":"\tprivate readonly sql: Sql;","atom":"a1500","author":"trials","version":151},{"n":346,"text":"\tprivate readonly now: () => number;","atom":"a1500","author":"trials","version":151},{"n":347,"text":"\tprivate readonly atomic: (<T>(action: () => T) => T) | null;","atom":"a1500","author":"trials","version":151},{"n":348,"text":"","atom":"a1500","author":"trials","version":151},{"n":349,"text":"\t/** Over tables SCHEMA made (the object's migrations, or createTables in tests). `atomic` runs a transaction. */","atom":"a1500","author":"trials","version":151},{"n":350,"text":"\tconstructor(sql: Sql, now: () => number = Date.now, atomic?: <T>(action: () => T) => T) {","atom":"a1500","author":"trials","version":151},{"n":351,"text":"\t\tthis.sql = sql;","atom":"a1500","author":"trials","version":151},{"n":352,"text":"\t\tthis.now = now;","atom":"a1500","author":"trials","version":151},{"n":353,"text":"\t\tthis.atomic = atomic ?? null;","atom":"a1500","author":"trials","version":151},{"n":354,"text":"\t}","atom":"a1500","author":"trials","version":151},{"n":355,"text":"","atom":"a1500","author":"trials","version":151},{"n":356,"text":"\tprivate transaction<T>(action: () => T): T {","atom":"a1500","author":"trials","version":151},{"n":357,"text":"\t\treturn this.atomic ? this.atomic(action) : action();","atom":"a1500","author":"trials","version":151},{"n":358,"text":"\t}","atom":"a1500","author":"trials","version":151},{"n":359,"text":"","atom":"a1500","author":"trials","version":151},{"n":360,"text":"\tstatic createTables(sql: Sql): void {","atom":"a1500","author":"trials","version":151},{"n":361,"text":"\t\tfor (const s of SCHEMA) sql.exec(s);","atom":"a1500","author":"trials","version":151},{"n":362,"text":"\t}","atom":"a1500","author":"trials","version":151},{"n":363,"text":"","atom":"a1500","author":"trials","version":151},{"n":364,"text":"\t/**","atom":"a1450","author":"trials","version":151},{"n":365,"text":"\t * The change count: it grows with every write an issue read can show and never goes back, so a read cached","atom":"a1500","author":"trials","version":151},{"n":366,"text":"\t * under it is never older than its key.","atom":"a1500","author":"trials","version":151},{"n":367,"text":"\t */","atom":"a1450","author":"trials","version":151},{"n":368,"text":"\tchanges(): number {","atom":"a1500","author":"trials","version":151},{"n":369,"text":"\t\treturn (this.rows(\"SELECT n FROM change_count WHERE id = 1\")[0]?.n as number | undefined) ?? 0;","atom":"a1500","author":"trials","version":151},{"n":370,"text":"\t}","atom":"a1500","author":"trials","version":151},{"n":371,"text":"","atom":"a1500","author":"trials","version":151},{"n":372,"text":"\t/**","atom":"a1500","author":"trials","version":151},{"n":373,"text":"\t * A reader's news here, newest first, at most NEWS_MAX: answers to their asks, replies on asks they asked or","atom":"a1500","author":"trials","version":151},{"n":374,"text":"\t * were asked, comments naming them as @name (`names`: what they go by; an agent goes by its own name), and on","atom":"a1500","author":"trials","version":151},{"n":375,"text":"\t * issues they watch, others' comments, the edits that told them, and closes. Each is read when it is at or","atom":"a1500","author":"trials","version":151},{"n":376,"text":"\t * before their news_read mark. `before` pages back.","atom":"a1500","author":"trials","version":151},{"n":377,"text":"\t */","atom":"a1500","author":"trials","version":151},{"n":378,"text":"\tnewsFor(reader: Actor, names: readonly string[], before?: number): NewsItem[] {","atom":"a1500","author":"trials","version":151},{"n":379,"text":"\t\tconst me = who(reader);","atom":"a1500","author":"trials","version":151},{"n":380,"text":"\t\tconst until = before ?? Number.MAX_SAFE_INTEGER;","atom":"a1450","author":"trials","version":151},{"n":381,"text":"\t\tconst mark = (this.rows(\"SELECT at FROM news_read WHERE who = ?\", me)[0]?.at as number | undefined) ?? 0;","atom":"a1450","author":"trials","version":151},{"n":382,"text":"\t\tconst cut = (t: string) => (t.length > NEWS_TEXT_MAX ? `${t.slice(0, NEWS_TEXT_MAX - 1)}…` : t);","atom":"a1450","author":"trials","version":151},{"n":383,"text":"\t\tconst items: NewsItem[] = [];","atom":"a1450","author":"trials","version":151},{"n":384,"text":"\t\tconst add = (kind: NewsItem[\"kind\"], r: Row, text: string, ask?: number) =>","atom":"a1450","author":"trials","version":151},{"n":385,"text":"\t\t\titems.push({","atom":"a1450","author":"trials","version":151},{"n":386,"text":"\t\t\t\tkind,","atom":"a1450","author":"trials","version":151},{"n":387,"text":"\t\t\t\tissue: (r.issue as number | null) ?? null,","atom":"a1450","author":"trials","version":151},{"n":388,"text":"\t\t\t\ttitle: (r.title as string | null) ?? null,","atom":"a1450","author":"trials","version":151},{"n":389,"text":"\t\t\t\t...(ask !== undefined ? { ask } : {}),","atom":"a1450","author":"trials","version":151},{"n":390,"text":"\t\t\t\tby: r.by as string,","atom":"a1450","author":"trials","version":151},{"n":391,"text":"\t\t\t\ttext: cut(text),","atom":"a1450","author":"trials","version":151},{"n":392,"text":"\t\t\t\tat: r.at as number,","atom":"a1450","author":"trials","version":151},{"n":393,"text":"\t\t\t\tread: (r.at as number) <= mark,","atom":"a1450","author":"trials","version":151},{"n":394,"text":"\t\t\t});","atom":"a1450","author":"trials","version":151},{"n":395,"text":"\t\tfor (const r of this.rows(","atom":"a1450","author":"trials","version":151},{"n":396,"text":"\t\t\t`SELECT a.id, a.issue, i.title, a.answer_by AS by, a.answer_words AS text, a.answer_at AS at FROM asks a","atom":"a1450","author":"trials","version":151},{"n":397,"text":"\t\t\tLEFT JOIN issues i ON i.id = a.issue","atom":"a1450","author":"trials","version":151},{"n":398,"text":"\t\t\tWHERE a.by = ? AND a.answer_at IS NOT NULL AND a.answer_at < ? AND a.answer_by <> ? AND (a.outcome IS NULL OR a.outcome <> 'withdrawn')","atom":"a1450","author":"trials","version":151},{"n":399,"text":"\t\t\tORDER BY a.answer_at DESC LIMIT ?`,","atom":"a1450","author":"trials","version":151},{"n":400,"text":"\t\t\tme,","atom":"a1450","author":"trials","version":151},{"n":401,"text":"\t\t\tuntil,","atom":"a1450","author":"trials","version":151},{"n":402,"text":"\t\t\tme,","atom":"a1450","author":"trials","version":151},{"n":403,"text":"\t\t\tNEWS_MAX,","atom":"a1450","author":"trials","version":151},{"n":404,"text":"\t\t))","atom":"a1450","author":"trials","version":151},{"n":405,"text":"\t\t\tadd(\"answer\", r, (r.text as string | null) ?? \"\", r.id as number);","atom":"a1450","author":"trials","version":151},{"n":406,"text":"\t\tfor (const r of this.rows(","atom":"a1450","author":"trials","version":151},{"n":407,"text":"\t\t\t`SELECT r.ask, a.issue, i.title, r.by, r.text, r.at FROM ask_replies r JOIN asks a ON a.id = r.ask","atom":"a1450","author":"trials","version":151},{"n":408,"text":"\t\t\tLEFT JOIN issues i ON i.id = a.issue","atom":"a1450","author":"trials","version":151},{"n":409,"text":"\t\t\tWHERE (a.by = ? OR a.of = ?) AND r.by <> ? AND r.at < ? ORDER BY r.at DESC LIMIT ?`,","atom":"a1450","author":"trials","version":151},{"n":410,"text":"\t\t\tme,","atom":"a1450","author":"trials","version":151},{"n":411,"text":"\t\t\tme,","atom":"a1450","author":"trials","version":151},{"n":412,"text":"\t\t\tme,","atom":"a1450","author":"trials","version":151},{"n":413,"text":"\t\t\tuntil,","atom":"a1450","author":"trials","version":151},{"n":414,"text":"\t\t\tNEWS_MAX,","atom":"a1450","author":"trials","version":151},{"n":415,"text":"\t\t))","atom":"a1450","author":"trials","version":151},{"n":416,"text":"\t\t\tadd(\"reply\", r, r.text as string, r.ask as number);","atom":"a1450","author":"trials","version":151},{"n":417,"text":"\t\tconst named = names.filter((n) => /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/.test(n)).slice(0, 4);","atom":"a1450","author":"trials","version":151},{"n":418,"text":"\t\tconst mentioned = new Set<number>();","atom":"a1450","author":"trials","version":151},{"n":419,"text":"\t\tfor (const n of named)","atom":"a1450","author":"trials","version":151},{"n":420,"text":"\t\t\tfor (const r of this.rows(","atom":"a1450","author":"trials","version":151},{"n":421,"text":"\t\t\t\t`SELECT c.id, c.issue, i.title, c.by, c.text, c.at FROM comments c JOIN issues i ON i.id = c.issue","atom":"a1450","author":"trials","version":151},{"n":422,"text":"\t\t\t\tWHERE c.by <> ? AND c.at < ? AND instr(lower(c.text), ?) > 0 ORDER BY c.at DESC LIMIT ?`,","atom":"a1450","author":"trials","version":151},{"n":423,"text":"\t\t\t\tme,","atom":"a1450","author":"trials","version":151},{"n":424,"text":"\t\t\t\tuntil,","atom":"a1450","author":"trials","version":151},{"n":425,"text":"\t\t\t\t`@${n.toLowerCase()}`,","atom":"a1450","author":"trials","version":151},{"n":426,"text":"\t\t\t\tNEWS_MAX,","atom":"a1450","author":"trials","version":151},{"n":427,"text":"\t\t\t)) {","atom":"a1450","author":"trials","version":151},{"n":428,"text":"\t\t\t\tconst said = mentionsIn(r.text as string).map((m) => m.toLowerCase());","atom":"a1450","author":"trials","version":151},{"n":429,"text":"\t\t\t\tif (!said.includes(n.toLowerCase()) || mentioned.has(r.id as number)) continue;","atom":"a1450","author":"trials","version":151},{"n":430,"text":"\t\t\t\tmentioned.add(r.id as number);","atom":"a1450","author":"trials","version":151},{"n":431,"text":"\t\t\t\tadd(\"mention\", r, r.text as string);","atom":"a1450","author":"trials","version":151},{"n":432,"text":"\t\t\t}","atom":"a1450","author":"trials","version":151},{"n":433,"text":"\t\tfor (const r of this.rows(","atom":"a1450","author":"trials","version":151},{"n":434,"text":"\t\t\t`SELECT c.id, c.issue, i.title, c.by, c.text, c.at FROM comments c JOIN watchers w ON w.issue = c.issue AND w.who = ?","atom":"a1450","author":"trials","version":151},{"n":435,"text":"\t\t\tJOIN issues i ON i.id = c.issue","atom":"a1450","author":"trials","version":151},{"n":436,"text":"\t\t\tWHERE c.by <> ? AND c.at >= w.since AND c.at < ? ORDER BY c.at DESC LIMIT ?`,","atom":"a1450","author":"trials","version":151},{"n":437,"text":"\t\t\tme,","atom":"a1450","author":"trials","version":151},{"n":438,"text":"\t\t\tme,","atom":"a1450","author":"trials","version":151},{"n":439,"text":"\t\t\tuntil,","atom":"a1450","author":"trials","version":151},{"n":440,"text":"\t\t\tNEWS_MAX,","atom":"a1450","author":"trials","version":151},{"n":441,"text":"\t\t))","atom":"a1450","author":"trials","version":151},{"n":442,"text":"\t\t\tif (!mentioned.has(r.id as number)) add(\"comment\", r, r.text as string);","atom":"a1450","author":"trials","version":151},{"n":443,"text":"\t\tfor (const r of this.rows(","atom":"a1450","author":"trials","version":151},{"n":444,"text":"\t\t\t`SELECT e.issue, i.title, e.by, e.changes, e.at FROM edits e JOIN issues i ON i.id = e.issue","atom":"a1450","author":"trials","version":151},{"n":445,"text":"\t\t\tWHERE e.by <> ? AND e.at < ? AND EXISTS (SELECT 1 FROM json_each(e.notified) WHERE value = ?)","atom":"a1450","author":"trials","version":151},{"n":446,"text":"\t\t\tORDER BY e.at DESC LIMIT ?`,","atom":"a1450","author":"trials","version":151},{"n":447,"text":"\t\t\tme,","atom":"a1450","author":"trials","version":151},{"n":448,"text":"\t\t\tuntil,","atom":"a1450","author":"trials","version":151},{"n":449,"text":"\t\t\tme,","atom":"a1450","author":"trials","version":151},{"n":450,"text":"\t\t\tNEWS_MAX,","atom":"a1450","author":"trials","version":151},{"n":451,"text":"\t\t))","atom":"a1450","author":"trials","version":151},{"n":452,"text":"\t\t\tadd(\"edit\", r, Object.keys(JSON.parse(r.changes as string) as object).join(\", \"));","atom":"a1450","author":"trials","version":151},{"n":453,"text":"\t\tfor (const r of this.rows(","atom":"a1450","author":"trials","version":151},{"n":454,"text":"\t\t\t`SELECT i.id AS issue, i.title, i.closed_by AS by, i.closed_how, i.closed_at AS at FROM issues i","atom":"a1450","author":"trials","version":151},{"n":455,"text":"\t\t\tJOIN watchers w ON w.issue = i.id AND w.who = ?","atom":"a1450","author":"trials","version":151},{"n":456,"text":"\t\t\tWHERE i.closed_at IS NOT NULL AND i.closed_at < ? AND i.closed_by <> ? ORDER BY i.closed_at DESC LIMIT ?`,","atom":"a1450","author":"trials","version":151},{"n":457,"text":"\t\t\tme,","atom":"a1450","author":"trials","version":151},{"n":458,"text":"\t\t\tuntil,","atom":"a1450","author":"trials","version":151},{"n":459,"text":"\t\t\tme,","atom":"a1450","author":"trials","version":151},{"n":460,"text":"\t\t\tNEWS_MAX,","atom":"a1450","author":"trials","version":151},{"n":461,"text":"\t\t))","atom":"a1450","author":"trials","version":151},{"n":462,"text":"\t\t\tadd(\"closed\", r, r.closed_how as string);","atom":"a1450","author":"trials","version":151},{"n":463,"text":"\t\treturn items.sort((x, y) => y.at - x.at).slice(0, NEWS_MAX);","atom":"a1450","author":"trials","version":151},{"n":464,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":465,"text":"","atom":"a1450","author":"trials","version":151},{"n":466,"text":"\t/** Marks the reader's news here read up to `at` (never back); answers the mark. */","atom":"a1500","author":"trials","version":151},{"n":467,"text":"\tmarkNewsRead(reader: Actor, at: number): { read: number } {","atom":"a1500","author":"trials","version":151},{"n":468,"text":"\t\tthis.sql.exec(","atom":"a1450","author":"trials","version":151},{"n":469,"text":"\t\t\t\"INSERT INTO news_read (who, at) VALUES (?, ?) ON CONFLICT (who) DO UPDATE SET at = MAX(at, excluded.at)\",","atom":"a1450","author":"trials","version":151},{"n":470,"text":"\t\t\twho(reader),","atom":"a1500","author":"trials","version":151},{"n":471,"text":"\t\t\tat,","atom":"a1450","author":"trials","version":151},{"n":472,"text":"\t\t);","atom":"a1450","author":"trials","version":151},{"n":473,"text":"\t\treturn { read: this.rows(\"SELECT at FROM news_read WHERE who = ?\", who(reader))[0]!.at as number };","atom":"a1500","author":"trials","version":151},{"n":474,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":475,"text":"","atom":"a1500","author":"trials","version":151},{"n":476,"text":"\tprivate rows(query: string, ...b: unknown[]): Row[] {","atom":"a1450","author":"trials","version":151},{"n":477,"text":"\t\treturn this.sql.exec(query, ...b).toArray() as Row[];","atom":"a1450","author":"trials","version":151},{"n":478,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":479,"text":"","atom":"a1450","author":"trials","version":151},{"n":480,"text":"\tprivate exists(id: number): Row {","atom":"a1450","author":"trials","version":151},{"n":481,"text":"\t\tconst row = this.rows(\"SELECT * FROM issues WHERE id = ?\", issueNumber(id))[0];","atom":"a1450","author":"trials","version":151},{"n":482,"text":"\t\tif (!row) throw new IssuesError(`there's no issue ${id}`, 404);","atom":"a1450","author":"trials","version":151},{"n":483,"text":"\t\treturn row;","atom":"a1450","author":"trials","version":151},{"n":484,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":485,"text":"","atom":"a1450","author":"trials","version":151},{"n":486,"text":"\tprivate touch(...ids: (number | null)[]): void {","atom":"a1450","author":"trials","version":151},{"n":487,"text":"\t\tconst at = this.now();","atom":"a1450","author":"trials","version":151},{"n":488,"text":"\t\tfor (const id of ids)","atom":"a1450","author":"trials","version":151},{"n":489,"text":"\t\t\tif (id !== null) this.sql.exec(\"UPDATE issues SET updated_at = MAX(updated_at, ?) WHERE id = ?\", at, id);","atom":"a1450","author":"trials","version":151},{"n":490,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":491,"text":"","atom":"a1450","author":"trials","version":151},{"n":492,"text":"\t/** Refuses labels that would take the project past LABELS_PER_PROJECT distinct ones. */","atom":"a1450","author":"trials","version":151},{"n":493,"text":"\tprivate roomForLabels(labels: readonly string[]): void {","atom":"a1450","author":"trials","version":151},{"n":494,"text":"\t\tconst inUse = new Set(this.rows(\"SELECT DISTINCT label FROM labels\").map((l) => l.label as string));","atom":"a1450","author":"trials","version":151},{"n":495,"text":"\t\tconst added = labels.filter((l) => !inUse.has(l));","atom":"a1450","author":"trials","version":151},{"n":496,"text":"\t\tif (added.length > 0 && inUse.size + added.length > LABELS_PER_PROJECT)","atom":"a1450","author":"trials","version":151},{"n":497,"text":"\t\t\tthrow new IssuesError(","atom":"a1450","author":"trials","version":151},{"n":498,"text":"\t\t\t\t`this project has ${inUse.size} labels, and ${LABELS_PER_PROJECT} is the most; use one it has (abso issues --labels lists them)`,","atom":"a1450","author":"trials","version":151},{"n":499,"text":"\t\t\t);","atom":"a1450","author":"trials","version":151},{"n":500,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":501,"text":"","atom":"a1450","author":"trials","version":151},{"n":502,"text":"\t/** Attachments aren't kept on this server: a write that names any is refused before anything is written. */","atom":"a1500","author":"trials","version":151},{"n":503,"text":"\tprivate noAttachments(raw: unknown): void {","atom":"a1500","author":"trials","version":151},{"n":504,"text":"\t\tif (raw === undefined || raw === null || (Array.isArray(raw) && raw.length === 0)) return;","atom":"a1500","author":"trials","version":151},{"n":505,"text":"\t\tthrow new IssuesError(\"this server doesn't keep attachments; leave them off, or link the file instead\", 501);","atom":"a1500","author":"trials","version":151},{"n":506,"text":"\t}","atom":"a1500","author":"trials","version":151},{"n":507,"text":"","atom":"a1500","author":"trials","version":151},{"n":508,"text":"\t/** Adds watchers to an issue; each is kept once, from when they first watched. */","atom":"a1450","author":"trials","version":151},{"n":509,"text":"\tprivate watch(id: number, ...people: (string | null | undefined)[]): void {","atom":"a1450","author":"trials","version":151},{"n":510,"text":"\t\tconst at = this.now();","atom":"a1450","author":"trials","version":151},{"n":511,"text":"\t\tfor (const p of people)","atom":"a1450","author":"trials","version":151},{"n":512,"text":"\t\t\tif (p)","atom":"a1450","author":"trials","version":151},{"n":513,"text":"\t\t\t\tthis.sql.exec(","atom":"a1450","author":"trials","version":151},{"n":514,"text":"\t\t\t\t\t\"INSERT INTO watchers (issue, who, since) VALUES (?, ?, ?) ON CONFLICT (issue, who) DO NOTHING\",","atom":"a1450","author":"trials","version":151},{"n":515,"text":"\t\t\t\t\tid,","atom":"a1450","author":"trials","version":151},{"n":516,"text":"\t\t\t\t\tp,","atom":"a1450","author":"trials","version":151},{"n":517,"text":"\t\t\t\t\tat,","atom":"a1450","author":"trials","version":151},{"n":518,"text":"\t\t\t\t);","atom":"a1450","author":"trials","version":151},{"n":519,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":520,"text":"","atom":"a1450","author":"trials","version":151},{"n":521,"text":"\tprivate watchersOf(id: number): string[] {","atom":"a1450","author":"trials","version":151},{"n":522,"text":"\t\treturn this.rows(\"SELECT who FROM watchers WHERE issue = ? ORDER BY since, who\", id).map((w) => w.who as string);","atom":"a1450","author":"trials","version":151},{"n":523,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":524,"text":"","atom":"a1450","author":"trials","version":151},{"n":525,"text":"\t/** Blocked by, checked: issues that exist, not `id` itself, and none already waiting on `id` (that would wait forever). */","atom":"a1450","author":"trials","version":151},{"n":526,"text":"\tprivate blockersFor(id: number | null, value: unknown): number[] {","atom":"a1450","author":"trials","version":151},{"n":527,"text":"\t\tif (value === undefined || value === null) return [];","atom":"a1450","author":"trials","version":151},{"n":528,"text":"\t\tif (!Array.isArray(value) || value.length > LIMITS.blocks)","atom":"a1450","author":"trials","version":151},{"n":529,"text":"\t\t\tthrow new IssuesError(`blocked by is a list of up to ${LIMITS.blocks} issue numbers`);","atom":"a1450","author":"trials","version":151},{"n":530,"text":"\t\tconst ids = [...new Set(value.map((b) => issueNumber(b, \"an issue it's blocked by\")))];","atom":"a1450","author":"trials","version":151},{"n":531,"text":"\t\tfor (const b of ids) {","atom":"a1450","author":"trials","version":151},{"n":532,"text":"\t\t\tthis.exists(b);","atom":"a1450","author":"trials","version":151},{"n":533,"text":"\t\t\tif (b === id) throw new IssuesError(`issue ${id} can't be blocked by itself`);","atom":"a1450","author":"trials","version":151},{"n":534,"text":"\t\t\tif (id !== null && this.waitsOn(b, id))","atom":"a1450","author":"trials","version":151},{"n":535,"text":"\t\t\t\tthrow new IssuesError(`issue ${b} already waits on issue ${id}, so neither could start`);","atom":"a1450","author":"trials","version":151},{"n":536,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":537,"text":"\t\treturn ids;","atom":"a1450","author":"trials","version":151},{"n":538,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":539,"text":"","atom":"a1450","author":"trials","version":151},{"n":540,"text":"\t/** Whether `from` waits on `on`, through Blocked by. */","atom":"a1450","author":"trials","version":151},{"n":541,"text":"\tprivate waitsOn(from: number, on: number): boolean {","atom":"a1450","author":"trials","version":151},{"n":542,"text":"\t\tconst seen = new Set<number>();","atom":"a1450","author":"trials","version":151},{"n":543,"text":"\t\tconst next = [from];","atom":"a1450","author":"trials","version":151},{"n":544,"text":"\t\twhile (next.length > 0) {","atom":"a1450","author":"trials","version":151},{"n":545,"text":"\t\t\tconst at = next.pop()!;","atom":"a1450","author":"trials","version":151},{"n":546,"text":"\t\t\tif (at === on) return true;","atom":"a1450","author":"trials","version":151},{"n":547,"text":"\t\t\tif (seen.has(at)) continue;","atom":"a1450","author":"trials","version":151},{"n":548,"text":"\t\t\tseen.add(at);","atom":"a1450","author":"trials","version":151},{"n":549,"text":"\t\t\tfor (const b of this.rows(\"SELECT blocker FROM blocked_by WHERE issue = ?\", at)) next.push(b.blocker as number);","atom":"a1450","author":"trials","version":151},{"n":550,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":551,"text":"\t\treturn false;","atom":"a1450","author":"trials","version":151},{"n":552,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":553,"text":"","atom":"a1450","author":"trials","version":151},{"n":554,"text":"\t/** A parent, checked: an issue that exists and isn't `id` or under it. */","atom":"a1450","author":"trials","version":151},{"n":555,"text":"\tprivate parentFor(id: number | null, value: unknown): number | null {","atom":"a1450","author":"trials","version":151},{"n":556,"text":"\t\tif (value === undefined || value === null) return null;","atom":"a1450","author":"trials","version":151},{"n":557,"text":"\t\tconst parent = issueNumber(value, \"the parent\");","atom":"a1450","author":"trials","version":151},{"n":558,"text":"\t\tthis.exists(parent);","atom":"a1450","author":"trials","version":151},{"n":559,"text":"\t\tfor (","atom":"a1450","author":"trials","version":151},{"n":560,"text":"\t\t\tlet at: number | null = parent;","atom":"a1450","author":"trials","version":151},{"n":561,"text":"\t\t\tat !== null;","atom":"a1450","author":"trials","version":151},{"n":562,"text":"\t\t\tat = (this.rows(\"SELECT parent FROM issues WHERE id = ?\", at)[0]?.parent as number | null | undefined) ?? null","atom":"a1450","author":"trials","version":151},{"n":563,"text":"\t\t) {","atom":"a1450","author":"trials","version":151},{"n":564,"text":"\t\t\tif (at === id) throw new IssuesError(`issue ${parent} is issue ${id} or under it, so it can't be its parent`);","atom":"a1450","author":"trials","version":151},{"n":565,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":566,"text":"\t\treturn parent;","atom":"a1450","author":"trials","version":151},{"n":567,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":568,"text":"","atom":"a1450","author":"trials","version":151},{"n":569,"text":"\t/** Every row's status and joins at once: one query per table, not one per issue. */","atom":"a1450","author":"trials","version":151},{"n":570,"text":"\tprivate statuses(rows: Row[]): IssueRow[] {","atom":"a1500","author":"trials","version":151},{"n":571,"text":"\t\tif (rows.length === 0) return [];","atom":"a1450","author":"trials","version":151},{"n":572,"text":"\t\t// Open asks hold up their own issue and the ones they name in blocks.","atom":"a1450","author":"trials","version":151},{"n":573,"text":"\t\tconst held = new Map<number, { review: boolean; of: string }[]>();","atom":"a1450","author":"trials","version":151},{"n":574,"text":"\t\tfor (const a of this.rows(","atom":"a1450","author":"trials","version":151},{"n":575,"text":"\t\t\t\"SELECT issue, COALESCE(waiting_on, of) AS of, review, blocks FROM asks WHERE answer_at IS NULL\",","atom":"a1450","author":"trials","version":151},{"n":576,"text":"\t\t)) {","atom":"a1450","author":"trials","version":151},{"n":577,"text":"\t\t\tconst on = new Set<number>([","atom":"a1450","author":"trials","version":151},{"n":578,"text":"\t\t\t\t...(a.issue !== null ? [a.issue as number] : []),","atom":"a1450","author":"trials","version":151},{"n":579,"text":"\t\t\t\t...(JSON.parse(a.blocks as string) as number[]),","atom":"a1450","author":"trials","version":151},{"n":580,"text":"\t\t\t]);","atom":"a1450","author":"trials","version":151},{"n":581,"text":"\t\t\tfor (const id of on)","atom":"a1450","author":"trials","version":151},{"n":582,"text":"\t\t\t\theld.set(id, [...(held.get(id) ?? []), { review: a.review === 1 && id === a.issue, of: a.of as string }]);","atom":"a1450","author":"trials","version":151},{"n":583,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":584,"text":"\t\tconst claims = new Map<number, string[]>();","atom":"a1450","author":"trials","version":151},{"n":585,"text":"\t\tfor (const c of this.rows(\"SELECT issue, agent FROM claims ORDER BY since\"))","atom":"a1450","author":"trials","version":151},{"n":586,"text":"\t\t\tclaims.set(c.issue as number, [...(claims.get(c.issue as number) ?? []), c.agent as string]);","atom":"a1450","author":"trials","version":151},{"n":587,"text":"\t\tconst versions = new Map<number, number[]>();","atom":"a1450","author":"trials","version":151},{"n":588,"text":"\t\tfor (const v of this.rows(\"SELECT issue, version FROM versions ORDER BY version\"))","atom":"a1450","author":"trials","version":151},{"n":589,"text":"\t\t\tversions.set(v.issue as number, [...(versions.get(v.issue as number) ?? []), v.version as number]);","atom":"a1450","author":"trials","version":151},{"n":590,"text":"\t\tconst children = new Map<number, number>();","atom":"a1450","author":"trials","version":151},{"n":591,"text":"\t\tfor (const c of this.rows(\"SELECT parent, COUNT(*) AS n FROM issues WHERE parent IS NOT NULL GROUP BY parent\"))","atom":"a1450","author":"trials","version":151},{"n":592,"text":"\t\t\tchildren.set(c.parent as number, c.n as number);","atom":"a1450","author":"trials","version":151},{"n":593,"text":"\t\tconst labels = new Map<number, string[]>();","atom":"a1450","author":"trials","version":151},{"n":594,"text":"\t\tfor (const l of this.rows(\"SELECT issue, label FROM labels ORDER BY label\"))","atom":"a1450","author":"trials","version":151},{"n":595,"text":"\t\t\tlabels.set(l.issue as number, [...(labels.get(l.issue as number) ?? []), l.label as string]);","atom":"a1450","author":"trials","version":151},{"n":596,"text":"\t\t// Blocked by: each blocker, and whether it's Done yet.","atom":"a1450","author":"trials","version":151},{"n":597,"text":"\t\tconst blockers = new Map<number, { id: number; done: boolean }[]>();","atom":"a1450","author":"trials","version":151},{"n":598,"text":"\t\tfor (const b of this.rows(","atom":"a1450","author":"trials","version":151},{"n":599,"text":"\t\t\t\"SELECT blocked_by.issue, blocked_by.blocker, issues.closed_how FROM blocked_by LEFT JOIN issues ON issues.id = blocked_by.blocker ORDER BY blocked_by.blocker\",","atom":"a1450","author":"trials","version":151},{"n":600,"text":"\t\t)) {","atom":"a1450","author":"trials","version":151},{"n":601,"text":"\t\t\tblockers.set(b.issue as number, [","atom":"a1450","author":"trials","version":151},{"n":602,"text":"\t\t\t\t...(blockers.get(b.issue as number) ?? []),","atom":"a1450","author":"trials","version":151},{"n":603,"text":"\t\t\t\t{ id: b.blocker as number, done: b.closed_how === \"done\" },","atom":"a1450","author":"trials","version":151},{"n":604,"text":"\t\t\t]);","atom":"a1450","author":"trials","version":151},{"n":605,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":606,"text":"\t\tconst retitled = this.lastEditors(\"title\");","atom":"a1500","author":"trials","version":151},{"n":607,"text":"\t\treturn rows.map((r) => {","atom":"a1450","author":"trials","version":151},{"n":608,"text":"\t\t\tconst id = r.id as number;","atom":"a1450","author":"trials","version":151},{"n":609,"text":"\t\t\tconst asks = held.get(id) ?? [];","atom":"a1450","author":"trials","version":151},{"n":610,"text":"\t\t\tconst agents = claims.get(id) ?? [];","atom":"a1450","author":"trials","version":151},{"n":611,"text":"\t\t\tconst blockedBy = blockers.get(id) ?? [];","atom":"a1450","author":"trials","version":151},{"n":612,"text":"\t\t\tconst status: Status =","atom":"a1500","author":"trials","version":151},{"n":613,"text":"\t\t\t\tr.closed_how === \"done\"","atom":"a1500","author":"trials","version":151},{"n":614,"text":"\t\t\t\t\t? \"done\"","atom":"a1450","author":"trials","version":151},{"n":615,"text":"\t\t\t\t\t: r.closed_how === \"dropped\"","atom":"a1450","author":"trials","version":151},{"n":616,"text":"\t\t\t\t\t\t? \"dropped\"","atom":"a1450","author":"trials","version":151},{"n":617,"text":"\t\t\t\t\t\t: asks.some((a) => !a.review) || blockedBy.some((b) => !b.done)","atom":"a1450","author":"trials","version":151},{"n":618,"text":"\t\t\t\t\t\t\t? \"blocked\"","atom":"a1450","author":"trials","version":151},{"n":619,"text":"\t\t\t\t\t\t\t: asks.length > 0","atom":"a1450","author":"trials","version":151},{"n":620,"text":"\t\t\t\t\t\t\t\t? \"in review\"","atom":"a1450","author":"trials","version":151},{"n":621,"text":"\t\t\t\t\t\t\t\t: agents.length > 0","atom":"a1450","author":"trials","version":151},{"n":622,"text":"\t\t\t\t\t\t\t\t\t? \"in progress\"","atom":"a1450","author":"trials","version":151},{"n":623,"text":"\t\t\t\t\t\t\t\t\t: \"open\";","atom":"a1450","author":"trials","version":151},{"n":624,"text":"\t\t\treturn {","atom":"a1450","author":"trials","version":151},{"n":625,"text":"\t\t\t\tid,","atom":"a1450","author":"trials","version":151},{"n":626,"text":"\t\t\t\ttitle: r.title as string,","atom":"a1450","author":"trials","version":151},{"n":627,"text":"\t\t\t\tauthorship: { title: this.issueFieldAuthorship(r, retitled) },","atom":"a1500","author":"trials","version":151},{"n":628,"text":"\t\t\t\tparent: (r.parent as number | null) ?? null,","atom":"a1450","author":"trials","version":151},{"n":629,"text":"\t\t\t\tstatus,","atom":"a1450","author":"trials","version":151},{"n":630,"text":"\t\t\t\tagent: (r.agent as string | null | undefined) ?? null,","atom":"a1450","author":"trials","version":151},{"n":631,"text":"\t\t\t\tagents,","atom":"a1450","author":"trials","version":151},{"n":632,"text":"\t\t\t\tblockedBy: blockedBy.map((b) => b.id),","atom":"a1450","author":"trials","version":151},{"n":633,"text":"\t\t\t\tlabels: labels.get(id) ?? [],","atom":"a1450","author":"trials","version":151},{"n":634,"text":"\t\t\t\tversions: versions.get(id) ?? [],","atom":"a1450","author":"trials","version":151},{"n":635,"text":"\t\t\t\twaitingOn: r.closed_how ? [] : [...new Set(asks.map((a) => a.of))],","atom":"a1500","author":"trials","version":151},{"n":636,"text":"\t\t\t\topened: { by: r.opened_by as string, at: r.opened_at as number },","atom":"a1450","author":"trials","version":151},{"n":637,"text":"\t\t\t\tupdated: r.updated_at as number,","atom":"a1450","author":"trials","version":151},{"n":638,"text":"\t\t\t\tclosed: r.closed_how","atom":"a1450","author":"trials","version":151},{"n":639,"text":"\t\t\t\t\t? {","atom":"a1450","author":"trials","version":151},{"n":640,"text":"\t\t\t\t\t\t\thow: r.closed_how as \"done\" | \"dropped\",","atom":"a1450","author":"trials","version":151},{"n":641,"text":"\t\t\t\t\t\t\tby: r.closed_by as string,","atom":"a1450","author":"trials","version":151},{"n":642,"text":"\t\t\t\t\t\t\tversion: (r.closed_version as number | null) ?? null,","atom":"a1450","author":"trials","version":151},{"n":643,"text":"\t\t\t\t\t\t\tat: r.closed_at as number,","atom":"a1450","author":"trials","version":151},{"n":644,"text":"\t\t\t\t\t\t}","atom":"a1450","author":"trials","version":151},{"n":645,"text":"\t\t\t\t\t: null,","atom":"a1450","author":"trials","version":151},{"n":646,"text":"\t\t\t\tchildren: children.get(id) ?? 0,","atom":"a1450","author":"trials","version":151},{"n":647,"text":"\t\t\t};","atom":"a1450","author":"trials","version":151},{"n":648,"text":"\t\t});","atom":"a1450","author":"trials","version":151},{"n":649,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":650,"text":"","atom":"a1450","author":"trials","version":151},{"n":651,"text":"\t/** Who last edited `field` on each issue whose `field` was edited (or only issue `id`'s). */","atom":"a1500","author":"trials","version":151},{"n":652,"text":"\tprivate lastEditors(field: \"title\" | \"description\", id?: number): Map<number, string> {","atom":"a1500","author":"trials","version":151},{"n":653,"text":"\t\tconst edited = new Map<number, string>();","atom":"a1500","author":"trials","version":151},{"n":654,"text":"\t\tconst where = `json_type(changes, '$.${field}') IS NOT NULL${id === undefined ? \"\" : \" AND issue = ?\"}`;","atom":"a1500","author":"trials","version":151},{"n":655,"text":"\t\tfor (const e of this.rows(","atom":"a1500","author":"trials","version":151},{"n":656,"text":"\t\t\t`SELECT issue, by FROM edits WHERE ${where} ORDER BY id`,","atom":"a1500","author":"trials","version":151},{"n":657,"text":"\t\t\t...(id === undefined ? [] : [id]),","atom":"a1500","author":"trials","version":151},{"n":658,"text":"\t\t))","atom":"a1500","author":"trials","version":151},{"n":659,"text":"\t\t\tedited.set(e.issue as number, e.by as string);","atom":"a1500","author":"trials","version":151},{"n":660,"text":"\t\treturn edited;","atom":"a1500","author":"trials","version":151},{"n":661,"text":"\t}","atom":"a1500","author":"trials","version":151},{"n":662,"text":"","atom":"a1500","author":"trials","version":151},{"n":663,"text":"\t/** Who wrote an issue's title or description now: whoever last edited it, else whoever opened the issue. */","atom":"a1500","author":"trials","version":151},{"n":664,"text":"\tprivate issueFieldAuthorship(row: Row, edited: Map<number, string>): ProseAuthorship {","atom":"a1500","author":"trials","version":151},{"n":665,"text":"\t\tconst editor = edited.get(row.id as number);","atom":"a1500","author":"trials","version":151},{"n":666,"text":"\t\treturn editor === undefined ? authored(row.opened_by, \"issues.opened_by\") : authored(editor, \"edits.by\");","atom":"a1500","author":"trials","version":151},{"n":667,"text":"\t}","atom":"a1500","author":"trials","version":151},{"n":668,"text":"","atom":"a1500","author":"trials","version":151},{"n":669,"text":"\t/** An ask as the client reads it: each text with who wrote it. */","atom":"a1500","author":"trials","version":151},{"n":670,"text":"\tprivate authoredAsk(a: StoredAsk): Ask {","atom":"a1500","author":"trials","version":151},{"n":671,"text":"\t\tconst { ack, ...ask } = a;","atom":"a1500","author":"trials","version":151},{"n":672,"text":"\t\treturn {","atom":"a1500","author":"trials","version":151},{"n":673,"text":"\t\t\t...ask,","atom":"a1500","author":"trials","version":151},{"n":674,"text":"\t\t\tauthorship: { question: authored(a.by, \"asks.by\") },","atom":"a1500","author":"trials","version":151},{"n":675,"text":"\t\t\tanswer: a.answer ? { ...a.answer, authorship: authored(a.answer.by, \"asks.answer_by\") } : null,","atom":"a1500","author":"trials","version":151},{"n":676,"text":"\t\t\t...(ack","atom":"a1500","author":"trials","version":151},{"n":677,"text":"\t\t\t\t? { ack: { ...ack, authorship: { note: ack.note === \"\" ? UNKNOWN : authored(a.of, \"ask_replies.by\") } } }","atom":"a1500","author":"trials","version":151},{"n":678,"text":"\t\t\t\t: {}),","atom":"a1500","author":"trials","version":151},{"n":679,"text":"\t\t\treplies: a.replies.map((reply) => ({ ...reply, authorship: authored(reply.by, \"ask_replies.by\") })),","atom":"a1500","author":"trials","version":151},{"n":680,"text":"\t\t};","atom":"a1500","author":"trials","version":151},{"n":681,"text":"\t}","atom":"a1500","author":"trials","version":151},{"n":682,"text":"","atom":"a1500","author":"trials","version":151},{"n":683,"text":"\t// Reads ----------------------------------------------------------------------------------","atom":"a1450","author":"trials","version":151},{"n":684,"text":"","atom":"a1450","author":"trials","version":151},{"n":685,"text":"\t/**","atom":"a1450","author":"trials","version":151},{"n":686,"text":"\t * Issues, most recently updated first, filtered by status, agent, parent or","atom":"a1450","author":"trials","version":151},{"n":687,"text":"\t * labels (each one named). `open` leaves out done and dropped.","atom":"a1450","author":"trials","version":151},{"n":688,"text":"\t */","atom":"a1450","author":"trials","version":151},{"n":689,"text":"\tlist(","atom":"a1450","author":"trials","version":151},{"n":690,"text":"\t\tfilter: { status?: string; agent?: string; parent?: number; open?: boolean; labels?: readonly string[] } = {},","atom":"a1450","author":"trials","version":151},{"n":691,"text":"\t): IssueRow[] {","atom":"a1500","author":"trials","version":151},{"n":692,"text":"\t\tconst wanted = filter.labels === undefined ? [] : labelsOf([...filter.labels], \"the labels to filter by\");","atom":"a1450","author":"trials","version":151},{"n":693,"text":"\t\tconst rows =","atom":"a1450","author":"trials","version":151},{"n":694,"text":"\t\t\tfilter.parent === undefined","atom":"a1450","author":"trials","version":151},{"n":695,"text":"\t\t\t\t? this.rows(\"SELECT * FROM issues ORDER BY updated_at DESC, id DESC\")","atom":"a1450","author":"trials","version":151},{"n":696,"text":"\t\t\t\t: this.rows(\"SELECT * FROM issues WHERE parent = ? ORDER BY updated_at DESC, id DESC\", filter.parent);","atom":"a1450","author":"trials","version":151},{"n":697,"text":"\t\treturn this.statuses(rows).filter(","atom":"a1450","author":"trials","version":151},{"n":698,"text":"\t\t\t(i) =>","atom":"a1450","author":"trials","version":151},{"n":699,"text":"\t\t\t\t(filter.status === undefined || i.status === filter.status) &&","atom":"a1450","author":"trials","version":151},{"n":700,"text":"\t\t\t\t(filter.agent === undefined || i.agent === filter.agent || i.agents.includes(filter.agent)) &&","atom":"a1450","author":"trials","version":151},{"n":701,"text":"\t\t\t\t(!filter.open || (i.status !== \"done\" && i.status !== \"dropped\")) &&","atom":"a1450","author":"trials","version":151},{"n":702,"text":"\t\t\t\twanted.every((l) => i.labels.includes(l)),","atom":"a1450","author":"trials","version":151},{"n":703,"text":"\t\t);","atom":"a1450","author":"trials","version":151},{"n":704,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":705,"text":"","atom":"a1450","author":"trials","version":151},{"n":706,"text":"\t/** Issues matching `q`, best first, one hit per issue at its best place. Drafts aren't searched. */","atom":"a1500","author":"trials","version":151},{"n":707,"text":"\tsearch(q: string, limit = 20): AuthoredSearchHit[] {","atom":"a1450","author":"trials","version":151},{"n":708,"text":"\t\tconst query = searchQuery(q);","atom":"a1450","author":"trials","version":151},{"n":709,"text":"\t\tif (query === undefined) return [];","atom":"a1450","author":"trials","version":151},{"n":710,"text":"\t\tconst rows = this.rows(","atom":"a1450","author":"trials","version":151},{"n":711,"text":"\t\t\t\"SELECT issue, comment, field, snippet(search, 3, char(1), char(2), '…', 14) AS snip, bm25(search) AS rank FROM search WHERE search MATCH ? ORDER BY rank LIMIT 500\",","atom":"a1450","author":"trials","version":151},{"n":712,"text":"\t\t\tquery,","atom":"a1450","author":"trials","version":151},{"n":713,"text":"\t\t);","atom":"a1450","author":"trials","version":151},{"n":714,"text":"\t\tconst best = new Map<number, Row>();","atom":"a1450","author":"trials","version":151},{"n":715,"text":"\t\tfor (const r of rows) if (!best.has(r.issue as number)) best.set(r.issue as number, r);","atom":"a1450","author":"trials","version":151},{"n":716,"text":"\t\tconst ids = [...best.keys()].slice(0, Math.max(1, Math.min(limit, 50)));","atom":"a1450","author":"trials","version":151},{"n":717,"text":"\t\tif (ids.length === 0) return [];","atom":"a1450","author":"trials","version":151},{"n":718,"text":"\t\tconst found = this.rows(`SELECT * FROM issues WHERE id IN (${ids.map(() => \"?\").join(\", \")})`, ...ids);","atom":"a1500","author":"trials","version":151},{"n":719,"text":"\t\tconst rowOf = new Map(found.map((r) => [r.id as number, r]));","atom":"a1500","author":"trials","version":151},{"n":720,"text":"\t\tconst issues = new Map(this.statuses(found).map((i) => [i.id, i]));","atom":"a1500","author":"trials","version":151},{"n":721,"text":"\t\treturn ids.flatMap((id) => {","atom":"a1450","author":"trials","version":151},{"n":722,"text":"\t\t\tconst r = best.get(id)!;","atom":"a1450","author":"trials","version":151},{"n":723,"text":"\t\t\tconst i = issues.get(id);","atom":"a1450","author":"trials","version":151},{"n":724,"text":"\t\t\tif (!i) return [];","atom":"a1450","author":"trials","version":151},{"n":725,"text":"\t\t\tconst { text, marks } = marked(r.snip as string);","atom":"a1450","author":"trials","version":151},{"n":726,"text":"\t\t\tconst where = r.field as SearchHit[\"where\"];","atom":"a1450","author":"trials","version":151},{"n":727,"text":"\t\t\tconst commentId = r.comment as number;","atom":"a1450","author":"trials","version":151},{"n":728,"text":"\t\t\tconst matchedAuthorship =","atom":"a1450","author":"trials","version":151},{"n":729,"text":"\t\t\t\twhere === \"comment\"","atom":"a1450","author":"trials","version":151},{"n":730,"text":"\t\t\t\t\t? authored(this.rows(\"SELECT by FROM comments WHERE id = ?\", commentId)[0]?.by, \"comments.by\")","atom":"a1500","author":"trials","version":151},{"n":731,"text":"\t\t\t\t\t: where === \"title\"","atom":"a1500","author":"trials","version":151},{"n":732,"text":"\t\t\t\t\t\t? i.authorship.title","atom":"a1500","author":"trials","version":151},{"n":733,"text":"\t\t\t\t\t\t: this.issueFieldAuthorship(rowOf.get(id)!, this.lastEditors(\"description\", id));","atom":"a1500","author":"trials","version":151},{"n":734,"text":"\t\t\treturn [","atom":"a1450","author":"trials","version":151},{"n":735,"text":"\t\t\t\t{","atom":"a1450","author":"trials","version":151},{"n":736,"text":"\t\t\t\t\tissue: {","atom":"a1450","author":"trials","version":151},{"n":737,"text":"\t\t\t\t\t\tid,","atom":"a1450","author":"trials","version":151},{"n":738,"text":"\t\t\t\t\t\ttitle: i.title,","atom":"a1450","author":"trials","version":151},{"n":739,"text":"\t\t\t\t\t\tstatus: i.status,","atom":"a1450","author":"trials","version":151},{"n":740,"text":"\t\t\t\t\t\tupdated: i.updated,","atom":"a1450","author":"trials","version":151},{"n":741,"text":"\t\t\t\t\t\tlabels: i.labels,","atom":"a1450","author":"trials","version":151},{"n":742,"text":"\t\t\t\t\t\tauthorship: i.authorship,","atom":"a1450","author":"trials","version":151},{"n":743,"text":"\t\t\t\t\t},","atom":"a1450","author":"trials","version":151},{"n":744,"text":"\t\t\t\t\twhere,","atom":"a1450","author":"trials","version":151},{"n":745,"text":"\t\t\t\t\t...(where === \"comment\" ? { comment: commentId } : {}),","atom":"a1450","author":"trials","version":151},{"n":746,"text":"\t\t\t\t\tsnippet: text,","atom":"a1450","author":"trials","version":151},{"n":747,"text":"\t\t\t\t\tmarks,","atom":"a1450","author":"trials","version":151},{"n":748,"text":"\t\t\t\t\tmatchedAuthorship,","atom":"a1450","author":"trials","version":151},{"n":749,"text":"\t\t\t\t\tscore: -(r.rank as number),","atom":"a1450","author":"trials","version":151},{"n":750,"text":"\t\t\t\t},","atom":"a1450","author":"trials","version":151},{"n":751,"text":"\t\t\t];","atom":"a1450","author":"trials","version":151},{"n":752,"text":"\t\t});","atom":"a1450","author":"trials","version":151},{"n":753,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":754,"text":"","atom":"a1450","author":"trials","version":151},{"n":755,"text":"\t/** Every label in use, with how many issues have it and how many of those are open (done and dropped aren't). */","atom":"a1450","author":"trials","version":151},{"n":756,"text":"\tlabelsInUse(): { label: string; open: number; total: number }[] {","atom":"a1450","author":"trials","version":151},{"n":757,"text":"\t\treturn this.rows(","atom":"a1450","author":"trials","version":151},{"n":758,"text":"\t\t\t\"SELECT label, COUNT(*) AS total, SUM(CASE WHEN issues.closed_how IS NULL THEN 1 ELSE 0 END) AS open FROM labels JOIN issues ON issues.id = labels.issue GROUP BY label ORDER BY label\",","atom":"a1450","author":"trials","version":151},{"n":759,"text":"\t\t).map((r) => ({ label: r.label as string, open: r.open as number, total: r.total as number }));","atom":"a1450","author":"trials","version":151},{"n":760,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":761,"text":"","atom":"a1500","author":"trials","version":151},{"n":762,"text":"\t/** An ask with its thread, oldest first. */","atom":"a1500","author":"trials","version":151},{"n":763,"text":"\tprivate withFiles(a: StoredAsk): StoredAsk {","atom":"a1450","author":"trials","version":151},{"n":764,"text":"\t\treturn { ...a, replies: this.repliesOf(a.id) };","atom":"a1500","author":"trials","version":151},{"n":765,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":766,"text":"","atom":"a1450","author":"trials","version":151},{"n":767,"text":"\t/** An ask's replies, oldest first. */","atom":"a1450","author":"trials","version":151},{"n":768,"text":"\tprivate repliesOf(ask: number): StoredAskReply[] {","atom":"a1450","author":"trials","version":151},{"n":769,"text":"\t\treturn this.rows(\"SELECT id, by, text, question, at FROM ask_replies WHERE ask = ? ORDER BY id\", ask).map((r) => ({","atom":"a1450","author":"trials","version":151},{"n":770,"text":"\t\t\tid: r.id as number,","atom":"a1450","author":"trials","version":151},{"n":771,"text":"\t\t\tby: r.by as string,","atom":"a1450","author":"trials","version":151},{"n":772,"text":"\t\t\ttext: r.text as string,","atom":"a1450","author":"trials","version":151},{"n":773,"text":"\t\t\tat: r.at as number,","atom":"a1450","author":"trials","version":151},{"n":774,"text":"\t\t\t...(r.question === 1 ? { question: true as const } : {}),","atom":"a1450","author":"trials","version":151},{"n":775,"text":"\t\t}));","atom":"a1450","author":"trials","version":151},{"n":776,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":777,"text":"","atom":"a1500","author":"trials","version":151},{"n":778,"text":"\tpage(id: number): IssuePage {","atom":"a1500","author":"trials","version":151},{"n":779,"text":"\t\tconst row = this.exists(id);","atom":"a1450","author":"trials","version":151},{"n":780,"text":"\t\tconst [issue] = this.statuses([row]);","atom":"a1450","author":"trials","version":151},{"n":781,"text":"\t\treturn {","atom":"a1450","author":"trials","version":151},{"n":782,"text":"\t\t\tissue: {","atom":"a1450","author":"trials","version":151},{"n":783,"text":"\t\t\t\t...issue!,","atom":"a1450","author":"trials","version":151},{"n":784,"text":"\t\t\t\tdescription: row.description as string,","atom":"a1450","author":"trials","version":151},{"n":785,"text":"\t\t\t\tauthorship: {","atom":"a1450","author":"trials","version":151},{"n":786,"text":"\t\t\t\t\ttitle: issue!.authorship.title,","atom":"a1500","author":"trials","version":151},{"n":787,"text":"\t\t\t\t\tdescription: this.issueFieldAuthorship(row, this.lastEditors(\"description\", id)),","atom":"a1500","author":"trials","version":151},{"n":788,"text":"\t\t\t\t},","atom":"a1450","author":"trials","version":151},{"n":789,"text":"\t\t\t\tlines: row.lines ? (JSON.parse(row.lines as string) as Lines) : null,","atom":"a1450","author":"trials","version":151},{"n":790,"text":"\t\t\t\twatchers: this.watchersOf(id),","atom":"a1450","author":"trials","version":151},{"n":791,"text":"\t\t\t},","atom":"a1450","author":"trials","version":151},{"n":792,"text":"\t\t\tcomments: this.rows(\"SELECT * FROM comments WHERE issue = ? ORDER BY id\", id).map((c) => ({","atom":"a1450","author":"trials","version":151},{"n":793,"text":"\t\t\t\tid: c.id as number,","atom":"a1450","author":"trials","version":151},{"n":794,"text":"\t\t\t\tby: c.by as string,","atom":"a1450","author":"trials","version":151},{"n":795,"text":"\t\t\t\tkind: c.kind as Comment[\"kind\"],","atom":"a1450","author":"trials","version":151},{"n":796,"text":"\t\t\t\ttext: c.text as string,","atom":"a1450","author":"trials","version":151},{"n":797,"text":"\t\t\t\tauthorship: authored(c.by, \"comments.by\"),","atom":"a1500","author":"trials","version":151},{"n":798,"text":"\t\t\t\tat: c.at as number,","atom":"a1450","author":"trials","version":151},{"n":799,"text":"\t\t\t})),","atom":"a1450","author":"trials","version":151},{"n":800,"text":"\t\t\tedits: this.rows(\"SELECT * FROM edits WHERE issue = ? ORDER BY id\", id).map((e) => ({","atom":"a1450","author":"trials","version":151},{"n":801,"text":"\t\t\t\tid: e.id as number,","atom":"a1450","author":"trials","version":151},{"n":802,"text":"\t\t\t\tby: e.by as string,","atom":"a1450","author":"trials","version":151},{"n":803,"text":"\t\t\t\tchanges: JSON.parse(e.changes as string) as Edit[\"changes\"],","atom":"a1450","author":"trials","version":151},{"n":804,"text":"\t\t\t\tnotified: JSON.parse(e.notified as string) as string[],","atom":"a1450","author":"trials","version":151},{"n":805,"text":"\t\t\t\tat: e.at as number,","atom":"a1450","author":"trials","version":151},{"n":806,"text":"\t\t\t})),","atom":"a1450","author":"trials","version":151},{"n":807,"text":"\t\t\tasks: this.rows(\"SELECT * FROM asks WHERE issue = ? ORDER BY id\", id).map((r) =>","atom":"a1450","author":"trials","version":151},{"n":808,"text":"\t\t\t\tthis.authoredAsk(this.withFiles(askOf(r))),","atom":"a1450","author":"trials","version":151},{"n":809,"text":"\t\t\t),","atom":"a1450","author":"trials","version":151},{"n":810,"text":"\t\t\tclaims: this.rows(\"SELECT agent, change, since FROM claims WHERE issue = ? ORDER BY since\", id).map((c) => ({","atom":"a1450","author":"trials","version":151},{"n":811,"text":"\t\t\t\tagent: c.agent as string,","atom":"a1450","author":"trials","version":151},{"n":812,"text":"\t\t\t\tchange: c.change as number,","atom":"a1450","author":"trials","version":151},{"n":813,"text":"\t\t\t\tsince: c.since as number,","atom":"a1450","author":"trials","version":151},{"n":814,"text":"\t\t\t})),","atom":"a1450","author":"trials","version":151},{"n":815,"text":"\t\t\tchildren: this.statuses(this.rows(\"SELECT * FROM issues WHERE parent = ? ORDER BY id\", id)),","atom":"a1450","author":"trials","version":151},{"n":816,"text":"\t\t};","atom":"a1450","author":"trials","version":151},{"n":817,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":818,"text":"","atom":"a1450","author":"trials","version":151},{"n":819,"text":"\t/**","atom":"a1450","author":"trials","version":151},{"n":820,"text":"\t * Open asks of someone (an agent's name or person:<id>), soonest due first, then oldest; every open ask without `of`.","atom":"a1450","author":"trials","version":151},{"n":821,"text":"\t * When `reader` is the one they're asked of, each listed for the first time is marked seen now; the answer still","atom":"a1450","author":"trials","version":151},{"n":822,"text":"\t * says null for those, so the reader can tell what's new.","atom":"a1450","author":"trials","version":151},{"n":823,"text":"\t */","atom":"a1450","author":"trials","version":151},{"n":824,"text":"\twaiting(of?: string, reader?: string): (Ask & { title: string | null })[] {","atom":"a1500","author":"trials","version":151},{"n":825,"text":"\t\tconst select =","atom":"a1450","author":"trials","version":151},{"n":826,"text":"\t\t\t\"SELECT asks.*, issues.title FROM asks LEFT JOIN issues ON issues.id = asks.issue WHERE answer_at IS NULL\";","atom":"a1450","author":"trials","version":151},{"n":827,"text":"\t\tconst rows =","atom":"a1450","author":"trials","version":151},{"n":828,"text":"\t\t\tof === undefined","atom":"a1450","author":"trials","version":151},{"n":829,"text":"\t\t\t\t? this.rows(`${select} ORDER BY due IS NULL, due, asks.id`)","atom":"a1450","author":"trials","version":151},{"n":830,"text":"\t\t\t\t: this.rows(`${select} AND COALESCE(waiting_on, of) = ? ORDER BY due IS NULL, due, asks.id`, of);","atom":"a1450","author":"trials","version":151},{"n":831,"text":"\t\tif (of !== undefined && reader === of)","atom":"a1450","author":"trials","version":151},{"n":832,"text":"\t\t\tthis.sql.exec(","atom":"a1450","author":"trials","version":151},{"n":833,"text":"\t\t\t\t\"UPDATE asks SET seen_at = ? WHERE of = ? AND answer_at IS NULL AND seen_at IS NULL\",","atom":"a1450","author":"trials","version":151},{"n":834,"text":"\t\t\t\tthis.now(),","atom":"a1450","author":"trials","version":151},{"n":835,"text":"\t\t\t\tof,","atom":"a1450","author":"trials","version":151},{"n":836,"text":"\t\t\t);","atom":"a1450","author":"trials","version":151},{"n":837,"text":"\t\treturn rows.map((r) => ({","atom":"a1450","author":"trials","version":151},{"n":838,"text":"\t\t\t...this.authoredAsk(this.withFiles(askOf(r))),","atom":"a1450","author":"trials","version":151},{"n":839,"text":"\t\t\ttitle: (r.title as string | null) ?? null,","atom":"a1450","author":"trials","version":151},{"n":840,"text":"\t\t}));","atom":"a1450","author":"trials","version":151},{"n":841,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":842,"text":"","atom":"a1450","author":"trials","version":151},{"n":843,"text":"\t/** Asks of someone that have been answered, newest first. */","atom":"a1500","author":"trials","version":151},{"n":844,"text":"\tanswered(of: string, limit = 50): (Ask & { title: string | null })[] {","atom":"a1500","author":"trials","version":151},{"n":845,"text":"\t\treturn this.rows(","atom":"a1450","author":"trials","version":151},{"n":846,"text":"\t\t\t\"SELECT asks.*, issues.title FROM asks LEFT JOIN issues ON issues.id = asks.issue WHERE answer_at IS NOT NULL AND of = ? ORDER BY answer_at DESC LIMIT ?\",","atom":"a1450","author":"trials","version":151},{"n":847,"text":"\t\t\tof,","atom":"a1450","author":"trials","version":151},{"n":848,"text":"\t\t\tlimit,","atom":"a1450","author":"trials","version":151},{"n":849,"text":"\t\t).map((r) => ({","atom":"a1450","author":"trials","version":151},{"n":850,"text":"\t\t\t...this.authoredAsk(this.withFiles(askOf(r))),","atom":"a1450","author":"trials","version":151},{"n":851,"text":"\t\t\ttitle: (r.title as string | null) ?? null,","atom":"a1450","author":"trials","version":151},{"n":852,"text":"\t\t}));","atom":"a1450","author":"trials","version":151},{"n":853,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":854,"text":"","atom":"a1450","author":"trials","version":151},{"n":855,"text":"\t/** The asks someone sent: every open one, oldest first, then the latest answered or withdrawn. */","atom":"a1500","author":"trials","version":151},{"n":856,"text":"\tasked(by: string, answeredLimit = 10): (Ask & { title: string | null })[] {","atom":"a1500","author":"trials","version":151},{"n":857,"text":"\t\tconst select = \"SELECT asks.*, issues.title FROM asks LEFT JOIN issues ON issues.id = asks.issue WHERE asks.by = ?\";","atom":"a1450","author":"trials","version":151},{"n":858,"text":"\t\treturn [","atom":"a1450","author":"trials","version":151},{"n":859,"text":"\t\t\t...this.rows(`${select} AND answer_at IS NULL ORDER BY asks.id`, by),","atom":"a1450","author":"trials","version":151},{"n":860,"text":"\t\t\t...this.rows(`${select} AND answer_at IS NOT NULL ORDER BY answer_at DESC LIMIT ?`, by, answeredLimit),","atom":"a1450","author":"trials","version":151},{"n":861,"text":"\t\t].map((r) => ({ ...this.authoredAsk(this.withFiles(askOf(r))), title: (r.title as string | null) ?? null }));","atom":"a1450","author":"trials","version":151},{"n":862,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":863,"text":"","atom":"a1450","author":"trials","version":151},{"n":864,"text":"\t/** One ask, open or closed, with its thread and its issue's title. */","atom":"a1450","author":"trials","version":151},{"n":865,"text":"\taskThread(id: number): Ask & { title: string | null } {","atom":"a1500","author":"trials","version":151},{"n":866,"text":"\t\tconst r = this.rows(","atom":"a1450","author":"trials","version":151},{"n":867,"text":"\t\t\t\"SELECT asks.*, issues.title FROM asks LEFT JOIN issues ON issues.id = asks.issue WHERE asks.id = ?\",","atom":"a1450","author":"trials","version":151},{"n":868,"text":"\t\t\tissueNumber(id, \"an ask number\"),","atom":"a1450","author":"trials","version":151},{"n":869,"text":"\t\t)[0];","atom":"a1450","author":"trials","version":151},{"n":870,"text":"\t\tif (!r) throw new IssuesError(`there's no ask ${id}`, 404);","atom":"a1450","author":"trials","version":151},{"n":871,"text":"\t\treturn { ...this.authoredAsk(this.withFiles(askOf(r))), title: (r.title as string | null) ?? null };","atom":"a1450","author":"trials","version":151},{"n":872,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":873,"text":"","atom":"a1450","author":"trials","version":151},{"n":874,"text":"\t// Writes ---------------------------------------------------------------------------------","atom":"a1450","author":"trials","version":151},{"n":875,"text":"","atom":"a1450","author":"trials","version":151},{"n":876,"text":"\t/**","atom":"a1450","author":"trials","version":151},{"n":877,"text":"\t * A new issue. Agent is who it's for, Blocked by the issues it waits on,","atom":"a1450","author":"trials","version":151},{"n":878,"text":"\t * and Notify who the writer tells. The writer, its Agent and Notify watch","atom":"a1450","author":"trials","version":151},{"n":879,"text":"\t * it; Agent and Notify hear it now, and the parent's agents hear it too.","atom":"a1450","author":"trials","version":151},{"n":880,"text":"\t */","atom":"a1450","author":"trials","version":151},{"n":881,"text":"\topen(actor: Actor, body: Parameters<IssuesStore[\"openRecord\"]>[1]): { id: number; news?: News } {","atom":"a1500","author":"trials","version":151},{"n":882,"text":"\t\treturn this.transaction(() => this.openRecord(actor, body));","atom":"a1500","author":"trials","version":151},{"n":883,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":884,"text":"","atom":"a1450","author":"trials","version":151},{"n":885,"text":"\tprivate openRecord(","atom":"a1450","author":"trials","version":151},{"n":886,"text":"\t\tactor: Actor,","atom":"a1450","author":"trials","version":151},{"n":887,"text":"\t\tbody: {","atom":"a1450","author":"trials","version":151},{"n":888,"text":"\t\t\ttitle?: unknown;","atom":"a1450","author":"trials","version":151},{"n":889,"text":"\t\t\tdescription?: unknown;","atom":"a1450","author":"trials","version":151},{"n":890,"text":"\t\t\tparent?: unknown;","atom":"a1450","author":"trials","version":151},{"n":891,"text":"\t\t\tlines?: unknown;","atom":"a1450","author":"trials","version":151},{"n":892,"text":"\t\t\tagent?: unknown;","atom":"a1450","author":"trials","version":151},{"n":893,"text":"\t\t\tblockedBy?: unknown;","atom":"a1450","author":"trials","version":151},{"n":894,"text":"\t\t\tnotify?: unknown;","atom":"a1450","author":"trials","version":151},{"n":895,"text":"\t\t\tlabels?: unknown;","atom":"a1450","author":"trials","version":151},{"n":896,"text":"\t\t\tattachments?: unknown;","atom":"a1450","author":"trials","version":151},{"n":897,"text":"\t\t},","atom":"a1450","author":"trials","version":151},{"n":898,"text":"\t): { id: number; news?: News } {","atom":"a1450","author":"trials","version":151},{"n":899,"text":"\t\tconst title = text(body.title, \"the title\", LIMITS.title, true, true);","atom":"a1450","author":"trials","version":151},{"n":900,"text":"\t\tthis.noAttachments(body.attachments);","atom":"a1500","author":"trials","version":151},{"n":901,"text":"\t\tconst description = text(body.description, \"the description\", LIMITS.description, false);","atom":"a1450","author":"trials","version":151},{"n":902,"text":"\t\tconst parent = this.parentFor(null, body.parent);","atom":"a1450","author":"trials","version":151},{"n":903,"text":"\t\tconst at_ = lines(body.lines);","atom":"a1450","author":"trials","version":151},{"n":904,"text":"\t\tconst agent = body.agent === undefined ? null : nameOrNone(body.agent, \"the Agent\");","atom":"a1450","author":"trials","version":151},{"n":905,"text":"\t\tconst blockedBy = this.blockersFor(null, body.blockedBy);","atom":"a1450","author":"trials","version":151},{"n":906,"text":"\t\tconst notify = names(body.notify, \"Notify\");","atom":"a1450","author":"trials","version":151},{"n":907,"text":"\t\tconst labels = labelsOf(body.labels);","atom":"a1450","author":"trials","version":151},{"n":908,"text":"\t\tthis.roomForLabels(labels);","atom":"a1450","author":"trials","version":151},{"n":909,"text":"\t\tconst open = this.rows(\"SELECT COUNT(*) AS n FROM issues WHERE closed_how IS NULL\")[0]!.n as number;","atom":"a1450","author":"trials","version":151},{"n":910,"text":"\t\tif (open >= LIMITS.open)","atom":"a1450","author":"trials","version":151},{"n":911,"text":"\t\t\tthrow new IssuesError(`this project has ${open} open issues, the most it can hold; close some first`);","atom":"a1450","author":"trials","version":151},{"n":912,"text":"\t\tconst at = this.now();","atom":"a1450","author":"trials","version":151},{"n":913,"text":"\t\tconst row = this.rows(","atom":"a1450","author":"trials","version":151},{"n":914,"text":"\t\t\t\"INSERT INTO issues (parent, title, description, opened_by, opened_at, updated_at, lines, agent) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING id\",","atom":"a1450","author":"trials","version":151},{"n":915,"text":"\t\t\tparent,","atom":"a1450","author":"trials","version":151},{"n":916,"text":"\t\t\ttitle,","atom":"a1450","author":"trials","version":151},{"n":917,"text":"\t\t\tdescription,","atom":"a1450","author":"trials","version":151},{"n":918,"text":"\t\t\twho(actor),","atom":"a1450","author":"trials","version":151},{"n":919,"text":"\t\t\tat,","atom":"a1450","author":"trials","version":151},{"n":920,"text":"\t\t\tat,","atom":"a1450","author":"trials","version":151},{"n":921,"text":"\t\t\tat_ ? JSON.stringify(at_) : null,","atom":"a1450","author":"trials","version":151},{"n":922,"text":"\t\t\tagent,","atom":"a1450","author":"trials","version":151},{"n":923,"text":"\t\t)[0]!;","atom":"a1450","author":"trials","version":151},{"n":924,"text":"\t\tconst id = row.id as number;","atom":"a1450","author":"trials","version":151},{"n":925,"text":"\t\tfor (const b of blockedBy) this.sql.exec(\"INSERT INTO blocked_by (issue, blocker) VALUES (?, ?)\", id, b);","atom":"a1450","author":"trials","version":151},{"n":926,"text":"\t\tfor (const l of labels)","atom":"a1450","author":"trials","version":151},{"n":927,"text":"\t\t\tthis.sql.exec(\"INSERT INTO labels (issue, label, by, at) VALUES (?, ?, ?, ?)\", id, l, who(actor), at);","atom":"a1450","author":"trials","version":151},{"n":928,"text":"\t\tthis.watch(id, who(actor), agent, ...notify);","atom":"a1450","author":"trials","version":151},{"n":929,"text":"\t\tthis.touch(parent);","atom":"a1450","author":"trials","version":151},{"n":930,"text":"\t\tconst to = unique([...(agent !== null ? [agent] : []), ...notify]).filter((p) => p !== who(actor));","atom":"a1450","author":"trials","version":151},{"n":931,"text":"\t\tif (parent === null && to.length === 0) return { id };","atom":"a1450","author":"trials","version":151},{"n":932,"text":"\t\tconst detail = [","atom":"a1450","author":"trials","version":151},{"n":933,"text":"\t\t\tagent !== null ? `Agent ${agent}` : \"\",","atom":"a1450","author":"trials","version":151},{"n":934,"text":"\t\t\tblockedBy.length > 0 ? `blocked by ${blockedBy.join(\", \")}` : \"\",","atom":"a1450","author":"trials","version":151},{"n":935,"text":"\t\t].filter((d) => d !== \"\");","atom":"a1450","author":"trials","version":151},{"n":936,"text":"\t\treturn {","atom":"a1450","author":"trials","version":151},{"n":937,"text":"\t\t\tid,","atom":"a1450","author":"trials","version":151},{"n":938,"text":"\t\t\tnews: {","atom":"a1450","author":"trials","version":151},{"n":939,"text":"\t\t\t\tissue: parent,","atom":"a1450","author":"trials","version":151},{"n":940,"text":"\t\t\t\ttext: `${who(actor)} opened issue ${id}${parent !== null ? ` under issue ${parent}` : \"\"}: ${JSON.stringify(title)}${detail.length > 0 ? ` (${detail.join(\"; \")})` : \"\"}`,","atom":"a1450","author":"trials","version":151},{"n":941,"text":"\t\t\t\t...(to.length > 0 ? { to } : {}),","atom":"a1450","author":"trials","version":151},{"n":942,"text":"\t\t\t},","atom":"a1450","author":"trials","version":151},{"n":943,"text":"\t\t};","atom":"a1450","author":"trials","version":151},{"n":944,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":945,"text":"","atom":"a1450","author":"trials","version":151},{"n":946,"text":"\t/**","atom":"a1450","author":"trials","version":151},{"n":947,"text":"\t * An edit: title, description, parent, Agent, Blocked by or labels, kept in","atom":"a1450","author":"trials","version":151},{"n":948,"text":"\t * the issue's activity. It tells the watchers unless `notify` is false;","atom":"a1450","author":"trials","version":151},{"n":949,"text":"\t * `also` adds people, who watch it from then on (they were named in Notify).","atom":"a1450","author":"trials","version":151},{"n":950,"text":"\t * Labels are `labels` (the whole set) or `addLabels` and `removeLabels`; an","atom":"a1450","author":"trials","version":151},{"n":951,"text":"\t * edit that only changes labels tells nobody unless `notify` is true, since","atom":"a1450","author":"trials","version":151},{"n":952,"text":"\t * agents sort issues by label all the time.","atom":"a1450","author":"trials","version":151},{"n":953,"text":"\t */","atom":"a1450","author":"trials","version":151},{"n":954,"text":"\tedit(","atom":"a1450","author":"trials","version":151},{"n":955,"text":"\t\tactor: Actor,","atom":"a1450","author":"trials","version":151},{"n":956,"text":"\t\tid: number,","atom":"a1450","author":"trials","version":151},{"n":957,"text":"\t\tbody: {","atom":"a1450","author":"trials","version":151},{"n":958,"text":"\t\t\ttitle?: unknown;","atom":"a1450","author":"trials","version":151},{"n":959,"text":"\t\t\tdescription?: unknown;","atom":"a1450","author":"trials","version":151},{"n":960,"text":"\t\t\tparent?: unknown;","atom":"a1450","author":"trials","version":151},{"n":961,"text":"\t\t\tagent?: unknown;","atom":"a1450","author":"trials","version":151},{"n":962,"text":"\t\t\tblockedBy?: unknown;","atom":"a1450","author":"trials","version":151},{"n":963,"text":"\t\t\tnotify?: unknown;","atom":"a1450","author":"trials","version":151},{"n":964,"text":"\t\t\talso?: unknown;","atom":"a1450","author":"trials","version":151},{"n":965,"text":"\t\t\tlabels?: unknown;","atom":"a1450","author":"trials","version":151},{"n":966,"text":"\t\t\taddLabels?: unknown;","atom":"a1450","author":"trials","version":151},{"n":967,"text":"\t\t\tremoveLabels?: unknown;","atom":"a1450","author":"trials","version":151},{"n":968,"text":"\t\t},","atom":"a1450","author":"trials","version":151},{"n":969,"text":"\t): { edit: number; notified: string[]; news?: News } {","atom":"a1450","author":"trials","version":151},{"n":970,"text":"\t\tconst row = this.exists(id);","atom":"a1450","author":"trials","version":151},{"n":971,"text":"\t\tconst changes: Edit[\"changes\"] = {};","atom":"a1450","author":"trials","version":151},{"n":972,"text":"\t\tconst sets: [string, unknown][] = [];","atom":"a1450","author":"trials","version":151},{"n":973,"text":"\t\tif (body.title !== undefined) {","atom":"a1450","author":"trials","version":151},{"n":974,"text":"\t\t\tconst title = text(body.title, \"the title\", LIMITS.title, true, true);","atom":"a1450","author":"trials","version":151},{"n":975,"text":"\t\t\tif (title !== row.title) (changes.title = { from: row.title, to: title }), sets.push([\"title\", title]);","atom":"a1450","author":"trials","version":151},{"n":976,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":977,"text":"\t\tif (body.description !== undefined) {","atom":"a1450","author":"trials","version":151},{"n":978,"text":"\t\t\tconst description = text(body.description, \"the description\", LIMITS.description, false);","atom":"a1450","author":"trials","version":151},{"n":979,"text":"\t\t\tif (description !== row.description)","atom":"a1450","author":"trials","version":151},{"n":980,"text":"\t\t\t\t(changes.description = { from: row.description, to: description }), sets.push([\"description\", description]);","atom":"a1450","author":"trials","version":151},{"n":981,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":982,"text":"\t\tif (body.parent !== undefined) {","atom":"a1450","author":"trials","version":151},{"n":983,"text":"\t\t\tconst parent = this.parentFor(id, body.parent);","atom":"a1450","author":"trials","version":151},{"n":984,"text":"\t\t\tif (parent !== ((row.parent as number | null) ?? null))","atom":"a1450","author":"trials","version":151},{"n":985,"text":"\t\t\t\t(changes.parent = { from: row.parent ?? null, to: parent }), sets.push([\"parent\", parent]);","atom":"a1450","author":"trials","version":151},{"n":986,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":987,"text":"\t\tif (body.agent !== undefined) {","atom":"a1450","author":"trials","version":151},{"n":988,"text":"\t\t\tconst agent = nameOrNone(body.agent, \"the Agent\");","atom":"a1450","author":"trials","version":151},{"n":989,"text":"\t\t\tif (agent !== ((row.agent as string | null | undefined) ?? null))","atom":"a1450","author":"trials","version":151},{"n":990,"text":"\t\t\t\t(changes.agent = { from: row.agent ?? null, to: agent }), sets.push([\"agent\", agent]);","atom":"a1450","author":"trials","version":151},{"n":991,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":992,"text":"\t\tlet blockedBy: number[] | undefined;","atom":"a1450","author":"trials","version":151},{"n":993,"text":"\t\tif (body.blockedBy !== undefined) {","atom":"a1450","author":"trials","version":151},{"n":994,"text":"\t\t\tblockedBy = this.blockersFor(id, body.blockedBy);","atom":"a1450","author":"trials","version":151},{"n":995,"text":"\t\t\tconst before = this.rows(\"SELECT blocker FROM blocked_by WHERE issue = ? ORDER BY blocker\", id).map(","atom":"a1450","author":"trials","version":151},{"n":996,"text":"\t\t\t\t(b) => b.blocker as number,","atom":"a1450","author":"trials","version":151},{"n":997,"text":"\t\t\t);","atom":"a1450","author":"trials","version":151},{"n":998,"text":"\t\t\tconst after = [...blockedBy].sort((a, b) => a - b);","atom":"a1450","author":"trials","version":151},{"n":999,"text":"\t\t\tif (before.join() !== after.join()) changes.blockedBy = { from: before, to: after };","atom":"a1450","author":"trials","version":151},{"n":1000,"text":"\t\t\telse blockedBy = undefined;","atom":"a1450","author":"trials","version":151},{"n":1001,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":1002,"text":"\t\tlet labels: string[] | undefined;","atom":"a1450","author":"trials","version":151},{"n":1003,"text":"\t\tif (body.labels !== undefined || body.addLabels !== undefined || body.removeLabels !== undefined) {","atom":"a1450","author":"trials","version":151},{"n":1004,"text":"\t\t\tif (body.labels !== undefined && (body.addLabels !== undefined || body.removeLabels !== undefined))","atom":"a1450","author":"trials","version":151},{"n":1005,"text":"\t\t\t\tthrow new IssuesError(\"labels is the whole set; addLabels and removeLabels change it; send one or the other\");","atom":"a1450","author":"trials","version":151},{"n":1006,"text":"\t\t\tconst before = this.rows(\"SELECT label FROM labels WHERE issue = ? ORDER BY label\", id).map(","atom":"a1450","author":"trials","version":151},{"n":1007,"text":"\t\t\t\t(l) => l.label as string,","atom":"a1450","author":"trials","version":151},{"n":1008,"text":"\t\t\t);","atom":"a1450","author":"trials","version":151},{"n":1009,"text":"\t\t\tconst drop = new Set(labelsOf(body.removeLabels, \"removeLabels\"));","atom":"a1450","author":"trials","version":151},{"n":1010,"text":"\t\t\tconst after =","atom":"a1450","author":"trials","version":151},{"n":1011,"text":"\t\t\t\tbody.labels !== undefined","atom":"a1450","author":"trials","version":151},{"n":1012,"text":"\t\t\t\t\t? labelsOf(body.labels)","atom":"a1450","author":"trials","version":151},{"n":1013,"text":"\t\t\t\t\t: labelsOf([...before.filter((l) => !drop.has(l)), ...labelsOf(body.addLabels, \"addLabels\")]);","atom":"a1450","author":"trials","version":151},{"n":1014,"text":"\t\t\tif (before.join() !== after.join()) {","atom":"a1450","author":"trials","version":151},{"n":1015,"text":"\t\t\t\tthis.roomForLabels(after);","atom":"a1450","author":"trials","version":151},{"n":1016,"text":"\t\t\t\tchanges.labels = { from: before, to: after };","atom":"a1450","author":"trials","version":151},{"n":1017,"text":"\t\t\t\tlabels = after;","atom":"a1450","author":"trials","version":151},{"n":1018,"text":"\t\t\t}","atom":"a1450","author":"trials","version":151},{"n":1019,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":1020,"text":"\t\tif (body.notify !== undefined && typeof body.notify !== \"boolean\")","atom":"a1450","author":"trials","version":151},{"n":1021,"text":"\t\t\tthrow new IssuesError(\"notify is true (tell the watchers, the default) or false (tell nobody)\");","atom":"a1450","author":"trials","version":151},{"n":1022,"text":"\t\tconst also = names(body.also, \"who else to tell\");","atom":"a1450","author":"trials","version":151},{"n":1023,"text":"\t\tif (Object.keys(changes).length === 0) throw new IssuesError(`that changes nothing on issue ${id}`);","atom":"a1450","author":"trials","version":151},{"n":1024,"text":"\t\tconst labelsOnly = Object.keys(changes).every((k) => k === \"labels\");","atom":"a1450","author":"trials","version":151},{"n":1025,"text":"\t\tconst e = this.transaction(() => {","atom":"a1450","author":"trials","version":151},{"n":1026,"text":"\t\t\tfor (const [column, value] of sets) this.sql.exec(`UPDATE issues SET ${column} = ? WHERE id = ?`, value, id);","atom":"a1500","author":"trials","version":151},{"n":1027,"text":"\t\t\tif (blockedBy !== undefined) {","atom":"a1500","author":"trials","version":151},{"n":1028,"text":"\t\t\t\tthis.sql.exec(\"DELETE FROM blocked_by WHERE issue = ?\", id);","atom":"a1500","author":"trials","version":151},{"n":1029,"text":"\t\t\t\tfor (const b of blockedBy) this.sql.exec(\"INSERT INTO blocked_by (issue, blocker) VALUES (?, ?)\", id, b);","atom":"a1500","author":"trials","version":151},{"n":1030,"text":"\t\t\t}","atom":"a1500","author":"trials","version":151},{"n":1031,"text":"\t\t\tif (labels !== undefined) {","atom":"a1500","author":"trials","version":151},{"n":1032,"text":"\t\t\t\t// A label kept keeps who added it and when.","atom":"a1500","author":"trials","version":151},{"n":1033,"text":"\t\t\t\tfor (const l of this.rows(\"SELECT label FROM labels WHERE issue = ?\", id))","atom":"a1500","author":"trials","version":151},{"n":1034,"text":"\t\t\t\t\tif (!labels.includes(l.label as string))","atom":"a1500","author":"trials","version":151},{"n":1035,"text":"\t\t\t\t\t\tthis.sql.exec(\"DELETE FROM labels WHERE issue = ? AND label = ?\", id, l.label);","atom":"a1500","author":"trials","version":151},{"n":1036,"text":"\t\t\t\tfor (const l of labels)","atom":"a1500","author":"trials","version":151},{"n":1037,"text":"\t\t\t\t\tthis.sql.exec(","atom":"a1500","author":"trials","version":151},{"n":1038,"text":"\t\t\t\t\t\t\"INSERT OR IGNORE INTO labels (issue, label, by, at) VALUES (?, ?, ?, ?)\",","atom":"a1500","author":"trials","version":151},{"n":1039,"text":"\t\t\t\t\t\tid,","atom":"a1500","author":"trials","version":151},{"n":1040,"text":"\t\t\t\t\t\tl,","atom":"a1500","author":"trials","version":151},{"n":1041,"text":"\t\t\t\t\t\twho(actor),","atom":"a1500","author":"trials","version":151},{"n":1042,"text":"\t\t\t\t\t\tthis.now(),","atom":"a1500","author":"trials","version":151},{"n":1043,"text":"\t\t\t\t\t);","atom":"a1500","author":"trials","version":151},{"n":1044,"text":"\t\t\t}","atom":"a1500","author":"trials","version":151},{"n":1045,"text":"\t\t\tthis.watch(id, (changes.agent?.to as string | null | undefined) ?? null, ...also);","atom":"a1500","author":"trials","version":151},{"n":1046,"text":"\t\t\tconst notified =","atom":"a1500","author":"trials","version":151},{"n":1047,"text":"\t\t\t\tbody.notify === false || (labelsOnly && body.notify !== true)","atom":"a1500","author":"trials","version":151},{"n":1048,"text":"\t\t\t\t\t? []","atom":"a1500","author":"trials","version":151},{"n":1049,"text":"\t\t\t\t\t: unique([...this.watchersOf(id), ...also]).filter((p) => p !== who(actor));","atom":"a1500","author":"trials","version":151},{"n":1050,"text":"\t\t\tconst edit = this.rows(","atom":"a1500","author":"trials","version":151},{"n":1051,"text":"\t\t\t\t\"INSERT INTO edits (issue, by, changes, notified, at) VALUES (?, ?, ?, ?, ?) RETURNING id\",","atom":"a1450","author":"trials","version":151},{"n":1052,"text":"\t\t\t\tid,","atom":"a1450","author":"trials","version":151},{"n":1053,"text":"\t\t\t\twho(actor),","atom":"a1450","author":"trials","version":151},{"n":1054,"text":"\t\t\t\tJSON.stringify(changes),","atom":"a1450","author":"trials","version":151},{"n":1055,"text":"\t\t\t\tJSON.stringify(notified),","atom":"a1450","author":"trials","version":151},{"n":1056,"text":"\t\t\t\tthis.now(),","atom":"a1450","author":"trials","version":151},{"n":1057,"text":"\t\t\t)[0]!.id as number;","atom":"a1500","author":"trials","version":151},{"n":1058,"text":"\t\t\treturn { id: edit, notified };","atom":"a1500","author":"trials","version":151},{"n":1059,"text":"\t\t});","atom":"a1450","author":"trials","version":151},{"n":1060,"text":"\t\tconst notified = e.notified;","atom":"a1500","author":"trials","version":151},{"n":1061,"text":"\t\tthis.touch(id, (row.parent as number | null) ?? null, (changes.parent?.to as number | null | undefined) ?? null);","atom":"a1450","author":"trials","version":151},{"n":1062,"text":"\t\tif (notified.length === 0) return { edit: e.id as number, notified };","atom":"a1450","author":"trials","version":151},{"n":1063,"text":"\t\tconst title = (changes.title?.to as string | undefined) ?? (row.title as string);","atom":"a1450","author":"trials","version":151},{"n":1064,"text":"\t\tconst said = [","atom":"a1450","author":"trials","version":151},{"n":1065,"text":"\t\t\tchanges.title ? `title was ${JSON.stringify(changes.title.from)}` : \"\",","atom":"a1450","author":"trials","version":151},{"n":1066,"text":"\t\t\tchanges.description ? \"description changed\" : \"\",","atom":"a1450","author":"trials","version":151},{"n":1067,"text":"\t\t\tchanges.parent ? (changes.parent.to === null ? \"no parent now\" : `parent is issue ${changes.parent.to}`) : \"\",","atom":"a1450","author":"trials","version":151},{"n":1068,"text":"\t\t\tchanges.agent ? (changes.agent.to === null ? \"no Agent now\" : `Agent is ${changes.agent.to}`) : \"\",","atom":"a1450","author":"trials","version":151},{"n":1069,"text":"\t\t\tchanges.blockedBy","atom":"a1450","author":"trials","version":151},{"n":1070,"text":"\t\t\t\t? (changes.blockedBy.to as number[]).length === 0","atom":"a1450","author":"trials","version":151},{"n":1071,"text":"\t\t\t\t\t? \"blocked by nothing now\"","atom":"a1450","author":"trials","version":151},{"n":1072,"text":"\t\t\t\t\t: `blocked by ${(changes.blockedBy.to as number[]).join(\", \")}`","atom":"a1450","author":"trials","version":151},{"n":1073,"text":"\t\t\t\t: \"\",","atom":"a1450","author":"trials","version":151},{"n":1074,"text":"\t\t\tchanges.labels","atom":"a1450","author":"trials","version":151},{"n":1075,"text":"\t\t\t\t? (changes.labels.to as string[]).length === 0","atom":"a1450","author":"trials","version":151},{"n":1076,"text":"\t\t\t\t\t? \"no labels now\"","atom":"a1450","author":"trials","version":151},{"n":1077,"text":"\t\t\t\t\t: `labels ${(changes.labels.to as string[]).join(\", \")}`","atom":"a1450","author":"trials","version":151},{"n":1078,"text":"\t\t\t\t: \"\",","atom":"a1450","author":"trials","version":151},{"n":1079,"text":"\t\t].filter((s) => s !== \"\");","atom":"a1450","author":"trials","version":151},{"n":1080,"text":"\t\treturn {","atom":"a1450","author":"trials","version":151},{"n":1081,"text":"\t\t\tedit: e.id as number,","atom":"a1450","author":"trials","version":151},{"n":1082,"text":"\t\t\tnotified,","atom":"a1450","author":"trials","version":151},{"n":1083,"text":"\t\t\tnews: {","atom":"a1450","author":"trials","version":151},{"n":1084,"text":"\t\t\t\tissue: null,","atom":"a1450","author":"trials","version":151},{"n":1085,"text":"\t\t\t\ttext: `${who(actor)} edited issue ${id} (${JSON.stringify(title)}): ${said.join(\"; \")}`,","atom":"a1450","author":"trials","version":151},{"n":1086,"text":"\t\t\t\tto: notified,","atom":"a1450","author":"trials","version":151},{"n":1087,"text":"\t\t\t},","atom":"a1450","author":"trials","version":151},{"n":1088,"text":"\t\t};","atom":"a1450","author":"trials","version":151},{"n":1089,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1090,"text":"","atom":"a1450","author":"trials","version":151},{"n":1091,"text":"\tcomment(","atom":"a1450","author":"trials","version":151},{"n":1092,"text":"\t\tactor: Actor,","atom":"a1450","author":"trials","version":151},{"n":1093,"text":"\t\tid: number,","atom":"a1450","author":"trials","version":151},{"n":1094,"text":"\t\tbody: { text?: unknown; objection?: unknown; attachments?: unknown },","atom":"a1450","author":"trials","version":151},{"n":1095,"text":"\t): { id: number; news: News } {","atom":"a1450","author":"trials","version":151},{"n":1096,"text":"\t\tconst row = this.exists(id);","atom":"a1450","author":"trials","version":151},{"n":1097,"text":"\t\tconst t = text(body.text, \"the comment\", LIMITS.text);","atom":"a1450","author":"trials","version":151},{"n":1098,"text":"\t\tthis.noAttachments(body.attachments);","atom":"a1500","author":"trials","version":151},{"n":1099,"text":"\t\tconst kind = body.objection === true ? \"objection\" : \"comment\";","atom":"a1450","author":"trials","version":151},{"n":1100,"text":"\t\tconst c = this.rows(","atom":"a1500","author":"trials","version":151},{"n":1101,"text":"\t\t\t\"INSERT INTO comments (issue, by, kind, text, at) VALUES (?, ?, ?, ?, ?) RETURNING id\",","atom":"a1500","author":"trials","version":151},{"n":1102,"text":"\t\t\tid,","atom":"a1500","author":"trials","version":151},{"n":1103,"text":"\t\t\twho(actor),","atom":"a1500","author":"trials","version":151},{"n":1104,"text":"\t\t\tkind,","atom":"a1500","author":"trials","version":151},{"n":1105,"text":"\t\t\tt,","atom":"a1500","author":"trials","version":151},{"n":1106,"text":"\t\t\tthis.now(),","atom":"a1500","author":"trials","version":151},{"n":1107,"text":"\t\t)[0]!;","atom":"a1500","author":"trials","version":151},{"n":1108,"text":"\t\tthis.watch(id, who(actor));","atom":"a1450","author":"trials","version":151},{"n":1109,"text":"\t\tthis.touch(id);","atom":"a1450","author":"trials","version":151},{"n":1110,"text":"\t\t// @name in the text tells that agent too; the project drops names that aren't its agents.","atom":"a1500","author":"trials","version":151},{"n":1111,"text":"\t\tconst mentioned = mentionsIn(t).filter((name) => name !== who(actor));","atom":"a1450","author":"trials","version":151},{"n":1112,"text":"\t\treturn {","atom":"a1450","author":"trials","version":151},{"n":1113,"text":"\t\t\tid: c.id as number,","atom":"a1450","author":"trials","version":151},{"n":1114,"text":"\t\t\tnews: {","atom":"a1450","author":"trials","version":151},{"n":1115,"text":"\t\t\t\tissue: id,","atom":"a1450","author":"trials","version":151},{"n":1116,"text":"\t\t\t\ttext: `${who(actor)} ${kind === \"objection\" ? \"objects\" : \"comments\"} on issue ${id} (${JSON.stringify(row.title)}): ${JSON.stringify(t)}`,","atom":"a1450","author":"trials","version":151},{"n":1117,"text":"\t\t\t\t...(mentioned.length > 0 ? { to: mentioned } : {}),","atom":"a1450","author":"trials","version":151},{"n":1118,"text":"\t\t\t},","atom":"a1450","author":"trials","version":151},{"n":1119,"text":"\t\t};","atom":"a1450","author":"trials","version":151},{"n":1120,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1121,"text":"","atom":"a1450","author":"trials","version":151},{"n":1122,"text":"\t/**","atom":"a1450","author":"trials","version":151},{"n":1123,"text":"\t * An ask: a question or a task for someone, on an issue or on none. A","atom":"a1450","author":"trials","version":151},{"n":1124,"text":"\t * review ask (`review`) puts its issue In review; any other holds it up","atom":"a1450","author":"trials","version":151},{"n":1125,"text":"\t * (Blocked), and so do the issues it names in `blocks`.","atom":"a1450","author":"trials","version":151},{"n":1126,"text":"\t */","atom":"a1450","author":"trials","version":151},{"n":1127,"text":"\task(","atom":"a1450","author":"trials","version":151},{"n":1128,"text":"\t\tactor: Actor,","atom":"a1450","author":"trials","version":151},{"n":1129,"text":"\t\tbody: {","atom":"a1450","author":"trials","version":151},{"n":1130,"text":"\t\t\tissue?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1131,"text":"\t\t\tof?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1132,"text":"\t\t\tquestion?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1133,"text":"\t\t\treview?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1134,"text":"\t\t\tkind?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1135,"text":"\t\t\tdue?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1136,"text":"\t\t\trecommendation?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1137,"text":"\t\t\toptions?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1138,"text":"\t\t\tblocks?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1139,"text":"\t\t\tattachments?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1140,"text":"\t\t},","atom":"a1450","author":"trials","version":151},{"n":1141,"text":"\t): { id: number; news: News } {","atom":"a1500","author":"trials","version":151},{"n":1142,"text":"\t\tconst issue = body.issue === undefined || body.issue === null ? null : issueNumber(body.issue);","atom":"a1450","author":"trials","version":151},{"n":1143,"text":"\t\tconst row = issue === null ? undefined : this.exists(issue);","atom":"a1450","author":"trials","version":151},{"n":1144,"text":"\t\tif (row?.closed_how) throw new IssuesError(`issue ${issue} is closed`);","atom":"a1450","author":"trials","version":151},{"n":1145,"text":"\t\tconst of = text(body.of, \"who it asks\", 100, true, true);","atom":"a1450","author":"trials","version":151},{"n":1146,"text":"\t\tconst question = text(body.question, \"the question\", LIMITS.text);","atom":"a1450","author":"trials","version":151},{"n":1147,"text":"\t\tconst kind = body.kind === undefined || body.kind === null ? \"question\" : body.kind;","atom":"a1450","author":"trials","version":151},{"n":1148,"text":"\t\tif (kind !== \"question\" && kind !== \"task\")","atom":"a1450","author":"trials","version":151},{"n":1149,"text":"\t\t\tthrow new IssuesError('kind is \"question\", or \"task\" for something only they can do');","atom":"a1450","author":"trials","version":151},{"n":1150,"text":"\t\tconst review = body.review === true;","atom":"a1450","author":"trials","version":151},{"n":1151,"text":"\t\tif (review && issue === null) throw new IssuesError(\"a review ask is on an issue\");","atom":"a1450","author":"trials","version":151},{"n":1152,"text":"\t\tlet due: string | null = null;","atom":"a1450","author":"trials","version":151},{"n":1153,"text":"\t\tif (body.due !== undefined && body.due !== null && body.due !== \"\") {","atom":"a1450","author":"trials","version":151},{"n":1154,"text":"\t\t\tif (typeof body.due !== \"string\" || !/^\\d{4}-\\d{2}-\\d{2}/.test(body.due) || Number.isNaN(Date.parse(body.due)))","atom":"a1450","author":"trials","version":151},{"n":1155,"text":"\t\t\t\tthrow new IssuesError(\"due is a date, like 2026-10-14\");","atom":"a1450","author":"trials","version":151},{"n":1156,"text":"\t\t\tdue = body.due;","atom":"a1450","author":"trials","version":151},{"n":1157,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":1158,"text":"\t\tlet recommendation: { text: string; by: string } | null = null;","atom":"a1450","author":"trials","version":151},{"n":1159,"text":"\t\tif (body.recommendation !== undefined && body.recommendation !== null) {","atom":"a1450","author":"trials","version":151},{"n":1160,"text":"\t\t\tconst r = body.recommendation as { text?: unknown; by?: unknown };","atom":"a1450","author":"trials","version":151},{"n":1161,"text":"\t\t\trecommendation = {","atom":"a1450","author":"trials","version":151},{"n":1162,"text":"\t\t\t\ttext: text(r.text, \"the recommendation\", LIMITS.text),","atom":"a1450","author":"trials","version":151},{"n":1163,"text":"\t\t\t\tby: r.by === undefined ? who(actor) : text(r.by, \"who recommends it\", 100, true, true),","atom":"a1450","author":"trials","version":151},{"n":1164,"text":"\t\t\t};","atom":"a1450","author":"trials","version":151},{"n":1165,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":1166,"text":"\t\tconst options = body.options === undefined || body.options === null ? [] : body.options;","atom":"a1450","author":"trials","version":151},{"n":1167,"text":"\t\tif (!Array.isArray(options) || options.length > LIMITS.options)","atom":"a1450","author":"trials","version":151},{"n":1168,"text":"\t\t\tthrow new IssuesError(`options are a list of up to ${LIMITS.options} choices`);","atom":"a1450","author":"trials","version":151},{"n":1169,"text":"\t\tconst choices = options.map((o, i) => text(o, `option ${i + 1}`, 500, true, true));","atom":"a1450","author":"trials","version":151},{"n":1170,"text":"\t\tconst blocks = body.blocks === undefined || body.blocks === null ? [] : body.blocks;","atom":"a1450","author":"trials","version":151},{"n":1171,"text":"\t\tif (!Array.isArray(blocks) || blocks.length > LIMITS.blocks)","atom":"a1450","author":"trials","version":151},{"n":1172,"text":"\t\t\tthrow new IssuesError(`blocks is a list of up to ${LIMITS.blocks} issue numbers`);","atom":"a1450","author":"trials","version":151},{"n":1173,"text":"\t\tconst held = [...new Set(blocks.map((b) => issueNumber(b, \"an issue it blocks\")))].filter((b) => b !== issue);","atom":"a1450","author":"trials","version":151},{"n":1174,"text":"\t\tfor (const b of held) this.exists(b);","atom":"a1450","author":"trials","version":151},{"n":1175,"text":"\t\tthis.noAttachments(body.attachments);","atom":"a1500","author":"trials","version":151},{"n":1176,"text":"\t\tconst a = this.rows(","atom":"a1500","author":"trials","version":151},{"n":1177,"text":"\t\t\t\"INSERT INTO asks (issue, by, of, kind, question, review, due, recommendation, options, blocks, at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id\",","atom":"a1500","author":"trials","version":151},{"n":1178,"text":"\t\t\tissue,","atom":"a1500","author":"trials","version":151},{"n":1179,"text":"\t\t\twho(actor),","atom":"a1500","author":"trials","version":151},{"n":1180,"text":"\t\t\tof,","atom":"a1500","author":"trials","version":151},{"n":1181,"text":"\t\t\tkind,","atom":"a1500","author":"trials","version":151},{"n":1182,"text":"\t\t\tquestion,","atom":"a1500","author":"trials","version":151},{"n":1183,"text":"\t\t\treview ? 1 : 0,","atom":"a1500","author":"trials","version":151},{"n":1184,"text":"\t\t\tdue,","atom":"a1500","author":"trials","version":151},{"n":1185,"text":"\t\t\trecommendation ? JSON.stringify(recommendation) : null,","atom":"a1500","author":"trials","version":151},{"n":1186,"text":"\t\t\tJSON.stringify(choices),","atom":"a1500","author":"trials","version":151},{"n":1187,"text":"\t\t\tJSON.stringify(held),","atom":"a1500","author":"trials","version":151},{"n":1188,"text":"\t\t\tthis.now(),","atom":"a1500","author":"trials","version":151},{"n":1189,"text":"\t\t)[0]!;","atom":"a1500","author":"trials","version":151},{"n":1190,"text":"\t\tif (review && issue !== null) this.watch(issue, of);","atom":"a1450","author":"trials","version":151},{"n":1191,"text":"\t\tthis.touch(issue, ...held);","atom":"a1450","author":"trials","version":151},{"n":1192,"text":"\t\tconst what = review ? \"for a review\" : kind === \"task\" ? \"to do something\" : \"a question\";","atom":"a1450","author":"trials","version":151},{"n":1193,"text":"\t\tconst where = issue === null ? \"\" : ` on issue ${issue} (${JSON.stringify(row!.title)})`;","atom":"a1450","author":"trials","version":151},{"n":1194,"text":"\t\treturn {","atom":"a1450","author":"trials","version":151},{"n":1195,"text":"\t\t\tid: a.id as number,","atom":"a1450","author":"trials","version":151},{"n":1196,"text":"\t\t\t// An agent asked hears how to answer.","atom":"a1500","author":"trials","version":151},{"n":1197,"text":"\t\t\tnews: {","atom":"a1450","author":"trials","version":151},{"n":1198,"text":"\t\t\t\tissue,","atom":"a1450","author":"trials","version":151},{"n":1199,"text":"\t\t\t\ttext: `${who(actor)} asks ${of} ${what}${where}: ${JSON.stringify(question)}${of.startsWith(\"person:\") ? \"\" : ` Answer: abso answer ${a.id} \"...\"`}`,","atom":"a1450","author":"trials","version":151},{"n":1200,"text":"\t\t\t\tto: [of],","atom":"a1450","author":"trials","version":151},{"n":1201,"text":"\t\t\t},","atom":"a1450","author":"trials","version":151},{"n":1202,"text":"\t\t};","atom":"a1450","author":"trials","version":151},{"n":1203,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1204,"text":"","atom":"a1450","author":"trials","version":151},{"n":1205,"text":"\t/** An answer, in the answerer's own words, kept on the ask. Only the one asked answers; a task is answered \"done\". */","atom":"a1450","author":"trials","version":151},{"n":1206,"text":"\tanswer(","atom":"a1450","author":"trials","version":151},{"n":1207,"text":"\t\tactor: Actor,","atom":"a1450","author":"trials","version":151},{"n":1208,"text":"\t\taskId: number,","atom":"a1450","author":"trials","version":151},{"n":1209,"text":"\t\tbody: {","atom":"a1450","author":"trials","version":151},{"n":1210,"text":"\t\t\twords?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1211,"text":"\t\t\toption?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1212,"text":"\t\t\texpect?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1213,"text":"\t\t\tnote?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1214,"text":"\t\t\tdone?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1215,"text":"\t\t\tattachments?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1216,"text":"\t\t},","atom":"a1450","author":"trials","version":151},{"n":1217,"text":"\t): { ask: Ask; news: News } {","atom":"a1500","author":"trials","version":151},{"n":1218,"text":"\t\tconst r = this.rows(\"SELECT * FROM asks WHERE id = ?\", issueNumber(askId, \"an ask number\"))[0];","atom":"a1450","author":"trials","version":151},{"n":1219,"text":"\t\tif (!r) throw new IssuesError(`there's no ask ${askId}`, 404);","atom":"a1450","author":"trials","version":151},{"n":1220,"text":"\t\tif (r.of !== who(actor)) throw new IssuesError(`ask ${askId} is for ${r.of as string}, not ${who(actor)}`, 403);","atom":"a1450","author":"trials","version":151},{"n":1221,"text":"\t\tif (r.answer_at !== null && r.answer_at !== undefined)","atom":"a1450","author":"trials","version":151},{"n":1222,"text":"\t\t\tthrow new IssuesError(","atom":"a1450","author":"trials","version":151},{"n":1223,"text":"\t\t\t\tr.outcome === \"withdrawn\"","atom":"a1450","author":"trials","version":151},{"n":1224,"text":"\t\t\t\t\t? `ask ${askId} was withdrawn by ${r.answer_by as string}, who asked it; it needs no answer`","atom":"a1450","author":"trials","version":151},{"n":1225,"text":"\t\t\t\t\t: `ask ${askId} is answered already`,","atom":"a1450","author":"trials","version":151},{"n":1226,"text":"\t\t\t\t409,","atom":"a1450","author":"trials","version":151},{"n":1227,"text":"\t\t\t);","atom":"a1450","author":"trials","version":151},{"n":1228,"text":"\t\tconst words = this.answerWordsFor(r, body);","atom":"a1450","author":"trials","version":151},{"n":1229,"text":"\t\tthis.noAttachments(body.attachments);","atom":"a1500","author":"trials","version":151},{"n":1230,"text":"\t\tconst at = this.now();","atom":"a1450","author":"trials","version":151},{"n":1231,"text":"\t\tthis.sql.exec(","atom":"a1500","author":"trials","version":151},{"n":1232,"text":"\t\t\t\"UPDATE asks SET answer_by = ?, answer_words = ?, answer_at = ? WHERE id = ?\",","atom":"a1500","author":"trials","version":151},{"n":1233,"text":"\t\t\twho(actor),","atom":"a1500","author":"trials","version":151},{"n":1234,"text":"\t\t\twords,","atom":"a1500","author":"trials","version":151},{"n":1235,"text":"\t\t\tat,","atom":"a1500","author":"trials","version":151},{"n":1236,"text":"\t\t\tr.id,","atom":"a1500","author":"trials","version":151},{"n":1237,"text":"\t\t);","atom":"a1500","author":"trials","version":151},{"n":1238,"text":"\t\tconst ask = this.authoredAsk(","atom":"a1450","author":"trials","version":151},{"n":1239,"text":"\t\t\tthis.withFiles(askOf({ ...r, answer_by: who(actor), answer_words: words, answer_at: at })),","atom":"a1450","author":"trials","version":151},{"n":1240,"text":"\t\t);","atom":"a1450","author":"trials","version":151},{"n":1241,"text":"\t\tthis.touch(ask.issue, ...ask.blocks);","atom":"a1450","author":"trials","version":151},{"n":1242,"text":"\t\treturn {","atom":"a1450","author":"trials","version":151},{"n":1243,"text":"\t\t\task,","atom":"a1450","author":"trials","version":151},{"n":1244,"text":"\t\t\tnews: {","atom":"a1450","author":"trials","version":151},{"n":1245,"text":"\t\t\t\tissue: ask.issue,","atom":"a1450","author":"trials","version":151},{"n":1246,"text":"\t\t\t\ttext: `${who(actor)} answered ${ask.by}'s ${ask.review ? \"review ask\" : ask.kind}${ask.issue !== null ? ` on issue ${ask.issue}` : \"\"}: ${JSON.stringify(words)}`,","atom":"a1450","author":"trials","version":151},{"n":1247,"text":"\t\t\t\tto: [ask.by],","atom":"a1450","author":"trials","version":151},{"n":1248,"text":"\t\t\t},","atom":"a1450","author":"trials","version":151},{"n":1249,"text":"\t\t};","atom":"a1450","author":"trials","version":151},{"n":1250,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1251,"text":"","atom":"a1450","author":"trials","version":151},{"n":1252,"text":"\tprivate answerWordsFor(","atom":"a1450","author":"trials","version":151},{"n":1253,"text":"\t\tr: Row,","atom":"a1450","author":"trials","version":151},{"n":1254,"text":"\t\tbody: { words?: unknown; option?: unknown; expect?: unknown; note?: unknown; done?: unknown },","atom":"a1450","author":"trials","version":151},{"n":1255,"text":"\t): string {","atom":"a1450","author":"trials","version":151},{"n":1256,"text":"\t\tconst askId = r.id as number;","atom":"a1450","author":"trials","version":151},{"n":1257,"text":"\t\tconst options = JSON.parse(r.options as string) as string[];","atom":"a1450","author":"trials","version":151},{"n":1258,"text":"\t\tif (body.option !== undefined && body.option !== null) {","atom":"a1450","author":"trials","version":151},{"n":1259,"text":"\t\t\tconst pick = typeof body.option === \"number\" ? options[body.option] : options.find((o) => o === body.option);","atom":"a1450","author":"trials","version":151},{"n":1260,"text":"\t\t\tif (pick === undefined)","atom":"a1450","author":"trials","version":151},{"n":1261,"text":"\t\t\t\tthrow new IssuesError(`ask ${askId}'s options are: ${options.map((o) => JSON.stringify(o)).join(\", \")}`);","atom":"a1450","author":"trials","version":151},{"n":1262,"text":"\t\t\tif (body.expect !== undefined && body.expect !== pick)","atom":"a1450","author":"trials","version":151},{"n":1263,"text":"\t\t\t\tthrow new IssuesError(","atom":"a1450","author":"trials","version":151},{"n":1264,"text":"\t\t\t\t\t`ask ${askId}'s option ${Number(body.option) + 1} is ${JSON.stringify(pick)}, not the one you picked; look again`,","atom":"a1450","author":"trials","version":151},{"n":1265,"text":"\t\t\t\t\t409,","atom":"a1450","author":"trials","version":151},{"n":1266,"text":"\t\t\t\t);","atom":"a1450","author":"trials","version":151},{"n":1267,"text":"\t\t\tconst note = text(body.note, \"the note\", LIMITS.text, false);","atom":"a1450","author":"trials","version":151},{"n":1268,"text":"\t\t\treturn note === \"\" ? pick : `${pick}\\n\\n${note}`;","atom":"a1450","author":"trials","version":151},{"n":1269,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":1270,"text":"\t\tif (body.done === true && r.kind === \"task\") {","atom":"a1450","author":"trials","version":151},{"n":1271,"text":"\t\t\tconst note = text(body.note, \"the note\", LIMITS.text, false);","atom":"a1450","author":"trials","version":151},{"n":1272,"text":"\t\t\treturn note === \"\" ? \"done\" : `done\\n\\n${note}`;","atom":"a1450","author":"trials","version":151},{"n":1273,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":1274,"text":"\t\treturn text(body.words, \"the answer\", LIMITS.text);","atom":"a1450","author":"trials","version":151},{"n":1275,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1276,"text":"","atom":"a1450","author":"trials","version":151},{"n":1277,"text":"\t/**","atom":"a1450","author":"trials","version":151},{"n":1278,"text":"\t * The one asked says they have it and will answer later. The ask stays open and keeps","atom":"a1500","author":"trials","version":151},{"n":1279,"text":"\t * blocking; it shows as acknowledged, and the asker hears it with the note. Acknowledging again updates","atom":"a1450","author":"trials","version":151},{"n":1280,"text":"\t * the note and the time. abso answer closes it as before.","atom":"a1450","author":"trials","version":151},{"n":1281,"text":"\t */","atom":"a1450","author":"trials","version":151},{"n":1282,"text":"\tack(actor: Actor, askId: number, body: { note?: unknown }): { ask: Ask; news: News } {","atom":"a1500","author":"trials","version":151},{"n":1283,"text":"\t\tconst r = this.rows(\"SELECT * FROM asks WHERE id = ?\", issueNumber(askId, \"an ask number\"))[0];","atom":"a1450","author":"trials","version":151},{"n":1284,"text":"\t\tif (!r) throw new IssuesError(`there's no ask ${askId}`, 404);","atom":"a1450","author":"trials","version":151},{"n":1285,"text":"\t\tif (r.of !== who(actor)) throw new IssuesError(`ask ${askId} is for ${r.of as string}, not ${who(actor)}`, 403);","atom":"a1450","author":"trials","version":151},{"n":1286,"text":"\t\tif (r.answer_at !== null && r.answer_at !== undefined)","atom":"a1450","author":"trials","version":151},{"n":1287,"text":"\t\t\tthrow new IssuesError(","atom":"a1450","author":"trials","version":151},{"n":1288,"text":"\t\t\t\tr.outcome === \"withdrawn\"","atom":"a1450","author":"trials","version":151},{"n":1289,"text":"\t\t\t\t\t? `ask ${askId} was withdrawn by ${r.answer_by as string}, who asked it; it needs no answer`","atom":"a1450","author":"trials","version":151},{"n":1290,"text":"\t\t\t\t\t: `ask ${askId} is answered already`,","atom":"a1450","author":"trials","version":151},{"n":1291,"text":"\t\t\t\t409,","atom":"a1450","author":"trials","version":151},{"n":1292,"text":"\t\t\t);","atom":"a1450","author":"trials","version":151},{"n":1293,"text":"\t\tconst note = text(body.note, \"the note\", LIMITS.text, false);","atom":"a1450","author":"trials","version":151},{"n":1294,"text":"\t\tconst at = this.now();","atom":"a1450","author":"trials","version":151},{"n":1295,"text":"\t\tthis.transaction(() => {","atom":"a1450","author":"trials","version":151},{"n":1296,"text":"\t\t\tthis.sql.exec(\"UPDATE asks SET ack_at = ?, ack_note = ? WHERE id = ?\", at, note, r.id);","atom":"a1450","author":"trials","version":151},{"n":1297,"text":"\t\t\t// The note is a reply in the thread.","atom":"a1500","author":"trials","version":151},{"n":1298,"text":"\t\t\tif (note !== \"\")","atom":"a1500","author":"trials","version":151},{"n":1299,"text":"\t\t\t\tthis.sql.exec(","atom":"a1500","author":"trials","version":151},{"n":1300,"text":"\t\t\t\t\t\"INSERT INTO ask_replies (ask, by, text, question, at) VALUES (?, ?, ?, 0, ?)\",","atom":"a1500","author":"trials","version":151},{"n":1301,"text":"\t\t\t\t\tr.id,","atom":"a1450","author":"trials","version":151},{"n":1302,"text":"\t\t\t\t\twho(actor),","atom":"a1450","author":"trials","version":151},{"n":1303,"text":"\t\t\t\t\tnote,","atom":"a1450","author":"trials","version":151},{"n":1304,"text":"\t\t\t\t\tat,","atom":"a1450","author":"trials","version":151},{"n":1305,"text":"\t\t\t\t);","atom":"a1450","author":"trials","version":151},{"n":1306,"text":"\t\t});","atom":"a1450","author":"trials","version":151},{"n":1307,"text":"\t\tconst ask = this.authoredAsk(this.withFiles(askOf({ ...r, ack_at: at, ack_note: note })));","atom":"a1450","author":"trials","version":151},{"n":1308,"text":"\t\tthis.touch(ask.issue);","atom":"a1450","author":"trials","version":151},{"n":1309,"text":"\t\treturn {","atom":"a1450","author":"trials","version":151},{"n":1310,"text":"\t\t\task,","atom":"a1450","author":"trials","version":151},{"n":1311,"text":"\t\t\tnews: {","atom":"a1450","author":"trials","version":151},{"n":1312,"text":"\t\t\t\tissue: ask.issue,","atom":"a1450","author":"trials","version":151},{"n":1313,"text":"\t\t\t\ttext: `${who(actor)} has your ${ask.review ? \"review ask\" : ask.kind} (ask ${ask.id})${ask.issue !== null ? ` on issue ${ask.issue}` : \"\"} and will answer${note !== \"\" ? `: ${JSON.stringify(note)}` : \".\"}`,","atom":"a1450","author":"trials","version":151},{"n":1314,"text":"\t\t\t\tto: [ask.by],","atom":"a1450","author":"trials","version":151},{"n":1315,"text":"\t\t\t},","atom":"a1450","author":"trials","version":151},{"n":1316,"text":"\t\t};","atom":"a1450","author":"trials","version":151},{"n":1317,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1318,"text":"","atom":"a1450","author":"trials","version":151},{"n":1319,"text":"\t/**","atom":"a1450","author":"trials","version":151},{"n":1320,"text":"\t * A reply on an ask's thread, from the one asked or the asker, open or closed. The other side","atom":"a1500","author":"trials","version":151},{"n":1321,"text":"\t * hears it. Whose turn it is: a reply from the one asked marked as a question waits on the asker; the asker's","atom":"a1450","author":"trials","version":151},{"n":1322,"text":"\t * next reply hands it back. Answering still closes it.","atom":"a1450","author":"trials","version":151},{"n":1323,"text":"\t */","atom":"a1450","author":"trials","version":151},{"n":1324,"text":"\treply(actor: Actor, askId: number, body: { text?: unknown; question?: unknown }): { ask: Ask; news: News } {","atom":"a1500","author":"trials","version":151},{"n":1325,"text":"\t\tconst r = this.rows(\"SELECT * FROM asks WHERE id = ?\", issueNumber(askId, \"an ask number\"))[0];","atom":"a1450","author":"trials","version":151},{"n":1326,"text":"\t\tif (!r) throw new IssuesError(`there's no ask ${askId}`, 404);","atom":"a1450","author":"trials","version":151},{"n":1327,"text":"\t\tconst me = who(actor);","atom":"a1450","author":"trials","version":151},{"n":1328,"text":"\t\tif (me !== r.of && me !== r.by)","atom":"a1450","author":"trials","version":151},{"n":1329,"text":"\t\t\tthrow new IssuesError(","atom":"a1450","author":"trials","version":151},{"n":1330,"text":"\t\t\t\t`ask ${askId} is between ${r.by as string} and ${r.of as string}; only they reply on it. abso comment says something on its issue`,","atom":"a1450","author":"trials","version":151},{"n":1331,"text":"\t\t\t\t403,","atom":"a1450","author":"trials","version":151},{"n":1332,"text":"\t\t\t);","atom":"a1450","author":"trials","version":151},{"n":1333,"text":"\t\tconst words = text(body.text, \"the reply\", LIMITS.text);","atom":"a1450","author":"trials","version":151},{"n":1334,"text":"\t\tconst question = body.question === true;","atom":"a1450","author":"trials","version":151},{"n":1335,"text":"\t\tconst open = r.answer_at === null || r.answer_at === undefined;","atom":"a1450","author":"trials","version":151},{"n":1336,"text":"\t\tlet waitingOn = (r.waiting_on as string | null) ?? null;","atom":"a1450","author":"trials","version":151},{"n":1337,"text":"\t\tif (open) {","atom":"a1450","author":"trials","version":151},{"n":1338,"text":"\t\t\tif (me === r.of && me !== r.by) {","atom":"a1450","author":"trials","version":151},{"n":1339,"text":"\t\t\t\tif (question) waitingOn = r.by as string;","atom":"a1450","author":"trials","version":151},{"n":1340,"text":"\t\t\t} else waitingOn = null;","atom":"a1450","author":"trials","version":151},{"n":1341,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":1342,"text":"\t\tconst at = this.now();","atom":"a1450","author":"trials","version":151},{"n":1343,"text":"\t\tthis.transaction(() => {","atom":"a1450","author":"trials","version":151},{"n":1344,"text":"\t\t\tthis.sql.exec(","atom":"a1500","author":"trials","version":151},{"n":1345,"text":"\t\t\t\t\"INSERT INTO ask_replies (ask, by, text, question, at) VALUES (?, ?, ?, ?, ?)\",","atom":"a1500","author":"trials","version":151},{"n":1346,"text":"\t\t\t\tr.id,","atom":"a1450","author":"trials","version":151},{"n":1347,"text":"\t\t\t\tme,","atom":"a1450","author":"trials","version":151},{"n":1348,"text":"\t\t\t\twords,","atom":"a1450","author":"trials","version":151},{"n":1349,"text":"\t\t\t\tquestion ? 1 : 0,","atom":"a1450","author":"trials","version":151},{"n":1350,"text":"\t\t\t\tat,","atom":"a1450","author":"trials","version":151},{"n":1351,"text":"\t\t\t);","atom":"a1450","author":"trials","version":151},{"n":1352,"text":"\t\t\tif (open) this.sql.exec(\"UPDATE asks SET waiting_on = ? WHERE id = ?\", waitingOn, r.id);","atom":"a1450","author":"trials","version":151},{"n":1353,"text":"\t\t});","atom":"a1450","author":"trials","version":151},{"n":1354,"text":"\t\tconst ask = this.authoredAsk(this.withFiles(askOf({ ...r, waiting_on: waitingOn })));","atom":"a1450","author":"trials","version":151},{"n":1355,"text":"\t\tthis.touch(ask.issue);","atom":"a1450","author":"trials","version":151},{"n":1356,"text":"\t\tconst other = me === ask.of ? ask.by : ask.of;","atom":"a1450","author":"trials","version":151},{"n":1357,"text":"\t\tconst turn = open && (waitingOn ?? ask.of) === other ? \" It waits on you.\" : \"\";","atom":"a1450","author":"trials","version":151},{"n":1358,"text":"\t\treturn {","atom":"a1450","author":"trials","version":151},{"n":1359,"text":"\t\t\task,","atom":"a1450","author":"trials","version":151},{"n":1360,"text":"\t\t\tnews: {","atom":"a1450","author":"trials","version":151},{"n":1361,"text":"\t\t\t\tissue: ask.issue,","atom":"a1450","author":"trials","version":151},{"n":1362,"text":"\t\t\t\ttext: `${me} replied on ask ${ask.id}${ask.issue !== null ? ` (issue ${ask.issue})` : \"\"}${question ? \" with a question\" : \"\"}: ${JSON.stringify(words)}${turn}${other.startsWith(\"person:\") ? \"\" : ` abso ask ${ask.id} shows the thread.`}`,","atom":"a1450","author":"trials","version":151},{"n":1363,"text":"\t\t\t\tto: [other],","atom":"a1450","author":"trials","version":151},{"n":1364,"text":"\t\t\t},","atom":"a1450","author":"trials","version":151},{"n":1365,"text":"\t\t};","atom":"a1450","author":"trials","version":151},{"n":1366,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1367,"text":"","atom":"a1450","author":"trials","version":151},{"n":1368,"text":"\t/**","atom":"a1450","author":"trials","version":151},{"n":1369,"text":"\t * The asker takes back an ask nobody needs answered any more. It closes as an answer does","atom":"a1500","author":"trials","version":151},{"n":1370,"text":"\t * (answer_at set, so it leaves waiting and stops blocking), with the asker as answer_by, their note as","atom":"a1450","author":"trials","version":151},{"n":1371,"text":"\t * answer_words, and outcome 'withdrawn'. The one asked hears it.","atom":"a1450","author":"trials","version":151},{"n":1372,"text":"\t */","atom":"a1450","author":"trials","version":151},{"n":1373,"text":"\twithdraw(actor: Actor, askId: number, body: { note?: unknown }): { ask: Ask; news: News } {","atom":"a1500","author":"trials","version":151},{"n":1374,"text":"\t\tconst r = this.rows(\"SELECT * FROM asks WHERE id = ?\", issueNumber(askId, \"an ask number\"))[0];","atom":"a1450","author":"trials","version":151},{"n":1375,"text":"\t\tif (!r) throw new IssuesError(`there's no ask ${askId}`, 404);","atom":"a1450","author":"trials","version":151},{"n":1376,"text":"\t\tif (r.by !== who(actor))","atom":"a1450","author":"trials","version":151},{"n":1377,"text":"\t\t\tthrow new IssuesError(`ask ${askId} was asked by ${r.by as string}; only they can withdraw it`, 403);","atom":"a1450","author":"trials","version":151},{"n":1378,"text":"\t\tif (r.answer_at !== null && r.answer_at !== undefined)","atom":"a1450","author":"trials","version":151},{"n":1379,"text":"\t\t\tthrow new IssuesError(","atom":"a1450","author":"trials","version":151},{"n":1380,"text":"\t\t\t\tr.outcome === \"withdrawn\"","atom":"a1450","author":"trials","version":151},{"n":1381,"text":"\t\t\t\t\t? `ask ${askId} is withdrawn already`","atom":"a1450","author":"trials","version":151},{"n":1382,"text":"\t\t\t\t\t: `ask ${askId} is answered already, so there's nothing to withdraw`,","atom":"a1450","author":"trials","version":151},{"n":1383,"text":"\t\t\t\t409,","atom":"a1450","author":"trials","version":151},{"n":1384,"text":"\t\t\t);","atom":"a1450","author":"trials","version":151},{"n":1385,"text":"\t\tconst note = text(body.note, \"the note\", LIMITS.text, false);","atom":"a1450","author":"trials","version":151},{"n":1386,"text":"\t\tconst at = this.now();","atom":"a1450","author":"trials","version":151},{"n":1387,"text":"\t\tthis.sql.exec(","atom":"a1500","author":"trials","version":151},{"n":1388,"text":"\t\t\t\"UPDATE asks SET answer_by = ?, answer_words = ?, answer_at = ?, outcome = 'withdrawn' WHERE id = ?\",","atom":"a1500","author":"trials","version":151},{"n":1389,"text":"\t\t\twho(actor),","atom":"a1500","author":"trials","version":151},{"n":1390,"text":"\t\t\tnote,","atom":"a1500","author":"trials","version":151},{"n":1391,"text":"\t\t\tat,","atom":"a1500","author":"trials","version":151},{"n":1392,"text":"\t\t\tr.id,","atom":"a1500","author":"trials","version":151},{"n":1393,"text":"\t\t);","atom":"a1500","author":"trials","version":151},{"n":1394,"text":"\t\tconst ask = this.authoredAsk(","atom":"a1450","author":"trials","version":151},{"n":1395,"text":"\t\t\tthis.withFiles(askOf({ ...r, answer_by: who(actor), answer_words: note, answer_at: at, outcome: \"withdrawn\" })),","atom":"a1450","author":"trials","version":151},{"n":1396,"text":"\t\t);","atom":"a1450","author":"trials","version":151},{"n":1397,"text":"\t\tthis.touch(ask.issue, ...ask.blocks);","atom":"a1450","author":"trials","version":151},{"n":1398,"text":"\t\treturn {","atom":"a1450","author":"trials","version":151},{"n":1399,"text":"\t\t\task,","atom":"a1450","author":"trials","version":151},{"n":1400,"text":"\t\t\tnews: {","atom":"a1450","author":"trials","version":151},{"n":1401,"text":"\t\t\t\tissue: ask.issue,","atom":"a1450","author":"trials","version":151},{"n":1402,"text":"\t\t\t\ttext: `${who(actor)} withdrew their ${ask.review ? \"review ask\" : ask.kind} of ${ask.of}${ask.issue !== null ? ` on issue ${ask.issue}` : \"\"}${note !== \"\" ? `: ${JSON.stringify(note)}` : \"\"}`,","atom":"a1450","author":"trials","version":151},{"n":1403,"text":"\t\t\t\tto: [ask.of],","atom":"a1450","author":"trials","version":151},{"n":1404,"text":"\t\t\t},","atom":"a1450","author":"trials","version":151},{"n":1405,"text":"\t\t};","atom":"a1450","author":"trials","version":151},{"n":1406,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1407,"text":"","atom":"a1450","author":"trials","version":151},{"n":1408,"text":"\t/** Dropped by hand; done comes only from a publish (the done hook). */","atom":"a1450","author":"trials","version":151},{"n":1409,"text":"\tdrop(actor: Actor, id: number): { news: News } {","atom":"a1450","author":"trials","version":151},{"n":1410,"text":"\t\tconst row = this.exists(id);","atom":"a1450","author":"trials","version":151},{"n":1411,"text":"\t\tif (row.closed_how)","atom":"a1450","author":"trials","version":151},{"n":1412,"text":"\t\t\tthrow new IssuesError(`issue ${id} is ${row.closed_how === \"done\" ? \"done\" : \"dropped\"} already`, 409);","atom":"a1450","author":"trials","version":151},{"n":1413,"text":"\t\tthis.sql.exec(","atom":"a1450","author":"trials","version":151},{"n":1414,"text":"\t\t\t\"UPDATE issues SET closed_how = 'dropped', closed_by = ?, closed_at = ?, updated_at = ? WHERE id = ?\",","atom":"a1450","author":"trials","version":151},{"n":1415,"text":"\t\t\twho(actor),","atom":"a1450","author":"trials","version":151},{"n":1416,"text":"\t\t\tthis.now(),","atom":"a1450","author":"trials","version":151},{"n":1417,"text":"\t\t\tthis.now(),","atom":"a1450","author":"trials","version":151},{"n":1418,"text":"\t\t\tid,","atom":"a1450","author":"trials","version":151},{"n":1419,"text":"\t\t);","atom":"a1450","author":"trials","version":151},{"n":1420,"text":"\t\tthis.sql.exec(\"DELETE FROM claims WHERE issue = ?\", id);","atom":"a1450","author":"trials","version":151},{"n":1421,"text":"\t\treturn { news: { issue: id, text: `${who(actor)} dropped issue ${id} (${JSON.stringify(row.title)})` } };","atom":"a1450","author":"trials","version":151},{"n":1422,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1423,"text":"","atom":"a1450","author":"trials","version":151},{"n":1424,"text":"\t/** A dropped issue opens again. A done issue stays done. */","atom":"a1500","author":"trials","version":151},{"n":1425,"text":"\treopen(actor: Actor, id: number): { news: News } {","atom":"a1450","author":"trials","version":151},{"n":1426,"text":"\t\tconst row = this.exists(id);","atom":"a1450","author":"trials","version":151},{"n":1427,"text":"\t\tif (row.closed_how !== \"dropped\")","atom":"a1450","author":"trials","version":151},{"n":1428,"text":"\t\t\tthrow new IssuesError(","atom":"a1450","author":"trials","version":151},{"n":1429,"text":"\t\t\t\trow.closed_how === \"done\" ? `issue ${id} is done; it stays done` : `issue ${id} is open`,","atom":"a1450","author":"trials","version":151},{"n":1430,"text":"\t\t\t\t409,","atom":"a1450","author":"trials","version":151},{"n":1431,"text":"\t\t\t);","atom":"a1450","author":"trials","version":151},{"n":1432,"text":"\t\tthis.sql.exec(","atom":"a1450","author":"trials","version":151},{"n":1433,"text":"\t\t\t\"UPDATE issues SET closed_how = NULL, closed_by = NULL, closed_version = NULL, closed_at = NULL, updated_at = ? WHERE id = ?\",","atom":"a1450","author":"trials","version":151},{"n":1434,"text":"\t\t\tthis.now(),","atom":"a1450","author":"trials","version":151},{"n":1435,"text":"\t\t\tid,","atom":"a1450","author":"trials","version":151},{"n":1436,"text":"\t\t);","atom":"a1450","author":"trials","version":151},{"n":1437,"text":"\t\tthis.touch(id);","atom":"a1450","author":"trials","version":151},{"n":1438,"text":"\t\treturn { news: { issue: id, text: `${who(actor)} reopened issue ${id} (${JSON.stringify(row.title)})` } };","atom":"a1450","author":"trials","version":151},{"n":1439,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1440,"text":"","atom":"a1450","author":"trials","version":151},{"n":1441,"text":"\t/**","atom":"a1450","author":"trials","version":151},{"n":1442,"text":"\t * Marked done by hand, by a person or an agent. `version`, when","atom":"a1500","author":"trials","version":151},{"n":1443,"text":"\t * given, is the published version that did the work. A dropped issue is reopened first.","atom":"a1450","author":"trials","version":151},{"n":1444,"text":"\t */","atom":"a1450","author":"trials","version":151},{"n":1445,"text":"\tmarkDone(actor: Actor, id: number, version: number | null): { news: News } {","atom":"a1450","author":"trials","version":151},{"n":1446,"text":"\t\tconst row = this.exists(id);","atom":"a1450","author":"trials","version":151},{"n":1447,"text":"\t\tif (row.closed_how === \"dropped\")","atom":"a1450","author":"trials","version":151},{"n":1448,"text":"\t\t\tthrow new IssuesError(`issue ${id} is dropped; reopen it before marking it done`, 409);","atom":"a1450","author":"trials","version":151},{"n":1449,"text":"\t\tif (row.closed_how === \"done\") throw new IssuesError(`issue ${id} is done already`, 409);","atom":"a1450","author":"trials","version":151},{"n":1450,"text":"\t\tif (version !== null && (!Number.isInteger(version) || version < 1))","atom":"a1450","author":"trials","version":151},{"n":1451,"text":"\t\t\tthrow new IssuesError(\"version is a published version, 1 or more\");","atom":"a1450","author":"trials","version":151},{"n":1452,"text":"\t\tconst at = this.now();","atom":"a1450","author":"trials","version":151},{"n":1453,"text":"\t\tconst by = who(actor);","atom":"a1450","author":"trials","version":151},{"n":1454,"text":"\t\tif (version !== null)","atom":"a1450","author":"trials","version":151},{"n":1455,"text":"\t\t\tthis.sql.exec(","atom":"a1450","author":"trials","version":151},{"n":1456,"text":"\t\t\t\t\"INSERT INTO versions (issue, version, agent, at) VALUES (?, ?, ?, ?) ON CONFLICT (issue, version) DO NOTHING\",","atom":"a1450","author":"trials","version":151},{"n":1457,"text":"\t\t\t\tid,","atom":"a1450","author":"trials","version":151},{"n":1458,"text":"\t\t\t\tversion,","atom":"a1450","author":"trials","version":151},{"n":1459,"text":"\t\t\t\tby,","atom":"a1450","author":"trials","version":151},{"n":1460,"text":"\t\t\t\tat,","atom":"a1450","author":"trials","version":151},{"n":1461,"text":"\t\t\t);","atom":"a1450","author":"trials","version":151},{"n":1462,"text":"\t\tthis.sql.exec(","atom":"a1450","author":"trials","version":151},{"n":1463,"text":"\t\t\t\"UPDATE issues SET closed_how = 'done', closed_by = ?, closed_version = ?, closed_at = ?, updated_at = ? WHERE id = ?\",","atom":"a1450","author":"trials","version":151},{"n":1464,"text":"\t\t\tby,","atom":"a1450","author":"trials","version":151},{"n":1465,"text":"\t\t\tversion,","atom":"a1450","author":"trials","version":151},{"n":1466,"text":"\t\t\tat,","atom":"a1450","author":"trials","version":151},{"n":1467,"text":"\t\t\tat,","atom":"a1450","author":"trials","version":151},{"n":1468,"text":"\t\t\tid,","atom":"a1450","author":"trials","version":151},{"n":1469,"text":"\t\t);","atom":"a1450","author":"trials","version":151},{"n":1470,"text":"\t\tthis.sql.exec(\"DELETE FROM claims WHERE issue = ?\", id);","atom":"a1450","author":"trials","version":151},{"n":1471,"text":"\t\tconst where = version === null ? \"by hand\" : `in version ${version}`;","atom":"a1450","author":"trials","version":151},{"n":1472,"text":"\t\treturn {","atom":"a1450","author":"trials","version":151},{"n":1473,"text":"\t\t\tnews: { issue: id, text: `${by} marked issue ${id} (${JSON.stringify(row.title)}) done ${where}` },","atom":"a1450","author":"trials","version":151},{"n":1474,"text":"\t\t};","atom":"a1450","author":"trials","version":151},{"n":1475,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1476,"text":"","atom":"a1450","author":"trials","version":151},{"n":1477,"text":"\t/**","atom":"a1450","author":"trials","version":151},{"n":1478,"text":"\t * Refuses a write past the writer's limit: WRITES_PER_MINUTE a","atom":"a1500","author":"trials","version":151},{"n":1479,"text":"\t * minute and WRITES_PER_DAY a day, opens, edits, comments, asks, answers and","atom":"a1450","author":"trials","version":151},{"n":1480,"text":"\t * drops together. A 429 says when to try again.","atom":"a1450","author":"trials","version":151},{"n":1481,"text":"\t */","atom":"a1450","author":"trials","version":151},{"n":1482,"text":"\tcheckLimit(actor: Actor): void {","atom":"a1450","author":"trials","version":151},{"n":1483,"text":"\t\tconst now = this.now();","atom":"a1450","author":"trials","version":151},{"n":1484,"text":"\t\tthis.sql.exec(\"DELETE FROM writes WHERE who = ? AND at <= ?\", who(actor), now - 86_400_000);","atom":"a1450","author":"trials","version":151},{"n":1485,"text":"\t\tconst times = this.rows(\"SELECT at FROM writes WHERE who = ? ORDER BY at\", who(actor)).map((w) => w.at as number);","atom":"a1450","author":"trials","version":151},{"n":1486,"text":"\t\tconst minute = times.filter((t) => now - t < 60_000);","atom":"a1450","author":"trials","version":151},{"n":1487,"text":"\t\tif (minute.length >= WRITES_PER_MINUTE)","atom":"a1450","author":"trials","version":151},{"n":1488,"text":"\t\t\tthrow new IssuesError(","atom":"a1450","author":"trials","version":151},{"n":1489,"text":"\t\t\t\t`at most ${WRITES_PER_MINUTE} issue writes a minute; try again shortly`,","atom":"a1450","author":"trials","version":151},{"n":1490,"text":"\t\t\t\t429,","atom":"a1450","author":"trials","version":151},{"n":1491,"text":"\t\t\t\tMath.max(1, Math.ceil((minute[0]! + 60_000 - now) / 1000)),","atom":"a1450","author":"trials","version":151},{"n":1492,"text":"\t\t\t);","atom":"a1450","author":"trials","version":151},{"n":1493,"text":"\t\tif (times.length >= WRITES_PER_DAY)","atom":"a1450","author":"trials","version":151},{"n":1494,"text":"\t\t\tthrow new IssuesError(","atom":"a1450","author":"trials","version":151},{"n":1495,"text":"\t\t\t\t`at most ${WRITES_PER_DAY} issue writes a day in a project`,","atom":"a1450","author":"trials","version":151},{"n":1496,"text":"\t\t\t\t429,","atom":"a1450","author":"trials","version":151},{"n":1497,"text":"\t\t\t\tMath.max(1, Math.ceil((times[0]! + 86_400_000 - now) / 1000)),","atom":"a1450","author":"trials","version":151},{"n":1498,"text":"\t\t\t);","atom":"a1450","author":"trials","version":151},{"n":1499,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1500,"text":"","atom":"a1450","author":"trials","version":151},{"n":1501,"text":"\t/** Counts a write that went through. */","atom":"a1450","author":"trials","version":151},{"n":1502,"text":"\twrote(actor: Actor): void {","atom":"a1450","author":"trials","version":151},{"n":1503,"text":"\t\tthis.sql.exec(\"INSERT INTO writes (who, at) VALUES (?, ?)\", who(actor), this.now());","atom":"a1450","author":"trials","version":151},{"n":1504,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1505,"text":"","atom":"a1450","author":"trials","version":151},{"n":1506,"text":"\t// Drafts: a writer's own, with no number and no news ------------------------","atom":"a1500","author":"trials","version":151},{"n":1507,"text":"","atom":"a1450","author":"trials","version":151},{"n":1508,"text":"\t/** The actor's drafts, newest first. Nobody else's. */","atom":"a1450","author":"trials","version":151},{"n":1509,"text":"\tdrafts(actor: Actor): Draft[] {","atom":"a1450","author":"trials","version":151},{"n":1510,"text":"\t\treturn this.rows(\"SELECT * FROM drafts WHERE owner = ? ORDER BY saved_at DESC, id DESC\", who(actor)).map(asDraft);","atom":"a1450","author":"trials","version":151},{"n":1511,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1512,"text":"","atom":"a1450","author":"trials","version":151},{"n":1513,"text":"\t/**","atom":"a1450","author":"trials","version":151},{"n":1514,"text":"\t * Saves a draft, new or (with `id`) over one of the actor's. Any field may","atom":"a1450","author":"trials","version":151},{"n":1515,"text":"\t * be empty; what's there is checked as open would check it, but not whether","atom":"a1450","author":"trials","version":151},{"n":1516,"text":"\t * the issues it names still exist (that waits for opening).","atom":"a1450","author":"trials","version":151},{"n":1517,"text":"\t */","atom":"a1450","author":"trials","version":151},{"n":1518,"text":"\tsaveDraft(","atom":"a1450","author":"trials","version":151},{"n":1519,"text":"\t\tactor: Actor,","atom":"a1450","author":"trials","version":151},{"n":1520,"text":"\t\tbody: {","atom":"a1450","author":"trials","version":151},{"n":1521,"text":"\t\t\tid?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1522,"text":"\t\t\ttitle?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1523,"text":"\t\t\tdescription?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1524,"text":"\t\t\tparent?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1525,"text":"\t\t\tlines?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1526,"text":"\t\t\tagent?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1527,"text":"\t\t\tblockedBy?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1528,"text":"\t\t\tnotify?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1529,"text":"\t\t\tlabels?: unknown;","atom":"a1450","author":"trials","version":151},{"n":1530,"text":"\t\t},","atom":"a1450","author":"trials","version":151},{"n":1531,"text":"\t): { id: number } {","atom":"a1450","author":"trials","version":151},{"n":1532,"text":"\t\tconst draft: DraftBody = {};","atom":"a1450","author":"trials","version":151},{"n":1533,"text":"\t\tconst title = text(body.title, \"the title\", LIMITS.title, false, true);","atom":"a1450","author":"trials","version":151},{"n":1534,"text":"\t\tif (title !== \"\") draft.title = title;","atom":"a1450","author":"trials","version":151},{"n":1535,"text":"\t\tconst description = text(body.description, \"the description\", LIMITS.description, false);","atom":"a1450","author":"trials","version":151},{"n":1536,"text":"\t\tif (description !== \"\") draft.description = description;","atom":"a1450","author":"trials","version":151},{"n":1537,"text":"\t\tif (body.parent !== undefined && body.parent !== null) draft.parent = issueNumber(body.parent, \"the parent\");","atom":"a1450","author":"trials","version":151},{"n":1538,"text":"\t\tconst l = lines(body.lines);","atom":"a1450","author":"trials","version":151},{"n":1539,"text":"\t\tif (l !== null) draft.lines = l;","atom":"a1450","author":"trials","version":151},{"n":1540,"text":"\t\tif (body.agent !== undefined && body.agent !== null && body.agent !== \"\")","atom":"a1450","author":"trials","version":151},{"n":1541,"text":"\t\t\tdraft.agent = nameOrNone(body.agent, \"the Agent\");","atom":"a1450","author":"trials","version":151},{"n":1542,"text":"\t\tif (body.blockedBy !== undefined && body.blockedBy !== null) {","atom":"a1450","author":"trials","version":151},{"n":1543,"text":"\t\t\tif (!Array.isArray(body.blockedBy) || body.blockedBy.length > LIMITS.blocks)","atom":"a1450","author":"trials","version":151},{"n":1544,"text":"\t\t\t\tthrow new IssuesError(`blocked by is a list of up to ${LIMITS.blocks} issue numbers`);","atom":"a1450","author":"trials","version":151},{"n":1545,"text":"\t\t\tdraft.blockedBy = [...new Set(body.blockedBy.map((b) => issueNumber(b, \"an issue it's blocked by\")))];","atom":"a1450","author":"trials","version":151},{"n":1546,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":1547,"text":"\t\tconst notify = names(body.notify, \"Notify\");","atom":"a1450","author":"trials","version":151},{"n":1548,"text":"\t\tif (notify.length > 0) draft.notify = notify;","atom":"a1450","author":"trials","version":151},{"n":1549,"text":"\t\tconst labels = labelsOf(body.labels);","atom":"a1450","author":"trials","version":151},{"n":1550,"text":"\t\tif (labels.length > 0) draft.labels = labels;","atom":"a1450","author":"trials","version":151},{"n":1551,"text":"\t\tconst saved = JSON.stringify(draft);","atom":"a1450","author":"trials","version":151},{"n":1552,"text":"\t\tif (saved.length > LIMITS.draft) throw new IssuesError(`a draft holds at most ${LIMITS.draft} characters`);","atom":"a1450","author":"trials","version":151},{"n":1553,"text":"\t\tif (body.id !== undefined && body.id !== null) {","atom":"a1450","author":"trials","version":151},{"n":1554,"text":"\t\t\tconst id = this.draftOf(actor, body.id).id as number;","atom":"a1450","author":"trials","version":151},{"n":1555,"text":"\t\t\tthis.sql.exec(\"UPDATE drafts SET body = ?, saved_at = ? WHERE id = ?\", saved, this.now(), id);","atom":"a1450","author":"trials","version":151},{"n":1556,"text":"\t\t\treturn { id };","atom":"a1450","author":"trials","version":151},{"n":1557,"text":"\t\t}","atom":"a1450","author":"trials","version":151},{"n":1558,"text":"\t\tconst n = this.rows(\"SELECT COUNT(*) AS n FROM drafts WHERE owner = ?\", who(actor))[0]!.n as number;","atom":"a1450","author":"trials","version":151},{"n":1559,"text":"\t\tif (n >= LIMITS.drafts)","atom":"a1450","author":"trials","version":151},{"n":1560,"text":"\t\t\tthrow new IssuesError(`you have ${n} drafts, the most there can be; open or delete some first`);","atom":"a1450","author":"trials","version":151},{"n":1561,"text":"\t\treturn {","atom":"a1450","author":"trials","version":151},{"n":1562,"text":"\t\t\tid: this.rows(","atom":"a1450","author":"trials","version":151},{"n":1563,"text":"\t\t\t\t\"INSERT INTO drafts (owner, body, saved_at) VALUES (?, ?, ?) RETURNING id\",","atom":"a1450","author":"trials","version":151},{"n":1564,"text":"\t\t\t\twho(actor),","atom":"a1450","author":"trials","version":151},{"n":1565,"text":"\t\t\t\tsaved,","atom":"a1450","author":"trials","version":151},{"n":1566,"text":"\t\t\t\tthis.now(),","atom":"a1450","author":"trials","version":151},{"n":1567,"text":"\t\t\t)[0]!.id as number,","atom":"a1450","author":"trials","version":151},{"n":1568,"text":"\t\t};","atom":"a1450","author":"trials","version":151},{"n":1569,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1570,"text":"","atom":"a1450","author":"trials","version":151},{"n":1571,"text":"\tdeleteDraft(actor: Actor, id: number): void {","atom":"a1450","author":"trials","version":151},{"n":1572,"text":"\t\tthis.sql.exec(\"DELETE FROM drafts WHERE id = ?\", this.draftOf(actor, id).id);","atom":"a1450","author":"trials","version":151},{"n":1573,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1574,"text":"","atom":"a1450","author":"trials","version":151},{"n":1575,"text":"\t/** Opens a draft as an issue, with `body`'s fields over the draft's, and deletes the draft. The issue's news is the only news. */","atom":"a1450","author":"trials","version":151},{"n":1576,"text":"\topenDraft(actor: Actor, id: number, body: Record<string, unknown> = {}): { id: number; news?: News } {","atom":"a1500","author":"trials","version":151},{"n":1577,"text":"\t\tconst d = this.draftOf(actor, id);","atom":"a1450","author":"trials","version":151},{"n":1578,"text":"\t\tconst opened = this.open(actor, { ...(JSON.parse(d.body as string) as DraftBody), ...body });","atom":"a1500","author":"trials","version":151},{"n":1579,"text":"\t\tthis.sql.exec(\"DELETE FROM drafts WHERE id = ?\", d.id);","atom":"a1450","author":"trials","version":151},{"n":1580,"text":"\t\treturn opened;","atom":"a1450","author":"trials","version":151},{"n":1581,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1582,"text":"","atom":"a1450","author":"trials","version":151},{"n":1583,"text":"\t/** One of the actor's drafts; another's is \"no such draft\", as if it weren't there. */","atom":"a1450","author":"trials","version":151},{"n":1584,"text":"\tprivate draftOf(actor: Actor, id: unknown): Row {","atom":"a1450","author":"trials","version":151},{"n":1585,"text":"\t\tconst row = this.rows(","atom":"a1450","author":"trials","version":151},{"n":1586,"text":"\t\t\t\"SELECT * FROM drafts WHERE id = ? AND owner = ?\",","atom":"a1450","author":"trials","version":151},{"n":1587,"text":"\t\t\tissueNumber(id, \"a draft number\"),","atom":"a1450","author":"trials","version":151},{"n":1588,"text":"\t\t\twho(actor),","atom":"a1450","author":"trials","version":151},{"n":1589,"text":"\t\t)[0];","atom":"a1450","author":"trials","version":151},{"n":1590,"text":"\t\tif (!row) throw new IssuesError(`you have no draft ${String(id)}`, 404);","atom":"a1450","author":"trials","version":151},{"n":1591,"text":"\t\treturn row;","atom":"a1450","author":"trials","version":151},{"n":1592,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1593,"text":"\t// From the project: start notes and publish --done ----------------------------------------","atom":"a1500","author":"trials","version":151},{"n":1594,"text":"","atom":"a1450","author":"trials","version":151},{"n":1595,"text":"\t/** A start note names the issue (`change` is the note's log position). One that doesn't exist, or is closed, is ignored: the note stands either way. */","atom":"a1450","author":"trials","version":151},{"n":1596,"text":"\tclaim(agent: string, id: number, change: number): void {","atom":"a1450","author":"trials","version":151},{"n":1597,"text":"\t\tconst row = this.rows(\"SELECT closed_how FROM issues WHERE id = ?\", id)[0];","atom":"a1450","author":"trials","version":151},{"n":1598,"text":"\t\tif (!row || row.closed_how) return;","atom":"a1450","author":"trials","version":151},{"n":1599,"text":"\t\tthis.sql.exec(","atom":"a1450","author":"trials","version":151},{"n":1600,"text":"\t\t\t\"INSERT INTO claims (issue, agent, change, since) VALUES (?, ?, ?, ?) ON CONFLICT (issue, agent) DO UPDATE SET change = excluded.change, since = excluded.since\",","atom":"a1450","author":"trials","version":151},{"n":1601,"text":"\t\t\tid,","atom":"a1450","author":"trials","version":151},{"n":1602,"text":"\t\t\tagent,","atom":"a1450","author":"trials","version":151},{"n":1603,"text":"\t\t\tchange,","atom":"a1450","author":"trials","version":151},{"n":1604,"text":"\t\t\tthis.now(),","atom":"a1450","author":"trials","version":151},{"n":1605,"text":"\t\t);","atom":"a1450","author":"trials","version":151},{"n":1606,"text":"\t\tthis.sql.exec(\"UPDATE issues SET agent = ? WHERE id = ? AND agent IS NULL\", agent, id);","atom":"a1450","author":"trials","version":151},{"n":1607,"text":"\t\tthis.watch(id, agent);","atom":"a1450","author":"trials","version":151},{"n":1608,"text":"\t\tthis.touch(id);","atom":"a1450","author":"trials","version":151},{"n":1609,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1610,"text":"","atom":"a1450","author":"trials","version":151},{"n":1611,"text":"\t/** The start note ended without finishing the issue. */","atom":"a1450","author":"trials","version":151},{"n":1612,"text":"\tunclaim(agent: string, id: number): void {","atom":"a1450","author":"trials","version":151},{"n":1613,"text":"\t\tthis.sql.exec(\"DELETE FROM claims WHERE issue = ? AND agent = ?\", id, agent);","atom":"a1450","author":"trials","version":151},{"n":1614,"text":"\t\tthis.touch(id);","atom":"a1450","author":"trials","version":151},{"n":1615,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1616,"text":"","atom":"a1450","author":"trials","version":151},{"n":1617,"text":"\t/** publish --done finished it, with that version of shared: done, and the version is one out for it. */","atom":"a1450","author":"trials","version":151},{"n":1618,"text":"\tdone(agent: string, id: number, version: number): { news?: News } {","atom":"a1450","author":"trials","version":151},{"n":1619,"text":"\t\tthis.sql.exec(\"DELETE FROM claims WHERE issue = ? AND agent = ?\", id, agent);","atom":"a1450","author":"trials","version":151},{"n":1620,"text":"\t\tconst row = this.rows(\"SELECT title, closed_how FROM issues WHERE id = ?\", id)[0];","atom":"a1450","author":"trials","version":151},{"n":1621,"text":"\t\tif (!row) return {};","atom":"a1450","author":"trials","version":151},{"n":1622,"text":"\t\tthis.sql.exec(","atom":"a1450","author":"trials","version":151},{"n":1623,"text":"\t\t\t\"INSERT INTO versions (issue, version, agent, at) VALUES (?, ?, ?, ?) ON CONFLICT (issue, version) DO NOTHING\",","atom":"a1450","author":"trials","version":151},{"n":1624,"text":"\t\t\tid,","atom":"a1450","author":"trials","version":151},{"n":1625,"text":"\t\t\tversion,","atom":"a1450","author":"trials","version":151},{"n":1626,"text":"\t\t\tagent,","atom":"a1450","author":"trials","version":151},{"n":1627,"text":"\t\t\tthis.now(),","atom":"a1450","author":"trials","version":151},{"n":1628,"text":"\t\t);","atom":"a1450","author":"trials","version":151},{"n":1629,"text":"\t\tthis.touch(id);","atom":"a1450","author":"trials","version":151},{"n":1630,"text":"\t\tif (row.closed_how) return {};","atom":"a1500","author":"trials","version":151},{"n":1631,"text":"\t\tthis.sql.exec(","atom":"a1450","author":"trials","version":151},{"n":1632,"text":"\t\t\t\"UPDATE issues SET closed_how = 'done', closed_by = ?, closed_version = ?, closed_at = ? WHERE id = ?\",","atom":"a1450","author":"trials","version":151},{"n":1633,"text":"\t\t\tagent,","atom":"a1450","author":"trials","version":151},{"n":1634,"text":"\t\t\tversion,","atom":"a1450","author":"trials","version":151},{"n":1635,"text":"\t\t\tthis.now(),","atom":"a1450","author":"trials","version":151},{"n":1636,"text":"\t\t\tid,","atom":"a1450","author":"trials","version":151},{"n":1637,"text":"\t\t);","atom":"a1450","author":"trials","version":151},{"n":1638,"text":"\t\tthis.sql.exec(\"DELETE FROM claims WHERE issue = ?\", id);","atom":"a1450","author":"trials","version":151},{"n":1639,"text":"\t\treturn {","atom":"a1450","author":"trials","version":151},{"n":1640,"text":"\t\t\tnews: { issue: id, text: `${agent} finished issue ${id} (${JSON.stringify(row.title)}) in version ${version}` },","atom":"a1450","author":"trials","version":151},{"n":1641,"text":"\t\t};","atom":"a1450","author":"trials","version":151},{"n":1642,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1643,"text":"}","atom":"a1450","author":"trials","version":151},{"n":1644,"text":"","atom":"a1450","author":"trials","version":151},{"n":1645,"text":"function asDraft(d: Row): Draft {","atom":"a1450","author":"trials","version":151},{"n":1646,"text":"\treturn {","atom":"a1450","author":"trials","version":151},{"n":1647,"text":"\t\tid: d.id as number,","atom":"a1450","author":"trials","version":151},{"n":1648,"text":"\t\tbody: JSON.parse(d.body as string) as DraftBody,","atom":"a1450","author":"trials","version":151},{"n":1649,"text":"\t\tsaved: d.saved_at as number,","atom":"a1450","author":"trials","version":151},{"n":1650,"text":"\t};","atom":"a1450","author":"trials","version":151},{"n":1651,"text":"}","atom":"a1450","author":"trials","version":151},{"n":1652,"text":"","atom":"a1450","author":"trials","version":151},{"n":1653,"text":"/** A snippet with \\u0001 and \\u0002 around each match, as plain text and the matches' spans. */","atom":"a1450","author":"trials","version":151},{"n":1654,"text":"function marked(snip: string): { text: string; marks: number[][] } {","atom":"a1450","author":"trials","version":151},{"n":1655,"text":"\tlet text = \"\";","atom":"a1450","author":"trials","version":151},{"n":1656,"text":"\tconst marks: number[][] = [];","atom":"a1450","author":"trials","version":151},{"n":1657,"text":"\tlet open = -1;","atom":"a1450","author":"trials","version":151},{"n":1658,"text":"\tfor (const ch of snip) {","atom":"a1450","author":"trials","version":151},{"n":1659,"text":"\t\tif (ch === \"\\u0001\") open = text.length;","atom":"a1450","author":"trials","version":151},{"n":1660,"text":"\t\telse if (ch === \"\\u0002\") {","atom":"a1450","author":"trials","version":151},{"n":1661,"text":"\t\t\tif (open >= 0) marks.push([open, text.length]);","atom":"a1450","author":"trials","version":151},{"n":1662,"text":"\t\t\topen = -1;","atom":"a1450","author":"trials","version":151},{"n":1663,"text":"\t\t} else text += ch;","atom":"a1450","author":"trials","version":151},{"n":1664,"text":"\t}","atom":"a1450","author":"trials","version":151},{"n":1665,"text":"\treturn { text, marks };","atom":"a1450","author":"trials","version":151},{"n":1666,"text":"}","atom":"a1450","author":"trials","version":151},{"n":1667,"text":"","atom":"a1450","author":"trials","version":151},{"n":1668,"text":"function askOf(r: Row): StoredAsk {","atom":"a1450","author":"trials","version":151},{"n":1669,"text":"\treturn {","atom":"a1450","author":"trials","version":151},{"n":1670,"text":"\t\tid: r.id as number,","atom":"a1450","author":"trials","version":151},{"n":1671,"text":"\t\tissue: (r.issue as number | null) ?? null,","atom":"a1450","author":"trials","version":151},{"n":1672,"text":"\t\tby: r.by as string,","atom":"a1450","author":"trials","version":151},{"n":1673,"text":"\t\tof: r.of as string,","atom":"a1450","author":"trials","version":151},{"n":1674,"text":"\t\tkind: r.kind as Ask[\"kind\"],","atom":"a1450","author":"trials","version":151},{"n":1675,"text":"\t\tquestion: r.question as string,","atom":"a1450","author":"trials","version":151},{"n":1676,"text":"\t\treview: r.review === 1,","atom":"a1450","author":"trials","version":151},{"n":1677,"text":"\t\tdue: (r.due as string | null) ?? null,","atom":"a1450","author":"trials","version":151},{"n":1678,"text":"\t\trecommendation: r.recommendation ? (JSON.parse(r.recommendation as string) as Ask[\"recommendation\"]) : null,","atom":"a1450","author":"trials","version":151},{"n":1679,"text":"\t\toptions: JSON.parse(r.options as string) as string[],","atom":"a1450","author":"trials","version":151},{"n":1680,"text":"\t\tblocks: JSON.parse(r.blocks as string) as number[],","atom":"a1450","author":"trials","version":151},{"n":1681,"text":"\t\tat: r.at as number,","atom":"a1450","author":"trials","version":151},{"n":1682,"text":"\t\tanswer:","atom":"a1450","author":"trials","version":151},{"n":1683,"text":"\t\t\tr.answer_at !== null && r.answer_at !== undefined","atom":"a1450","author":"trials","version":151},{"n":1684,"text":"\t\t\t\t? { by: r.answer_by as string, words: r.answer_words as string, at: r.answer_at as number }","atom":"a1450","author":"trials","version":151},{"n":1685,"text":"\t\t\t\t: null,","atom":"a1450","author":"trials","version":151},{"n":1686,"text":"\t\tseen: (r.seen_at as number | null | undefined) ?? null,","atom":"a1450","author":"trials","version":151},{"n":1687,"text":"\t\twaitingOn:","atom":"a1450","author":"trials","version":151},{"n":1688,"text":"\t\t\tr.answer_at !== null && r.answer_at !== undefined ? null : ((r.waiting_on as string | null) ?? (r.of as string)),","atom":"a1450","author":"trials","version":151},{"n":1689,"text":"\t\treplies: [],","atom":"a1450","author":"trials","version":151},{"n":1690,"text":"\t\t...(r.outcome === \"withdrawn\" ? { outcome: \"withdrawn\" as const } : {}),","atom":"a1450","author":"trials","version":151},{"n":1691,"text":"\t\t...(typeof r.ack_at === \"number\" ? { ack: { at: r.ack_at, note: (r.ack_note as string | null) ?? \"\" } } : {}),","atom":"a1450","author":"trials","version":151},{"n":1692,"text":"\t};","atom":"a1450","author":"trials","version":151},{"n":1693,"text":"}","atom":"a1450","author":"trials","version":151}],"atoms":[{"atom":"a1450","author":"trials","message":"","time":1791906738468,"timeIso":"2026-10-13T15:52:18.468Z","publish":{"draft":null,"version":151,"agent":"trials","message":"Open server: issues, asks and news, served at <server>/issues, and the client finds them there when no issues server is named","time":1791907770251,"timeIso":"2026-10-13T16:09:30.251Z"}},{"atom":"a1451","author":"trials","message":"","time":1791906738721,"timeIso":"2026-10-13T15:52:18.721Z","publish":{"draft":null,"version":151,"agent":"trials","message":"Open server: issues, asks and news, served at <server>/issues, and the client finds them there when no issues server is named","time":1791907770251,"timeIso":"2026-10-13T16:09:30.251Z"}},{"atom":"a1500","author":"trials","message":"","time":1791907494521,"timeIso":"2026-10-13T16:04:54.521Z","publish":{"draft":null,"version":151,"agent":"trials","message":"Open server: issues, asks and news, served at <server>/issues, and the client finds them there when no issues server is named","time":1791907770251,"timeIso":"2026-10-13T16:09:30.251Z"}}]}