2021-03-19 21:05:33 -05:00
|
|
|
import { EntityRepository, In, Repository } from 'typeorm';
|
2021-08-19 07:55:45 -05:00
|
|
|
import { Users, Notes, UserGroupInvitations, AccessTokens, NoteReactions } from '../index';
|
2021-08-19 08:04:15 -05:00
|
|
|
import { Notification } from '@/models/entities/notification';
|
|
|
|
import { awaitAll } from '@/prelude/await-all';
|
2021-09-22 08:35:55 -05:00
|
|
|
import { Packed } from '@/misc/schema';
|
2021-08-19 08:04:15 -05:00
|
|
|
import { Note } from '@/models/entities/note';
|
|
|
|
import { NoteReaction } from '@/models/entities/note-reaction';
|
|
|
|
import { User } from '@/models/entities/user';
|
2021-08-19 07:55:45 -05:00
|
|
|
import { aggregateNoteEmojis, prefetchEmojis } from '@/misc/populate-emojis';
|
2021-09-09 06:23:31 -05:00
|
|
|
import { notificationTypes } from '@/types';
|
2019-04-23 08:35:26 -05:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
@EntityRepository(Notification)
|
|
|
|
export class NotificationRepository extends Repository<Notification> {
|
|
|
|
public async pack(
|
|
|
|
src: Notification['id'] | Notification,
|
2021-03-19 21:05:33 -05:00
|
|
|
options: {
|
2021-03-21 08:26:45 -05:00
|
|
|
_hintForEachNotes_?: {
|
2021-03-19 21:05:33 -05:00
|
|
|
myReactions: Map<Note['id'], NoteReaction | null>;
|
|
|
|
};
|
|
|
|
}
|
2021-09-22 08:35:55 -05:00
|
|
|
): Promise<Packed<'Notification'>> {
|
2021-02-13 00:33:38 -06:00
|
|
|
const notification = typeof src === 'object' ? src : await this.findOneOrFail(src);
|
|
|
|
const token = notification.appAccessTokenId ? await AccessTokens.findOneOrFail(notification.appAccessTokenId) : null;
|
2019-04-07 07:50:36 -05:00
|
|
|
|
2019-04-23 08:35:26 -05:00
|
|
|
return await awaitAll({
|
2019-04-07 07:50:36 -05:00
|
|
|
id: notification.id,
|
2019-04-23 08:35:26 -05:00
|
|
|
createdAt: notification.createdAt.toISOString(),
|
2019-04-07 07:50:36 -05:00
|
|
|
type: notification.type,
|
2020-05-26 00:33:55 -05:00
|
|
|
isRead: notification.isRead,
|
2019-04-07 07:50:36 -05:00
|
|
|
userId: notification.notifierId,
|
2020-03-28 04:07:41 -05:00
|
|
|
user: notification.notifierId ? Users.pack(notification.notifier || notification.notifierId) : null,
|
2019-04-07 07:50:36 -05:00
|
|
|
...(notification.type === 'mention' ? {
|
2021-03-23 21:05:37 -05:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, { id: notification.notifieeId }, {
|
2021-03-19 21:05:33 -05:00
|
|
|
detail: true,
|
2021-12-09 08:58:30 -06:00
|
|
|
_hint_: options._hintForEachNotes_,
|
2021-03-19 21:05:33 -05:00
|
|
|
}),
|
2019-04-07 07:50:36 -05:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'reply' ? {
|
2021-03-23 21:05:37 -05:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, { id: notification.notifieeId }, {
|
2021-03-19 21:05:33 -05:00
|
|
|
detail: true,
|
2021-12-09 08:58:30 -06:00
|
|
|
_hint_: options._hintForEachNotes_,
|
2021-03-19 21:05:33 -05:00
|
|
|
}),
|
2019-04-07 07:50:36 -05:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'renote' ? {
|
2021-03-23 21:05:37 -05:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, { id: notification.notifieeId }, {
|
2021-03-19 21:05:33 -05:00
|
|
|
detail: true,
|
2021-12-09 08:58:30 -06:00
|
|
|
_hint_: options._hintForEachNotes_,
|
2021-03-19 21:05:33 -05:00
|
|
|
}),
|
2019-04-07 07:50:36 -05:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'quote' ? {
|
2021-03-23 21:05:37 -05:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, { id: notification.notifieeId }, {
|
2021-03-19 21:05:33 -05:00
|
|
|
detail: true,
|
2021-12-09 08:58:30 -06:00
|
|
|
_hint_: options._hintForEachNotes_,
|
2021-03-19 21:05:33 -05:00
|
|
|
}),
|
2019-04-07 07:50:36 -05:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'reaction' ? {
|
2021-03-23 21:05:37 -05:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, { id: notification.notifieeId }, {
|
2021-03-19 21:05:33 -05:00
|
|
|
detail: true,
|
2021-12-09 08:58:30 -06:00
|
|
|
_hint_: options._hintForEachNotes_,
|
2021-03-19 21:05:33 -05:00
|
|
|
}),
|
2021-12-09 08:58:30 -06:00
|
|
|
reaction: notification.reaction,
|
2019-04-07 07:50:36 -05:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'pollVote' ? {
|
2021-03-23 21:05:37 -05:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, { id: notification.notifieeId }, {
|
2021-03-19 21:05:33 -05:00
|
|
|
detail: true,
|
2021-12-09 08:58:30 -06:00
|
|
|
_hint_: options._hintForEachNotes_,
|
2021-03-19 21:05:33 -05:00
|
|
|
}),
|
2021-12-09 08:58:30 -06:00
|
|
|
choice: notification.choice,
|
2020-02-12 11:17:54 -06:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'groupInvited' ? {
|
|
|
|
invitation: UserGroupInvitations.pack(notification.userGroupInvitationId!),
|
|
|
|
} : {}),
|
2020-03-28 04:07:41 -05:00
|
|
|
...(notification.type === 'app' ? {
|
|
|
|
body: notification.customBody,
|
2020-03-28 20:49:43 -05:00
|
|
|
header: notification.customHeader || token?.name,
|
|
|
|
icon: notification.customIcon || token?.iconUrl,
|
2020-03-28 04:07:41 -05:00
|
|
|
} : {}),
|
2019-04-07 07:50:36 -05:00
|
|
|
});
|
|
|
|
}
|
2019-04-24 23:27:07 -05:00
|
|
|
|
2021-03-19 21:05:33 -05:00
|
|
|
public async packMany(
|
2021-03-19 04:22:14 -05:00
|
|
|
notifications: Notification[],
|
2021-03-19 21:05:33 -05:00
|
|
|
meId: User['id']
|
2019-04-24 23:27:07 -05:00
|
|
|
) {
|
2021-03-19 21:05:33 -05: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];
|
|
|
|
const myReactions = await NoteReactions.find({
|
|
|
|
userId: meId,
|
|
|
|
noteId: In(targets),
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const target of targets) {
|
|
|
|
myReactionsMap.set(target, myReactions.find(reaction => reaction.noteId === target) || null);
|
|
|
|
}
|
|
|
|
|
2021-03-21 22:41:33 -05:00
|
|
|
await prefetchEmojis(aggregateNoteEmojis(notes));
|
|
|
|
|
2021-03-19 21:05:33 -05:00
|
|
|
return await Promise.all(notifications.map(x => this.pack(x, {
|
|
|
|
_hintForEachNotes_: {
|
2021-12-09 08:58:30 -06:00
|
|
|
myReactions: myReactionsMap,
|
|
|
|
},
|
2021-03-19 21:05:33 -05:00
|
|
|
})));
|
2019-04-24 23:27:07 -05:00
|
|
|
}
|
2019-04-07 07:50:36 -05:00
|
|
|
}
|
2019-04-23 08:35:26 -05:00
|
|
|
|
|
|
|
export const packedNotificationSchema = {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
format: 'id',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
format: 'date-time',
|
|
|
|
},
|
2021-09-09 06:23:31 -05:00
|
|
|
isRead: {
|
|
|
|
type: 'boolean' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
},
|
2019-04-23 08:35:26 -05:00
|
|
|
type: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2021-09-09 06:23:31 -05:00
|
|
|
enum: [...notificationTypes],
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
type: 'object' as const,
|
2021-09-11 11:12:23 -05:00
|
|
|
ref: 'User' as const,
|
2021-09-09 06:23:31 -05:00
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
},
|
|
|
|
userId: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
format: 'id',
|
|
|
|
},
|
2021-09-09 06:23:31 -05:00
|
|
|
note: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'object' as const,
|
2021-09-11 11:12:23 -05:00
|
|
|
ref: 'Note' as const,
|
2021-09-09 06:23:31 -05:00
|
|
|
optional: true as const, nullable: true as const,
|
|
|
|
},
|
|
|
|
reaction: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
|
|
|
},
|
|
|
|
choice: {
|
|
|
|
type: 'number' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
|
|
|
},
|
|
|
|
invitation: {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
|
|
|
},
|
|
|
|
body: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
|
|
|
},
|
|
|
|
header: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
|
|
|
},
|
|
|
|
icon: {
|
|
|
|
type: 'string' as const,
|
2019-06-27 04:04:09 -05:00
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
},
|
2021-12-09 08:58:30 -06:00
|
|
|
},
|
2019-04-23 08:35:26 -05:00
|
|
|
};
|