media: add attachment view, unify types

This commit is contained in:
Alex Gleason
2023-09-09 21:33:12 -05:00
parent cf9a754b02
commit 43499f2dfd
6 changed files with 82 additions and 65 deletions

34
src/views/attachment.ts Normal file
View File

@@ -0,0 +1,34 @@
import { UnattachedMedia } from '@/db/unattached-media.ts';
import { type TypeFest } from '@/deps.ts';
type DittoAttachment = TypeFest.SetOptional<UnattachedMedia, 'id' | 'pubkey' | 'uploaded_at'>;
function renderAttachment(media: DittoAttachment) {
const { id, data, url } = media;
return {
id: id ?? url ?? data.cid,
type: getAttachmentType(data.mime ?? ''),
url,
preview_url: url,
remote_url: null,
description: data.description ?? '',
blurhash: data.blurhash || null,
cid: data.cid,
};
}
/** MIME to Mastodon API `Attachment` type. */
function getAttachmentType(mime: string): string {
const [type] = mime.split('/');
switch (type) {
case 'image':
case 'video':
case 'audio':
return type;
default:
return 'unknown';
}
}
export { type DittoAttachment, renderAttachment };