From 9208825975f56bab8aca7ae8d6507f6cfe0f599a Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 22 Sep 2021 17:34:48 +0900 Subject: [PATCH] =?UTF-8?q?feat(server):=20=E7=AE=A1=E7=90=86=E8=80=85?= =?UTF-8?q?=E7=94=A8=E3=82=A2=E3=82=AB=E3=82=A6=E3=83=B3=E3=83=88=E5=89=8A?= =?UTF-8?q?=E9=99=A4API=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 動作確認済み Resolve #7735 --- CHANGELOG.md | 2 + src/queue/index.ts | 5 +- src/queue/processors/db/delete-account.ts | 11 +++- src/queue/types.ts | 7 ++- .../api/endpoints/admin/accounts/delete.ts | 58 +++++++++++++++++++ src/server/api/endpoints/i/delete-account.ts | 4 +- 6 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 src/server/api/endpoints/admin/accounts/delete.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index cf5621fc0..e034c4e55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ - ActivityPub: リモートユーザーのDeleteアクティビティに対応 - ActivityPub: add resolver check for blocked instance - ActivityPub: deliverキューのメモリ使用量を削減 +- API: 管理者用アカウント削除APIを実装(/admin/accounts/delete) + - リモートユーザーの削除も可能に - アカウントが凍結された場合に、凍結された旨を表示してからログアウトするように - 凍結されたアカウントにログインしようとしたときに、凍結されている旨を表示するように - リスト、アンテナタイムラインを個別ページとして分割 diff --git a/src/queue/index.ts b/src/queue/index.ts index ee34ed47e..0ce10a4c6 100644 --- a/src/queue/index.ts +++ b/src/queue/index.ts @@ -173,9 +173,10 @@ export function createImportUserListsJob(user: ThinUser, fileId: DriveFile['id'] }); } -export function createDeleteAccountJob(user: ThinUser) { +export function createDeleteAccountJob(user: ThinUser, opts: { soft?: boolean; }) { return dbQueue.add('deleteAccount', { - user: user + user: user, + soft: opts.soft }, { removeOnComplete: true, removeOnFail: true diff --git a/src/queue/processors/db/delete-account.ts b/src/queue/processors/db/delete-account.ts index 65327754c..e54f38e35 100644 --- a/src/queue/processors/db/delete-account.ts +++ b/src/queue/processors/db/delete-account.ts @@ -1,7 +1,7 @@ import * as Bull from 'bull'; import { queueLogger } from '../../logger'; import { DriveFiles, Notes, UserProfiles, Users } from '@/models/index'; -import { DbUserJobData } from '@/queue/types'; +import { DbUserDeleteJobData } from '@/queue/types'; import { Note } from '@/models/entities/note'; import { DriveFile } from '@/models/entities/drive-file'; import { MoreThan } from 'typeorm'; @@ -10,7 +10,7 @@ import { sendEmail } from '@/services/send-email'; const logger = queueLogger.createSubLogger('delete-account'); -export async function deleteAccount(job: Bull.Job): Promise { +export async function deleteAccount(job: Bull.Job): Promise { logger.info(`Deleting account of ${job.data.user.id} ...`); const user = await Users.findOne(job.data.user.id); @@ -83,7 +83,12 @@ export async function deleteAccount(job: Bull.Job): Promise { + const user = await Users.findOne(ps.userId); + + if (user == null) { + throw new Error('user not found'); + } + + if (user.isAdmin) { + throw new Error('cannot suspend admin'); + } + + if (user.isModerator) { + throw new Error('cannot suspend moderator'); + } + + if (Users.isLocalUser(user)) { + // 物理削除する前にDelete activityを送信する + await doPostSuspend(user).catch(e => {}); + + createDeleteAccountJob(user, { + soft: false + }); + } else { + createDeleteAccountJob(user, { + soft: true // リモートユーザーの削除は、完全にDBから物理削除してしまうと再度連合してきてアカウントが復活する可能性があるため、soft指定する + }); + } + + await Users.update(user.id, { + isDeleted: true, + }); + + if (Users.isLocalUser(user)) { + // Terminate streaming + publishUserEvent(user.id, 'terminate', {}); + } +}); diff --git a/src/server/api/endpoints/i/delete-account.ts b/src/server/api/endpoints/i/delete-account.ts index 77f11925c..10e5adf64 100644 --- a/src/server/api/endpoints/i/delete-account.ts +++ b/src/server/api/endpoints/i/delete-account.ts @@ -35,7 +35,9 @@ export default define(meta, async (ps, user) => { // 物理削除する前にDelete activityを送信する await doPostSuspend(user).catch(e => {}); - createDeleteAccountJob(user); + createDeleteAccountJob(user, { + soft: false + }); await Users.update(user.id, { isDeleted: true,