diff --git a/src/controllers/api/statuses.ts b/src/controllers/api/statuses.ts index 984dfdb..00f9a98 100644 --- a/src/controllers/api/statuses.ts +++ b/src/controllers/api/statuses.ts @@ -4,6 +4,7 @@ import { z } from 'zod'; import { type AppController } from '@/app.ts'; import { Conf } from '@/config.ts'; +import { DittoDB } from '@/db/DittoDB.ts'; import { getUnattachedMediaByIds } from '@/db/unattached-media.ts'; import { getAncestors, getAuthor, getDescendants, getEvent } from '@/queries.ts'; import { addTag, deleteTag } from '@/tags.ts'; @@ -56,6 +57,7 @@ const statusController: AppController = async (c) => { const createStatusController: AppController = async (c) => { const body = await parseBody(c.req.raw); const result = createStatusSchema.safeParse(body); + const kysely = await DittoDB.getInstance(); if (!result.success) { return c.json({ error: 'Bad request', schema: result.error }, 400); @@ -92,7 +94,7 @@ const createStatusController: AppController = async (c) => { const viewerPubkey = await c.get('signer')?.getPublicKey(); if (data.media_ids?.length) { - const media = await getUnattachedMediaByIds(data.media_ids) + const media = await getUnattachedMediaByIds(kysely, data.media_ids) .then((media) => media.filter(({ pubkey }) => pubkey === viewerPubkey)) .then((media) => media.map(({ url, data }) => ['media', url, data])); diff --git a/src/db/unattached-media.ts b/src/db/unattached-media.ts index 8063628..cee1e3a 100644 --- a/src/db/unattached-media.ts +++ b/src/db/unattached-media.ts @@ -1,6 +1,8 @@ +import { Kysely } from 'kysely'; import uuid62 from 'uuid62'; import { DittoDB } from '@/db/DittoDB.ts'; +import { DittoTables } from '@/db/DittoTables.ts'; import { type MediaData } from '@/schemas/nostr.ts'; interface UnattachedMedia { @@ -28,8 +30,7 @@ async function insertUnattachedMedia(media: Omit) { return kysely.selectFrom('unattached_media') .select([ 'unattached_media.id', @@ -41,9 +42,8 @@ async function selectUnattachedMediaQuery() { } /** Find attachments that exist but aren't attached to any events. */ -async function getUnattachedMedia(until: Date) { - const query = await selectUnattachedMediaQuery(); - return query +function getUnattachedMedia(kysely: Kysely, until: Date) { + return selectUnattachedMediaQuery(kysely) .leftJoin('nostr_tags', 'unattached_media.url', 'nostr_tags.value') .where('uploaded_at', '<', until.getTime()) .execute(); @@ -58,10 +58,9 @@ async function deleteUnattachedMediaByUrl(url: string) { } /** Get unattached media by IDs. */ -async function getUnattachedMediaByIds(ids: string[]) { +async function getUnattachedMediaByIds(kysely: Kysely, ids: string[]) { if (!ids.length) return []; - const query = await selectUnattachedMediaQuery(); - return query + return await selectUnattachedMediaQuery(kysely) .where('id', 'in', ids) .execute(); }