From b36609bc74d64b82063dcfddae8a0fba70f26ddc Mon Sep 17 00:00:00 2001 From: mattyatea Date: Fri, 15 Sep 2023 18:46:07 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=87=8D=E8=A4=87=E3=81=97=E3=81=9F?= =?UTF-8?q?=E7=B5=B5=E6=96=87=E5=AD=97=E3=81=AF=E7=99=BB=E9=8C=B2=E3=81=A7?= =?UTF-8?q?=E3=81=8D=E3=81=AA=E3=81=84=E3=82=88=E3=81=86=E3=81=AB=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/api/endpoints/admin/emoji/add.ts | 21 +++++++++++++++++-- .../server/api/endpoints/admin/emoji/copy.ts | 15 +++++++++++++ .../api/endpoints/admin/emoji/update.ts | 17 +++++++++++++-- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/add.ts b/packages/backend/src/server/api/endpoints/admin/emoji/add.ts index 7bd920c312..afad538925 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/add.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/add.ts @@ -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 { // 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 { // 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 { // eslint- roleIdsThatCanBeUsedThisEmojiAsReaction: ps.roleIdsThatCanBeUsedThisEmojiAsReaction ?? [], }); + + this.moderationLogService.insertModerationLog(me, 'addEmoji', { emojiId: emoji.id, }); diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/copy.ts b/packages/backend/src/server/api/endpoints/admin/emoji/copy.ts index f374b31303..5a592b989c 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/copy.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/copy.ts @@ -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 { // 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 { // 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(), diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/update.ts b/packages/backend/src/server/api/endpoints/admin/emoji/update.ts index f01be9e27a..c8fb2a2a2d 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/update.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/update.ts @@ -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 { // 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 { // 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,