From a015524cb5874dd33c884588dd2e35faa63ca08f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E3=81=93=E3=81=B4=E3=81=AA=E3=81=9F=E3=81=BF=E3=81=BD?=
 <syuilotan@yahoo.co.jp>
Date: Thu, 12 Apr 2018 07:13:15 +0900
Subject: [PATCH] wip

---
 src/models/following.ts | 27 +++++++++++++++++++++++++++
 src/models/user.ts      |  8 +++++++-
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/src/models/following.ts b/src/models/following.ts
index b4090d8c7e..f10e349ee9 100644
--- a/src/models/following.ts
+++ b/src/models/following.ts
@@ -11,3 +11,30 @@ export type IFollowing = {
 	followeeId: mongo.ObjectID;
 	followerId: mongo.ObjectID;
 };
+
+/**
+ * Followingを物理削除します
+ */
+export async function deleteFollowing(following: string | mongo.ObjectID | IFollowing) {
+	let f: IFollowing;
+
+	// Populate
+	if (mongo.ObjectID.prototype.isPrototypeOf(following)) {
+		f = await Following.findOne({
+			_id: following
+		});
+	} else if (typeof following === 'string') {
+		f = await Following.findOne({
+			_id: new mongo.ObjectID(following)
+		});
+	} else {
+		f = following as IFollowing;
+	}
+
+	if (f == null) return;
+
+	// このFollowingを削除
+	await Following.remove({
+		_id: f._id
+	});
+}
diff --git a/src/models/user.ts b/src/models/user.ts
index ff1c11e76c..cbc445256b 100644
--- a/src/models/user.ts
+++ b/src/models/user.ts
@@ -3,7 +3,7 @@ import deepcopy = require('deepcopy');
 import rap from '@prezzemolo/rap';
 import db from '../db/mongodb';
 import Note, { INote, pack as packNote, deleteNote } from './note';
-import Following from './following';
+import Following, { deleteFollowing } from './following';
 import Mute, { deleteMute } from './mute';
 import getFriends from '../server/api/common/get-friends';
 import config from '../config';
@@ -212,8 +212,14 @@ export async function deleteUser(user: string | mongo.ObjectID | IUser) {
 	).map(x => deleteMute(x)));
 
 	// このユーザーのFollowingをすべて削除
+	await Promise.all((
+		await Following.find({ followerId: u._id })
+	).map(x => deleteFollowing(x)));
 
 	// このユーザーへのFollowingをすべて削除
+	await Promise.all((
+		await Following.find({ followeeId: u._id })
+	).map(x => deleteFollowing(x)));
 
 	// このユーザーのFollowingLogをすべて削除