| 1 | import "./Question.ts"; // contains schema |
| 2 | |
| 3 | const db = getDb("questions.sqlite"); |
| 4 | |
| 5 | export interface PendingQuestionData { |
| 6 | date: string; |
| 7 | prompt: string; |
| 8 | sourceName: string | "unknown"; |
| 9 | sourceLocation: string | "unknown"; |
| 10 | sourceVPN: string | null; |
| 11 | } |
| 12 | |
| 13 | /** PendingQuestion is actually a `Question` but with type == .pending */ |
| 14 | export class PendingQuestion { |
| 15 | /** |
| 16 | * questions are identified by a 'minute identifier' |
| 17 | * which is time / 60000 (minutes since epoch) |
| 18 | */ |
| 19 | qmid!: number; |
| 20 | private type!: QuestionType.pending; |
| 21 | private text!: string; |
| 22 | private cached_json?: PendingQuestionData; |
| 23 | |
| 24 | // -- instance ops -- |
| 25 | |
| 26 | /** "https://paperclover.net/q+a/${id}" - #YYMMDDHHMM */ |
| 27 | get id(): string { |
| 28 | return formatQuestionId(this.date); |
| 29 | } |
| 30 | get date(): Date { |
| 31 | return new Date(this.qmid * 1000 * 60); |
| 32 | } |
| 33 | get data(): PendingQuestionData { |
| 34 | assert(this.type === QuestionType.pending); |
| 35 | return this.cached_json ??= JSON.parse(this.text); |
| 36 | } |
| 37 | get prompt() { |
| 38 | return this.data.prompt; |
| 39 | } |
| 40 | get sourceName() { |
| 41 | return this.data.sourceName; |
| 42 | } |
| 43 | get sourceLocation() { |
| 44 | return this.data.sourceLocation; |
| 45 | } |
| 46 | get sourceVPN() { |
| 47 | return this.data.sourceVPN; |
| 48 | } |
| 49 | |
| 50 | // -- static ops -- |
| 51 | |
| 52 | static getAll() { |
| 53 | return getAllQuery.iter(); |
| 54 | } |
| 55 | static getByDate(timestamp: Date) { |
| 56 | const qmid = Math.floor(timestamp.getTime() / 1000 / 60); |
| 57 | return getByDateQuery.get(qmid); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | const getAllQuery = db.prepare(` |
| 62 | SELECT * FROM questions WHERE type = ${QuestionType.pending} ORDER BY qmid DESC |
| 63 | `).as(PendingQuestion); |
| 64 | |
| 65 | const getByDateQuery = db.prepare<[qmid: number]>(` |
| 66 | SELECT * FROM questions WHERE qmid = ? AND type = ${QuestionType.pending} LIMIT 1 |
| 67 | `).as(PendingQuestion); |
| 68 | |
| 69 | import { getDb } from "#sitegen/sqlite"; |
| 70 | import assert from "node:assert"; |
| 71 | import { formatQuestionId } from "../format.ts"; |
| 72 | import { QuestionType } from "./Question.ts"; |