diff --git a/packages/backend/src/models/repositories/note-reaction.ts b/packages/backend/src/models/repositories/note-reaction.ts index 4deae51c9..46084a9a1 100644 --- a/packages/backend/src/models/repositories/note-reaction.ts +++ b/packages/backend/src/models/repositories/note-reaction.ts @@ -25,8 +25,22 @@ export const NoteReactionRepository = db.getRepository(NoteReaction).extend({ user: await Users.pack(reaction.user ?? reaction.userId, me), type: convertLegacyReaction(reaction.reaction), ...(opts.withNote ? { + // may throw error note: await Notes.pack(reaction.note ?? reaction.noteId, me), } : {}), }; }, + + async packMany( + src: NoteReaction[], + me?: { id: User['id'] } | null | undefined, + options?: { + withNote: booleam; + }, + ): Promise[]> { + const reactions = await Promise.allSettled(src.map(reaction => this.pack(reaction, me, options))); + + // filter out rejected promises, only keep fulfilled values + return reactions.flatMap(result => result.status === 'fulfilled' ? [result.value] : []); + } }); diff --git a/packages/backend/src/server/api/endpoints/notes/reactions.ts b/packages/backend/src/server/api/endpoints/notes/reactions.ts index 02a288138..d9388b47f 100644 --- a/packages/backend/src/server/api/endpoints/notes/reactions.ts +++ b/packages/backend/src/server/api/endpoints/notes/reactions.ts @@ -75,5 +75,5 @@ export default define(meta, paramDef, async (ps, user) => { relations: ['user', 'user.avatar', 'user.banner', 'note'], }); - return await Promise.all(reactions.map(reaction => NoteReactions.pack(reaction, user))); + return await NoteReactions.packMany(reactions, user); }); diff --git a/packages/backend/src/server/api/endpoints/users/reactions.ts b/packages/backend/src/server/api/endpoints/users/reactions.ts index 9668bd21b..4750dc4f9 100644 --- a/packages/backend/src/server/api/endpoints/users/reactions.ts +++ b/packages/backend/src/server/api/endpoints/users/reactions.ts @@ -62,5 +62,5 @@ export default define(meta, paramDef, async (ps, me) => { .take(ps.limit) .getMany(); - return await Promise.all(reactions.map(reaction => NoteReactions.pack(reaction, me, { withNote: true }))); + return await NoteReactions.packMany(reactions, me, { withNote: true }); });