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