Fix media uploads due to 'awaiting' a query builder instance

This commit is contained in:
Alex Gleason 2024-05-16 18:53:04 -05:00
parent 04980089b4
commit 5aacbe7af5
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 10 additions and 9 deletions

View File

@ -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]));

View File

@ -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<UnattachedMedia, 'id' | 'upload
}
/** Select query for unattached media. */
async function selectUnattachedMediaQuery() {
const kysely = await DittoDB.getInstance();
function selectUnattachedMediaQuery(kysely: Kysely<DittoTables>) {
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<DittoTables>, 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<DittoTables>, ids: string[]) {
if (!ids.length) return [];
const query = await selectUnattachedMediaQuery();
return query
return await selectUnattachedMediaQuery(kysely)
.where('id', 'in', ids)
.execute();
}