From 5c019eec0408809534193b2bf370efddeb4d1c3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=BE=E3=81=A3=E3=81=A1=E3=82=83=E3=81=A8=E3=83=BC?= =?UTF-8?q?=E3=81=AB=E3=82=85?= <17376330+u1-liquid@users.noreply.github.com> Date: Sat, 16 Mar 2024 17:37:33 +0900 Subject: [PATCH] =?UTF-8?q?fix(queue):=20=E3=82=A2=E3=82=AB=E3=82=A6?= =?UTF-8?q?=E3=83=B3=E3=83=88=E5=89=8A=E9=99=A4=E3=81=8C=E7=B5=82=E3=82=8F?= =?UTF-8?q?=E3=82=89=E3=81=AA=E3=81=84=E5=95=8F=E9=A1=8C=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=20(MisskeyIO#526)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cheery-picked from tanukey-dev/tanukey@7ddecf1eab341952f75730f445445a9c15e8cfa0 Co-authored-by: tar_bin --- .../DeleteAccountProcessorService.ts | 113 ++++++++---------- 1 file changed, 50 insertions(+), 63 deletions(-) diff --git a/packages/backend/src/queue/processors/DeleteAccountProcessorService.ts b/packages/backend/src/queue/processors/DeleteAccountProcessorService.ts index 1893af5532..660e1d1ca4 100644 --- a/packages/backend/src/queue/processors/DeleteAccountProcessorService.ts +++ b/packages/backend/src/queue/processors/DeleteAccountProcessorService.ts @@ -4,13 +4,11 @@ */ import { Inject, Injectable } from '@nestjs/common'; -import { MoreThan } from 'typeorm'; import { DI } from '@/di-symbols.js'; import type { DriveFilesRepository, NotesRepository, UserProfilesRepository, UsersRepository } from '@/models/_.js'; import type Logger from '@/logger.js'; import { DriveService } from '@/core/DriveService.js'; -import type { MiDriveFile } from '@/models/DriveFile.js'; -import type { MiNote } from '@/models/Note.js'; +import type { MiUser } from '@/models/User.js'; import { EmailService } from '@/core/EmailService.js'; import { bindThis } from '@/decorators.js'; import { SearchService } from '@/core/SearchService.js'; @@ -43,6 +41,50 @@ export class DeleteAccountProcessorService { this.logger = this.queueLoggerService.logger.createSubLogger('delete-account'); } + private async deleteNotes(user: MiUser) { + while (true) { + const notes = await this.notesRepository.find({ + where: { + userId: user.id, + }, + take: 100, + }); + + if (notes.length === 0) { + break; + } + + await this.notesRepository.delete(notes.map(note => note.id)); + + for (const note of notes) { + await this.searchService.unindexNote(note); + } + } + + this.logger.succ('All of notes deleted'); + } + + private async deleteFiles(user: MiUser) { + while (true) { + const files = await this.driveFilesRepository.find({ + where: { + userId: user.id, + }, + take: 10, + }); + + if (files.length === 0) { + break; + } + + for (const file of files) { + await this.driveService.deleteFileSync(file); + } + } + + this.logger.succ('All of files deleted'); + } + @bindThis public async process(job: Bull.Job): Promise { this.logger.info(`Deleting account of ${job.data.user.id} ...`); @@ -52,70 +94,15 @@ export class DeleteAccountProcessorService { return; } - { // Delete notes - let cursor: MiNote['id'] | null = null; - - while (true) { - const notes = await this.notesRepository.find({ - where: { - userId: user.id, - ...(cursor ? { id: MoreThan(cursor) } : {}), - }, - take: 100, - order: { - id: 1, - }, - }) as MiNote[]; - - if (notes.length === 0) { - break; - } - - cursor = notes[notes.length - 1].id; - - await this.notesRepository.delete(notes.map(note => note.id)); - - for (const note of notes) { - await this.searchService.unindexNote(note); - } - } - - this.logger.succ('All of notes deleted'); - } - - { // Delete files - let cursor: MiDriveFile['id'] | null = null; - - while (true) { - const files = await this.driveFilesRepository.find({ - where: { - userId: user.id, - ...(cursor ? { id: MoreThan(cursor) } : {}), - }, - take: 10, - order: { - id: 1, - }, - }) as MiDriveFile[]; - - if (files.length === 0) { - break; - } - - cursor = files[files.length - 1].id; - - for (const file of files) { - await this.driveService.deleteFileSync(file); - } - } - - this.logger.succ('All of files deleted'); - } + await Promise.all([ + this.deleteNotes(user), + this.deleteFiles(user), + ]); { // Send email notification const profile = await this.userProfilesRepository.findOneByOrFail({ userId: user.id }); if (profile.email && profile.emailVerified) { - this.emailService.sendEmail(profile.email, 'Account deleted', + await this.emailService.sendEmail(profile.email, 'Account deleted', 'Your account has been deleted.', 'Your account has been deleted.'); }