From e47c3549168237d8b9824e67cc534f53495d7845 Mon Sep 17 00:00:00 2001
From: syuilo <syuilotan@yahoo.co.jp>
Date: Mon, 11 Mar 2019 19:57:50 +0900
Subject: [PATCH] Refactor

---
 src/queue/processors/db/import-user-lists.ts | 19 +++-------------
 src/server/api/endpoints/users/lists/push.ts | 21 ++----------------
 src/services/user-list/push.ts               | 23 ++++++++++++++++++++
 3 files changed, 28 insertions(+), 35 deletions(-)
 create mode 100644 src/services/user-list/push.ts

diff --git a/src/queue/processors/db/import-user-lists.ts b/src/queue/processors/db/import-user-lists.ts
index aad11df7f2..d003090da8 100644
--- a/src/queue/processors/db/import-user-lists.ts
+++ b/src/queue/processors/db/import-user-lists.ts
@@ -6,7 +6,7 @@ import * as mongo from 'mongodb';
 import * as request from 'request';
 
 import { queueLogger } from '../../logger';
-import User, { isRemoteUser, fetchProxyAccount } from '../../../models/user';
+import User from '../../../models/user';
 import config from '../../../config';
 import UserList from '../../../models/user-list';
 import DriveFile from '../../../models/drive-file';
@@ -14,9 +14,7 @@ import chalk from 'chalk';
 import { getOriginalUrl } from '../../../misc/get-drive-file-url';
 import parseAcct from '../../../misc/acct/parse';
 import resolveUser from '../../../remote/resolve-user';
-import { renderActivity } from '../../../remote/activitypub/renderer';
-import renderFollow from '../../../remote/activitypub/renderer/follow';
-import { deliver } from '../..';
+import { pushUserToUserList } from '../../../services/user-list/push';
 
 const logger = queueLogger.createSubLogger('import-user-lists');
 
@@ -130,18 +128,7 @@ export async function importUserLists(job: Bull.Job, done: any): Promise<void> {
 			target = await resolveUser(username, host);
 		}
 
-		await UserList.update({ _id: list._id }, {
-			$push: {
-				userIds: target._id
-			}
-		});
-
-		// このインスタンス内にこのリモートユーザーをフォローしているユーザーがいなくても投稿を受け取るためにダミーのユーザーがフォローしたということにする
-		if (isRemoteUser(target)) {
-			const proxy = await fetchProxyAccount();
-			const content = renderActivity(renderFollow(proxy, user));
-			deliver(proxy, content, target.inbox);
-		}
+		pushUserToUserList(target, list);
 	}
 
 	logger.succ('Imported');
diff --git a/src/server/api/endpoints/users/lists/push.ts b/src/server/api/endpoints/users/lists/push.ts
index 6b47f9dc71..eea2f39a8c 100644
--- a/src/server/api/endpoints/users/lists/push.ts
+++ b/src/server/api/endpoints/users/lists/push.ts
@@ -1,14 +1,10 @@
 import $ from 'cafy';
 import ID, { transform } from '../../../../../misc/cafy-id';
 import UserList from '../../../../../models/user-list';
-import { pack as packUser, isRemoteUser, fetchProxyAccount } from '../../../../../models/user';
-import { publishUserListStream } from '../../../../../services/stream';
-import { renderActivity } from '../../../../../remote/activitypub/renderer';
-import renderFollow from '../../../../../remote/activitypub/renderer/follow';
-import { deliver } from '../../../../../queue';
 import define from '../../../define';
 import { ApiError } from '../../../error';
 import { getUser } from '../../../common/getters';
+import { pushUserToUserList } from '../../../../../services/user-list/push';
 
 export const meta = {
 	desc: {
@@ -81,18 +77,5 @@ export default define(meta, async (ps, me) => {
 	}
 
 	// Push the user
-	await UserList.update({ _id: userList._id }, {
-		$push: {
-			userIds: user._id
-		}
-	});
-
-	publishUserListStream(userList._id, 'userAdded', await packUser(user));
-
-	// このインスタンス内にこのリモートユーザーをフォローしているユーザーがいなくても投稿を受け取るためにダミーのユーザーがフォローしたということにする
-	if (isRemoteUser(user)) {
-		const proxy = await fetchProxyAccount();
-		const content = renderActivity(renderFollow(proxy, user));
-		deliver(proxy, content, user.inbox);
-	}
+	pushUserToUserList(user, userList);
 });
diff --git a/src/services/user-list/push.ts b/src/services/user-list/push.ts
new file mode 100644
index 0000000000..5ad4a14827
--- /dev/null
+++ b/src/services/user-list/push.ts
@@ -0,0 +1,23 @@
+import { pack as packUser, IUser, isRemoteUser, fetchProxyAccount } from '../../models/user';
+import UserList, { IUserList } from '../../models/user-list';
+import { renderActivity } from '../../remote/activitypub/renderer';
+import { deliver } from '../../queue';
+import renderFollow from '../../remote/activitypub/renderer/follow';
+import { publishUserListStream } from '../stream';
+
+export async function pushUserToUserList(target: IUser, list: IUserList) {
+	await UserList.update({ _id: list._id }, {
+		$push: {
+			userIds: target._id
+		}
+	});
+
+	publishUserListStream(list._id, 'userAdded', await packUser(target));
+
+	// このインスタンス内にこのリモートユーザーをフォローしているユーザーがいなくても投稿を受け取るためにダミーのユーザーがフォローしたということにする
+	if (isRemoteUser(target)) {
+		const proxy = await fetchProxyAccount();
+		const content = renderActivity(renderFollow(proxy, target));
+		deliver(proxy, content, target.inbox);
+	}
+}