From 120474ec6a4744e111eb06fb26d1638a7fb0eb6c Mon Sep 17 00:00:00 2001
From: syuilo <syuilotan@yahoo.co.jp>
Date: Tue, 30 Oct 2018 21:55:16 +0900
Subject: [PATCH] Fix bug

---
 src/services/note/create.ts | 14 ++++++++------
 test/api.ts                 | 18 ++++++++++++++++++
 2 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/src/services/note/create.ts b/src/services/note/create.ts
index 6d371e370f..7e97740edd 100644
--- a/src/services/note/create.ts
+++ b/src/services/note/create.ts
@@ -611,19 +611,21 @@ function incNotesCount(user: IUser) {
 async function extractMentionedUsers(tokens: ReturnType<typeof parse>): Promise<IUser[]> {
 	if (tokens == null) return [];
 
-	const mentionTokens = unique(
-		tokens
-			.filter(t => t.type == 'mention') as TextElementMention[]
-	);
+	const mentionTokens = tokens
+		.filter(t => t.type == 'mention') as TextElementMention[];
 
-	const mentionedUsers = unique(
+	let mentionedUsers =
 		erase(null, await Promise.all(mentionTokens.map(async m => {
 			try {
 				return await resolveUser(m.username, m.host);
 			} catch (e) {
 				return null;
 			}
-		})))
+		})));
+
+	// Drop duplicate users
+	mentionedUsers = mentionedUsers.filter((u, i, self) =>
+		i === self.findIndex(u2 => u._id.equals(u2._id))
 	);
 
 	return mentionedUsers;
diff --git a/test/api.ts b/test/api.ts
index 1bb8e20b17..6debea4d12 100644
--- a/test/api.ts
+++ b/test/api.ts
@@ -508,6 +508,24 @@ describe('API', () => {
 			}, me);
 			expect(res).have.status(400);
 		}));
+
+		it('同じユーザーに複数メンションしても内部的にまとめられる', async(async () => {
+			const alice = await signup({ username: 'alice' });
+			const bob = await signup({ username: 'bob' });
+			const post = {
+				text: '@bob @bob @bob yo'
+			};
+
+			const res = await request('/notes/create', post, alice);
+
+			expect(res).have.status(200);
+			expect(res.body).be.a('object');
+			expect(res.body).have.property('createdNote');
+			expect(res.body.createdNote).have.property('text').eql(post.text);
+
+			const noteDoc = await db.get('notes').findOne({ _id: res.body.createdNote.id });
+			expect(noteDoc.mentions.map((id: any) => id.toString())).eql([bob.id.toString()]);
+		}));
 	});
 
 	describe('notes/show', () => {