Fix media uploads due to 'awaiting' a query builder instance
This commit is contained in:
parent
04980089b4
commit
5aacbe7af5
|
@ -4,6 +4,7 @@ import { z } from 'zod';
|
||||||
|
|
||||||
import { type AppController } from '@/app.ts';
|
import { type AppController } from '@/app.ts';
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
|
import { DittoDB } from '@/db/DittoDB.ts';
|
||||||
import { getUnattachedMediaByIds } from '@/db/unattached-media.ts';
|
import { getUnattachedMediaByIds } from '@/db/unattached-media.ts';
|
||||||
import { getAncestors, getAuthor, getDescendants, getEvent } from '@/queries.ts';
|
import { getAncestors, getAuthor, getDescendants, getEvent } from '@/queries.ts';
|
||||||
import { addTag, deleteTag } from '@/tags.ts';
|
import { addTag, deleteTag } from '@/tags.ts';
|
||||||
|
@ -56,6 +57,7 @@ const statusController: AppController = async (c) => {
|
||||||
const createStatusController: AppController = async (c) => {
|
const createStatusController: AppController = async (c) => {
|
||||||
const body = await parseBody(c.req.raw);
|
const body = await parseBody(c.req.raw);
|
||||||
const result = createStatusSchema.safeParse(body);
|
const result = createStatusSchema.safeParse(body);
|
||||||
|
const kysely = await DittoDB.getInstance();
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
return c.json({ error: 'Bad request', schema: result.error }, 400);
|
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();
|
const viewerPubkey = await c.get('signer')?.getPublicKey();
|
||||||
|
|
||||||
if (data.media_ids?.length) {
|
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.filter(({ pubkey }) => pubkey === viewerPubkey))
|
||||||
.then((media) => media.map(({ url, data }) => ['media', url, data]));
|
.then((media) => media.map(({ url, data }) => ['media', url, data]));
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
|
import { Kysely } from 'kysely';
|
||||||
import uuid62 from 'uuid62';
|
import uuid62 from 'uuid62';
|
||||||
|
|
||||||
import { DittoDB } from '@/db/DittoDB.ts';
|
import { DittoDB } from '@/db/DittoDB.ts';
|
||||||
|
import { DittoTables } from '@/db/DittoTables.ts';
|
||||||
import { type MediaData } from '@/schemas/nostr.ts';
|
import { type MediaData } from '@/schemas/nostr.ts';
|
||||||
|
|
||||||
interface UnattachedMedia {
|
interface UnattachedMedia {
|
||||||
|
@ -28,8 +30,7 @@ async function insertUnattachedMedia(media: Omit<UnattachedMedia, 'id' | 'upload
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Select query for unattached media. */
|
/** Select query for unattached media. */
|
||||||
async function selectUnattachedMediaQuery() {
|
function selectUnattachedMediaQuery(kysely: Kysely<DittoTables>) {
|
||||||
const kysely = await DittoDB.getInstance();
|
|
||||||
return kysely.selectFrom('unattached_media')
|
return kysely.selectFrom('unattached_media')
|
||||||
.select([
|
.select([
|
||||||
'unattached_media.id',
|
'unattached_media.id',
|
||||||
|
@ -41,9 +42,8 @@ async function selectUnattachedMediaQuery() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Find attachments that exist but aren't attached to any events. */
|
/** Find attachments that exist but aren't attached to any events. */
|
||||||
async function getUnattachedMedia(until: Date) {
|
function getUnattachedMedia(kysely: Kysely<DittoTables>, until: Date) {
|
||||||
const query = await selectUnattachedMediaQuery();
|
return selectUnattachedMediaQuery(kysely)
|
||||||
return query
|
|
||||||
.leftJoin('nostr_tags', 'unattached_media.url', 'nostr_tags.value')
|
.leftJoin('nostr_tags', 'unattached_media.url', 'nostr_tags.value')
|
||||||
.where('uploaded_at', '<', until.getTime())
|
.where('uploaded_at', '<', until.getTime())
|
||||||
.execute();
|
.execute();
|
||||||
|
@ -58,10 +58,9 @@ async function deleteUnattachedMediaByUrl(url: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get unattached media by IDs. */
|
/** Get unattached media by IDs. */
|
||||||
async function getUnattachedMediaByIds(ids: string[]) {
|
async function getUnattachedMediaByIds(kysely: Kysely<DittoTables>, ids: string[]) {
|
||||||
if (!ids.length) return [];
|
if (!ids.length) return [];
|
||||||
const query = await selectUnattachedMediaQuery();
|
return await selectUnattachedMediaQuery(kysely)
|
||||||
return query
|
|
||||||
.where('id', 'in', ids)
|
.where('id', 'in', ids)
|
||||||
.execute();
|
.execute();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue