From 8efd6fbb20403159bcb63b0493f1ff8eed6d3e98 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 7 Sep 2024 10:24:56 -0500 Subject: [PATCH 1/2] Remove unattached_media table, replace with LRUCache, fix media upload order problem --- src/DittoUploads.ts | 17 ++++ src/controllers/api/media.ts | 23 +++-- src/controllers/api/statuses.ts | 30 ++++--- src/db/DittoTables.ts | 9 -- src/db/migrations/031_rm_unattached_media.ts | 34 ++++++++ src/db/unattached-media.ts | 88 -------------------- src/pipeline.ts | 10 --- src/utils/upload.ts | 18 ++-- src/views/mastodon/attachments.ts | 4 +- src/views/mastodon/statuses.ts | 6 +- 10 files changed, 100 insertions(+), 139 deletions(-) create mode 100644 src/DittoUploads.ts create mode 100644 src/db/migrations/031_rm_unattached_media.ts delete mode 100644 src/db/unattached-media.ts diff --git a/src/DittoUploads.ts b/src/DittoUploads.ts new file mode 100644 index 00000000..044d3585 --- /dev/null +++ b/src/DittoUploads.ts @@ -0,0 +1,17 @@ +import { LRUCache } from 'lru-cache'; + +import { Time } from '@/utils/time.ts'; + +export interface DittoUpload { + id: string; + pubkey: string; + description?: string; + url: string; + tags: string[][]; + uploadedAt: Date; +} + +export const dittoUploads = new LRUCache({ + max: 1000, + ttl: Time.minutes(15), +}); diff --git a/src/controllers/api/media.ts b/src/controllers/api/media.ts index 3fa3ee68..9dc2de27 100644 --- a/src/controllers/api/media.ts +++ b/src/controllers/api/media.ts @@ -5,7 +5,7 @@ import { fileSchema } from '@/schema.ts'; import { parseBody } from '@/utils/api.ts'; import { renderAttachment } from '@/views/mastodon/attachments.ts'; import { uploadFile } from '@/utils/upload.ts'; -import { setMediaDescription } from '@/db/unattached-media.ts'; +import { dittoUploads } from '@/DittoUploads.ts'; const mediaBodySchema = z.object({ file: fileSchema, @@ -39,19 +39,24 @@ const mediaController: AppController = async (c) => { const updateMediaController: AppController = async (c) => { const result = mediaUpdateSchema.safeParse(await parseBody(c.req.raw)); + if (!result.success) { return c.json({ error: 'Bad request.', schema: result.error }, 422); } - try { - const { description } = result.data; - if (!await setMediaDescription(c.req.param('id'), description)) { - return c.json({ error: 'File with specified ID not found.' }, 404); - } - } catch (e) { - console.error(e); - return c.json({ error: 'Failed to set media description.' }, 500); + + const id = c.req.param('id'); + const { description } = result.data; + const upload = dittoUploads.get(id); + + if (!upload) { + return c.json({ error: 'File with specified ID not found.' }, 404); } + dittoUploads.set(id, { + ...upload, + description, + }); + return c.json({ message: 'ok' }, 200); }; diff --git a/src/controllers/api/statuses.ts b/src/controllers/api/statuses.ts index 282698e3..47adc572 100644 --- a/src/controllers/api/statuses.ts +++ b/src/controllers/api/statuses.ts @@ -1,3 +1,4 @@ +import { HTTPException } from '@hono/hono/http-exception'; import { NostrEvent, NSchema as n } from '@nostrify/nostrify'; import ISO6391 from 'iso-639-1'; import 'linkify-plugin-hashtag'; @@ -6,22 +7,22 @@ import { nip19 } from 'nostr-tools'; import { z } from 'zod'; import { type AppController } from '@/app.ts'; -import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts'; -import { addTag, deleteTag } from '@/utils/tags.ts'; -import { asyncReplaceAll } from '@/utils/text.ts'; import { Conf } from '@/config.ts'; import { DittoDB } from '@/db/DittoDB.ts'; +import { DittoUpload, dittoUploads } from '@/DittoUploads.ts'; import { DittoEvent } from '@/interfaces/DittoEvent.ts'; import { getAncestors, getAuthor, getDescendants, getEvent } from '@/queries.ts'; -import { getUnattachedMediaByIds } from '@/db/unattached-media.ts'; +import { addTag, deleteTag } from '@/utils/tags.ts'; +import { asyncReplaceAll } from '@/utils/text.ts'; import { lookupPubkey } from '@/utils/lookup.ts'; -import { renderEventAccounts } from '@/views.ts'; -import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts'; import { Storages } from '@/storages.ts'; import { hydrateEvents, purifyEvent } from '@/storages/hydrate.ts'; import { createEvent, paginated, paginatedList, parseBody, updateListEvent } from '@/utils/api.ts'; import { getInvoice, getLnurl } from '@/utils/lnurl.ts'; import { getZapSplits } from '@/utils/zap-split.ts'; +import { renderEventAccounts } from '@/views.ts'; +import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts'; +import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts'; const createStatusSchema = z.object({ in_reply_to_id: n.id().nullish(), @@ -63,7 +64,6 @@ 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(); const store = c.get('store'); if (!result.success) { @@ -112,10 +112,18 @@ const createStatusController: AppController = async (c) => { tags.push(['l', data.language, 'ISO-639-1']); } - const media = data.media_ids?.length ? await getUnattachedMediaByIds(kysely, data.media_ids) : []; + const media: DittoUpload[] = (data.media_ids ?? []).map((id) => { + const upload = dittoUploads.get(id); - const imeta: string[][] = media.map(({ data }) => { - const values: string[] = data.map((tag) => tag.join(' ')); + if (!upload) { + throw new HTTPException(422, { message: 'Uploaded attachment is no longer available.' }); + } + + return upload; + }); + + const imeta: string[][] = media.map(({ tags }) => { + const values: string[] = tags.map((tag) => tag.join(' ')); return ['imeta', ...values]; }); @@ -165,7 +173,7 @@ const createStatusController: AppController = async (c) => { } const mediaUrls: string[] = media - .map(({ data }) => data.find(([name]) => name === 'url')?.[1]) + .map(({ url }) => url) .filter((url): url is string => Boolean(url)); const quoteCompat = data.quote_id ? `\n\nnostr:${nip19.noteEncode(data.quote_id)}` : ''; diff --git a/src/db/DittoTables.ts b/src/db/DittoTables.ts index 69356649..09bf3e43 100644 --- a/src/db/DittoTables.ts +++ b/src/db/DittoTables.ts @@ -1,6 +1,5 @@ export interface DittoTables { nip46_tokens: NIP46TokenRow; - unattached_media: UnattachedMediaRow; author_stats: AuthorStatsRow; event_stats: EventStatsRow; pubkey_domains: PubkeyDomainRow; @@ -33,14 +32,6 @@ interface NIP46TokenRow { connected_at: Date; } -interface UnattachedMediaRow { - id: string; - pubkey: string; - url: string; - data: string; - uploaded_at: number; -} - interface PubkeyDomainRow { pubkey: string; domain: string; diff --git a/src/db/migrations/031_rm_unattached_media.ts b/src/db/migrations/031_rm_unattached_media.ts new file mode 100644 index 00000000..febd85e1 --- /dev/null +++ b/src/db/migrations/031_rm_unattached_media.ts @@ -0,0 +1,34 @@ +import { Kysely } from 'kysely'; + +export async function up(db: Kysely): Promise { + await db.schema.dropTable('unattached_media').execute(); +} + +export async function down(db: Kysely): Promise { + await db.schema + .createTable('unattached_media') + .addColumn('id', 'text', (c) => c.primaryKey()) + .addColumn('pubkey', 'text', (c) => c.notNull()) + .addColumn('url', 'text', (c) => c.notNull()) + .addColumn('data', 'text', (c) => c.notNull()) + .addColumn('uploaded_at', 'bigint', (c) => c.notNull()) + .execute(); + + await db.schema + .createIndex('unattached_media_id') + .on('unattached_media') + .column('id') + .execute(); + + await db.schema + .createIndex('unattached_media_pubkey') + .on('unattached_media') + .column('pubkey') + .execute(); + + await db.schema + .createIndex('unattached_media_url') + .on('unattached_media') + .column('url') + .execute(); +} diff --git a/src/db/unattached-media.ts b/src/db/unattached-media.ts deleted file mode 100644 index 260a9d5a..00000000 --- a/src/db/unattached-media.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { Kysely } from 'kysely'; - -import { DittoDB } from '@/db/DittoDB.ts'; -import { DittoTables } from '@/db/DittoTables.ts'; - -interface UnattachedMedia { - id: string; - pubkey: string; - url: string; - /** NIP-94 tags. */ - data: string[][]; - uploaded_at: number; -} - -/** Add unattached media into the database. */ -async function insertUnattachedMedia(media: UnattachedMedia) { - const { kysely } = await DittoDB.getInstance(); - await kysely.insertInto('unattached_media') - .values({ ...media, data: JSON.stringify(media.data) }) - .execute(); - - return media; -} - -/** Select query for unattached media. */ -function selectUnattachedMediaQuery(kysely: Kysely) { - return kysely.selectFrom('unattached_media') - .select([ - 'unattached_media.id', - 'unattached_media.pubkey', - 'unattached_media.url', - 'unattached_media.data', - 'unattached_media.uploaded_at', - ]); -} - -/** Delete unattached media by URL. */ -async function deleteUnattachedMediaByUrl(url: string) { - const { kysely } = await DittoDB.getInstance(); - return kysely.deleteFrom('unattached_media') - .where('url', '=', url) - .execute(); -} - -/** Get unattached media by IDs. */ -async function getUnattachedMediaByIds(kysely: Kysely, ids: string[]): Promise { - if (!ids.length) return []; - - const results = await selectUnattachedMediaQuery(kysely) - .where('id', 'in', ids) - .execute(); - - return results.map((row) => ({ - ...row, - data: JSON.parse(row.data), - })); -} - -async function setMediaDescription(id: string, desc = '') { - const { kysely } = await DittoDB.getInstance(); - const existing = await selectUnattachedMediaQuery(kysely).where('id', '=', id).executeTakeFirst(); - if (!existing) return false; - const parsed = (await JSON.parse(existing.data) as string[][]).filter((itm) => itm[0] !== 'alt'); - parsed.push(['alt', desc]); - await kysely.updateTable('unattached_media') - .set({ data: JSON.stringify(parsed) }) - .execute(); - return true; -} - -/** Delete rows as an event with media is being created. */ -async function deleteAttachedMedia(pubkey: string, urls: string[]): Promise { - if (!urls.length) return; - const { kysely } = await DittoDB.getInstance(); - await kysely.deleteFrom('unattached_media') - .where('pubkey', '=', pubkey) - .where('url', 'in', urls) - .execute(); -} - -export { - deleteAttachedMedia, - deleteUnattachedMediaByUrl, - getUnattachedMediaByIds, - insertUnattachedMedia, - setMediaDescription, - type UnattachedMedia, -}; diff --git a/src/pipeline.ts b/src/pipeline.ts index 15c1ef34..dd59cb8d 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -6,7 +6,6 @@ import { z } from 'zod'; import { Conf } from '@/config.ts'; import { DittoDB } from '@/db/DittoDB.ts'; -import { deleteAttachedMedia } from '@/db/unattached-media.ts'; import { DittoEvent } from '@/interfaces/DittoEvent.ts'; import { pipelineEventsCounter, policyEventsCounter } from '@/metrics.ts'; import { RelayError } from '@/RelayError.ts'; @@ -61,7 +60,6 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise { +): Promise { const uploader = c.get('uploader'); if (!uploader) { throw new HTTPException(500, { @@ -36,11 +36,15 @@ export async function uploadFile( tags.push(['alt', description]); } - return insertUnattachedMedia({ + const upload = { id: crypto.randomUUID(), - pubkey, url, - data: tags, - uploaded_at: Date.now(), - }); + tags, + pubkey, + uploadedAt: new Date(), + }; + + dittoUploads.set(upload.id, upload); + + return upload; } diff --git a/src/views/mastodon/attachments.ts b/src/views/mastodon/attachments.ts index 9320f604..4e9401fd 100644 --- a/src/views/mastodon/attachments.ts +++ b/src/views/mastodon/attachments.ts @@ -3,9 +3,9 @@ import { getUrlMediaType } from '@/utils/media.ts'; /** Render Mastodon media attachment. */ function renderAttachment( - media: { id?: string; data: string[][] }, + media: { id?: string; tags: string[][] }, ): (MastodonAttachment & { cid?: string }) | undefined { - const { id, data: tags } = media; + const { id, tags } = media; const url = tags.find(([name]) => name === 'url')?.[1]; diff --git a/src/views/mastodon/statuses.ts b/src/views/mastodon/statuses.ts index bc42765b..e21c9e1c 100644 --- a/src/views/mastodon/statuses.ts +++ b/src/views/mastodon/statuses.ts @@ -125,9 +125,9 @@ async function renderStatus(event: DittoEvent, opts: RenderStatusOpts): Promise< pinned: Boolean(pinEvent), reblog: null, application: null, - media_attachments: media.map((m) => renderAttachment({ data: m })).filter((m): m is MastodonAttachment => - Boolean(m) - ), + media_attachments: media + .map((m) => renderAttachment({ tags: m })) + .filter((m): m is MastodonAttachment => Boolean(m)), mentions, tags: [], emojis: renderEmojis(event), From 99a25e1e18c502d7d6abb227f99333a87f99a991 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 7 Sep 2024 10:32:14 -0500 Subject: [PATCH 2/2] media: fix setting description --- src/DittoUploads.ts | 1 - src/controllers/api/media.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/DittoUploads.ts b/src/DittoUploads.ts index 044d3585..0024b1bf 100644 --- a/src/DittoUploads.ts +++ b/src/DittoUploads.ts @@ -5,7 +5,6 @@ import { Time } from '@/utils/time.ts'; export interface DittoUpload { id: string; pubkey: string; - description?: string; url: string; tags: string[][]; uploadedAt: Date; diff --git a/src/controllers/api/media.ts b/src/controllers/api/media.ts index 9dc2de27..7dc398ca 100644 --- a/src/controllers/api/media.ts +++ b/src/controllers/api/media.ts @@ -54,7 +54,7 @@ const updateMediaController: AppController = async (c) => { dittoUploads.set(id, { ...upload, - description, + tags: upload.tags.filter(([name]) => name !== 'alt').concat([['alt', description]]), }); return c.json({ message: 'ok' }, 200);