2019-04-07 14:50:36 +02:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
2020-03-28 10:07:41 +01:00
|
|
|
import { Users, Notes, UserGroupInvitations, AccessTokens } from '..';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { Notification } from '../entities/notification';
|
2019-04-23 15:35:26 +02:00
|
|
|
import { awaitAll } from '../../prelude/await-all';
|
2019-06-27 11:04:09 +02:00
|
|
|
import { SchemaType } from '../../misc/schema';
|
2019-04-23 15:35:26 +02:00
|
|
|
|
|
|
|
export type PackedNotification = SchemaType<typeof packedNotificationSchema>;
|
2019-04-07 14:50:36 +02:00
|
|
|
|
|
|
|
@EntityRepository(Notification)
|
|
|
|
export class NotificationRepository extends Repository<Notification> {
|
|
|
|
public async pack(
|
|
|
|
src: Notification['id'] | Notification,
|
2019-04-23 15:35:26 +02:00
|
|
|
): Promise<PackedNotification> {
|
2021-02-13 07:33:38 +01:00
|
|
|
const notification = typeof src === 'object' ? src : await this.findOneOrFail(src);
|
|
|
|
const token = notification.appAccessTokenId ? await AccessTokens.findOneOrFail(notification.appAccessTokenId) : null;
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2019-04-23 15:35:26 +02:00
|
|
|
return await awaitAll({
|
2019-04-07 14:50:36 +02:00
|
|
|
id: notification.id,
|
2019-04-23 15:35:26 +02:00
|
|
|
createdAt: notification.createdAt.toISOString(),
|
2019-04-07 14:50:36 +02:00
|
|
|
type: notification.type,
|
2020-05-26 07:33:55 +02:00
|
|
|
isRead: notification.isRead,
|
2019-04-07 14:50:36 +02:00
|
|
|
userId: notification.notifierId,
|
2020-03-28 10:07:41 +01:00
|
|
|
user: notification.notifierId ? Users.pack(notification.notifier || notification.notifierId) : null,
|
2019-04-07 14:50:36 +02:00
|
|
|
...(notification.type === 'mention' ? {
|
2019-09-23 19:57:34 +02:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId),
|
2019-04-07 14:50:36 +02:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'reply' ? {
|
2019-09-23 19:57:34 +02:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId),
|
2019-04-07 14:50:36 +02:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'renote' ? {
|
2019-09-23 19:57:34 +02:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId),
|
2019-04-07 14:50:36 +02:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'quote' ? {
|
2019-09-23 19:57:34 +02:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId),
|
2019-04-07 14:50:36 +02:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'reaction' ? {
|
2019-09-23 19:57:34 +02:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId),
|
2019-04-07 14:50:36 +02:00
|
|
|
reaction: notification.reaction
|
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'pollVote' ? {
|
2019-09-23 19:57:34 +02:00
|
|
|
note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId),
|
2019-04-07 14:50:36 +02:00
|
|
|
choice: notification.choice
|
2020-02-12 18:17:54 +01:00
|
|
|
} : {}),
|
|
|
|
...(notification.type === 'groupInvited' ? {
|
|
|
|
invitation: UserGroupInvitations.pack(notification.userGroupInvitationId!),
|
|
|
|
} : {}),
|
2020-03-28 10:07:41 +01:00
|
|
|
...(notification.type === 'app' ? {
|
|
|
|
body: notification.customBody,
|
2020-03-29 03:49:43 +02:00
|
|
|
header: notification.customHeader || token?.name,
|
|
|
|
icon: notification.customIcon || token?.iconUrl,
|
2020-03-28 10:07:41 +01:00
|
|
|
} : {}),
|
2019-04-07 14:50:36 +02:00
|
|
|
});
|
|
|
|
}
|
2019-04-25 06:27:07 +02:00
|
|
|
|
|
|
|
public packMany(
|
|
|
|
notifications: any[],
|
|
|
|
) {
|
|
|
|
return Promise.all(notifications.map(x => this.pack(x)));
|
|
|
|
}
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|
2019-04-23 15:35:26 +02:00
|
|
|
|
|
|
|
export const packedNotificationSchema = {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 15:35:26 +02:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 15:35:26 +02:00
|
|
|
format: 'id',
|
|
|
|
description: 'The unique identifier for this notification.',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 15:35:26 +02:00
|
|
|
format: 'date-time',
|
|
|
|
description: 'The date that the notification was created.'
|
|
|
|
},
|
|
|
|
type: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2020-01-29 20:37:25 +01:00
|
|
|
enum: ['follow', 'followRequestAccepted', 'receiveFollowRequest', 'mention', 'reply', 'renote', 'quote', 'reaction', 'pollVote'],
|
2019-04-23 15:35:26 +02:00
|
|
|
description: 'The type of the notification.'
|
|
|
|
},
|
|
|
|
userId: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 15:35:26 +02:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
user: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'object' as const,
|
2019-04-23 15:35:26 +02:00
|
|
|
ref: 'User',
|
2019-06-27 11:04:09 +02:00
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 15:35:26 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|