2022-03-26 06:34:00 +00:00
|
|
|
import { db } from '@/db/postgre.js';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { NoteReaction } from '@/models/entities/note-reaction.js';
|
|
|
|
import { Notes, Users } from '../index.js';
|
|
|
|
import { Packed } from '@/misc/schema.js';
|
|
|
|
import { convertLegacyReaction } from '@/misc/reaction-lib.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
2019-04-23 13:35:26 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
export const NoteReactionRepository = db.getRepository(NoteReaction).extend({
|
|
|
|
async pack(
|
2019-04-07 12:50:36 +00:00
|
|
|
src: NoteReaction['id'] | NoteReaction,
|
2021-10-16 16:33:15 +00:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
|
|
|
options?: {
|
|
|
|
withNote: boolean;
|
|
|
|
},
|
2021-09-22 13:35:55 +00:00
|
|
|
): Promise<Packed<'NoteReaction'>> {
|
2021-10-16 16:33:15 +00:00
|
|
|
const opts = Object.assign({
|
|
|
|
withNote: false,
|
|
|
|
}, options);
|
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
const reaction = typeof src === 'object' ? src : await this.findOneByOrFail({ id: src });
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
id: reaction.id,
|
2019-04-23 13:35:26 +00:00
|
|
|
createdAt: reaction.createdAt.toISOString(),
|
2022-02-27 04:59:10 +00:00
|
|
|
user: await Users.pack(reaction.user ?? reaction.userId, me),
|
2020-01-29 19:37:25 +00:00
|
|
|
type: convertLegacyReaction(reaction.reaction),
|
2021-10-16 16:33:15 +00:00
|
|
|
...(opts.withNote ? {
|
2022-07-25 16:15:21 +00:00
|
|
|
// may throw error
|
2022-02-27 04:59:10 +00:00
|
|
|
note: await Notes.pack(reaction.note ?? reaction.noteId, me),
|
2021-12-09 14:58:30 +00:00
|
|
|
} : {}),
|
2019-04-07 12:50:36 +00:00
|
|
|
};
|
2022-03-26 06:34:00 +00:00
|
|
|
},
|
2022-07-25 16:15:21 +00:00
|
|
|
|
|
|
|
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] : []);
|
|
|
|
}
|
2022-03-26 06:34:00 +00:00
|
|
|
});
|