diff --git a/database/entities/Attachment.ts b/database/entities/Attachment.ts index 8c16acf5..319cb304 100644 --- a/database/entities/Attachment.ts +++ b/database/entities/Attachment.ts @@ -4,6 +4,7 @@ import { MediaBackendType } from "media-manager"; import type { APIAsyncAttachment } from "~types/entities/async_attachment"; import type { APIAttachment } from "~types/entities/attachment"; import type * as Lysand from "lysand-types"; +import { client } from "~database/datasource"; export const attachmentToAPI = ( attachment: Attachment, @@ -80,6 +81,28 @@ export const attachmentToLysand = ( }; }; +export const attachmentFromLysand = async ( + attachment: Lysand.ContentFormat, +): Promise => { + const key = Object.keys(attachment)[0]; + const value = attachment[key]; + + return await client.attachment.create({ + data: { + url: value.content, + description: value.description || undefined, + duration: value.duration || undefined, + fps: value.fps || undefined, + height: value.height || undefined, + size: value.size || undefined, + width: value.width || undefined, + sha256: value.hash?.sha256 || undefined, + mime_type: key, + blurhash: value.blurhash || undefined, + }, + }); +}; + export const getUrl = (name: string, config: Config) => { if (config.media.backend === MediaBackendType.LOCAL) { return new URL(`/media/${name}`, config.http.base_url).toString(); diff --git a/database/entities/Status.ts b/database/entities/Status.ts index e330dd1e..73155639 100644 --- a/database/entities/Status.ts +++ b/database/entities/Status.ts @@ -7,6 +7,7 @@ import { type Relationship, type Status, type User, + type Attachment, } from "@prisma/client"; import { sanitizeHtml } from "@sanitization"; import { config } from "config-manager"; @@ -20,7 +21,11 @@ import type { APIStatus } from "~types/entities/status"; import type { Note } from "~types/lysand/Object"; import type * as Lysand from "lysand-types"; import { applicationToAPI } from "./Application"; -import { attachmentToAPI, attachmentToLysand } from "./Attachment"; +import { + attachmentFromLysand, + attachmentToAPI, + attachmentToLysand, +} from "./Attachment"; import { emojiToAPI, emojiToLysand, parseEmojis } from "./Emoji"; import type { UserWithRelations } from "./User"; import { getUserUri, resolveUser, resolveWebFinger, userToAPI } from "./User"; @@ -111,6 +116,17 @@ export const resolveStatus = async ( throw new Error("Invalid object author"); } + const attachments = ( + await Promise.all( + (note.attachments ?? []).map((attachment) => + attachmentFromLysand(attachment).catch((e) => { + console.error(e); + return null; + }), + ), + ) + ).filter((attachment) => attachment !== null) as Attachment[]; + return await createNewStatus( author, note.content ?? { @@ -130,8 +146,7 @@ export const resolveStatus = async ( (mention) => mention !== null, ) as Promise[], ), - // TODO: Add attachments - [], + attachments.map((a) => a.id), note.replies_to ? await resolveStatus(note.replies_to) : undefined, note.quotes ? await resolveStatus(note.quotes) : undefined, );