2019-04-07 07:50:36 -05:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
|
|
|
import { Users, Notes } from '..';
|
|
|
|
import rap from '@prezzemolo/rap';
|
|
|
|
import { Notification } from '../entities/notification';
|
2019-04-12 11:43:22 -05:00
|
|
|
import { ensure } from '../../prelude/ensure';
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
@EntityRepository(Notification)
|
|
|
|
export class NotificationRepository extends Repository<Notification> {
|
|
|
|
public packMany(
|
|
|
|
notifications: any[],
|
|
|
|
) {
|
|
|
|
return Promise.all(notifications.map(x => this.pack(x)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public async pack(
|
|
|
|
src: Notification['id'] | Notification,
|
|
|
|
) {
|
2019-04-12 11:43:22 -05:00
|
|
|
const notification = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
return await rap({
|
|
|
|
id: notification.id,
|
|
|
|
createdAt: notification.createdAt,
|
|
|
|
type: notification.type,
|
|
|
|
userId: notification.notifierId,
|
|
|
|
user: Users.pack(notification.notifier || notification.notifierId),
|
|
|
|
...(notification.type === 'mention' ? {
|
2019-04-12 11:43:22 -05:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!),
|
2019-04-07 07:50:36 -05:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'reply' ? {
|
2019-04-12 11:43:22 -05:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!),
|
2019-04-07 07:50:36 -05:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'renote' ? {
|
2019-04-12 11:43:22 -05:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!),
|
2019-04-07 07:50:36 -05:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'quote' ? {
|
2019-04-12 11:43:22 -05:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!),
|
2019-04-07 07:50:36 -05:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'reaction' ? {
|
2019-04-12 11:43:22 -05:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!),
|
2019-04-07 07:50:36 -05:00
|
|
|
reaction: notification.reaction
|
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'pollVote' ? {
|
2019-04-12 11:43:22 -05:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!),
|
2019-04-07 07:50:36 -05:00
|
|
|
choice: notification.choice
|
|
|
|
} : {})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|