imeta: don't get attachment ID from a tag

This commit is contained in:
Alex Gleason 2024-05-18 15:36:17 -05:00
parent b1b341d3b8
commit c8b999a1f7
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 10 additions and 19 deletions

View File

@ -1,5 +1,5 @@
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { insertUnattachedMedia } from '@/db/unattached-media.ts'; import { insertUnattachedMedia, UnattachedMedia } from '@/db/unattached-media.ts';
import { configUploader as uploader } from '@/uploaders/config.ts'; import { configUploader as uploader } from '@/uploaders/config.ts';
interface FileMeta { interface FileMeta {
@ -8,7 +8,7 @@ interface FileMeta {
} }
/** Upload a file, track it in the database, and return the resulting media object. */ /** Upload a file, track it in the database, and return the resulting media object. */
async function uploadFile(file: File, meta: FileMeta, signal?: AbortSignal): Promise<string[][]> { async function uploadFile(file: File, meta: FileMeta, signal?: AbortSignal): Promise<UnattachedMedia> {
const { type, size } = file; const { type, size } = file;
const { pubkey, description } = meta; const { pubkey, description } = meta;
@ -36,18 +36,13 @@ async function uploadFile(file: File, meta: FileMeta, signal?: AbortSignal): Pro
data.push(['alt', description]); data.push(['alt', description]);
} }
const uuid = crypto.randomUUID(); return insertUnattachedMedia({
data.push(['uuid', uuid]); id: crypto.randomUUID(),
await insertUnattachedMedia({
id: uuid,
pubkey, pubkey,
url, url,
data, data,
uploaded_at: Date.now(), uploaded_at: Date.now(),
}); });
return data;
} }
export { uploadFile }; export { uploadFile };

View File

@ -1,21 +1,17 @@
import * as TypeFest from 'type-fest'; /** Render Mastodon media attachment. */
function renderAttachment(media: { id?: string; data: string[][] }) {
const { id, data: tags } = media;
import { UnattachedMedia } from '@/db/unattached-media.ts';
type DittoAttachment = TypeFest.SetOptional<UnattachedMedia, 'id' | 'pubkey' | 'uploaded_at'>;
function renderAttachment(tags: string[][]) {
const m = tags.find(([name]) => name === 'm')?.[1]; const m = tags.find(([name]) => name === 'm')?.[1];
const url = tags.find(([name]) => name === 'url')?.[1]; const url = tags.find(([name]) => name === 'url')?.[1];
const alt = tags.find(([name]) => name === 'alt')?.[1]; const alt = tags.find(([name]) => name === 'alt')?.[1];
const cid = tags.find(([name]) => name === 'cid')?.[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]; const blurhash = tags.find(([name]) => name === 'blurhash')?.[1];
if (!url) return; if (!url) return;
return { return {
id: uuid ?? url, id: id ?? url,
type: getAttachmentType(m ?? ''), type: getAttachmentType(m ?? ''),
url, url,
preview_url: url, preview_url: url,
@ -40,4 +36,4 @@ function getAttachmentType(mime: string): string {
} }
} }
export { type DittoAttachment, renderAttachment }; export { renderAttachment };

View File

@ -106,7 +106,7 @@ async function renderStatus(event: DittoEvent, opts: RenderStatusOpts): Promise<
pinned: Boolean(pinEvent), pinned: Boolean(pinEvent),
reblog: null, reblog: null,
application: null, application: null,
media_attachments: media.map(renderAttachment).filter(Boolean), media_attachments: media.map((m) => renderAttachment({ data: m })).filter(Boolean),
mentions, mentions,
tags: [], tags: [],
emojis: renderEmojis(event), emojis: renderEmojis(event),