From 4d1d25e02f9863ede37b6c981f3c2418b14cf3a7 Mon Sep 17 00:00:00 2001
From: syuilo <Syuilotan@yahoo.co.jp>
Date: Thu, 19 Oct 2023 08:07:22 +0900
Subject: [PATCH] perf(backend): improve my reaction population performance

---
 packages/backend/src/core/entities/NoteEntityService.ts | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/packages/backend/src/core/entities/NoteEntityService.ts b/packages/backend/src/core/entities/NoteEntityService.ts
index 9a59da4349..66dbb66167 100644
--- a/packages/backend/src/core/entities/NoteEntityService.ts
+++ b/packages/backend/src/core/entities/NoteEntityService.ts
@@ -177,10 +177,9 @@ export class NoteEntityService implements OnModuleInit {
 			const reaction = _hint_.myReactions.get(noteId);
 			if (reaction) {
 				return this.reactionService.convertLegacyReaction(reaction.reaction);
-			} else if (reaction === null) {
+			} else {
 				return undefined;
 			}
-		// 実装上抜けがあるだけかもしれないので、「ヒントに含まれてなかったら(=undefinedなら)return」のようにはしない
 		}
 
 		// パフォーマンスのためノートが作成されてから2秒以上経っていない場合はリアクションを取得しない
@@ -387,11 +386,11 @@ export class NoteEntityService implements OnModuleInit {
 			const renoteIds = notes.filter(n => n.renoteId != null).map(n => n.renoteId!);
 			// パフォーマンスのためノートが作成されてから2秒以上経っていない場合はリアクションを取得しない
 			const oldId = this.idService.gen(Date.now() - 2000);
-			const targets = [...notes.filter(n => n.id < oldId).map(n => n.id), ...renoteIds];
-			const myReactions = await this.noteReactionsRepository.findBy({
+			const targets = [...notes.filter(n => (n.id < oldId) && (Object.keys(n.reactions).length > 0)).map(n => n.id), ...renoteIds];
+			const myReactions = targets.length > 0 ? await this.noteReactionsRepository.findBy({
 				userId: meId,
 				noteId: In(targets),
-			});
+			}) : [];
 
 			for (const target of targets) {
 				myReactionsMap.set(target, myReactions.find(reaction => reaction.noteId === target) ?? null);