From 4a7da723b3327c5905b95e3a01cd4b43dd5e9ad9 Mon Sep 17 00:00:00 2001
From: okayurisotto <okayurisotto@proton.me>
Date: Thu, 6 Jul 2023 11:25:46 +0900
Subject: [PATCH] =?UTF-8?q?refactor(backend):=20=E3=83=8E=E3=83=BC?=
 =?UTF-8?q?=E3=83=88=E5=89=8A=E9=99=A4=E6=99=82=E3=81=AE`findCascadingNote?=
 =?UTF-8?q?s`=E3=81=AE=E5=87=A6=E7=90=86=E3=82=92=E6=95=B4=E7=90=86=20(#11?=
 =?UTF-8?q?131)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* refactor(backend): ノート削除時の`findCascadingNotes`の処理を整理

* cleanup: unneeded async await

Co-authored-by: syuilo <Syuilotan@yahoo.co.jp>

---------

Co-authored-by: syuilo <Syuilotan@yahoo.co.jp>
---
 packages/backend/src/core/NoteDeleteService.ts | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/packages/backend/src/core/NoteDeleteService.ts b/packages/backend/src/core/NoteDeleteService.ts
index dd878f7bb..3612ac806 100644
--- a/packages/backend/src/core/NoteDeleteService.ts
+++ b/packages/backend/src/core/NoteDeleteService.ts
@@ -121,10 +121,8 @@ export class NoteDeleteService {
 	}
 
 	@bindThis
-	private async findCascadingNotes(note: Note) {
-		const cascadingNotes: Note[] = [];
-
-		const recursive = async (noteId: string) => {
+	private async findCascadingNotes(note: Note): Promise<Note[]> {
+		const recursive = async (noteId: string): Promise<Note[]> => {
 			const query = this.notesRepository.createQueryBuilder('note')
 				.where('note.replyId = :noteId', { noteId })
 				.orWhere(new Brackets(q => {
@@ -133,12 +131,14 @@ export class NoteDeleteService {
 				}))
 				.leftJoinAndSelect('note.user', 'user');
 			const replies = await query.getMany();
-			for (const reply of replies) {
-				cascadingNotes.push(reply);
-				await recursive(reply.id);
-			}
+
+			return [
+				replies,
+				...await Promise.all(replies.map(reply => recursive(reply.id))),
+			].flat();
 		};
-		await recursive(note.id);
+
+		const cascadingNotes: Note[] = await recursive(note.id);
 
 		return cascadingNotes.filter(note => note.userHost === null); // filter out non-local users
 	}