From 249cc63da1e21fa0027b06ffbe26c6c7b8a93af0 Mon Sep 17 00:00:00 2001 From: eternal-flame-AD Date: Tue, 19 Nov 2024 10:35:56 -0600 Subject: [PATCH] improve emoji rendering Signed-off-by: eternal-flame-AD --- .../src/core/entities/EmojiEntityService.ts | 62 ++++++++++++++----- .../src/server/api/endpoints/emojis.ts | 17 +++-- 2 files changed, 54 insertions(+), 25 deletions(-) diff --git a/packages/backend/src/core/entities/EmojiEntityService.ts b/packages/backend/src/core/entities/EmojiEntityService.ts index 841bd731c0..0dd183d703 100644 --- a/packages/backend/src/core/entities/EmojiEntityService.ts +++ b/packages/backend/src/core/entities/EmojiEntityService.ts @@ -10,6 +10,7 @@ import type { Packed } from '@/misc/json-schema.js'; import type { } from '@/models/Blocking.js'; import type { MiEmoji } from '@/models/Emoji.js'; import { bindThis } from '@/decorators.js'; +import { In } from 'typeorm'; @Injectable() export class EmojiEntityService { @@ -20,11 +21,9 @@ export class EmojiEntityService { } @bindThis - public async packSimple( - src: MiEmoji['id'] | MiEmoji, - ): Promise> { - const emoji = typeof src === 'object' ? src : await this.emojisRepository.findOneByOrFail({ id: src }); - + public packSimpleNoQuery( + emoji: MiEmoji, + ): Packed<'EmojiSimple'> { return { aliases: emoji.aliases, name: emoji.name, @@ -38,18 +37,34 @@ export class EmojiEntityService { } @bindThis - public packSimpleMany( - emojis: any[], - ) { - return Promise.all(emojis.map(x => this.packSimple(x))); + public async packSimple( + src: MiEmoji['id'] | MiEmoji, + ): Promise> { + const emoji = typeof src === 'object' ? src : await this.emojisRepository.findOneByOrFail({ id: src }); + + return this.packSimpleNoQuery(emoji); } @bindThis - public async packDetailed( - src: MiEmoji['id'] | MiEmoji, - ): Promise> { - const emoji = typeof src === 'object' ? src : await this.emojisRepository.findOneByOrFail({ id: src }); + public async packSimpleMany( + emojis: MiEmoji['id'][] | MiEmoji[], + ): Promise[]> { + if (emojis.length === 0) { + return []; + } + + if (typeof emojis[0] === 'string') { + const res = await this.emojisRepository.findBy({ id: In(emojis as MiEmoji['id'][]) }); + return res.map(this.packSimpleNoQuery); + } + return (emojis as MiEmoji[]).map(this.packSimpleNoQuery); + } + + @bindThis + public packDetailedNoQuery( + emoji: MiEmoji, + ): Packed<'EmojiDetailed'> { return { id: emoji.id, aliases: emoji.aliases, @@ -65,11 +80,28 @@ export class EmojiEntityService { }; } + @bindThis + public async packDetailed( + src: MiEmoji['id'] | MiEmoji, + ): Promise> { + const emoji = typeof src === 'object' ? src : await this.emojisRepository.findOneByOrFail({ id: src }); + + return this.packDetailedNoQuery(emoji); + } + @bindThis public packDetailedMany( - emojis: any[], + emojis: MiEmoji['id'][] | MiEmoji[], ) { - return Promise.all(emojis.map(x => this.packDetailed(x))); + if (emojis.length === 0) { + return []; + } + + if (typeof emojis[0] === 'string') { + return this.emojisRepository.findBy({ id: In(emojis as MiEmoji['id'][]) }).then(res => res.map(this.packDetailedNoQuery)); + } + + return (emojis as MiEmoji[]).map(this.packDetailedNoQuery); } } diff --git a/packages/backend/src/server/api/endpoints/emojis.ts b/packages/backend/src/server/api/endpoints/emojis.ts index 46ef4eca1b..7888e65794 100644 --- a/packages/backend/src/server/api/endpoints/emojis.ts +++ b/packages/backend/src/server/api/endpoints/emojis.ts @@ -50,18 +50,15 @@ export default class extends Endpoint { // eslint- private emojiEntityService: EmojiEntityService, ) { super(meta, paramDef, async (ps, me) => { - const emojis = await this.emojisRepository.find({ - where: { - host: IsNull(), - }, - order: { - category: 'ASC', - name: 'ASC', - }, - }); + const emojis = await this.emojisRepository + .createQueryBuilder() + .where({ host: IsNull() }) + .orderBy('LOWER(category)', 'ASC') + .addOrderBy('LOWER(name)', 'ASC') + .getMany(); return { - emojis: await this.emojiEntityService.packSimpleMany(emojis), + emojis: emojis.map(this.emojiEntityService.packSimpleNoQuery), }; }); }