(
notifications: {
type: DeepNotesNotificationType;
recipients: Record<string, { encryptedSymmetricKey: Uint8Array }>;
encryptedContent: Uint8Array;
}[],
)
| 30 | export type NotificationsResponse = z.infer<typeof notificationsResponseSchema>; |
| 31 | |
| 32 | export async function notifyUsers( |
| 33 | notifications: { |
| 34 | type: DeepNotesNotificationType; |
| 35 | |
| 36 | recipients: Record<string, { encryptedSymmetricKey: Uint8Array }>; |
| 37 | |
| 38 | encryptedContent: Uint8Array; |
| 39 | }[], |
| 40 | ) { |
| 41 | await dataAbstraction().transaction(async ({ trx }) => { |
| 42 | const dateTime = new Date(); |
| 43 | |
| 44 | await Promise.all( |
| 45 | notifications.map(async ({ recipients, type, encryptedContent }) => { |
| 46 | const notificationId = parseInt( |
| 47 | ( |
| 48 | await NotificationModel.query(trx) |
| 49 | .insert({ |
| 50 | type, |
| 51 | |
| 52 | encrypted_content: encryptedContent, |
| 53 | |
| 54 | datetime: dateTime, |
| 55 | }) |
| 56 | .returning('id') |
| 57 | ).id as any, |
| 58 | ); |
| 59 | |
| 60 | await Promise.all( |
| 61 | objEntries(recipients).map( |
| 62 | async ([userId, { encryptedSymmetricKey }]) => { |
| 63 | await UserNotificationModel.query(trx).insert({ |
| 64 | user_id: userId, |
| 65 | notification_id: notificationId, |
| 66 | |
| 67 | encrypted_symmetric_key: encryptedSymmetricKey, |
| 68 | }); |
| 69 | |
| 70 | await getRedis().publish( |
| 71 | `user-notification:${userId}`, |
| 72 | pack({ |
| 73 | id: notificationId, |
| 74 | |
| 75 | type, |
| 76 | |
| 77 | encryptedSymmetricKey: encryptedSymmetricKey, |
| 78 | encryptedContent: encryptedContent, |
| 79 | |
| 80 | dateTime, |
| 81 | } as DeepNotesNotification), |
| 82 | ); |
| 83 | }, |
| 84 | ), |
| 85 | ); |
| 86 | }), |
| 87 | ); |
| 88 | }); |
| 89 | } |
no test coverage detected