2019-04-07 12:50:36 +00:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
|
|
|
import { Users, Notes } from '..';
|
|
|
|
import { Notification } from '../entities/notification';
|
2019-04-12 16:43:22 +00:00
|
|
|
import { ensure } from '../../prelude/ensure';
|
2019-04-23 13:35:26 +00:00
|
|
|
import { awaitAll } from '../../prelude/await-all';
|
2019-06-27 09:04:09 +00:00
|
|
|
import { SchemaType } from '../../misc/schema';
|
2019-04-23 13:35:26 +00:00
|
|
|
|
|
|
|
export type PackedNotification = SchemaType<typeof packedNotificationSchema>;
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
@EntityRepository(Notification)
|
|
|
|
export class NotificationRepository extends Repository<Notification> {
|
|
|
|
public async pack(
|
|
|
|
src: Notification['id'] | Notification,
|
2019-04-23 13:35:26 +00:00
|
|
|
): Promise<PackedNotification> {
|
2019-04-12 16:43:22 +00:00
|
|
|
const notification = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
|
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,
|
|
|
|
userId: notification.notifierId,
|
|
|
|
user: Users.pack(notification.notifier || notification.notifierId),
|
|
|
|
...(notification.type === 'mention' ? {
|
2019-04-12 16:43:22 +00:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!),
|
2019-04-07 12:50:36 +00:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'reply' ? {
|
2019-04-12 16:43:22 +00:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!),
|
2019-04-07 12:50:36 +00:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'renote' ? {
|
2019-04-12 16:43:22 +00:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!),
|
2019-04-07 12:50:36 +00:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'quote' ? {
|
2019-04-12 16:43:22 +00:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!),
|
2019-04-07 12:50:36 +00:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'reaction' ? {
|
2019-04-12 16:43:22 +00:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!),
|
2019-04-07 12:50:36 +00:00
|
|
|
reaction: notification.reaction
|
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'pollVote' ? {
|
2019-04-12 16:43:22 +00:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!),
|
2019-04-07 12:50:36 +00:00
|
|
|
choice: notification.choice
|
|
|
|
} : {})
|
|
|
|
});
|
|
|
|
}
|
2019-04-25 04:27:07 +00:00
|
|
|
|
|
|
|
public packMany(
|
|
|
|
notifications: any[],
|
|
|
|
) {
|
|
|
|
return Promise.all(notifications.map(x => this.pack(x)));
|
|
|
|
}
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
2019-04-23 13:35:26 +00:00
|
|
|
|
|
|
|
export const packedNotificationSchema = {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
format: 'id',
|
|
|
|
description: 'The unique identifier for this notification.',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
format: 'date-time',
|
|
|
|
description: 'The date that the notification was created.'
|
|
|
|
},
|
|
|
|
type: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
enum: ['follow', 'receiveFollowRequest', 'mention', 'reply', 'renote', 'quote', 'reaction', 'pollVote'],
|
|
|
|
description: 'The type of the notification.'
|
|
|
|
},
|
|
|
|
userId: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
user: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'object' as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
ref: 'User',
|
2019-06-27 09:04:09 +00:00
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|