From 79354f4fafcf2794d4f9aef2fc523d420c088f8c Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 31 Mar 2020 09:15:04 +0900 Subject: [PATCH] Refactoring --- src/models/index.ts | 4 +-- src/models/repositories/emoji.ts | 27 +++++++++++++++++++ .../api/endpoints/admin/emoji/list-remote.ts | 9 +------ src/server/api/endpoints/admin/emoji/list.ts | 9 +------ src/server/api/endpoints/meta.ts | 8 +----- 5 files changed, 32 insertions(+), 25 deletions(-) create mode 100644 src/models/repositories/emoji.ts diff --git a/src/models/index.ts b/src/models/index.ts index 39f185e6f4..c3b329f4f8 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -2,7 +2,6 @@ import { getRepository, getCustomRepository } from 'typeorm'; import { Announcement } from './entities/announcement'; import { AnnouncementRead } from './entities/announcement-read'; import { Instance } from './entities/instance'; -import { Emoji } from './entities/emoji'; import { Poll } from './entities/poll'; import { PollVote } from './entities/poll-vote'; import { Meta } from './entities/meta'; @@ -52,6 +51,7 @@ import { AntennaRepository } from './repositories/antenna'; import { AntennaNote } from './entities/antenna-note'; import { PromoNote } from './entities/promo-note'; import { PromoRead } from './entities/promo-read'; +import { EmojiRepository } from './repositories/emoji'; export const Announcements = getRepository(Announcement); export const AnnouncementReads = getRepository(AnnouncementRead); @@ -79,7 +79,7 @@ export const UsedUsernames = getRepository(UsedUsername); export const Followings = getCustomRepository(FollowingRepository); export const FollowRequests = getCustomRepository(FollowRequestRepository); export const Instances = getRepository(Instance); -export const Emojis = getRepository(Emoji); +export const Emojis = getCustomRepository(EmojiRepository); export const DriveFiles = getCustomRepository(DriveFileRepository); export const DriveFolders = getCustomRepository(DriveFolderRepository); export const Notifications = getCustomRepository(NotificationRepository); diff --git a/src/models/repositories/emoji.ts b/src/models/repositories/emoji.ts new file mode 100644 index 0000000000..b485b37da5 --- /dev/null +++ b/src/models/repositories/emoji.ts @@ -0,0 +1,27 @@ +import { EntityRepository, Repository } from 'typeorm'; +import { Emoji } from '../entities/emoji'; +import { ensure } from '../../prelude/ensure'; + +@EntityRepository(Emoji) +export class EmojiRepository extends Repository { + public async pack( + src: Emoji['id'] | Emoji, + ) { + const emoji = typeof src === 'object' ? src : await this.findOne(src).then(ensure); + + return { + id: emoji.id, + aliases: emoji.aliases, + name: emoji.name, + category: emoji.category, + host: emoji.host, + url: emoji.url, + }; + } + + public packMany( + emojis: any[], + ) { + return Promise.all(emojis.map(x => this.pack(x))); + } +} diff --git a/src/server/api/endpoints/admin/emoji/list-remote.ts b/src/server/api/endpoints/admin/emoji/list-remote.ts index fd215ed6fe..7ced4623bb 100644 --- a/src/server/api/endpoints/admin/emoji/list-remote.ts +++ b/src/server/api/endpoints/admin/emoji/list-remote.ts @@ -51,12 +51,5 @@ export default define(meta, async (ps) => { .take(ps.limit!) .getMany(); - return emojis.map(e => ({ - id: e.id, - name: e.name, - category: e.category, - aliases: e.aliases, - host: e.host, - url: e.url - })); + return Emojis.packMany(emojis); }); diff --git a/src/server/api/endpoints/admin/emoji/list.ts b/src/server/api/endpoints/admin/emoji/list.ts index 2b6516a1aa..e3aab4cf7c 100644 --- a/src/server/api/endpoints/admin/emoji/list.ts +++ b/src/server/api/endpoints/admin/emoji/list.ts @@ -36,12 +36,5 @@ export default define(meta, async (ps) => { .take(ps.limit!) .getMany(); - return emojis.map(e => ({ - id: e.id, - name: e.name, - category: e.category, - aliases: e.aliases, - host: e.host, - url: e.url - })); + return Emojis.packMany(emojis); }); diff --git a/src/server/api/endpoints/meta.ts b/src/server/api/endpoints/meta.ts index 0cb6f75f11..280a6fbae6 100644 --- a/src/server/api/endpoints/meta.ts +++ b/src/server/api/endpoints/meta.ts @@ -130,13 +130,7 @@ export default define(meta, async (ps, me) => { errorImageUrl: instance.errorImageUrl, iconUrl: instance.iconUrl, maxNoteTextLength: Math.min(instance.maxNoteTextLength, DB_MAX_NOTE_TEXT_LENGTH), - emojis: emojis.map(e => ({ - id: e.id, - aliases: e.aliases, - name: e.name, - category: e.category, - url: e.url, - })), + emojis: await Emojis.packMany(emojis), requireSetup: (await Users.count({ host: null, })) === 0,