2021-08-21 05:41:56 +02:00
|
|
|
import * as Bull from 'bull';
|
|
|
|
import { queueLogger } from '../../logger';
|
2021-08-21 05:48:50 +02:00
|
|
|
import { DriveFiles, Notes, UserProfiles, Users } from '@/models/index';
|
2021-09-22 10:34:48 +02:00
|
|
|
import { DbUserDeleteJobData } from '@/queue/types';
|
2021-08-21 05:41:56 +02:00
|
|
|
import { Note } from '@/models/entities/note';
|
|
|
|
import { DriveFile } from '@/models/entities/drive-file';
|
|
|
|
import { MoreThan } from 'typeorm';
|
|
|
|
import { deleteFileSync } from '@/services/drive/delete-file';
|
2021-08-21 05:48:50 +02:00
|
|
|
import { sendEmail } from '@/services/send-email';
|
2021-08-21 05:41:56 +02:00
|
|
|
|
|
|
|
const logger = queueLogger.createSubLogger('delete-account');
|
|
|
|
|
2021-09-22 10:34:48 +02:00
|
|
|
export async function deleteAccount(job: Bull.Job<DbUserDeleteJobData>): Promise<string | void> {
|
2021-08-21 05:41:56 +02:00
|
|
|
logger.info(`Deleting account of ${job.data.user.id} ...`);
|
|
|
|
|
|
|
|
const user = await Users.findOne(job.data.user.id);
|
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Delete notes
|
|
|
|
let cursor: Note['id'] | null = null;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const notes = await Notes.find({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
...(cursor ? { id: MoreThan(cursor) } : {})
|
|
|
|
},
|
|
|
|
take: 100,
|
|
|
|
order: {
|
|
|
|
id: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (notes.length === 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor = notes[notes.length - 1].id;
|
|
|
|
|
|
|
|
await Notes.delete(notes.map(note => note.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.succ(`All of notes deleted`);
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Delete files
|
|
|
|
let cursor: DriveFile['id'] | null = null;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const files = await DriveFiles.find({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
...(cursor ? { id: MoreThan(cursor) } : {})
|
|
|
|
},
|
|
|
|
take: 10,
|
|
|
|
order: {
|
|
|
|
id: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (files.length === 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor = files[files.length - 1].id;
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
await deleteFileSync(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.succ(`All of files deleted`);
|
|
|
|
}
|
|
|
|
|
2021-08-21 05:48:50 +02:00
|
|
|
{ // Send email notification
|
|
|
|
const profile = await UserProfiles.findOneOrFail(user.id);
|
|
|
|
if (profile.email && profile.emailVerified) {
|
|
|
|
sendEmail(profile.email, 'Account deleted',
|
|
|
|
`Your account has been deleted.`,
|
|
|
|
`Your account has been deleted.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 10:34:48 +02:00
|
|
|
// soft指定されている場合は物理削除しない
|
|
|
|
if (job.data.soft) {
|
|
|
|
// nop
|
|
|
|
} else {
|
|
|
|
await Users.delete(job.data.user.id);
|
|
|
|
}
|
2021-08-21 05:41:56 +02:00
|
|
|
|
|
|
|
return 'Account deleted';
|
|
|
|
}
|