From 9c4e64b7b54886f97a9770b9d4904be29ba68eec Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Thu, 18 Jul 2019 02:03:28 +0900 Subject: [PATCH] Send Delete activity on suspend (#5165) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Send Delete Person activity * Delete activityの後にフォロー解除する * アカウント削除でもDelete activity --- .../api/endpoints/admin/suspend-user.ts | 6 +++- .../api/endpoints/admin/unsuspend-user.ts | 3 ++ src/server/api/endpoints/i/delete-account.ts | 4 +++ src/services/suspend-user.ts | 34 ++++++++++++++++++ src/services/unsuspend-user.ts | 35 +++++++++++++++++++ 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/services/suspend-user.ts create mode 100644 src/services/unsuspend-user.ts diff --git a/src/server/api/endpoints/admin/suspend-user.ts b/src/server/api/endpoints/admin/suspend-user.ts index 09fdbb070..6ba0d9150 100644 --- a/src/server/api/endpoints/admin/suspend-user.ts +++ b/src/server/api/endpoints/admin/suspend-user.ts @@ -5,6 +5,7 @@ import deleteFollowing from '../../../../services/following/delete'; import { Users, Followings } from '../../../../models'; import { User } from '../../../../models/entities/user'; import { insertModerationLog } from '../../../../services/insert-moderation-log'; +import { doPostSuspend } from '../../../../services/suspend-user'; export const meta = { desc: { @@ -51,7 +52,10 @@ export default define(meta, async (ps, me) => { targetId: user.id, }); - unFollowAll(user); + (async () => { + await doPostSuspend(user).catch(e => {}); + await unFollowAll(user).catch(e => {}); + })(); }); async function unFollowAll(follower: User) { diff --git a/src/server/api/endpoints/admin/unsuspend-user.ts b/src/server/api/endpoints/admin/unsuspend-user.ts index a1c80d312..237585e27 100644 --- a/src/server/api/endpoints/admin/unsuspend-user.ts +++ b/src/server/api/endpoints/admin/unsuspend-user.ts @@ -3,6 +3,7 @@ import { ID } from '../../../../misc/cafy-id'; import define from '../../define'; import { Users } from '../../../../models'; import { insertModerationLog } from '../../../../services/insert-moderation-log'; +import { doPostUnsuspend } from '../../../../services/unsuspend-user'; export const meta = { desc: { @@ -40,4 +41,6 @@ export default define(meta, async (ps, me) => { insertModerationLog(me, 'unsuspend', { targetId: user.id, }); + + doPostUnsuspend(user); }); diff --git a/src/server/api/endpoints/i/delete-account.ts b/src/server/api/endpoints/i/delete-account.ts index 8ec85c9f4..b4950cb1f 100644 --- a/src/server/api/endpoints/i/delete-account.ts +++ b/src/server/api/endpoints/i/delete-account.ts @@ -3,6 +3,7 @@ import * as bcrypt from 'bcryptjs'; import define from '../../define'; import { Users, UserProfiles } from '../../../../models'; import { ensure } from '../../../../prelude/ensure'; +import { doPostSuspend } from '../../../../services/suspend-user'; export const meta = { requireCredential: true, @@ -26,5 +27,8 @@ export default define(meta, async (ps, user) => { throw new Error('incorrect password'); } + // 物理削除する前にDelete activityを送信する + await doPostSuspend(user).catch(e => {}); + await Users.delete(user.id); }); diff --git a/src/services/suspend-user.ts b/src/services/suspend-user.ts new file mode 100644 index 000000000..a85188acb --- /dev/null +++ b/src/services/suspend-user.ts @@ -0,0 +1,34 @@ +import renderDelete from '../remote/activitypub/renderer/delete'; +import { renderActivity } from '../remote/activitypub/renderer'; +import { deliver } from '../queue'; +import config from '../config'; +import { User } from '../models/entities/user'; +import { Users, Followings } from '../models'; +import { Not, IsNull } from 'typeorm'; + +export async function doPostSuspend(user: User) { + if (Users.isLocalUser(user)) { + // 知り得る全SharedInboxにDelete配信 + const content = renderActivity(renderDelete(`${config.url}/users/${user.id}`, user)); + + const queue: string[] = []; + + const followings = await Followings.find({ + where: [ + { followerSharedInbox: Not(IsNull()) }, + { followeeSharedInbox: Not(IsNull()) } + ], + select: ['followerSharedInbox', 'followeeSharedInbox'] + }); + + const inboxes = followings.map(x => x.followerSharedInbox || x.followeeSharedInbox); + + for (const inbox of inboxes) { + if (inbox != null && !queue.includes(inbox)) queue.push(inbox); + } + + for (const inbox of queue) { + deliver(user as any, content, inbox); + } + } +} diff --git a/src/services/unsuspend-user.ts b/src/services/unsuspend-user.ts new file mode 100644 index 000000000..6cab37582 --- /dev/null +++ b/src/services/unsuspend-user.ts @@ -0,0 +1,35 @@ +import renderDelete from '../remote/activitypub/renderer/delete'; +import renderUndo from '../remote/activitypub/renderer/undo'; +import { renderActivity } from '../remote/activitypub/renderer'; +import { deliver } from '../queue'; +import config from '../config'; +import { User } from '../models/entities/user'; +import { Users, Followings } from '../models'; +import { Not, IsNull } from 'typeorm'; + +export async function doPostUnsuspend(user: User) { + if (Users.isLocalUser(user)) { + // 知り得る全SharedInboxにUndo Delete配信 + const content = renderActivity(renderUndo(renderDelete(`${config.url}/users/${user.id}`, user), user)); + + const queue: string[] = []; + + const followings = await Followings.find({ + where: [ + { followerSharedInbox: Not(IsNull()) }, + { followeeSharedInbox: Not(IsNull()) } + ], + select: ['followerSharedInbox', 'followeeSharedInbox'] + }); + + const inboxes = followings.map(x => x.followerSharedInbox || x.followeeSharedInbox); + + for (const inbox of inboxes) { + if (inbox != null && !queue.includes(inbox)) queue.push(inbox); + } + + for (const inbox of queue) { + deliver(user as any, content, inbox); + } + } +}