2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DataSource, In, IsNull } from 'typeorm';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-01-22 23:53:24 +09:00
|
|
|
import { EmojiEntityService } from '@/core/entities/EmojiEntityService.js';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import type { DriveFile } from '@/models/entities/DriveFile.js';
|
|
|
|
import type { Emoji } from '@/models/entities/Emoji.js';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { EmojisRepository } from '@/models/index.js';
|
2022-12-04 17:05:32 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class CustomEmojiService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.db)
|
|
|
|
private db: DataSource,
|
|
|
|
|
|
|
|
@Inject(DI.emojisRepository)
|
|
|
|
private emojisRepository: EmojisRepository,
|
|
|
|
|
|
|
|
private idService: IdService,
|
2023-01-22 23:53:24 +09:00
|
|
|
private emojiEntityService: EmojiEntityService,
|
|
|
|
private globalEventService: GlobalEventService,
|
2022-09-18 03:27:08 +09:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
public async add(data: {
|
|
|
|
driveFile: DriveFile;
|
|
|
|
name: string;
|
|
|
|
category: string | null;
|
|
|
|
aliases: string[];
|
|
|
|
host: string | null;
|
|
|
|
}): Promise<Emoji> {
|
|
|
|
const emoji = await this.emojisRepository.insert({
|
|
|
|
id: this.idService.genId(),
|
|
|
|
updatedAt: new Date(),
|
|
|
|
name: data.name,
|
|
|
|
category: data.category,
|
|
|
|
host: data.host,
|
|
|
|
aliases: data.aliases,
|
|
|
|
originalUrl: data.driveFile.url,
|
|
|
|
publicUrl: data.driveFile.webpublicUrl ?? data.driveFile.url,
|
|
|
|
type: data.driveFile.webpublicType ?? data.driveFile.type,
|
|
|
|
}).then(x => this.emojisRepository.findOneByOrFail(x.identifiers[0]));
|
|
|
|
|
|
|
|
await this.db.queryResultCache!.remove(['meta_emojis']);
|
|
|
|
|
2023-01-22 23:53:24 +09:00
|
|
|
this.globalEventService.publishBroadcastStream('emojiAdded', {
|
|
|
|
emoji: await this.emojiEntityService.pack(emoji.id),
|
|
|
|
});
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
return emoji;
|
|
|
|
}
|
|
|
|
}
|