From 5f7fc54ee9359d7dae82ad70e89f930d6a2b2e61 Mon Sep 17 00:00:00 2001 From: Mar0xy Date: Wed, 29 Nov 2023 18:45:42 +0100 Subject: [PATCH] upd: delete emoji file from drive on emoji deletion --- .../backend/src/core/CustomEmojiService.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/core/CustomEmojiService.ts b/packages/backend/src/core/CustomEmojiService.ts index 75335f6ff..5a1fe3d08 100644 --- a/packages/backend/src/core/CustomEmojiService.ts +++ b/packages/backend/src/core/CustomEmojiService.ts @@ -12,7 +12,7 @@ import { EmojiEntityService } from '@/core/entities/EmojiEntityService.js'; import { GlobalEventService } from '@/core/GlobalEventService.js'; import type { MiDriveFile } from '@/models/DriveFile.js'; import type { MiEmoji } from '@/models/Emoji.js'; -import type { EmojisRepository, MiRole, MiUser } from '@/models/_.js'; +import type { DriveFilesRepository, EmojisRepository, MiRole, MiUser } from '@/models/_.js'; import { bindThis } from '@/decorators.js'; import { MemoryKVCache, RedisSingleCache } from '@/misc/cache.js'; import { UtilityService } from '@/core/UtilityService.js'; @@ -20,6 +20,7 @@ import { query } from '@/misc/prelude/url.js'; import type { Serialized } from '@/types.js'; import { ModerationLogService } from '@/core/ModerationLogService.js'; import type { Config } from '@/config.js'; +import { DriveService } from './DriveService.js'; const parseEmojiStrRegexp = /^(\w+)(?:@([\w.-]+))?$/; @@ -38,11 +39,15 @@ export class CustomEmojiService implements OnApplicationShutdown { @Inject(DI.emojisRepository) private emojisRepository: EmojisRepository, + @Inject(DI.driveFilesRepository) + private driveFilesRepository: DriveFilesRepository, + private utilityService: UtilityService, private idService: IdService, private emojiEntityService: EmojiEntityService, private moderationLogService: ModerationLogService, private globalEventService: GlobalEventService, + private driveService: DriveService, ) { this.cache = new MemoryKVCache(1000 * 60 * 60 * 12); @@ -259,6 +264,12 @@ export class CustomEmojiService implements OnApplicationShutdown { this.localEmojisCache.refresh(); + const file = await this.driveFilesRepository.findOneBy({ url: emoji.originalUrl, userHost: emoji.host ? emoji.host : IsNull() }); + + if (file) { + await this.driveService.deleteFile(file, false, moderator ? moderator : undefined); + } + this.globalEventService.publishBroadcastStream('emojiDeleted', { emojis: [await this.emojiEntityService.packDetailed(emoji)], }); @@ -280,6 +291,12 @@ export class CustomEmojiService implements OnApplicationShutdown { for (const emoji of emojis) { await this.emojisRepository.delete(emoji.id); + const file = await this.driveFilesRepository.findOneBy({ url: emoji.originalUrl, userHost: emoji.host ? emoji.host : IsNull() }); + + if (file) { + await this.driveService.deleteFile(file, false, moderator ? moderator : undefined); + } + if (moderator) { this.moderationLogService.log(moderator, 'deleteCustomEmoji', { emojiId: emoji.id,