From d5d995a3e6a5e87969dc7eecdd612abc10dd7c84 Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 5 Nov 2018 13:38:50 +0900 Subject: Refactor --- src/models/mastodon/emoji.ts | 35 -------------- src/server/api/mastodon.ts | 101 --------------------------------------- src/server/api/mastodon/emoji.ts | 35 ++++++++++++++ src/server/api/mastodon/index.ts | 101 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+), 136 deletions(-) delete mode 100644 src/models/mastodon/emoji.ts delete mode 100644 src/server/api/mastodon.ts create mode 100644 src/server/api/mastodon/emoji.ts create mode 100644 src/server/api/mastodon/index.ts (limited to 'src') diff --git a/src/models/mastodon/emoji.ts b/src/models/mastodon/emoji.ts deleted file mode 100644 index df19c6e758..0000000000 --- a/src/models/mastodon/emoji.ts +++ /dev/null @@ -1,35 +0,0 @@ -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 deleted file mode 100644 index e08fbc6f57..0000000000 --- a/src/server/api/mastodon.ts +++ /dev/null @@ -1,101 +0,0 @@ -import * as Router from 'koa-router'; -import User from '../../models/user'; -import { toASCII } from 'punycode'; -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 }, { - fields: { - _id: false - } - })).map(x => toMastodonEmojis(x))); - -router.get('/v1/instance', async ctx => { // TODO: This is a temporary implementation. Consider creating helper methods! - const meta = await Meta.findOne() || {}; - const { originalNotesCount, originalUsersCount } = meta.stats || { - originalNotesCount: 0, - originalUsersCount: 0 - }; - const domains = await User.distinct('host', { host: { $ne: null } }) as any as [] || []; - const maintainer = await User.findOne({ isAdmin: true }) || { - _id: ObjectID.createFromTime(0), - username: '', // TODO: Consider making this better! - host: config.host, - name: '', - isLocked: false, - isBot: false, - createdAt: new Date(0), - description: '', - avatarUrl: '', - bannerUrl: '', - followersCount: 0, - followingCount: 0, - 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, - title: meta.name || 'Misskey', - description: meta.description || '', - email: config.maintainer.email || config.maintainer.url.startsWith('mailto:') ? config.maintainer.url.slice(7) : '', - version: `0.0.0:compatible:misskey:${pkg.version}`, // TODO: How to tell about that this is an api for compatibility? - thumbnail: meta.bannerUrl, - /* - urls: { - streaming_api: config.ws_url + '/mastodon' // TODO: Implement compatible streaming API - }, */ - stats: { - user_count: originalUsersCount, - status_count: originalNotesCount, - domain_count: domains.length - }, - languages: config.languages || [ 'ja' ], - contact_account: { - id: maintainer._id, - username: maintainer.username, - acct: acct, - display_name: maintainer.name || '', - locked: maintainer.isLocked, - bot: maintainer.isBot, - created_at: maintainer.createdAt, - note: maintainer.description, - url: `${config.url}/@${acct}`, - avatar: maintainer.avatarUrl || '', - /* - avatar_static: maintainer.avatarUrl || '', // TODO: Implement static avatar url (ensure non-animated GIF) - */ - header: maintainer.bannerUrl || '', - /* - header_static: maintainer.bannerUrl || '', // TODO: Implement static header url (ensure non-animated GIF) - */ - followers_count: maintainer.followersCount, - following_count: maintainer.followingCount, - statuses_count: maintainer.notesCount, - emojis: emojis, - moved: null, - fields: null - } - }; -}); - -router.get('/v1/instance/peers', async ctx => { - const peers = await User.distinct('host', { host: { $ne: null } }) as any as string[]; - const punyCodes = peers.map(peer => toASCII(peer)); - ctx.body = punyCodes; -}); - -module.exports = router; diff --git a/src/server/api/mastodon/emoji.ts b/src/server/api/mastodon/emoji.ts new file mode 100644 index 0000000000..df19c6e758 --- /dev/null +++ b/src/server/api/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/index.ts b/src/server/api/mastodon/index.ts new file mode 100644 index 0000000000..9e8a6f505f --- /dev/null +++ b/src/server/api/mastodon/index.ts @@ -0,0 +1,101 @@ +import * as Router from 'koa-router'; +import User from '../../../models/user'; +import { toASCII } from 'punycode'; +import config from '../../../config'; +import Meta from '../../../models/meta'; +import { ObjectID } from 'bson'; +import Emoji from '../../../models/emoji'; +import { toMastodonEmojis } from './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 }, { + fields: { + _id: false + } + })).map(x => toMastodonEmojis(x))); + +router.get('/v1/instance', async ctx => { // TODO: This is a temporary implementation. Consider creating helper methods! + const meta = await Meta.findOne() || {}; + const { originalNotesCount, originalUsersCount } = meta.stats || { + originalNotesCount: 0, + originalUsersCount: 0 + }; + const domains = await User.distinct('host', { host: { $ne: null } }) as any as [] || []; + const maintainer = await User.findOne({ isAdmin: true }) || { + _id: ObjectID.createFromTime(0), + username: '', // TODO: Consider making this better! + host: config.host, + name: '', + isLocked: false, + isBot: false, + createdAt: new Date(0), + description: '', + avatarUrl: '', + bannerUrl: '', + followersCount: 0, + followingCount: 0, + 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, + title: meta.name || 'Misskey', + description: meta.description || '', + email: config.maintainer.email || config.maintainer.url.startsWith('mailto:') ? config.maintainer.url.slice(7) : '', + version: `0.0.0:compatible:misskey:${pkg.version}`, // TODO: How to tell about that this is an api for compatibility? + thumbnail: meta.bannerUrl, + /* + urls: { + streaming_api: config.ws_url + '/mastodon' // TODO: Implement compatible streaming API + }, */ + stats: { + user_count: originalUsersCount, + status_count: originalNotesCount, + domain_count: domains.length + }, + languages: config.languages || [ 'ja' ], + contact_account: { + id: maintainer._id, + username: maintainer.username, + acct: acct, + display_name: maintainer.name || '', + locked: maintainer.isLocked, + bot: maintainer.isBot, + created_at: maintainer.createdAt, + note: maintainer.description, + url: `${config.url}/@${acct}`, + avatar: maintainer.avatarUrl || '', + /* + avatar_static: maintainer.avatarUrl || '', // TODO: Implement static avatar url (ensure non-animated GIF) + */ + header: maintainer.bannerUrl || '', + /* + header_static: maintainer.bannerUrl || '', // TODO: Implement static header url (ensure non-animated GIF) + */ + followers_count: maintainer.followersCount, + following_count: maintainer.followingCount, + statuses_count: maintainer.notesCount, + emojis: emojis, + moved: null, + fields: null + } + }; +}); + +router.get('/v1/instance/peers', async ctx => { + const peers = await User.distinct('host', { host: { $ne: null } }) as any as string[]; + const punyCodes = peers.map(peer => toASCII(peer)); + ctx.body = punyCodes; +}); + +module.exports = router; -- cgit v1.2.3-freya