From e7d350a0e305a58191331b6664866419d215802f Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 18 May 2024 14:54:10 -0500 Subject: [PATCH] Fix uploading by URL --- deno.json | 1 - src/controllers/api/statuses.ts | 4 ++-- src/db/unattached-media.ts | 25 +++++-------------------- src/upload.ts | 11 ++++++++++- src/utils/api.ts | 2 -- src/views/mastodon/attachments.ts | 6 +++--- 6 files changed, 20 insertions(+), 29 deletions(-) diff --git a/deno.json b/deno.json index 54ba468..946b4e0 100644 --- a/deno.json +++ b/deno.json @@ -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/" }, diff --git a/src/controllers/api/statuses.ts b/src/controllers/api/statuses.ts index d8186f5..f7a603f 100644 --- a/src/controllers/api/statuses.ts +++ b/src/controllers/api/statuses.ts @@ -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 }) => { diff --git a/src/db/unattached-media.ts b/src/db/unattached-media.ts index 397a1f7..0ab46b6 100644 --- a/src/db/unattached-media.ts +++ b/src/db/unattached-media.ts @@ -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) { - 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, ids: string[ .execute(); } -/** Get unattached media by URLs. */ -async function getUnattachedMediaByUrls(kysely: Kysely, 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 { if (!urls.length) return; @@ -87,7 +73,6 @@ export { deleteUnattachedMediaByUrl, getUnattachedMedia, getUnattachedMediaByIds, - getUnattachedMediaByUrls, insertUnattachedMedia, type UnattachedMedia, }; diff --git a/src/upload.ts b/src/upload.ts index 4f5fd14..d815bd8 100644 --- a/src/upload.ts +++ b/src/upload.ts @@ -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; } diff --git a/src/utils/api.ts b/src/utils/api.ts index c54f5aa..dceede7 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -29,8 +29,6 @@ async function createEvent(t: EventStub, c: AppContext): Promise { }); } - console.log(t); - const event = await signer.signEvent({ content: '', created_at: nostrNow(), diff --git a/src/views/mastodon/attachments.ts b/src/views/mastodon/attachments.ts index 8922985..1dbcda9 100644 --- a/src/views/mastodon/attachments.ts +++ b/src/views/mastodon/attachments.ts @@ -5,15 +5,15 @@ import { UnattachedMedia } from '@/db/unattached-media.ts'; type DittoAttachment = TypeFest.SetOptional; 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,