From ff67fb337ea74099ba45e705473d46cd1eb65128 Mon Sep 17 00:00:00 2001
From: Ehsan Javadynia <31900907+ehsanjavadynia@users.noreply.github.com>
Date: Sat, 30 Jan 2021 05:39:46 +0330
Subject: [PATCH] using set instead of array for search (#7126)

* Resolve #6905

* Resolve #6905

* Resolve #6905
---
 src/misc/is-muted-user-related.ts                 |  8 ++++----
 src/server/api/stream/channels/home-timeline.ts   |  4 ++--
 src/server/api/stream/channels/hybrid-timeline.ts |  4 ++--
 src/server/api/stream/channels/local-timeline.ts  |  2 +-
 src/server/api/stream/channels/main.ts            |  4 ++--
 src/server/api/stream/index.ts                    | 12 ++++++------
 src/services/add-note-to-antenna.ts               |  2 +-
 7 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/src/misc/is-muted-user-related.ts b/src/misc/is-muted-user-related.ts
index 6f074bcb90..2caa743f95 100644
--- a/src/misc/is-muted-user-related.ts
+++ b/src/misc/is-muted-user-related.ts
@@ -1,13 +1,13 @@
-export function isMutedUserRelated(note: any, mutedUserIds: string[]): boolean {
-	if (mutedUserIds.includes(note.userId)) {
+export function isMutedUserRelated(note: any, mutedUserIds: Set<string>): boolean {
+	if (mutedUserIds.has(note.userId)) {
 		return true;
 	}
 
-	if (note.reply != null && mutedUserIds.includes(note.reply.userId)) {
+	if (note.reply != null && mutedUserIds.has(note.reply.userId)) {
 		return true;
 	}
 
-	if (note.renote != null && mutedUserIds.includes(note.renote.userId)) {
+	if (note.renote != null && mutedUserIds.has(note.renote.userId)) {
 		return true;
 	}
 
diff --git a/src/server/api/stream/channels/home-timeline.ts b/src/server/api/stream/channels/home-timeline.ts
index 15fe7fa6f0..6cfa6eae7b 100644
--- a/src/server/api/stream/channels/home-timeline.ts
+++ b/src/server/api/stream/channels/home-timeline.ts
@@ -19,10 +19,10 @@ export default class extends Channel {
 	@autobind
 	private async onNote(note: PackedNote) {
 		if (note.channelId) {
-			if (!this.followingChannels.includes(note.channelId)) return;
+			if (!this.followingChannels.has(note.channelId)) return;
 		} else {
 			// その投稿のユーザーをフォローしていなかったら弾く
-			if ((this.user!.id !== note.userId) && !this.following.includes(note.userId)) return;
+			if ((this.user!.id !== note.userId) && !this.following.has(note.userId)) return;
 		}
 
 		if (['followers', 'specified'].includes(note.visibility)) {
diff --git a/src/server/api/stream/channels/hybrid-timeline.ts b/src/server/api/stream/channels/hybrid-timeline.ts
index 4dc5f01a32..a9e577cacb 100644
--- a/src/server/api/stream/channels/hybrid-timeline.ts
+++ b/src/server/api/stream/channels/hybrid-timeline.ts
@@ -29,9 +29,9 @@ export default class extends Channel {
 		// フォローしているチャンネルの投稿 の場合だけ
 		if (!(
 			(note.channelId == null && this.user!.id === note.userId) ||
-			(note.channelId == null && this.following.includes(note.userId)) ||
+			(note.channelId == null && this.following.has(note.userId)) ||
 			(note.channelId == null && ((note.user as PackedUser).host == null && note.visibility === 'public')) ||
-			(note.channelId != null && this.followingChannels.includes(note.channelId))
+			(note.channelId != null && this.followingChannels.has(note.channelId))
 		)) return;
 
 		if (['followers', 'specified'].includes(note.visibility)) {
diff --git a/src/server/api/stream/channels/local-timeline.ts b/src/server/api/stream/channels/local-timeline.ts
index baeae86603..a3a5e491fc 100644
--- a/src/server/api/stream/channels/local-timeline.ts
+++ b/src/server/api/stream/channels/local-timeline.ts
@@ -27,7 +27,7 @@ export default class extends Channel {
 	private async onNote(note: PackedNote) {
 		if ((note.user as PackedUser).host !== null) return;
 		if (note.visibility !== 'public') return;
-		if (note.channelId != null && !this.followingChannels.includes(note.channelId)) return;
+		if (note.channelId != null && !this.followingChannels.has(note.channelId)) return;
 
 		// リプライなら再pack
 		if (note.replyId != null) {
diff --git a/src/server/api/stream/channels/main.ts b/src/server/api/stream/channels/main.ts
index 22e664baca..b69c2ec355 100644
--- a/src/server/api/stream/channels/main.ts
+++ b/src/server/api/stream/channels/main.ts
@@ -16,7 +16,7 @@ export default class extends Channel {
 
 			switch (type) {
 				case 'notification': {
-					if (this.muting.includes(body.userId)) return;
+					if (this.muting.has(body.userId)) return;
 					if (body.note && body.note.isHidden) {
 						body.note = await Notes.pack(body.note.id, this.user, {
 							detail: true
@@ -25,7 +25,7 @@ export default class extends Channel {
 					break;
 				}
 				case 'mention': {
-					if (this.muting.includes(body.userId)) return;
+					if (this.muting.has(body.userId)) return;
 					if (body.isHidden) {
 						body = await Notes.pack(body.id, this.user, {
 							detail: true
diff --git a/src/server/api/stream/index.ts b/src/server/api/stream/index.ts
index 36e08ec05f..5b975d07db 100644
--- a/src/server/api/stream/index.ts
+++ b/src/server/api/stream/index.ts
@@ -19,9 +19,9 @@ import { UserProfile } from '../../../models/entities/user-profile';
 export default class Connection {
 	public user?: User;
 	public userProfile?: UserProfile;
-	public following: User['id'][] = [];
-	public muting: User['id'][] = [];
-	public followingChannels: ChannelModel['id'][] = [];
+	public following: Set<User['id']> = new Set();
+	public muting: Set<User['id']> = new Set();
+	public followingChannels: Set<ChannelModel['id']> = new Set();
 	public token?: AccessToken;
 	private wsConnection: websocket.connection;
 	public subscriber: EventEmitter;
@@ -267,7 +267,7 @@ export default class Connection {
 			select: ['followeeId']
 		});
 
-		this.following = followings.map(x => x.followeeId);
+		this.following = new Set<string>(followings.map(x => x.followeeId));
 	}
 
 	@autobind
@@ -279,7 +279,7 @@ export default class Connection {
 			select: ['muteeId']
 		});
 
-		this.muting = mutings.map(x => x.muteeId);
+		this.muting = new Set<string>(mutings.map(x => x.muteeId));
 	}
 
 	@autobind
@@ -291,7 +291,7 @@ export default class Connection {
 			select: ['followeeId']
 		});
 
-		this.followingChannels = followings.map(x => x.followeeId);
+		this.followingChannels = new Set<string>(followings.map(x => x.followeeId));
 	}
 
 	@autobind
diff --git a/src/services/add-note-to-antenna.ts b/src/services/add-note-to-antenna.ts
index e486494c9b..f11607fd43 100644
--- a/src/services/add-note-to-antenna.ts
+++ b/src/services/add-note-to-antenna.ts
@@ -40,7 +40,7 @@ export async function addNoteToAntenna(antenna: Antenna, note: Note, noteUser: U
 			_note.renote = await Notes.findOne(note.renoteId).then(ensure);
 		}
 
-		if (isMutedUserRelated(_note, mutings.map(x => x.muteeId))) {
+		if (isMutedUserRelated(_note, new Set<string>(mutings.map(x => x.muteeId)))) {
 			return;
 		}
 
-- 
GitLab