feat: 重複した絵文字は登録できないようにする

This commit is contained in:
mattyatea 2023-09-15 18:46:07 +09:00
parent 382daa61ae
commit b36609bc74
3 changed files with 49 additions and 4 deletions

View file

@ -5,7 +5,7 @@
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { DriveFilesRepository } from '@/models/_.js';
import type { DriveFilesRepository, EmojisRepository } from '@/models/_.js';
import { DI } from '@/di-symbols.js';
import { CustomEmojiService } from '@/core/CustomEmojiService.js';
import { ModerationLogService } from '@/core/ModerationLogService.js';
@ -24,6 +24,11 @@ export const meta = {
code: 'NO_SUCH_FILE',
id: 'fc46b5a4-6b92-4c33-ac66-b806659bb5cf',
},
duplicationEmojiAdd: {
message: 'This emoji is already added.',
code: 'DUPLICATION_EMOJI_ADD',
id: 'mattyaski_emoji_duplication_error',
}
},
} as const;
@ -57,7 +62,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
constructor(
@Inject(DI.driveFilesRepository)
private driveFilesRepository: DriveFilesRepository,
private emojisRepository: EmojisRepository,
private customEmojiService: CustomEmojiService,
private emojiEntityService: EmojiEntityService,
@ -67,6 +72,16 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
const driveFile = await this.driveFilesRepository.findOneBy({ id: ps.fileId });
if (driveFile == null) throw new ApiError(meta.errors.noSuchFile);
const existEmoji = await this.emojisRepository.exist({
where: {
name: ps.name,
},
});
if (existEmoji) {
throw new ApiError(meta.errors.duplicationEmojiAdd);
}
const emoji = await this.customEmojiService.add({
driveFile,
name: ps.name,
@ -79,6 +94,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
roleIdsThatCanBeUsedThisEmojiAsReaction: ps.roleIdsThatCanBeUsedThisEmojiAsReaction ?? [],
});
this.moderationLogService.insertModerationLog(me, 'addEmoji', {
emojiId: emoji.id,
});

View file

@ -26,6 +26,11 @@ export const meta = {
code: 'NO_SUCH_EMOJI',
id: 'e2785b66-dca3-4087-9cac-b93c541cc425',
},
duplicationEmojiAdd: {
message: 'This emoji is already added.',
code: 'DUPLICATION_EMOJI_ADD',
id: 'mattyaski_emoji_duplication_error',
}
},
res: {
@ -57,6 +62,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
@Inject(DI.emojisRepository)
private emojisRepository: EmojisRepository,
private emojiEntityService: EmojiEntityService,
private idService: IdService,
private globalEventService: GlobalEventService,
@ -77,6 +83,15 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
} catch (e) {
throw new ApiError();
}
const existEmoji = await this.emojisRepository.exist({
where: {
name: emoji.name,
},
});
if (existEmoji) {
throw new ApiError(meta.errors.duplicationEmojiAdd);
}
const copied = await this.emojisRepository.insert({
id: this.idService.genId(),

View file

@ -6,7 +6,7 @@
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { CustomEmojiService } from '@/core/CustomEmojiService.js';
import type { DriveFilesRepository } from '@/models/_.js';
import type { DriveFilesRepository , EmojisRepository } from '@/models/_.js';
import { DI } from '@/di-symbols.js';
import { ApiError } from '../../../error.js';
@ -32,6 +32,11 @@ export const meta = {
code: 'SAME_NAME_EMOJI_EXISTS',
id: '7180fe9d-1ee3-bff9-647d-fe9896d2ffb8',
},
duplicationEmojiAdd: {
message: 'This emoji is already added.',
code: 'DUPLICATION_EMOJI_ADD',
id: 'mattyaski_emoji_duplication_error',
}
},
} as const;
@ -64,7 +69,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
constructor(
@Inject(DI.driveFilesRepository)
private driveFilesRepository: DriveFilesRepository,
private emojisRepository: EmojisRepository,
private customEmojiService: CustomEmojiService,
) {
super(meta, paramDef, async (ps, me) => {
@ -74,7 +79,15 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
driveFile = await this.driveFilesRepository.findOneBy({ id: ps.fileId });
if (driveFile == null) throw new ApiError(meta.errors.noSuchFile);
}
const existEmoji = await this.emojisRepository.exist({
where: {
name: ps.name,
},
});
if (existEmoji) {
throw new ApiError(meta.errors.duplicationEmojiAdd);
}
await this.customEmojiService.update(ps.id, {
driveFile,
name: ps.name,