diff --git a/db.ts b/db.ts index 1459f5d..f25f519 100644 --- a/db.ts +++ b/db.ts @@ -61,6 +61,26 @@ export type PMeta = { width: number; height: number; }; +export type ExtendedPMeta = { + gid: number; + index: number; + token: string; + name: string; + width: number; + height: number; + is_nsfw: boolean; + is_ad: boolean; +}; +export type ExtendedPMetaRaw = { + gid: number; + index: number; + token: string; + name: string; + width: number; + height: number; + is_nsfw: number | null; + is_ad: number | null; +}; export type Tag = { id: number; tag: string; @@ -75,7 +95,7 @@ export type EhFile = { height: number; is_original: boolean; }; -export type EhFileRawV1 = { +type EhFileRawV1 = { id: number; gid: number; token: string; @@ -92,7 +112,26 @@ export type EhFileRaw = { height: number; is_original: number; }; -const ALL_TABLES = ["version", "task", "gmeta", "pmeta", "tag", "gtag", "file"]; +export type EhFileMeta = { + token: string; + is_nsfw: boolean; + is_ad: boolean; +}; +export type EhFileMetaRaw = { + token: string; + is_nsfw: number; + is_ad: number; +}; +const ALL_TABLES = [ + "version", + "task", + "gmeta", + "pmeta", + "tag", + "gtag", + "file", + "filemeta", +]; const VERSION_TABLE = `CREATE TABLE version ( id TEXT, ver TEXT, @@ -152,6 +191,12 @@ const FILE_TABLE = `CREATE TABLE file ( height INT, is_original BOOLEAN );`; +const FILEMETA_TABLE = `CREATE TABLE filemeta ( + token TEXT, + is_nsfw BOOLEAN, + is_ad BOOLEAN, + PRIMARY KEY (token) +);`; export class EhDb { db; @@ -316,6 +361,9 @@ export class EhDb { if (!this.#exist_table.has("file")) { this.db.execute(FILE_TABLE); } + if (!this.#exist_table.has("filemeta")) { + this.db.execute(FILEMETA_TABLE); + } this.#updateExistsTable(); } #read_version() { @@ -559,6 +607,16 @@ export class EhDb { } } } + convert_extended_pmeta(m: ExtendedPMetaRaw[]) { + return m.map((m) => { + const n = m.is_nsfw ? true : false; + const a = m.is_ad ? true : false; + const t = m; + t.is_nsfw = n; + t.is_ad = a; + return t; + }); + } convert_file(f: EhFileRaw[]) { return f.map((m) => { const b = m.is_original !== 0; @@ -567,6 +625,16 @@ export class EhDb { return t; }); } + convert_filemeta(m: EhFileMetaRaw[]) { + return m.map((m) => { + const n = m.is_nsfw !== 0; + const a = m.is_ad !== 0; + const t = m; + t.is_nsfw = n; + t.is_ad = a; + return t; + }); + } convert_gmeta(m: GMetaRaw[]): GMeta[] { return m.map((m) => { const b = m.expunged !== 0; @@ -599,6 +667,14 @@ export class EhDb { if (!this.#dblock) return; eval(`Deno.funlockSync(${this.#dblock.rid});`); } + get_extended_pmeta(gid: number) { + return this.convert_extended_pmeta( + this.db.queryEntries( + "SELECT pmeta.*, filemeta.is_nsfw, filemeta.is_ad FROM pmeta LEFT JOIN filemeta ON filemeta.token = pmeta.token WHERE gid = ?;", + [gid], + ), + ); + } get_file(id: number) { const d = this.convert_file(this.db.queryEntries( "SELECT * FROM file WHERE id = ?;", diff --git a/routes/api/gallery/[gid].ts b/routes/api/gallery/[gid].ts index a593738..2f1d722 100644 --- a/routes/api/gallery/[gid].ts +++ b/routes/api/gallery/[gid].ts @@ -15,7 +15,9 @@ export const handler: Handlers = { const data: GalleryData = { meta, tags: m.db.get_gtags_full(gid).sort((a, b) => a.id - b.id), - pages: m.db.get_pmeta(gid).sort((a, b) => a.index - b.index), + pages: m.db.get_extended_pmeta(gid).sort((a, b) => + a.index - b.index + ), }; return return_data(data); }, diff --git a/server/gallery.ts b/server/gallery.ts index eeb2dcc..aeffc71 100644 --- a/server/gallery.ts +++ b/server/gallery.ts @@ -1,10 +1,10 @@ -import { GMeta, PMeta, Tag } from "../db.ts"; +import { ExtendedPMeta, GMeta, Tag } from "../db.ts"; import { JSONResult } from "./utils.ts"; export type GalleryData = { meta: GMeta; tags: Tag[]; - pages: PMeta[]; + pages: ExtendedPMeta[]; }; export type GalleryResult = JSONResult;