refactor: add NoteReactions.packMany

This commit is contained in:
Johann150 2022-06-09 16:50:56 +02:00
parent 6775028b1e
commit b630cd7eac
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1
3 changed files with 16 additions and 2 deletions

View file

@ -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<Packed<'NoteReaction'>[]> {
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] : []);
}
});

View file

@ -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);
});

View file

@ -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 });
});