2023-01-13 05:40:33 +01:00
|
|
|
import { db } from "@/db/postgre.js";
|
|
|
|
import { NoteReaction } from "@/models/entities/note-reaction.js";
|
|
|
|
import { Notes, Users } from "../index.js";
|
|
|
|
import type { Packed } from "@/misc/schema.js";
|
|
|
|
import { convertLegacyReaction } from "@/misc/reaction-lib.js";
|
|
|
|
import type { User } from "@/models/entities/user.js";
|
2019-04-23 15:35:26 +02:00
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
export const NoteReactionRepository = db.getRepository(NoteReaction).extend({
|
|
|
|
async pack(
|
2023-01-13 05:40:33 +01:00
|
|
|
src: NoteReaction["id"] | NoteReaction,
|
|
|
|
me?: { id: User["id"] } | null | undefined,
|
2021-10-16 18:33:15 +02:00
|
|
|
options?: {
|
|
|
|
withNote: boolean;
|
|
|
|
},
|
2023-01-13 05:40:33 +01:00
|
|
|
): Promise<Packed<"NoteReaction">> {
|
|
|
|
const opts = Object.assign(
|
|
|
|
{
|
|
|
|
withNote: false,
|
|
|
|
},
|
|
|
|
options,
|
|
|
|
);
|
2021-10-16 18:33:15 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const reaction =
|
|
|
|
typeof src === "object" ? src : await this.findOneByOrFail({ id: src });
|
2019-04-07 14:50:36 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
id: reaction.id,
|
2019-04-23 15:35:26 +02:00
|
|
|
createdAt: reaction.createdAt.toISOString(),
|
2022-02-27 05:59:10 +01:00
|
|
|
user: await Users.pack(reaction.user ?? reaction.userId, me),
|
2020-01-29 20:37:25 +01:00
|
|
|
type: convertLegacyReaction(reaction.reaction),
|
2023-01-13 05:40:33 +01:00
|
|
|
...(opts.withNote
|
|
|
|
? {
|
|
|
|
// may throw error
|
|
|
|
note: await Notes.pack(reaction.note ?? reaction.noteId, me),
|
|
|
|
}
|
|
|
|
: {}),
|
2019-04-07 14:50:36 +02:00
|
|
|
};
|
2022-03-26 07:34:00 +01:00
|
|
|
},
|
2022-07-25 18:15:21 +02:00
|
|
|
|
|
|
|
async packMany(
|
|
|
|
src: NoteReaction[],
|
2023-01-13 05:40:33 +01:00
|
|
|
me?: { id: User["id"] } | null | undefined,
|
2022-07-25 18:15:21 +02:00
|
|
|
options?: {
|
|
|
|
withNote: booleam;
|
|
|
|
},
|
2023-01-13 05:40:33 +01:00
|
|
|
): Promise<Packed<"NoteReaction">[]> {
|
|
|
|
const reactions = await Promise.allSettled(
|
|
|
|
src.map((reaction) => this.pack(reaction, me, options)),
|
|
|
|
);
|
2022-07-25 18:15:21 +02:00
|
|
|
|
|
|
|
// filter out rejected promises, only keep fulfilled values
|
2023-01-13 05:40:33 +01:00
|
|
|
return reactions.flatMap((result) =>
|
|
|
|
result.status === "fulfilled" ? [result.value] : [],
|
|
|
|
);
|
|
|
|
},
|
2022-03-26 07:34:00 +01:00
|
|
|
});
|