From 32afe77a269f414965373e3c53044c4a94cfeded Mon Sep 17 00:00:00 2001
From: syuilo <syuilotan@yahoo.co.jp>
Date: Sun, 16 Sep 2018 22:48:57 +0900
Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=88=86=E5=AE=9B=E3=81=A6=E3=81=AE?=
 =?UTF-8?q?=E6=8A=95=E7=A8=BF=E3=82=92=E3=82=BF=E3=82=A4=E3=83=A0=E3=83=A9?=
 =?UTF-8?q?=E3=82=A4=E3=83=B3=E3=81=A7=E8=A6=8B=E3=82=8C=E3=82=8B=E3=82=88?=
 =?UTF-8?q?=E3=81=86=E3=81=AB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 locales/ja-JP.yml                             |  3 +
 .../views/components/timeline.core.vue        |  6 +-
 .../app/desktop/views/components/timeline.vue |  2 +
 .../app/mobile/views/pages/home.timeline.vue  |  6 +-
 src/client/app/mobile/views/pages/home.vue    |  3 +
 src/models/note.ts                            |  2 +
 src/server/api/endpoints/notes/mentions.ts    | 61 ++++++++++---------
 src/services/note/create.ts                   |  4 ++
 8 files changed, 55 insertions(+), 32 deletions(-)

diff --git a/locales/ja-JP.yml b/locales/ja-JP.yml
index 3ced4dafe1..2a8cfebb57 100644
--- a/locales/ja-JP.yml
+++ b/locales/ja-JP.yml
@@ -167,6 +167,7 @@ common:
     local: "ローカル"
     hybrid: "ソーシャル"
     global: "グローバル"
+    mentions: "あなた宛て"
     notifications: "通知"
     list: "リスト"
     swap-left: "左に移動"
@@ -913,6 +914,7 @@ desktop/views/components/timeline.vue:
   local: "ローカル"
   hybrid: "ソーシャル"
   global: "グローバル"
+  mentions: "あなた宛て"
   list: "リスト"
 
 desktop/views/components/ui.header.vue:
@@ -1314,6 +1316,7 @@ mobile/views/pages/home.vue:
   local: "ローカル"
   hybrid: "ソーシャル"
   global: "グローバル"
+  mentions: "あなた宛て"
 
 mobile/views/pages/tag.vue:
   no-posts-found: "ハッシュタグ「{}」が付けられた投稿は見つかりませんでした。"
diff --git a/src/client/app/desktop/views/components/timeline.core.vue b/src/client/app/desktop/views/components/timeline.core.vue
index 25fd5d36ac..b6b5cca817 100644
--- a/src/client/app/desktop/views/components/timeline.core.vue
+++ b/src/client/app/desktop/views/components/timeline.core.vue
@@ -48,6 +48,7 @@ export default Vue.extend({
 				case 'local': return (this as any).os.streams.localTimelineStream;
 				case 'hybrid': return (this as any).os.streams.hybridTimelineStream;
 				case 'global': return (this as any).os.streams.globalTimelineStream;
+				case 'mentions': return (this as any).os.stream;
 			}
 		},
 
@@ -57,6 +58,7 @@ export default Vue.extend({
 				case 'local': return 'notes/local-timeline';
 				case 'hybrid': return 'notes/hybrid-timeline';
 				case 'global': return 'notes/global-timeline';
+				case 'mentions': return 'notes/mentions';
 			}
 		},
 
@@ -69,7 +71,7 @@ export default Vue.extend({
 		this.connection = this.stream.getConnection();
 		this.connectionId = this.stream.use();
 
-		this.connection.on('note', this.onNote);
+		this.connection.on(this.src == 'mentions' ? 'mention' : 'note', this.onNote);
 		if (this.src == 'home') {
 			this.connection.on('follow', this.onChangeFollowing);
 			this.connection.on('unfollow', this.onChangeFollowing);
@@ -81,7 +83,7 @@ export default Vue.extend({
 	},
 
 	beforeDestroy() {
-		this.connection.off('note', this.onNote);
+		this.connection.off(this.src == 'mentions' ? 'mention' : 'note', this.onNote);
 		if (this.src == 'home') {
 			this.connection.off('follow', this.onChangeFollowing);
 			this.connection.off('unfollow', this.onChangeFollowing);
diff --git a/src/client/app/desktop/views/components/timeline.vue b/src/client/app/desktop/views/components/timeline.vue
index 8d72016f22..3e51d12883 100644
--- a/src/client/app/desktop/views/components/timeline.vue
+++ b/src/client/app/desktop/views/components/timeline.vue
@@ -5,6 +5,7 @@
 		<span :data-active="src == 'local'" @click="src = 'local'" v-if="enableLocalTimeline">%fa:R comments% %i18n:@local%</span>
 		<span :data-active="src == 'hybrid'" @click="src = 'hybrid'" v-if="enableLocalTimeline">%fa:share-alt% %i18n:@hybrid%</span>
 		<span :data-active="src == 'global'" @click="src = 'global'">%fa:globe% %i18n:@global%</span>
+		<span :data-active="src == 'mentions'" @click="src = 'mentions'">%fa:at% %i18n:@mentions%</span>
 		<span :data-active="src == 'list'" @click="src = 'list'" v-if="list">%fa:list% {{ list.title }}</span>
 		<button @click="chooseList" title="%i18n:@list%">%fa:list%</button>
 	</header>
@@ -12,6 +13,7 @@
 	<x-core v-if="src == 'local'" ref="tl" key="local" src="local"/>
 	<x-core v-if="src == 'hybrid'" ref="tl" key="hybrid" src="hybrid"/>
 	<x-core v-if="src == 'global'" ref="tl" key="global" src="global"/>
+	<x-core v-if="src == 'mentions'" ref="tl" key="mentions" src="mentions"/>
 	<mk-user-list-timeline v-if="src == 'list'" ref="tl" :key="list.id" :list="list"/>
 </div>
 </template>
diff --git a/src/client/app/mobile/views/pages/home.timeline.vue b/src/client/app/mobile/views/pages/home.timeline.vue
index 416b006cd8..d4fcea1f93 100644
--- a/src/client/app/mobile/views/pages/home.timeline.vue
+++ b/src/client/app/mobile/views/pages/home.timeline.vue
@@ -47,6 +47,7 @@ export default Vue.extend({
 				case 'local': return (this as any).os.streams.localTimelineStream;
 				case 'hybrid': return (this as any).os.streams.hybridTimelineStream;
 				case 'global': return (this as any).os.streams.globalTimelineStream;
+				case 'mentions': return (this as any).os.stream;
 			}
 		},
 
@@ -56,6 +57,7 @@ export default Vue.extend({
 				case 'local': return 'notes/local-timeline';
 				case 'hybrid': return 'notes/hybrid-timeline';
 				case 'global': return 'notes/global-timeline';
+				case 'mentions': return 'notes/mentions';
 			}
 		},
 
@@ -68,7 +70,7 @@ export default Vue.extend({
 		this.connection = this.stream.getConnection();
 		this.connectionId = this.stream.use();
 
-		this.connection.on('note', this.onNote);
+		this.connection.on(this.src == 'mentions' ? 'mention' : 'note', this.onNote);
 		if (this.src == 'home') {
 			this.connection.on('follow', this.onChangeFollowing);
 			this.connection.on('unfollow', this.onChangeFollowing);
@@ -78,7 +80,7 @@ export default Vue.extend({
 	},
 
 	beforeDestroy() {
-		this.connection.off('note', this.onNote);
+		this.connection.off(this.src == 'mentions' ? 'mention' : 'note', this.onNote);
 		if (this.src == 'home') {
 			this.connection.off('follow', this.onChangeFollowing);
 			this.connection.off('unfollow', this.onChangeFollowing);
diff --git a/src/client/app/mobile/views/pages/home.vue b/src/client/app/mobile/views/pages/home.vue
index 333ca1a7a1..3150bb02b4 100644
--- a/src/client/app/mobile/views/pages/home.vue
+++ b/src/client/app/mobile/views/pages/home.vue
@@ -6,6 +6,7 @@
 			<span v-if="src == 'local'">%fa:R comments%%i18n:@local%</span>
 			<span v-if="src == 'hybrid'">%fa:share-alt%%i18n:@hybrid%</span>
 			<span v-if="src == 'global'">%fa:globe%%i18n:@global%</span>
+			<span v-if="src == 'mentions'">%fa:at%%i18n:@mentions%</span>
 			<span v-if="src == 'list'">%fa:list%{{ list.title }}</span>
 		</span>
 		<span style="margin-left:8px">
@@ -27,6 +28,7 @@
 					<span :data-active="src == 'local'" @click="src = 'local'" v-if="enableLocalTimeline">%fa:R comments% %i18n:@local%</span>
 					<span :data-active="src == 'hybrid'" @click="src = 'hybrid'" v-if="enableLocalTimeline">%fa:share-alt% %i18n:@hybrid%</span>
 					<span :data-active="src == 'global'" @click="src = 'global'">%fa:globe% %i18n:@global%</span>
+					<span :data-active="src == 'mentions'" @click="src = 'mentions'">%fa:at% %i18n:@mentions%</span>
 					<template v-if="lists">
 						<span v-for="l in lists" :data-active="src == 'list' && list == l" @click="src = 'list'; list = l" :key="l.id">%fa:list% {{ l.title }}</span>
 					</template>
@@ -39,6 +41,7 @@
 			<x-tl v-if="src == 'local'" ref="tl" key="local" src="local"/>
 			<x-tl v-if="src == 'hybrid'" ref="tl" key="hybrid" src="hybrid"/>
 			<x-tl v-if="src == 'global'" ref="tl" key="global" src="global"/>
+			<x-tl v-if="src == 'mentions'" ref="tl" key="mentions" src="mentions"/>
 			<mk-user-list-timeline v-if="src == 'list'" ref="tl" :key="list.id" :list="list"/>
 		</div>
 	</main>
diff --git a/src/models/note.ts b/src/models/note.ts
index 6530d0b324..62b1b3ecb1 100644
--- a/src/models/note.ts
+++ b/src/models/note.ts
@@ -17,6 +17,8 @@ import Following from './following';
 const Note = db.get<INote>('notes');
 Note.createIndex('uri', { sparse: true, unique: true });
 Note.createIndex('userId');
+Note.createIndex('mentions');
+Note.createIndex('visibleUserIds');
 Note.createIndex('tagsLower');
 Note.createIndex('_files.contentType');
 Note.createIndex({
diff --git a/src/server/api/endpoints/notes/mentions.ts b/src/server/api/endpoints/notes/mentions.ts
index a7fb14d8a9..3b2e262e4f 100644
--- a/src/server/api/endpoints/notes/mentions.ts
+++ b/src/server/api/endpoints/notes/mentions.ts
@@ -3,6 +3,7 @@ import Note from '../../../../models/note';
 import { getFriendIds } from '../../common/get-friends';
 import { pack } from '../../../../models/note';
 import { ILocalUser } from '../../../../models/user';
+import getParams from '../../get-params';
 
 export const meta = {
 	desc: {
@@ -10,42 +11,48 @@ export const meta = {
 		'en-US': 'Get mentions of myself.'
 	},
 
-	requireCredential: true
+	requireCredential: true,
+
+	params: {
+		following: $.bool.optional.note({
+			default: false
+		}),
+
+		limit: $.num.optional.range(1, 100).note({
+			default: 10
+		}),
+
+		sinceId: $.type(ID).optional.note({
+		}),
+
+		untilId: $.type(ID).optional.note({
+		}),
+	}
 };
 
 export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
-	// Get 'following' parameter
-	const [following = false, followingError] =
-		$.bool.optional.get(params.following);
-	if (followingError) return rej('invalid following param');
-
-	// Get 'limit' parameter
-	const [limit = 10, limitErr] = $.num.optional.range(1, 100).get(params.limit);
-	if (limitErr) return rej('invalid limit param');
-
-	// Get 'sinceId' parameter
-	const [sinceId, sinceIdErr] = $.type(ID).optional.get(params.sinceId);
-	if (sinceIdErr) return rej('invalid sinceId param');
-
-	// Get 'untilId' parameter
-	const [untilId, untilIdErr] = $.type(ID).optional.get(params.untilId);
-	if (untilIdErr) return rej('invalid untilId param');
+	const [ps, psErr] = getParams(meta, params);
+	if (psErr) throw psErr;
 
 	// Check if both of sinceId and untilId is specified
-	if (sinceId && untilId) {
+	if (ps.sinceId && ps.untilId) {
 		return rej('cannot set sinceId and untilId');
 	}
 
 	// Construct query
 	const query = {
-		mentions: user._id
+		$or: [{
+			mentions: user._id
+		}, {
+			visibleUserIds: user._id
+		}]
 	} as any;
 
 	const sort = {
 		_id: -1
 	};
 
-	if (following) {
+	if (ps.following) {
 		const followingIds = await getFriendIds(user._id);
 
 		query.userId = {
@@ -53,26 +60,24 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
 		};
 	}
 
-	if (sinceId) {
+	if (ps.sinceId) {
 		sort._id = 1;
 		query._id = {
-			$gt: sinceId
+			$gt: ps.sinceId
 		};
-	} else if (untilId) {
+	} else if (ps.untilId) {
 		query._id = {
-			$lt: untilId
+			$lt: ps.untilId
 		};
 	}
 
 	// Issue query
 	const mentions = await Note
 		.find(query, {
-			limit: limit,
+			limit: ps.limit,
 			sort: sort
 		});
 
 	// Serialize
-	res(await Promise.all(mentions.map(async mention =>
-		await pack(mention, user)
-	)));
+	res(await Promise.all(mentions.map(mention => pack(mention, user))));
 });
diff --git a/src/services/note/create.ts b/src/services/note/create.ts
index 771e9cade8..aa65cfe0cf 100644
--- a/src/services/note/create.ts
+++ b/src/services/note/create.ts
@@ -138,6 +138,10 @@ export default async (user: IUser, data: Option, silent = false) => new Promise<
 
 	const mentionedUsers = await extractMentionedUsers(tokens);
 
+	if (data.reply && !user._id.equals(data.reply.userId) && !mentionedUsers.some(u => u._id.equals(data.reply.userId))) {
+		mentionedUsers.push(await User.findOne({ _id: data.reply.userId }));
+	}
+
 	const note = await insertNote(user, data, tags, mentionedUsers);
 
 	res(note);