diff --git a/packages/backend/src/server/api/mastodon/converters/emoji.ts b/packages/backend/src/server/api/mastodon/converters/emoji.ts index 2f79aaafa..7e66f622b 100644 --- a/packages/backend/src/server/api/mastodon/converters/emoji.ts +++ b/packages/backend/src/server/api/mastodon/converters/emoji.ts @@ -7,7 +7,7 @@ export class EmojiConverter { static_url: e.url, url: e.url, visible_in_picker: true, - category: "unknown", //FIXME - e.category + category: null }; } } diff --git a/packages/backend/src/server/api/mastodon/endpoints/misc.ts b/packages/backend/src/server/api/mastodon/endpoints/misc.ts index 90edb34d6..fc30a3f09 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/misc.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/misc.ts @@ -9,16 +9,11 @@ import { convertId, IdType } from "@/misc/convert-id.js"; export function setupEndpointsMisc(router: Router): void { router.get("/v1/custom_emojis", async (ctx) => { - const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`; - const accessTokens = ctx.request.headers.authorization; - const client = getClient(BASE_URL, accessTokens); try { - const data = await client.getInstanceCustomEmojis(); - ctx.body = data.data; + ctx.body = await MiscHelpers.getCustomEmoji(); } catch (e: any) { - console.error(e); - ctx.status = 401; - ctx.body = e.response.data; + ctx.status = 500; + ctx.body = { error: e.message }; } }); diff --git a/packages/backend/src/server/api/mastodon/helpers/misc.ts b/packages/backend/src/server/api/mastodon/helpers/misc.ts index 1b0c20fd4..429c0d6a8 100644 --- a/packages/backend/src/server/api/mastodon/helpers/misc.ts +++ b/packages/backend/src/server/api/mastodon/helpers/misc.ts @@ -1,7 +1,7 @@ import config from "@/config/index.js"; import { FILE_TYPE_BROWSERSAFE, MAX_NOTE_TEXT_LENGTH } from "@/const.js"; import { fetchMeta } from "@/misc/fetch-meta.js"; -import { AnnouncementReads, Announcements, Instances, Notes, Users } from "@/models/index.js"; +import { AnnouncementReads, Announcements, Emojis, Instances, Notes, Users } from "@/models/index.js"; import { IsNull } from "typeorm"; import { awaitAll } from "@/prelude/await-all.js"; import { UserConverter } from "@/server/api/mastodon/converters/user.js"; @@ -16,6 +16,8 @@ import { UserHelpers } from "@/server/api/mastodon/helpers/user.js"; import { generateMutedUserQueryForUsers } from "@/server/api/common/generate-muted-user-query.js"; import { generateBlockQueryForUsers } from "@/server/api/common/generate-block-query.js"; import { uniqBy } from "@/prelude/array.js"; +import { EmojiConverter } from "@/server/api/mastodon/converters/emoji.js"; +import { populateEmojis } from "@/misc/populate-emojis.js"; export class MiscHelpers { public static async getInstance(): Promise { @@ -175,4 +177,30 @@ export class MiscHelpers { return Promise.all(results).then(p => uniqBy(p.flat(), (x: MastodonEntity.SuggestedAccount) => x.account.id).slice(0, limit)); } + + public static async getCustomEmoji() { + return Emojis.find({ + where: { + host: IsNull(), + }, + order: { + category: "ASC", + name: "ASC", + }, + cache: { + id: "meta_emojis", + milliseconds: 3600000, // 1 hour + }} + ) + .then(dbRes => populateEmojis(dbRes.map(p => p.name), null) + .then(p => p.map(x => EmojiConverter.encode(x)) + .map(x => { + return { + ...x, + category: dbRes.find(y => y.name === x.shortcode)?.category ?? null + } + }) + ) + ); + } } \ No newline at end of file