From 0d23ce3d459dc5c7e76f90ed0cd79e18a8980bc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Acid=20Chicken=20=28=E7=A1=AB=E9=85=B8=E9=B6=8F=29?= Date: Mon, 5 Nov 2018 11:57:17 +0900 Subject: [PATCH] Make /api/v1/instance and /api/v1/custom_emojis better (#3118) * Separate commits From commit dca110ebaa78f64600429f812c238a07d2f1dc1d. * Re-separate commits From commit 9719387bee40363f63a837e7ecffacf2a476c334. --- src/models/mastodon/emoji.ts | 35 +++++++++++++++++++++++++++++++++++ src/server/api/mastodon.ts | 12 +++++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 src/models/mastodon/emoji.ts diff --git a/src/models/mastodon/emoji.ts b/src/models/mastodon/emoji.ts new file mode 100644 index 000000000..df19c6e75 --- /dev/null +++ b/src/models/mastodon/emoji.ts @@ -0,0 +1,35 @@ +export type IMastodonEmoji = { + shortcode: string, + url: string, + static_url: string, + visible_in_picker: boolean +}; + +export async function toMastodonEmojis(emoji: any): Promise { + return [{ + shortcode: emoji.name, + url: emoji.url, + static_url: emoji.url, // TODO: Implement ensuring static emoji + visible_in_picker: true + }, ...(emoji.aliases as string[] || []).map(x => ({ + shortcode: x, + url: emoji.url, + static_url: emoji.url, + visible_in_picker: true + }))]; +} + +export function toMisskeyEmojiSync(emoji: IMastodonEmoji) { + return { + name: emoji.shortcode, + url: emoji.url + }; +} + +export function toMisskeyEmojiWithAliasesSync(emoji: IMastodonEmoji, ...aliases: string[]) { + return { + name: emoji.shortcode, + aliases, + url: emoji.url + }; +} diff --git a/src/server/api/mastodon.ts b/src/server/api/mastodon.ts index 33a0b4b5f..d1e1068da 100644 --- a/src/server/api/mastodon.ts +++ b/src/server/api/mastodon.ts @@ -5,17 +5,18 @@ import config from '../../config'; import Meta from '../../models/meta'; import { ObjectID } from 'bson'; import Emoji from '../../models/emoji'; +import { toMastodonEmojis } from '../../models/mastodon/emoji'; const pkg = require('../../../package.json'); // Init router const router = new Router(); router.get('/v1/custom_emojis', async ctx => ctx.body = - await Emoji.find({ host: null }, { + (await Emoji.find({ host: null }, { fields: { _id: false } - })); + })).map(toMastodonEmojis)); router.get('/v1/instance', async ctx => { // TODO: This is a temporary implementation. Consider creating helper methods! const meta = await Meta.findOne() || {}; @@ -40,6 +41,11 @@ router.get('/v1/instance', async ctx => { // TODO: This is a temporary implement notesCount: 0 }; const acct = maintainer.host ? `${maintainer.username}@${maintainer.host}` : maintainer.username; + const emojis = (await Emoji.find({ host: null }, { + fields: { + _id: false + } + })).map(toMastodonEmojis); ctx.body = { uri: config.hostname, @@ -79,7 +85,7 @@ router.get('/v1/instance', async ctx => { // TODO: This is a temporary implement followers_count: maintainer.followersCount, following_count: maintainer.followingCount, statuses_count: maintainer.notesCount, - emojis: [], + emojis: emojis, moved: null, fields: null }