2023-11-23 05:10:37 +01:00
|
|
|
import type { APIEmoji } from "~types/entities/emoji";
|
|
|
|
|
import type { Emoji as LysandEmoji } from "~types/lysand/extensions/org.lysand/custom_emojis";
|
2023-11-11 03:36:06 +01:00
|
|
|
import { client } from "~database/datasource";
|
2023-11-23 05:10:37 +01:00
|
|
|
import type { Emoji } from "@prisma/client";
|
2023-09-12 22:48:10 +02:00
|
|
|
|
2023-09-28 20:19:21 +02:00
|
|
|
/**
|
|
|
|
|
* Represents an emoji entity in the database.
|
|
|
|
|
*/
|
2023-09-12 22:48:10 +02:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
/**
|
|
|
|
|
* Used for parsing emojis from local text
|
|
|
|
|
* @param text The text to parse
|
|
|
|
|
* @returns An array of emojis
|
|
|
|
|
*/
|
|
|
|
|
export const parseEmojis = async (text: string): Promise<Emoji[]> => {
|
|
|
|
|
const regex = /:[a-zA-Z0-9_]+:/g;
|
|
|
|
|
const matches = text.match(regex);
|
|
|
|
|
if (!matches) return [];
|
|
|
|
|
return await client.emoji.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
shortcode: {
|
|
|
|
|
in: matches.map(match => match.replace(/:/g, "")),
|
|
|
|
|
},
|
2023-11-12 02:37:14 +01:00
|
|
|
instanceId: null,
|
2023-11-11 03:36:06 +01:00
|
|
|
},
|
|
|
|
|
include: {
|
|
|
|
|
instance: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
2023-09-12 22:48:10 +02:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
export const addEmojiIfNotExists = async (emoji: LysandEmoji) => {
|
|
|
|
|
const existingEmoji = await client.emoji.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
shortcode: emoji.name,
|
|
|
|
|
instance: null,
|
|
|
|
|
},
|
|
|
|
|
});
|
2023-11-04 04:34:31 +01:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
if (existingEmoji) return existingEmoji;
|
2023-11-04 04:34:31 +01:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
return await client.emoji.create({
|
|
|
|
|
data: {
|
|
|
|
|
shortcode: emoji.name,
|
|
|
|
|
url: emoji.url[0].content,
|
|
|
|
|
alt: emoji.alt || null,
|
|
|
|
|
content_type: emoji.url[0].content_type,
|
|
|
|
|
visible_in_picker: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
2023-09-12 22:48:10 +02:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
/**
|
|
|
|
|
* Converts the emoji to an APIEmoji object.
|
|
|
|
|
* @returns The APIEmoji object.
|
|
|
|
|
*/
|
2023-11-20 03:42:40 +01:00
|
|
|
export const emojiToAPI = (emoji: Emoji): APIEmoji => {
|
2023-11-11 03:36:06 +01:00
|
|
|
return {
|
|
|
|
|
shortcode: emoji.shortcode,
|
|
|
|
|
static_url: emoji.url, // TODO: Add static version
|
|
|
|
|
url: emoji.url,
|
|
|
|
|
visible_in_picker: emoji.visible_in_picker,
|
|
|
|
|
category: undefined,
|
|
|
|
|
};
|
|
|
|
|
};
|
2023-11-04 04:34:31 +01:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
export const emojiToLysand = (emoji: Emoji): LysandEmoji => {
|
|
|
|
|
return {
|
|
|
|
|
name: emoji.shortcode,
|
|
|
|
|
url: [
|
|
|
|
|
{
|
|
|
|
|
content: emoji.url,
|
|
|
|
|
content_type: emoji.content_type,
|
2023-11-04 04:34:31 +01:00
|
|
|
},
|
2023-11-11 03:36:06 +01:00
|
|
|
],
|
|
|
|
|
alt: emoji.alt || undefined,
|
|
|
|
|
};
|
|
|
|
|
};
|
2024-03-04 01:45:21 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts the emoji to an ActivityPub object.
|
|
|
|
|
* @returns The ActivityPub object.
|
|
|
|
|
*/
|
|
|
|
|
export const emojiToActivityPub = (emoji: Emoji): any => {
|
|
|
|
|
// replace any with your ActivityPub Emoji type
|
|
|
|
|
return {
|
|
|
|
|
type: "Emoji",
|
|
|
|
|
name: `:${emoji.shortcode}:`,
|
|
|
|
|
updated: new Date().toISOString(),
|
|
|
|
|
icon: {
|
|
|
|
|
type: "Image",
|
|
|
|
|
url: emoji.url,
|
|
|
|
|
mediaType: emoji.content_type,
|
|
|
|
|
alt: emoji.alt || undefined,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const addAPEmojiIfNotExists = async (apEmoji: any) => {
|
|
|
|
|
// replace any with your ActivityPub Emoji type
|
|
|
|
|
const existingEmoji = await client.emoji.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
shortcode: apEmoji.name.replace(/:/g, ""),
|
|
|
|
|
instance: null,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (existingEmoji) return existingEmoji;
|
|
|
|
|
|
|
|
|
|
return await client.emoji.create({
|
|
|
|
|
data: {
|
|
|
|
|
shortcode: apEmoji.name.replace(/:/g, ""),
|
|
|
|
|
url: apEmoji.icon.url,
|
|
|
|
|
alt: apEmoji.icon.alt || null,
|
|
|
|
|
content_type: apEmoji.icon.mediaType,
|
|
|
|
|
visible_in_picker: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|