Add fetching of attachments when fetching a remote post

This commit is contained in:
Jesse Wierzbinski 2024-04-10 15:31:33 -10:00
parent 5024eee5db
commit ec7b5dcbab
No known key found for this signature in database
2 changed files with 41 additions and 3 deletions

View file

@ -4,6 +4,7 @@ import { MediaBackendType } from "media-manager";
import type { APIAsyncAttachment } from "~types/entities/async_attachment"; import type { APIAsyncAttachment } from "~types/entities/async_attachment";
import type { APIAttachment } from "~types/entities/attachment"; import type { APIAttachment } from "~types/entities/attachment";
import type * as Lysand from "lysand-types"; import type * as Lysand from "lysand-types";
import { client } from "~database/datasource";
export const attachmentToAPI = ( export const attachmentToAPI = (
attachment: Attachment, attachment: Attachment,
@ -80,6 +81,28 @@ export const attachmentToLysand = (
}; };
}; };
export const attachmentFromLysand = async (
attachment: Lysand.ContentFormat,
): Promise<Attachment> => {
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) => { export const getUrl = (name: string, config: Config) => {
if (config.media.backend === MediaBackendType.LOCAL) { if (config.media.backend === MediaBackendType.LOCAL) {
return new URL(`/media/${name}`, config.http.base_url).toString(); return new URL(`/media/${name}`, config.http.base_url).toString();

View file

@ -7,6 +7,7 @@ import {
type Relationship, type Relationship,
type Status, type Status,
type User, type User,
type Attachment,
} from "@prisma/client"; } from "@prisma/client";
import { sanitizeHtml } from "@sanitization"; import { sanitizeHtml } from "@sanitization";
import { config } from "config-manager"; 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 { Note } from "~types/lysand/Object";
import type * as Lysand from "lysand-types"; import type * as Lysand from "lysand-types";
import { applicationToAPI } from "./Application"; import { applicationToAPI } from "./Application";
import { attachmentToAPI, attachmentToLysand } from "./Attachment"; import {
attachmentFromLysand,
attachmentToAPI,
attachmentToLysand,
} from "./Attachment";
import { emojiToAPI, emojiToLysand, parseEmojis } from "./Emoji"; import { emojiToAPI, emojiToLysand, parseEmojis } from "./Emoji";
import type { UserWithRelations } from "./User"; import type { UserWithRelations } from "./User";
import { getUserUri, resolveUser, resolveWebFinger, userToAPI } from "./User"; import { getUserUri, resolveUser, resolveWebFinger, userToAPI } from "./User";
@ -111,6 +116,17 @@ export const resolveStatus = async (
throw new Error("Invalid object author"); 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( return await createNewStatus(
author, author,
note.content ?? { note.content ?? {
@ -130,8 +146,7 @@ export const resolveStatus = async (
(mention) => mention !== null, (mention) => mention !== null,
) as Promise<UserWithRelations>[], ) as Promise<UserWithRelations>[],
), ),
// TODO: Add attachments attachments.map((a) => a.id),
[],
note.replies_to ? await resolveStatus(note.replies_to) : undefined, note.replies_to ? await resolveStatus(note.replies_to) : undefined,
note.quotes ? await resolveStatus(note.quotes) : undefined, note.quotes ? await resolveStatus(note.quotes) : undefined,
); );