improve emoji packing #32

Merged
yume merged 1 commit from develop into master 2024-11-19 11:30:14 -06:00
2 changed files with 57 additions and 27 deletions
Showing only changes of commit 416d71002a - Show all commits

View file

@ -10,6 +10,7 @@ import type { Packed } from '@/misc/json-schema.js';
import type { } from '@/models/Blocking.js'; import type { } from '@/models/Blocking.js';
import type { MiEmoji } from '@/models/Emoji.js'; import type { MiEmoji } from '@/models/Emoji.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
import { In } from 'typeorm';
@Injectable() @Injectable()
export class EmojiEntityService { export class EmojiEntityService {
@ -20,11 +21,9 @@ export class EmojiEntityService {
} }
@bindThis @bindThis
public async packSimple( public packSimpleNoQuery(
src: MiEmoji['id'] | MiEmoji, emoji: MiEmoji,
): Promise<Packed<'EmojiSimple'>> { ): Packed<'EmojiSimple'> {
const emoji = typeof src === 'object' ? src : await this.emojisRepository.findOneByOrFail({ id: src });
return { return {
aliases: emoji.aliases, aliases: emoji.aliases,
name: emoji.name, name: emoji.name,
@ -38,18 +37,34 @@ export class EmojiEntityService {
} }
@bindThis @bindThis
public packSimpleMany( public async packSimple(
emojis: any[], src: MiEmoji['id'] | MiEmoji,
) { ): Promise<Packed<'EmojiSimple'>> {
return Promise.all(emojis.map(x => this.packSimple(x))); const emoji = typeof src === 'object' ? src : await this.emojisRepository.findOneByOrFail({ id: src });
return this.packSimpleNoQuery(emoji);
} }
@bindThis @bindThis
public async packDetailed( public async packSimpleMany(
src: MiEmoji['id'] | MiEmoji, emojis: MiEmoji['id'][] | MiEmoji[],
): Promise<Packed<'EmojiDetailed'>> { ): Promise<Packed<'EmojiSimple'>[]> {
const emoji = typeof src === 'object' ? src : await this.emojisRepository.findOneByOrFail({ id: src }); 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 { return {
id: emoji.id, id: emoji.id,
aliases: emoji.aliases, aliases: emoji.aliases,
@ -66,10 +81,28 @@ export class EmojiEntityService {
} }
@bindThis @bindThis
public packDetailedMany( public async packDetailed(
emojis: any[], src: MiEmoji['id'] | MiEmoji,
) { ): Promise<Packed<'EmojiDetailed'>> {
return Promise.all(emojis.map(x => this.packDetailed(x))); const emoji = typeof src === 'object' ? src : await this.emojisRepository.findOneByOrFail({ id: src });
return this.packDetailedNoQuery(emoji);
}
@bindThis
public async packDetailedMany(
emojis: MiEmoji['id'][] | MiEmoji[],
) : Promise<Packed<'EmojiDetailed'>[]> {
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.packDetailedNoQuery);
}
return (emojis as MiEmoji[]).map(this.packDetailedNoQuery);
} }
} }

View file

@ -50,18 +50,15 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private emojiEntityService: EmojiEntityService, private emojiEntityService: EmojiEntityService,
) { ) {
super(meta, paramDef, async (ps, me) => { super(meta, paramDef, async (ps, me) => {
const emojis = await this.emojisRepository.find({ const emojis = await this.emojisRepository
where: { .createQueryBuilder()
host: IsNull(), .where({ host: IsNull() })
}, .orderBy('LOWER(category)', 'ASC')
order: { .addOrderBy('LOWER(name)', 'ASC')
category: 'ASC', .getMany();
name: 'ASC',
},
});
return { return {
emojis: await this.emojiEntityService.packSimpleMany(emojis), emojis: emojis.map(this.emojiEntityService.packSimpleNoQuery),
}; };
}); });
} }