From 6e9d3387e05f634e11976d516a05e2bff4149dc5 Mon Sep 17 00:00:00 2001 From: naskya Date: Sat, 23 Mar 2024 00:54:54 +0900 Subject: [PATCH] refactor (backend): use Promise.all to await multiple async processes --- packages/backend/src/services/note/delete.ts | 21 +++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/backend/src/services/note/delete.ts b/packages/backend/src/services/note/delete.ts index 58dfe551fe..b6f4e6815c 100644 --- a/packages/backend/src/services/note/delete.ts +++ b/packages/backend/src/services/note/delete.ts @@ -22,8 +22,8 @@ async function recalculateNotesCountOfLocalUser(user: { id: User["id"]; host: User["host"]; }) { - if (user.host == null) { - await Notes.countBy({ userId: user.id }).then((newCount) => + if (Users.isLocalUser(user)) { + await Notes.countBy({ userId: user.id }).then((newCount: number) => Users.update(user.id, { updatedAt: new Date(), notesCount: newCount }), ); } @@ -168,7 +168,7 @@ export default async function ( async function findCascadingNotes(note: Note) { const cascadingNotes: Note[] = []; - const recursive = async (noteId: string) => { + const findRepliesAndQuotes = async (noteId: string) => { const query = Notes.createQueryBuilder("note") .where("note.replyId = :noteId", { noteId }) .orWhere( @@ -179,13 +179,16 @@ async function findCascadingNotes(note: Note) { }), ) .leftJoinAndSelect("note.user", "user"); - const replies = await query.getMany(); - for (const reply of replies) { - cascadingNotes.push(reply); - await recursive(reply.id); - } + const repliesAndQuotes = await query.getMany(); + + await Promise.all( + repliesAndQuotes.map((n: Note) => { + cascadingNotes.push(n); + return findRepliesAndQuotes(n.id); + }), + ); }; - await recursive(note.id); + await findRepliesAndQuotes(note.id); return cascadingNotes; }