2023-07-27 08:31:52 +03:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { MoreThan } from 'typeorm';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-23 00:21:31 +03:00
|
|
|
import type { DriveFilesRepository, NotesRepository, UserProfilesRepository, UsersRepository } from '@/models/index.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import type Logger from '@/logger.js';
|
|
|
|
import { DriveService } from '@/core/DriveService.js';
|
2023-08-16 11:51:28 +03:00
|
|
|
import type { MiDriveFile } from '@/models/entities/DriveFile.js';
|
|
|
|
import type { MiNote } from '@/models/entities/Note.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import { EmailService } from '@/core/EmailService.js';
|
2023-05-29 05:54:49 +03:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-07-19 05:27:50 +03:00
|
|
|
import { SearchService } from '@/core/SearchService.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import { QueueLoggerService } from '../QueueLoggerService.js';
|
2023-05-29 05:54:49 +03:00
|
|
|
import type * as Bull from 'bullmq';
|
2022-09-17 21:27:08 +03:00
|
|
|
import type { DbUserDeleteJobData } from '../types.js';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class DeleteAccountProcessorService {
|
2022-09-18 21:11:50 +03:00
|
|
|
private logger: Logger;
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
|
|
|
private driveService: DriveService,
|
|
|
|
private emailService: EmailService,
|
|
|
|
private queueLoggerService: QueueLoggerService,
|
2023-07-08 15:31:38 +03:00
|
|
|
private searchService: SearchService,
|
2022-09-17 21:27:08 +03:00
|
|
|
) {
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger = this.queueLoggerService.logger.createSubLogger('delete-account');
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2022-09-17 21:27:08 +03:00
|
|
|
public async process(job: Bull.Job<DbUserDeleteJobData>): Promise<string | void> {
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.info(`Deleting account of ${job.data.user.id} ...`);
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
const user = await this.usersRepository.findOneBy({ id: job.data.user.id });
|
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Delete notes
|
2023-08-16 11:51:28 +03:00
|
|
|
let cursor: MiNote['id'] | null = null;
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const notes = await this.notesRepository.find({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
...(cursor ? { id: MoreThan(cursor) } : {}),
|
|
|
|
},
|
|
|
|
take: 100,
|
|
|
|
order: {
|
|
|
|
id: 1,
|
|
|
|
},
|
2023-08-16 11:51:28 +03:00
|
|
|
}) as MiNote[];
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
if (notes.length === 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-14 04:45:01 +03:00
|
|
|
cursor = notes.at(-1)?.id ?? null;
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
await this.notesRepository.delete(notes.map(note => note.id));
|
2023-07-08 15:31:38 +03:00
|
|
|
|
|
|
|
for (const note of notes) {
|
|
|
|
await this.searchService.unindexNote(note);
|
|
|
|
}
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.succ('All of notes deleted');
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // Delete files
|
2023-08-16 11:51:28 +03:00
|
|
|
let cursor: MiDriveFile['id'] | null = null;
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const files = await this.driveFilesRepository.find({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
...(cursor ? { id: MoreThan(cursor) } : {}),
|
|
|
|
},
|
|
|
|
take: 10,
|
|
|
|
order: {
|
|
|
|
id: 1,
|
|
|
|
},
|
2023-08-16 11:51:28 +03:00
|
|
|
}) as MiDriveFile[];
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
if (files.length === 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-14 04:45:01 +03:00
|
|
|
cursor = files.at(-1)?.id ?? null;
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
await this.driveService.deleteFileSync(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 21:11:50 +03:00
|
|
|
this.logger.succ('All of files deleted');
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // Send email notification
|
|
|
|
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: user.id });
|
|
|
|
if (profile.email && profile.emailVerified) {
|
|
|
|
this.emailService.sendEmail(profile.email, 'Account deleted',
|
|
|
|
'Your account has been deleted.',
|
|
|
|
'Your account has been deleted.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// soft指定されている場合は物理削除しない
|
|
|
|
if (job.data.soft) {
|
|
|
|
// nop
|
|
|
|
} else {
|
|
|
|
await this.usersRepository.delete(job.data.user.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'Account deleted';
|
|
|
|
}
|
|
|
|
}
|