2023-01-13 13:40:33 +09:00
|
|
|
import { In, Repository } from "typeorm";
|
|
|
|
import { Notification } from "@/models/entities/notification.js";
|
|
|
|
import { awaitAll } from "@/prelude/await-all.js";
|
|
|
|
import type { Packed } from "@/misc/schema.js";
|
|
|
|
import type { Note } from "@/models/entities/note.js";
|
|
|
|
import type { NoteReaction } from "@/models/entities/note-reaction.js";
|
|
|
|
import type { User } from "@/models/entities/user.js";
|
|
|
|
import { aggregateNoteEmojis, prefetchEmojis } from "@/misc/populate-emojis.js";
|
|
|
|
import { notificationTypes } from "@/types.js";
|
|
|
|
import { db } from "@/db/postgre.js";
|
|
|
|
import {
|
|
|
|
Users,
|
|
|
|
Notes,
|
|
|
|
UserGroupInvitations,
|
|
|
|
AccessTokens,
|
|
|
|
NoteReactions,
|
|
|
|
} from "../index.js";
|
2019-04-23 22:35:26 +09:00
|
|
|
|
2022-03-26 15:34:00 +09:00
|
|
|
export const NotificationRepository = db.getRepository(Notification).extend({
|
|
|
|
async pack(
|
2023-01-13 13:40:33 +09:00
|
|
|
src: Notification["id"] | Notification,
|
2021-03-20 11:05:33 +09:00
|
|
|
options: {
|
2021-03-21 22:26:45 +09:00
|
|
|
_hintForEachNotes_?: {
|
2023-01-13 13:40:33 +09:00
|
|
|
myReactions: Map<Note["id"], NoteReaction | null>;
|
2021-03-20 11:05:33 +09:00
|
|
|
};
|
2022-11-27 02:31:54 +09:00
|
|
|
},
|
2023-01-13 13:40:33 +09:00
|
|
|
): Promise<Packed<"Notification">> {
|
|
|
|
const notification =
|
|
|
|
typeof src === "object" ? src : await this.findOneByOrFail({ id: src });
|
|
|
|
const token = notification.appAccessTokenId
|
|
|
|
? await AccessTokens.findOneByOrFail({
|
|
|
|
id: notification.appAccessTokenId,
|
|
|
|
})
|
|
|
|
: null;
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2019-04-23 22:35:26 +09:00
|
|
|
return await awaitAll({
|
2019-04-07 21:50:36 +09:00
|
|
|
id: notification.id,
|
2019-04-23 22:35:26 +09:00
|
|
|
createdAt: notification.createdAt.toISOString(),
|
2019-04-07 21:50:36 +09:00
|
|
|
type: notification.type,
|
2020-05-26 14:33:55 +09:00
|
|
|
isRead: notification.isRead,
|
2019-04-07 21:50:36 +09:00
|
|
|
userId: notification.notifierId,
|
2023-01-13 13:40:33 +09:00
|
|
|
user: notification.notifierId
|
|
|
|
? Users.pack(notification.notifier || notification.notifierId)
|
|
|
|
: null,
|
|
|
|
...(notification.type === "mention"
|
|
|
|
? {
|
|
|
|
note: Notes.pack(
|
|
|
|
notification.note || notification.noteId!,
|
|
|
|
{ id: notification.notifieeId },
|
|
|
|
{
|
|
|
|
detail: true,
|
|
|
|
_hint_: options._hintForEachNotes_,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
...(notification.type === "reply"
|
|
|
|
? {
|
|
|
|
note: Notes.pack(
|
|
|
|
notification.note || notification.noteId!,
|
|
|
|
{ id: notification.notifieeId },
|
|
|
|
{
|
|
|
|
detail: true,
|
|
|
|
_hint_: options._hintForEachNotes_,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
...(notification.type === "renote"
|
|
|
|
? {
|
|
|
|
note: Notes.pack(
|
|
|
|
notification.note || notification.noteId!,
|
|
|
|
{ id: notification.notifieeId },
|
|
|
|
{
|
|
|
|
detail: true,
|
|
|
|
_hint_: options._hintForEachNotes_,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
...(notification.type === "quote"
|
|
|
|
? {
|
|
|
|
note: Notes.pack(
|
|
|
|
notification.note || notification.noteId!,
|
|
|
|
{ id: notification.notifieeId },
|
|
|
|
{
|
|
|
|
detail: true,
|
|
|
|
_hint_: options._hintForEachNotes_,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
...(notification.type === "reaction"
|
|
|
|
? {
|
|
|
|
note: Notes.pack(
|
|
|
|
notification.note || notification.noteId!,
|
|
|
|
{ id: notification.notifieeId },
|
|
|
|
{
|
|
|
|
detail: true,
|
|
|
|
_hint_: options._hintForEachNotes_,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
reaction: notification.reaction,
|
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
...(notification.type === "pollVote"
|
|
|
|
? {
|
|
|
|
note: Notes.pack(
|
|
|
|
notification.note || notification.noteId!,
|
|
|
|
{ id: notification.notifieeId },
|
|
|
|
{
|
|
|
|
detail: true,
|
|
|
|
_hint_: options._hintForEachNotes_,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
choice: notification.choice,
|
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
...(notification.type === "pollEnded"
|
|
|
|
? {
|
|
|
|
note: Notes.pack(
|
|
|
|
notification.note || notification.noteId!,
|
|
|
|
{ id: notification.notifieeId },
|
|
|
|
{
|
|
|
|
detail: true,
|
|
|
|
_hint_: options._hintForEachNotes_,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
...(notification.type === "groupInvited"
|
|
|
|
? {
|
|
|
|
invitation: UserGroupInvitations.pack(
|
|
|
|
notification.userGroupInvitationId!,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
...(notification.type === "app"
|
|
|
|
? {
|
|
|
|
body: notification.customBody,
|
|
|
|
header: notification.customHeader || token?.name,
|
|
|
|
icon: notification.customIcon || token?.iconUrl,
|
|
|
|
}
|
|
|
|
: {}),
|
2019-04-07 21:50:36 +09:00
|
|
|
});
|
2022-03-26 15:34:00 +09:00
|
|
|
},
|
2019-04-25 13:27:07 +09:00
|
|
|
|
2023-01-13 13:40:33 +09:00
|
|
|
async packMany(notifications: Notification[], meId: User["id"]) {
|
2021-03-20 11:05:33 +09:00
|
|
|
if (notifications.length === 0) return [];
|
|
|
|
|
2023-01-13 13:40:33 +09:00
|
|
|
const notes = notifications
|
|
|
|
.filter((x) => x.note != null)
|
|
|
|
.map((x) => x.note!);
|
|
|
|
const noteIds = notes.map((n) => n.id);
|
|
|
|
const myReactionsMap = new Map<Note["id"], NoteReaction | null>();
|
|
|
|
const renoteIds = notes
|
|
|
|
.filter((n) => n.renoteId != null)
|
|
|
|
.map((n) => n.renoteId!);
|
2021-03-20 11:05:33 +09:00
|
|
|
const targets = [...noteIds, ...renoteIds];
|
2022-03-26 15:34:00 +09:00
|
|
|
const myReactions = await NoteReactions.findBy({
|
2021-03-20 11:05:33 +09:00
|
|
|
userId: meId,
|
|
|
|
noteId: In(targets),
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const target of targets) {
|
2023-01-13 13:40:33 +09:00
|
|
|
myReactionsMap.set(
|
|
|
|
target,
|
|
|
|
myReactions.find((reaction) => reaction.noteId === target) || null,
|
|
|
|
);
|
2021-03-20 11:05:33 +09:00
|
|
|
}
|
|
|
|
|
2021-03-22 12:41:33 +09:00
|
|
|
await prefetchEmojis(aggregateNoteEmojis(notes));
|
|
|
|
|
2023-01-13 13:40:33 +09:00
|
|
|
const results = await Promise.all(
|
|
|
|
notifications.map((x) =>
|
2022-11-27 02:31:54 +09:00
|
|
|
this.pack(x, {
|
|
|
|
_hintForEachNotes_: {
|
|
|
|
myReactions: myReactionsMap,
|
|
|
|
},
|
2023-01-13 13:40:33 +09:00
|
|
|
}).catch((e) => null),
|
2022-11-27 02:31:54 +09:00
|
|
|
),
|
|
|
|
);
|
2023-01-13 13:40:33 +09:00
|
|
|
return results.filter((x) => x != null);
|
2022-03-26 15:34:00 +09:00
|
|
|
},
|
|
|
|
});
|