From de9d136a3f5477535d0f0825f907bfb988c6d69d Mon Sep 17 00:00:00 2001
From: syuilo <Syuilotan@yahoo.co.jp>
Date: Thu, 6 Apr 2023 18:27:42 +0900
Subject: [PATCH] perf(backend): reduce db query

---
 packages/backend/src/core/entities/NoteEntityService.ts | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/packages/backend/src/core/entities/NoteEntityService.ts b/packages/backend/src/core/entities/NoteEntityService.ts
index 94b3029c58..1320e56ca2 100644
--- a/packages/backend/src/core/entities/NoteEntityService.ts
+++ b/packages/backend/src/core/entities/NoteEntityService.ts
@@ -183,6 +183,11 @@ export class NoteEntityService implements OnModuleInit {
 		// 実装上抜けがあるだけかもしれないので、「ヒントに含まれてなかったら(=undefinedなら)return」のようにはしない
 		}
 
+		// パフォーマンスのためノートが作成されてから1秒以上経っていない場合はリアクションを取得しない
+		if (note.createdAt.getTime() + 1000 > Date.now()) {
+			return undefined;
+		}
+
 		const reaction = await this.noteReactionsRepository.findOneBy({
 			userId: meId,
 			noteId: note.id,
@@ -395,7 +400,8 @@ export class NoteEntityService implements OnModuleInit {
 		const myReactionsMap = new Map<Note['id'], NoteReaction | null>();
 		if (meId) {
 			const renoteIds = notes.filter(n => n.renoteId != null).map(n => n.renoteId!);
-			const targets = [...notes.map(n => n.id), ...renoteIds];
+			// パフォーマンスのためノートが作成されてから1秒以上経っていない場合はリアクションを取得しない
+			const targets = [...notes.filter(n => n.createdAt.getTime() + 1000 < Date.now()).map(n => n.id), ...renoteIds];
 			const myReactions = await this.noteReactionsRepository.findBy({
 				userId: meId,
 				noteId: In(targets),