Add source filters and a full-window media view
This commit is contained in:
@@ -320,17 +320,27 @@ export class ScrapeStore {
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
queryMedia(options: { kind?: MediaKind; limit?: number; offset?: number } = {}): MediaRow[] {
|
||||
queryMedia(
|
||||
options: { kind?: MediaKind; source?: string; limit?: number; offset?: number } = {}
|
||||
): MediaRow[] {
|
||||
const limit = Math.max(1, Math.min(options.limit ?? 50, 500));
|
||||
const offset = Math.max(0, options.offset ?? 0);
|
||||
|
||||
const statement = options.kind
|
||||
? this.db.prepare("SELECT * FROM media WHERE kind = ? ORDER BY id DESC LIMIT ? OFFSET ?")
|
||||
: this.db.prepare("SELECT * FROM media ORDER BY id DESC LIMIT ? OFFSET ?");
|
||||
const conditions: string[] = [];
|
||||
const values: Array<string | number> = [];
|
||||
if (options.kind) {
|
||||
conditions.push("kind = ?");
|
||||
values.push(options.kind);
|
||||
}
|
||||
if (options.source) {
|
||||
conditions.push("source = ?");
|
||||
values.push(options.source);
|
||||
}
|
||||
|
||||
const rows = options.kind
|
||||
? statement.all(options.kind, limit, offset)
|
||||
: statement.all(limit, offset);
|
||||
const where = conditions.length ? `WHERE ${conditions.join(" AND ")}` : "";
|
||||
const rows = this.db
|
||||
.prepare(`SELECT * FROM media ${where} ORDER BY id DESC LIMIT ? OFFSET ?`)
|
||||
.all(...values, limit, offset);
|
||||
|
||||
return rows as unknown as MediaRow[];
|
||||
}
|
||||
|
||||
@@ -396,6 +396,36 @@ void (async () => {
|
||||
assert.equal(existsSync(join(root, started.id, row.file_path)), true);
|
||||
});
|
||||
|
||||
await test("store filters by source and kind", () => {
|
||||
const dir = join(root, "store-source");
|
||||
mkdirSync(dir, { recursive: true });
|
||||
|
||||
const store = new ScrapeStore(dir);
|
||||
store.insertPage([
|
||||
message("700", {
|
||||
attachments: [attachment({ filename: "pic.png", contentType: "image/png" })],
|
||||
embeds: [
|
||||
{
|
||||
type: "video",
|
||||
url: "https://www.youtube.com/watch?v=x",
|
||||
title: null,
|
||||
provider: "YouTube",
|
||||
videoUrl: "https://www.youtube.com/embed/x",
|
||||
imageUrl: null,
|
||||
thumbnailUrl: null
|
||||
}
|
||||
]
|
||||
})
|
||||
]);
|
||||
|
||||
assert.equal(store.queryMedia({ source: "attachment" }).length, 1);
|
||||
assert.equal(store.queryMedia({ source: "embed" }).length, 1);
|
||||
assert.equal(store.queryMedia({ source: "sticker" }).length, 0);
|
||||
assert.equal(store.queryMedia({ kind: "image", source: "attachment" }).length, 1);
|
||||
assert.equal(store.queryMedia({ kind: "video", source: "attachment" }).length, 0);
|
||||
store.close();
|
||||
});
|
||||
|
||||
await test("preview urls are Discord-only resizes", () => {
|
||||
assert.equal(
|
||||
previewUrlFor({ url: "https://cdn.discordapp.com/attachments/1/2/pic.png", kind: "image" }),
|
||||
|
||||
Reference in New Issue
Block a user