Fix uploading by URL

This commit is contained in:
Alex Gleason 2024-05-18 14:54:10 -05:00
parent 611a94bdcf
commit e7d350a0e3
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
6 changed files with 20 additions and 29 deletions

View File

@ -54,7 +54,6 @@
"tseep": "npm:tseep@^1.2.1",
"type-fest": "npm:type-fest@^4.3.0",
"unfurl.js": "npm:unfurl.js@^6.4.0",
"uuid62": "npm:uuid62@^1.0.2",
"zod": "npm:zod@^3.23.5",
"~/fixtures/": "./fixtures/"
},

View File

@ -5,7 +5,7 @@ import { z } from 'zod';
import { type AppController } from '@/app.ts';
import { Conf } from '@/config.ts';
import { DittoDB } from '@/db/DittoDB.ts';
import { getUnattachedMediaByUrls } from '@/db/unattached-media.ts';
import { getUnattachedMediaByIds } from '@/db/unattached-media.ts';
import { getAncestors, getAuthor, getDescendants, getEvent } from '@/queries.ts';
import { addTag, deleteTag } from '@/tags.ts';
import { createEvent, paginationSchema, parseBody, updateListEvent } from '@/utils/api.ts';
@ -94,7 +94,7 @@ const createStatusController: AppController = async (c) => {
const viewerPubkey = await c.get('signer')?.getPublicKey();
if (data.media_ids?.length) {
const media = await getUnattachedMediaByUrls(kysely, data.media_ids)
const media = await getUnattachedMediaByIds(kysely, data.media_ids)
.then((media) => media.filter(({ pubkey }) => pubkey === viewerPubkey))
.then((media) =>
media.map(({ data }) => {

View File

@ -1,5 +1,4 @@
import { Kysely } from 'kysely';
import uuid62 from 'uuid62';
import { DittoDB } from '@/db/DittoDB.ts';
import { DittoTables } from '@/db/DittoTables.ts';
@ -8,24 +7,19 @@ interface UnattachedMedia {
id: string;
pubkey: string;
url: string;
data: string[][]; // NIP-94 tags
/** NIP-94 tags. */
data: string[][];
uploaded_at: number;
}
/** Add unattached media into the database. */
async function insertUnattachedMedia(media: Omit<UnattachedMedia, 'id' | 'uploaded_at'>) {
const result = {
id: uuid62.v4(),
uploaded_at: Date.now(),
...media,
};
async function insertUnattachedMedia(media: UnattachedMedia) {
const kysely = await DittoDB.getInstance();
await kysely.insertInto('unattached_media')
.values({ ...result, data: JSON.stringify(media.data) })
.values({ ...media, data: JSON.stringify(media.data) })
.execute();
return result;
return media;
}
/** Select query for unattached media. */
@ -64,14 +58,6 @@ async function getUnattachedMediaByIds(kysely: Kysely<DittoTables>, ids: string[
.execute();
}
/** Get unattached media by URLs. */
async function getUnattachedMediaByUrls(kysely: Kysely<DittoTables>, urls: string[]) {
if (!urls.length) return [];
return await selectUnattachedMediaQuery(kysely)
.where('url', 'in', urls)
.execute();
}
/** Delete rows as an event with media is being created. */
async function deleteAttachedMedia(pubkey: string, urls: string[]): Promise<void> {
if (!urls.length) return;
@ -87,7 +73,6 @@ export {
deleteUnattachedMediaByUrl,
getUnattachedMedia,
getUnattachedMediaByIds,
getUnattachedMediaByUrls,
insertUnattachedMedia,
type UnattachedMedia,
};

View File

@ -36,7 +36,16 @@ async function uploadFile(file: File, meta: FileMeta, signal?: AbortSignal): Pro
data.push(['alt', description]);
}
await insertUnattachedMedia({ pubkey, url, data });
const uuid = crypto.randomUUID();
data.push(['uuid', uuid]);
await insertUnattachedMedia({
id: uuid,
pubkey,
url,
data,
uploaded_at: Date.now(),
});
return data;
}

View File

@ -29,8 +29,6 @@ async function createEvent(t: EventStub, c: AppContext): Promise<NostrEvent> {
});
}
console.log(t);
const event = await signer.signEvent({
content: '',
created_at: nostrNow(),

View File

@ -5,15 +5,15 @@ import { UnattachedMedia } from '@/db/unattached-media.ts';
type DittoAttachment = TypeFest.SetOptional<UnattachedMedia, 'id' | 'pubkey' | 'uploaded_at'>;
function renderAttachment(tags: string[][]) {
const url = tags.find(([name]) => name === 'url')?.[1];
const m = tags.find(([name]) => name === 'm')?.[1];
const url = tags.find(([name]) => name === 'url')?.[1];
const alt = tags.find(([name]) => name === 'alt')?.[1];
const cid = tags.find(([name]) => name === 'cid')?.[1];
const uuid = tags.find(([name]) => name === 'uuid')?.[1];
const blurhash = tags.find(([name]) => name === 'blurhash')?.[1];
return {
id: url,
id: uuid,
type: getAttachmentType(m ?? ''),
url,
preview_url: url,