[mastodon-client] GET /v1/custom_emoji

This commit is contained in:
Laura Hausmann 2023-10-05 02:03:44 +02:00
parent 5f0d140bbe
commit fb7713c424
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 33 additions and 10 deletions

View file

@ -7,7 +7,7 @@ export class EmojiConverter {
static_url: e.url, static_url: e.url,
url: e.url, url: e.url,
visible_in_picker: true, visible_in_picker: true,
category: "unknown", //FIXME - e.category category: null
}; };
} }
} }

View file

@ -9,16 +9,11 @@ import { convertId, IdType } from "@/misc/convert-id.js";
export function setupEndpointsMisc(router: Router): void { export function setupEndpointsMisc(router: Router): void {
router.get("/v1/custom_emojis", async (ctx) => { 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 { try {
const data = await client.getInstanceCustomEmojis(); ctx.body = await MiscHelpers.getCustomEmoji();
ctx.body = data.data;
} catch (e: any) { } catch (e: any) {
console.error(e); ctx.status = 500;
ctx.status = 401; ctx.body = { error: e.message };
ctx.body = e.response.data;
} }
}); });

View file

@ -1,7 +1,7 @@
import config from "@/config/index.js"; import config from "@/config/index.js";
import { FILE_TYPE_BROWSERSAFE, MAX_NOTE_TEXT_LENGTH } from "@/const.js"; import { FILE_TYPE_BROWSERSAFE, MAX_NOTE_TEXT_LENGTH } from "@/const.js";
import { fetchMeta } from "@/misc/fetch-meta.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 { IsNull } from "typeorm";
import { awaitAll } from "@/prelude/await-all.js"; import { awaitAll } from "@/prelude/await-all.js";
import { UserConverter } from "@/server/api/mastodon/converters/user.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 { generateMutedUserQueryForUsers } from "@/server/api/common/generate-muted-user-query.js";
import { generateBlockQueryForUsers } from "@/server/api/common/generate-block-query.js"; import { generateBlockQueryForUsers } from "@/server/api/common/generate-block-query.js";
import { uniqBy } from "@/prelude/array.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 { export class MiscHelpers {
public static async getInstance(): Promise<MastodonEntity.Instance> { public static async getInstance(): Promise<MastodonEntity.Instance> {
@ -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)); 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
}
})
)
);
}
} }