2023-11-11 03:36:06 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
2023-09-12 22:48:10 +02:00
|
|
|
import { getConfig } from "@config";
|
2023-11-23 05:10:37 +01:00
|
|
|
import type { UserWithRelations } from "./User";
|
2023-09-12 22:48:10 +02:00
|
|
|
import {
|
2023-11-11 03:36:06 +01:00
|
|
|
fetchRemoteUser,
|
|
|
|
|
parseMentionsUris,
|
|
|
|
|
userRelations,
|
|
|
|
|
userToAPI,
|
|
|
|
|
} from "./User";
|
|
|
|
|
import { client } from "~database/datasource";
|
2023-11-23 05:10:37 +01:00
|
|
|
import type { LysandPublication, Note } from "~types/lysand/Object";
|
2023-11-04 04:34:31 +01:00
|
|
|
import { htmlToText } from "html-to-text";
|
2023-11-05 00:59:55 +01:00
|
|
|
import { getBestContentType } from "@content_types";
|
2023-11-27 01:56:16 +01:00
|
|
|
import {
|
|
|
|
|
Prisma,
|
|
|
|
|
type Application,
|
|
|
|
|
type Emoji,
|
|
|
|
|
type Relationship,
|
|
|
|
|
type Status,
|
|
|
|
|
type User,
|
2023-11-11 03:36:06 +01:00
|
|
|
} from "@prisma/client";
|
|
|
|
|
import { emojiToAPI, emojiToLysand, parseEmojis } from "./Emoji";
|
2023-11-23 05:10:37 +01:00
|
|
|
import type { APIStatus } from "~types/entities/status";
|
2023-11-11 03:36:06 +01:00
|
|
|
import { applicationToAPI } from "./Application";
|
2023-11-29 00:54:39 +01:00
|
|
|
import { attachmentToAPI } from "./Attachment";
|
|
|
|
|
import type { APIAttachment } from "~types/entities/attachment";
|
2023-12-03 05:11:30 +01:00
|
|
|
import { sanitizeHtml } from "@sanitization";
|
|
|
|
|
import { parse } from "marked";
|
|
|
|
|
import linkifyStr from "linkify-string";
|
|
|
|
|
import linkifyHtml from "linkify-html";
|
2023-09-12 22:48:10 +02:00
|
|
|
|
|
|
|
|
const config = getConfig();
|
|
|
|
|
|
2023-11-27 01:56:16 +01:00
|
|
|
export const statusAndUserRelations: Prisma.StatusInclude = {
|
2023-11-11 03:36:06 +01:00
|
|
|
author: {
|
|
|
|
|
include: userRelations,
|
|
|
|
|
},
|
|
|
|
|
application: true,
|
|
|
|
|
emojis: true,
|
|
|
|
|
inReplyToPost: {
|
|
|
|
|
include: {
|
|
|
|
|
author: {
|
|
|
|
|
include: userRelations,
|
|
|
|
|
},
|
|
|
|
|
application: true,
|
|
|
|
|
emojis: true,
|
|
|
|
|
inReplyToPost: {
|
|
|
|
|
include: {
|
|
|
|
|
author: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
instance: true,
|
|
|
|
|
mentions: true,
|
|
|
|
|
pinnedBy: true,
|
2023-11-12 02:37:14 +01:00
|
|
|
_count: {
|
|
|
|
|
select: {
|
|
|
|
|
replies: true,
|
2023-11-11 03:36:06 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-11-29 00:54:39 +01:00
|
|
|
reblogs: true,
|
2023-11-27 01:56:16 +01:00
|
|
|
attachments: true,
|
2023-11-11 03:36:06 +01:00
|
|
|
instance: true,
|
2023-11-28 23:57:48 +01:00
|
|
|
mentions: {
|
|
|
|
|
include: userRelations,
|
|
|
|
|
},
|
2023-11-11 03:36:06 +01:00
|
|
|
pinnedBy: true,
|
2023-11-12 02:37:14 +01:00
|
|
|
_count: {
|
|
|
|
|
select: {
|
|
|
|
|
replies: true,
|
|
|
|
|
likes: true,
|
2023-11-12 09:28:06 +01:00
|
|
|
reblogs: true,
|
2023-11-11 03:36:06 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
reblog: {
|
|
|
|
|
include: {
|
|
|
|
|
author: {
|
|
|
|
|
include: userRelations,
|
|
|
|
|
},
|
|
|
|
|
application: true,
|
|
|
|
|
emojis: true,
|
|
|
|
|
inReplyToPost: {
|
|
|
|
|
include: {
|
|
|
|
|
author: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
instance: true,
|
2023-11-29 00:16:22 +01:00
|
|
|
mentions: {
|
|
|
|
|
include: userRelations,
|
|
|
|
|
},
|
2023-11-11 03:36:06 +01:00
|
|
|
pinnedBy: true,
|
2023-11-12 02:37:14 +01:00
|
|
|
_count: {
|
|
|
|
|
select: {
|
|
|
|
|
replies: true,
|
2023-11-11 03:36:06 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
quotingPost: {
|
|
|
|
|
include: {
|
|
|
|
|
author: {
|
|
|
|
|
include: userRelations,
|
|
|
|
|
},
|
|
|
|
|
application: true,
|
|
|
|
|
emojis: true,
|
|
|
|
|
inReplyToPost: {
|
|
|
|
|
include: {
|
|
|
|
|
author: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
instance: true,
|
|
|
|
|
mentions: true,
|
|
|
|
|
pinnedBy: true,
|
2023-11-12 02:37:14 +01:00
|
|
|
_count: {
|
|
|
|
|
select: {
|
|
|
|
|
replies: true,
|
2023-11-11 03:36:06 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
likes: {
|
|
|
|
|
include: {
|
|
|
|
|
liker: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-27 01:56:16 +01:00
|
|
|
const statusRelations = Prisma.validator<Prisma.StatusDefaultArgs>()({
|
|
|
|
|
include: statusAndUserRelations,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export type StatusWithRelations = Prisma.StatusGetPayload<
|
|
|
|
|
typeof statusRelations
|
|
|
|
|
>;
|
2023-10-23 07:39:42 +02:00
|
|
|
|
2023-09-12 22:48:10 +02:00
|
|
|
/**
|
2023-09-28 20:19:21 +02:00
|
|
|
* Represents a status (i.e. a post)
|
2023-09-12 22:48:10 +02:00
|
|
|
*/
|
2023-11-05 00:59:55 +01:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
/**
|
|
|
|
|
* Returns whether this status is viewable by a user.
|
|
|
|
|
* @param user The user to check.
|
|
|
|
|
* @returns Whether this status is viewable by the user.
|
|
|
|
|
*/
|
|
|
|
|
export const isViewableByUser = (status: Status, user: User | null) => {
|
2023-11-12 07:39:59 +01:00
|
|
|
if (status.authorId === user?.id) return true;
|
2023-11-11 03:36:06 +01:00
|
|
|
if (status.visibility === "public") return true;
|
|
|
|
|
else if (status.visibility === "unlisted") return true;
|
|
|
|
|
else if (status.visibility === "private") {
|
|
|
|
|
// @ts-expect-error Prisma TypeScript types dont include relations
|
|
|
|
|
return !!(user?.relationships as Relationship[]).find(
|
|
|
|
|
rel => rel.id === status.authorId
|
2023-09-29 01:58:05 +02:00
|
|
|
);
|
2023-11-11 03:36:06 +01:00
|
|
|
} else {
|
|
|
|
|
// @ts-expect-error Prisma TypeScript types dont include relations
|
|
|
|
|
return user && (status.mentions as User[]).includes(user);
|
2023-09-29 01:58:05 +02:00
|
|
|
}
|
2023-11-11 03:36:06 +01:00
|
|
|
};
|
2023-09-29 01:58:05 +02:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
export const fetchFromRemote = async (uri: string): Promise<Status | null> => {
|
|
|
|
|
// Check if already in database
|
2023-11-05 00:59:55 +01:00
|
|
|
|
2023-11-12 02:37:14 +01:00
|
|
|
const existingStatus: StatusWithRelations | null =
|
|
|
|
|
await client.status.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
uri: uri,
|
|
|
|
|
},
|
|
|
|
|
include: statusAndUserRelations,
|
|
|
|
|
});
|
2023-11-05 00:59:55 +01:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
if (existingStatus) return existingStatus;
|
2023-11-05 00:59:55 +01:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
const status = await fetch(uri);
|
2023-11-05 00:59:55 +01:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
if (status.status === 404) return null;
|
2023-11-05 00:59:55 +01:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
const body = (await status.json()) as LysandPublication;
|
2023-11-05 00:59:55 +01:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
const content = getBestContentType(body.contents);
|
2023-11-05 00:59:55 +01:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
const emojis = await parseEmojis(content?.content || "");
|
2023-11-05 00:59:55 +01:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
const author = await fetchRemoteUser(body.author);
|
2023-11-05 00:59:55 +01:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
let replyStatus: Status | null = null;
|
|
|
|
|
let quotingStatus: Status | null = null;
|
2023-11-05 00:59:55 +01:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
if (body.replies_to.length > 0) {
|
|
|
|
|
replyStatus = await fetchFromRemote(body.replies_to[0]);
|
2023-11-05 00:59:55 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
if (body.quotes.length > 0) {
|
|
|
|
|
quotingStatus = await fetchFromRemote(body.quotes[0]);
|
2023-10-23 03:32:01 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-12 02:37:14 +01:00
|
|
|
return await createNewStatus({
|
2023-11-11 03:36:06 +01:00
|
|
|
account: author,
|
|
|
|
|
content: content?.content || "",
|
|
|
|
|
content_type: content?.content_type,
|
|
|
|
|
application: null,
|
|
|
|
|
// TODO: Add visibility
|
|
|
|
|
visibility: "public",
|
|
|
|
|
spoiler_text: body.subject || "",
|
|
|
|
|
uri: body.uri,
|
|
|
|
|
sensitive: body.is_sensitive,
|
|
|
|
|
emojis: emojis,
|
|
|
|
|
mentions: await parseMentionsUris(body.mentions),
|
|
|
|
|
reply: replyStatus
|
|
|
|
|
? {
|
|
|
|
|
status: replyStatus,
|
|
|
|
|
user: (replyStatus as any).author,
|
|
|
|
|
}
|
|
|
|
|
: undefined,
|
|
|
|
|
quote: quotingStatus || undefined,
|
|
|
|
|
});
|
|
|
|
|
};
|
2023-10-23 03:32:01 +02:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
/**
|
|
|
|
|
* Return all the ancestors of this post,
|
|
|
|
|
*/
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-unused-vars
|
2023-11-12 02:37:14 +01:00
|
|
|
export const getAncestors = async (
|
|
|
|
|
status: StatusWithRelations,
|
|
|
|
|
fetcher: UserWithRelations | null
|
|
|
|
|
) => {
|
|
|
|
|
const ancestors: StatusWithRelations[] = [];
|
|
|
|
|
|
|
|
|
|
let currentStatus = status;
|
|
|
|
|
|
|
|
|
|
while (currentStatus.inReplyToPostId) {
|
|
|
|
|
const parent = await client.status.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
id: currentStatus.inReplyToPostId,
|
|
|
|
|
},
|
|
|
|
|
include: statusAndUserRelations,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!parent) break;
|
|
|
|
|
|
|
|
|
|
ancestors.push(parent);
|
|
|
|
|
|
|
|
|
|
currentStatus = parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Filter for posts that are viewable by the user
|
|
|
|
|
|
|
|
|
|
const viewableAncestors = ancestors.filter(ancestor =>
|
|
|
|
|
isViewableByUser(ancestor, fetcher)
|
|
|
|
|
);
|
|
|
|
|
return viewableAncestors;
|
2023-11-11 03:36:06 +01:00
|
|
|
};
|
2023-09-22 03:09:14 +02:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
/**
|
2023-11-12 02:37:14 +01:00
|
|
|
* Return all the descendants of this post (recursive)
|
|
|
|
|
* Temporary implementation, will be replaced with a recursive SQL query when Prisma adds support for it
|
2023-11-11 03:36:06 +01:00
|
|
|
*/
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-unused-vars
|
2023-11-12 02:37:14 +01:00
|
|
|
export const getDescendants = async (
|
|
|
|
|
status: StatusWithRelations,
|
|
|
|
|
fetcher: UserWithRelations | null,
|
|
|
|
|
depth = 0
|
|
|
|
|
) => {
|
|
|
|
|
const descendants: StatusWithRelations[] = [];
|
|
|
|
|
|
|
|
|
|
const currentStatus = status;
|
|
|
|
|
|
|
|
|
|
// Fetch all children of children of children recursively calling getDescendants
|
|
|
|
|
|
|
|
|
|
const children = await client.status.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
inReplyToPostId: currentStatus.id,
|
|
|
|
|
},
|
|
|
|
|
include: statusAndUserRelations,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (const child of children) {
|
|
|
|
|
descendants.push(child);
|
|
|
|
|
|
|
|
|
|
if (depth < 20) {
|
|
|
|
|
const childDescendants = await getDescendants(
|
|
|
|
|
child,
|
|
|
|
|
fetcher,
|
|
|
|
|
depth + 1
|
|
|
|
|
);
|
|
|
|
|
descendants.push(...childDescendants);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Filter for posts that are viewable by the user
|
|
|
|
|
|
|
|
|
|
const viewableDescendants = descendants.filter(descendant =>
|
|
|
|
|
isViewableByUser(descendant, fetcher)
|
|
|
|
|
);
|
|
|
|
|
return viewableDescendants;
|
2023-11-11 03:36:06 +01:00
|
|
|
};
|
2023-09-22 03:09:14 +02:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
/**
|
|
|
|
|
* Creates a new status and saves it to the database.
|
|
|
|
|
* @param data The data for the new status.
|
|
|
|
|
* @returns A promise that resolves with the new status.
|
|
|
|
|
*/
|
2023-11-12 02:37:14 +01:00
|
|
|
export const createNewStatus = async (data: {
|
2023-11-11 03:36:06 +01:00
|
|
|
account: User;
|
|
|
|
|
application: Application | null;
|
|
|
|
|
content: string;
|
|
|
|
|
visibility: APIStatus["visibility"];
|
|
|
|
|
sensitive: boolean;
|
|
|
|
|
spoiler_text: string;
|
2023-12-03 05:11:30 +01:00
|
|
|
emojis?: Emoji[];
|
2023-11-11 03:36:06 +01:00
|
|
|
content_type?: string;
|
|
|
|
|
uri?: string;
|
|
|
|
|
mentions?: User[];
|
2023-11-23 00:40:31 +01:00
|
|
|
media_attachments?: string[];
|
2023-11-11 03:36:06 +01:00
|
|
|
reply?: {
|
|
|
|
|
status: Status;
|
|
|
|
|
user: User;
|
|
|
|
|
};
|
|
|
|
|
quote?: Status;
|
|
|
|
|
}) => {
|
2023-11-28 23:57:48 +01:00
|
|
|
// Get people mentioned in the content (match @username or @username@domain.com mentions)
|
|
|
|
|
const mentionedPeople =
|
|
|
|
|
data.content.match(/@[a-zA-Z0-9_]+(@[a-zA-Z0-9_]+)?/g) ?? [];
|
2023-10-23 03:32:01 +02:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
let mentions = data.mentions || [];
|
2023-09-22 03:09:14 +02:00
|
|
|
|
2023-12-03 05:11:30 +01:00
|
|
|
// TODO: Parse emojis
|
|
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
// Get list of mentioned users
|
|
|
|
|
if (mentions.length === 0) {
|
|
|
|
|
mentions = await client.user.findMany({
|
2023-10-28 22:21:04 +02:00
|
|
|
where: {
|
2023-11-11 03:36:06 +01:00
|
|
|
OR: mentionedPeople.map(person => ({
|
|
|
|
|
username: person.split("@")[1],
|
|
|
|
|
instance: {
|
|
|
|
|
base_url: person.split("@")[2],
|
|
|
|
|
},
|
|
|
|
|
})),
|
2023-10-28 22:21:04 +02:00
|
|
|
},
|
2023-11-11 03:36:06 +01:00
|
|
|
include: userRelations,
|
2023-10-28 22:21:04 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-03 05:11:30 +01:00
|
|
|
let formattedContent;
|
|
|
|
|
|
|
|
|
|
// Get HTML version of content
|
|
|
|
|
if (data.content_type === "text/markdown") {
|
|
|
|
|
formattedContent = linkifyHtml(await sanitizeHtml(parse(data.content)));
|
|
|
|
|
} else if (data.content_type === "text/x.misskeymarkdown") {
|
|
|
|
|
// Parse as MFM
|
|
|
|
|
} else {
|
|
|
|
|
// Parse as plaintext
|
|
|
|
|
formattedContent = linkifyStr(data.content);
|
|
|
|
|
|
|
|
|
|
// Split by newline and add <p> tags
|
|
|
|
|
formattedContent = formattedContent
|
|
|
|
|
.split("\n")
|
|
|
|
|
.map(line => `<p>${line}</p>`)
|
|
|
|
|
.join("\n");
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-12 07:39:59 +01:00
|
|
|
let status = await client.status.create({
|
2023-11-11 03:36:06 +01:00
|
|
|
data: {
|
|
|
|
|
authorId: data.account.id,
|
|
|
|
|
applicationId: data.application?.id,
|
2023-12-03 05:11:30 +01:00
|
|
|
content: formattedContent,
|
|
|
|
|
contentSource: data.content,
|
2023-11-11 03:36:06 +01:00
|
|
|
contentType: data.content_type,
|
|
|
|
|
visibility: data.visibility,
|
|
|
|
|
sensitive: data.sensitive,
|
|
|
|
|
spoilerText: data.spoiler_text,
|
|
|
|
|
emojis: {
|
2023-12-03 05:11:30 +01:00
|
|
|
connect: data.emojis?.map(emoji => {
|
2023-11-11 03:36:06 +01:00
|
|
|
return {
|
|
|
|
|
id: emoji.id,
|
|
|
|
|
};
|
|
|
|
|
}),
|
2023-10-23 07:39:42 +02:00
|
|
|
},
|
2023-11-23 00:40:31 +01:00
|
|
|
attachments: data.media_attachments
|
|
|
|
|
? {
|
|
|
|
|
connect: data.media_attachments.map(attachment => {
|
|
|
|
|
return {
|
|
|
|
|
id: attachment,
|
|
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
: undefined,
|
2023-11-11 03:36:06 +01:00
|
|
|
inReplyToPostId: data.reply?.status.id,
|
|
|
|
|
quotingPostId: data.quote?.id,
|
|
|
|
|
instanceId: data.account.instanceId || undefined,
|
|
|
|
|
isReblog: false,
|
2023-11-12 07:39:59 +01:00
|
|
|
uri:
|
|
|
|
|
data.uri ||
|
|
|
|
|
`${config.http.base_url}/statuses/FAKE-${crypto.randomUUID()}`,
|
2023-11-11 03:36:06 +01:00
|
|
|
mentions: {
|
|
|
|
|
connect: mentions.map(mention => {
|
|
|
|
|
return {
|
|
|
|
|
id: mention.id,
|
|
|
|
|
};
|
|
|
|
|
}),
|
2023-10-23 07:39:42 +02:00
|
|
|
},
|
2023-11-11 03:36:06 +01:00
|
|
|
},
|
|
|
|
|
include: statusAndUserRelations,
|
|
|
|
|
});
|
2023-10-23 07:39:42 +02:00
|
|
|
|
2023-11-12 07:39:59 +01:00
|
|
|
// Update URI
|
|
|
|
|
status = await client.status.update({
|
|
|
|
|
where: {
|
|
|
|
|
id: status.id,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
uri: data.uri || `${config.http.base_url}/statuses/${status.id}`,
|
|
|
|
|
},
|
|
|
|
|
include: statusAndUserRelations,
|
|
|
|
|
});
|
2023-10-28 22:21:04 +02:00
|
|
|
|
2023-11-23 19:35:43 +01:00
|
|
|
// Create notification
|
|
|
|
|
|
|
|
|
|
if (status.inReplyToPost) {
|
|
|
|
|
await client.notification.create({
|
|
|
|
|
data: {
|
|
|
|
|
notifiedId: status.inReplyToPost.authorId,
|
|
|
|
|
accountId: status.authorId,
|
|
|
|
|
type: "mention",
|
|
|
|
|
statusId: status.id,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
return status;
|
|
|
|
|
};
|
2023-10-28 22:21:04 +02:00
|
|
|
|
2023-12-03 05:11:30 +01:00
|
|
|
export const editStatus = async (
|
|
|
|
|
status: StatusWithRelations,
|
|
|
|
|
data: {
|
|
|
|
|
content: string;
|
|
|
|
|
visibility?: APIStatus["visibility"];
|
|
|
|
|
sensitive: boolean;
|
|
|
|
|
spoiler_text: string;
|
|
|
|
|
emojis?: Emoji[];
|
|
|
|
|
content_type?: string;
|
|
|
|
|
uri?: string;
|
|
|
|
|
mentions?: User[];
|
|
|
|
|
media_attachments?: string[];
|
|
|
|
|
}
|
|
|
|
|
) => {
|
|
|
|
|
// Get people mentioned in the content (match @username or @username@domain.com mentions
|
|
|
|
|
const mentionedPeople =
|
|
|
|
|
data.content.match(/@[a-zA-Z0-9_]+(@[a-zA-Z0-9_]+)?/g) ?? [];
|
|
|
|
|
|
|
|
|
|
let mentions = data.mentions || [];
|
|
|
|
|
|
|
|
|
|
// TODO: Parse emojis
|
|
|
|
|
|
|
|
|
|
// Get list of mentioned users
|
|
|
|
|
if (mentions.length === 0) {
|
|
|
|
|
mentions = await client.user.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
OR: mentionedPeople.map(person => ({
|
|
|
|
|
username: person.split("@")[1],
|
|
|
|
|
instance: {
|
|
|
|
|
base_url: person.split("@")[2],
|
|
|
|
|
},
|
|
|
|
|
})),
|
|
|
|
|
},
|
|
|
|
|
include: userRelations,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let formattedContent;
|
|
|
|
|
|
|
|
|
|
// Get HTML version of content
|
|
|
|
|
if (data.content_type === "text/markdown") {
|
|
|
|
|
formattedContent = linkifyHtml(await sanitizeHtml(parse(data.content)));
|
|
|
|
|
} else if (data.content_type === "text/x.misskeymarkdown") {
|
|
|
|
|
// Parse as MFM
|
|
|
|
|
} else {
|
|
|
|
|
// Parse as plaintext
|
|
|
|
|
formattedContent = linkifyStr(data.content);
|
|
|
|
|
|
|
|
|
|
// Split by newline and add <p> tags
|
|
|
|
|
formattedContent = formattedContent
|
|
|
|
|
.split("\n")
|
|
|
|
|
.map(line => `<p>${line}</p>`)
|
|
|
|
|
.join("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const newStatus = await client.status.update({
|
|
|
|
|
where: {
|
|
|
|
|
id: status.id,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
content: formattedContent,
|
|
|
|
|
contentSource: data.content,
|
|
|
|
|
contentType: data.content_type,
|
|
|
|
|
visibility: data.visibility,
|
|
|
|
|
sensitive: data.sensitive,
|
|
|
|
|
spoilerText: data.spoiler_text,
|
|
|
|
|
emojis: {
|
|
|
|
|
connect: data.emojis?.map(emoji => {
|
|
|
|
|
return {
|
|
|
|
|
id: emoji.id,
|
|
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
attachments: data.media_attachments
|
|
|
|
|
? {
|
|
|
|
|
connect: data.media_attachments.map(attachment => {
|
|
|
|
|
return {
|
|
|
|
|
id: attachment,
|
|
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
: undefined,
|
|
|
|
|
mentions: {
|
|
|
|
|
connect: mentions.map(mention => {
|
|
|
|
|
return {
|
|
|
|
|
id: mention.id,
|
|
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
include: statusAndUserRelations,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return newStatus;
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
export const isFavouritedBy = async (status: Status, user: User) => {
|
|
|
|
|
return !!(await client.like.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
likerId: user.id,
|
|
|
|
|
likedId: status.id,
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
};
|
2023-11-04 04:34:31 +01:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
/**
|
|
|
|
|
* Converts this status to an API status.
|
|
|
|
|
* @returns A promise that resolves with the API status.
|
|
|
|
|
*/
|
|
|
|
|
export const statusToAPI = async (
|
|
|
|
|
status: StatusWithRelations,
|
|
|
|
|
user?: UserWithRelations
|
|
|
|
|
): Promise<APIStatus> => {
|
|
|
|
|
return {
|
|
|
|
|
id: status.id,
|
|
|
|
|
in_reply_to_id: status.inReplyToPostId || null,
|
|
|
|
|
in_reply_to_account_id: status.inReplyToPost?.authorId || null,
|
2023-11-27 02:40:44 +01:00
|
|
|
// @ts-expect-error Prisma TypeScript types dont include relations
|
2023-11-20 03:42:40 +01:00
|
|
|
account: userToAPI(status.author),
|
2023-11-11 03:36:06 +01:00
|
|
|
created_at: new Date(status.createdAt).toISOString(),
|
|
|
|
|
application: status.application
|
2023-11-20 03:42:40 +01:00
|
|
|
? applicationToAPI(status.application)
|
2023-11-11 03:36:06 +01:00
|
|
|
: null,
|
|
|
|
|
card: null,
|
|
|
|
|
content: status.content,
|
2023-11-20 03:42:40 +01:00
|
|
|
emojis: status.emojis.map(emoji => emojiToAPI(emoji)),
|
2023-11-12 09:28:06 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
|
|
|
favourited: !!(status.likes ?? []).find(
|
|
|
|
|
like => like.likerId === user?.id
|
|
|
|
|
),
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
|
|
|
favourites_count: (status.likes ?? []).length,
|
2023-11-29 00:54:39 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
|
|
|
media_attachments: (status.attachments ?? []).map(
|
|
|
|
|
a => attachmentToAPI(a) as APIAttachment
|
|
|
|
|
),
|
2023-11-28 23:57:48 +01:00
|
|
|
// @ts-expect-error Prisma TypeScript types dont include relations
|
|
|
|
|
mentions: status.mentions.map(mention => userToAPI(mention)),
|
2023-11-11 03:36:06 +01:00
|
|
|
language: null,
|
|
|
|
|
muted: user
|
|
|
|
|
? user.relationships.find(r => r.subjectId == status.authorId)
|
|
|
|
|
?.muting || false
|
|
|
|
|
: false,
|
2023-11-27 01:56:16 +01:00
|
|
|
pinned: status.pinnedBy.find(u => u.id === user?.id) ? true : false,
|
2023-11-11 03:36:06 +01:00
|
|
|
// TODO: Add pols
|
|
|
|
|
poll: null,
|
|
|
|
|
reblog: status.reblog
|
|
|
|
|
? await statusToAPI(status.reblog as unknown as StatusWithRelations)
|
|
|
|
|
: null,
|
|
|
|
|
reblogged: !!(await client.status.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
authorId: user?.id,
|
|
|
|
|
reblogId: status.id,
|
2023-11-04 04:34:31 +01:00
|
|
|
},
|
2023-11-11 03:36:06 +01:00
|
|
|
})),
|
2023-11-29 00:54:39 +01:00
|
|
|
reblogs_count: status._count.reblogs,
|
2023-11-12 02:37:14 +01:00
|
|
|
replies_count: status._count.replies,
|
2023-11-11 03:36:06 +01:00
|
|
|
sensitive: status.sensitive,
|
|
|
|
|
spoiler_text: status.spoilerText,
|
|
|
|
|
tags: [],
|
|
|
|
|
uri: `${config.http.base_url}/statuses/${status.id}`,
|
|
|
|
|
visibility: "public",
|
|
|
|
|
url: `${config.http.base_url}/statuses/${status.id}`,
|
|
|
|
|
bookmarked: false,
|
2023-11-23 00:45:20 +01:00
|
|
|
quote: status.quotingPost
|
|
|
|
|
? await statusToAPI(
|
|
|
|
|
status.quotingPost as unknown as StatusWithRelations
|
|
|
|
|
)
|
|
|
|
|
: null,
|
|
|
|
|
quote_id: status.quotingPost?.id || undefined,
|
2023-11-11 03:36:06 +01:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const statusToLysand = (status: StatusWithRelations): Note => {
|
|
|
|
|
return {
|
|
|
|
|
type: "Note",
|
|
|
|
|
created_at: new Date(status.createdAt).toISOString(),
|
|
|
|
|
id: status.id,
|
|
|
|
|
author: status.authorId,
|
|
|
|
|
uri: `${config.http.base_url}/users/${status.authorId}/statuses/${status.id}`,
|
|
|
|
|
contents: [
|
|
|
|
|
{
|
|
|
|
|
content: status.content,
|
|
|
|
|
content_type: "text/html",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
// Content converted to plaintext
|
|
|
|
|
content: htmlToText(status.content),
|
|
|
|
|
content_type: "text/plain",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
// TODO: Add attachments
|
|
|
|
|
attachments: [],
|
|
|
|
|
is_sensitive: status.sensitive,
|
|
|
|
|
mentions: status.mentions.map(mention => mention.uri),
|
|
|
|
|
quotes: status.quotingPost ? [status.quotingPost.uri] : [],
|
|
|
|
|
replies_to: status.inReplyToPostId ? [status.inReplyToPostId] : [],
|
|
|
|
|
subject: status.spoilerText,
|
|
|
|
|
extensions: {
|
|
|
|
|
"org.lysand:custom_emojis": {
|
|
|
|
|
emojis: status.emojis.map(emoji => emojiToLysand(emoji)),
|
|
|
|
|
},
|
|
|
|
|
// TODO: Add polls and reactions
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
};
|