paricafe/packages/backend/src/core/DeleteAccountService.ts

44 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-09-17 13:27:08 -05:00
import { Inject, Injectable } from '@nestjs/common';
2022-09-20 15:33:11 -05:00
import type { UsersRepository } from '@/models/index.js';
2022-09-17 13:27:08 -05:00
import { QueueService } from '@/core/QueueService.js';
import { UserSuspendService } from '@/core/UserSuspendService.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js';
2022-09-17 13:27:08 -05:00
@Injectable()
export class DeleteAccountService {
constructor(
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
private userSuspendService: UserSuspendService,
private queueService: QueueService,
2023-02-03 19:02:03 -06:00
private globalEventService: GlobalEventService,
2022-09-17 13:27:08 -05:00
) {
}
@bindThis
2022-09-17 13:27:08 -05:00
public async deleteAccount(user: {
id: string;
host: string | null;
}): Promise<void> {
const _user = await this.usersRepository.findOneByOrFail({ id: user.id });
if (_user.isRoot) throw new Error('cannot delete a root account');
2022-09-17 13:27:08 -05:00
// 物理削除する前にDelete activityを送信する
await this.userSuspendService.doPostSuspend(user).catch(e => {});
this.queueService.createDeleteAccountJob(user, {
soft: false,
});
await this.usersRepository.update(user.id, {
isDeleted: true,
});
// Terminate streaming
2023-02-03 19:02:03 -06:00
this.globalEventService.publishUserEvent(user.id, 'terminate', {});
2022-09-17 13:27:08 -05:00
}
}