feat(api): Add new admin emoji API

This commit is contained in:
Jesse Wierzbinski 2024-05-11 15:27:28 -10:00
parent b979daa39a
commit 8fedd1a07d
No known key found for this signature in database
20 changed files with 954 additions and 167 deletions

View file

@ -1,3 +1,4 @@
import { emojiValidator } from "@api";
import { proxyUrl } from "@response";
import { type InferSelectModel, and, eq } from "drizzle-orm";
import type * as Lysand from "lysand-types";
@ -16,8 +17,7 @@ export type EmojiWithInstance = InferSelectModel<typeof Emojis> & {
* @returns An array of emojis
*/
export const parseEmojis = async (text: string) => {
const regex = /:[a-zA-Z0-9_]+:/g;
const matches = text.match(regex);
const matches = text.match(emojiValidator);
if (!matches) return [];
const emojis = await db.query.Emojis.findMany({
where: (emoji, { eq, or }) =>
@ -93,6 +93,8 @@ export const fetchEmoji = async (
*/
export const emojiToAPI = (emoji: EmojiWithInstance): APIEmoji => {
return {
// @ts-expect-error ID is not in regular Mastodon API
id: emoji.id,
shortcode: emoji.shortcode,
static_url: proxyUrl(emoji.url) ?? "", // TODO: Add static version
url: proxyUrl(emoji.url) ?? "",

View file

@ -1,6 +1,6 @@
import markdownItTaskLists from "@hackmd/markdown-it-task-lists";
import { dualLogger } from "@loggers";
import { sanitizeHtml } from "@sanitization";
import { sanitizeHtml, sanitizeHtmlInline } from "@sanitization";
import { config } from "config-manager";
import {
type InferSelectModel,
@ -498,18 +498,20 @@ export const replaceTextMentions = async (text: string, mentions: User[]) => {
export const contentToHtml = async (
content: Lysand.ContentFormat,
mentions: User[] = [],
inline = false,
): Promise<string> => {
let htmlContent: string;
const sanitizer = inline ? sanitizeHtmlInline : sanitizeHtml;
if (content["text/html"]) {
htmlContent = await sanitizeHtml(content["text/html"].content);
htmlContent = await sanitizer(content["text/html"].content);
} else if (content["text/markdown"]) {
htmlContent = await sanitizeHtml(
htmlContent = await sanitizer(
await markdownParse(content["text/markdown"].content),
);
} else if (content["text/plain"]?.content) {
// Split by newline and add <p> tags
htmlContent = (await sanitizeHtml(content["text/plain"].content))
htmlContent = (await sanitizer(content["text/plain"].content))
.split("\n")
.map((line) => `<p>${line}</p>`)
.join("\n");