diff --git a/keyserver/src/push/crypto.js b/keyserver/src/push/crypto.js --- a/keyserver/src/push/crypto.js +++ b/keyserver/src/push/crypto.js @@ -3,6 +3,7 @@ import apn from '@parse/node-apn'; import invariant from 'invariant'; +import type { AndroidNotification } from './types.js'; import { encryptAndUpdateOlmSession } from '../updaters/olm-session-updater.js'; async function encryptIOSNotification( @@ -65,6 +66,42 @@ return encryptedNotification; } +async function encryptAndroidNotification( + cookieID: string, + notification: AndroidNotification, +): Promise { + const { id, badgeOnly, ...unencryptedPayload } = notification.data; + const encryptedNotification = { data: { id, badgeOnly } }; + + let encryptedSerializedPayload; + try { + const unencryptedSerializedPayload = JSON.stringify(unencryptedPayload); + const { serializedPayload } = await encryptAndUpdateOlmSession( + cookieID, + 'notifications', + { + serializedPayload: unencryptedSerializedPayload, + }, + ); + encryptedSerializedPayload = serializedPayload; + } catch (e) { + console.log('Notification encryption failed: ' + e); + + encryptedNotification.data = { + ...unencryptedPayload, + ...encryptedNotification.data, + encryptionFailed: '1', + }; + return encryptedNotification; + } + + encryptedNotification.data = { + ...encryptedNotification.data, + encryptedPayload: encryptedSerializedPayload.body, + }; + return encryptedNotification; +} + function prepareEncryptedIOSNotifications( cookieIDs: $ReadOnlyArray, notification: apn.Notification, @@ -75,4 +112,17 @@ return Promise.all(notificationPromises); } -export { prepareEncryptedIOSNotifications }; +function prepareEncryptedAndroidNotifications( + cookieIDs: $ReadOnlyArray, + notification: AndroidNotification, +): Promise> { + const notificationPromises = cookieIDs.map(cookieID => + encryptAndroidNotification(cookieID, notification), + ); + return Promise.all(notificationPromises); +} + +export { + prepareEncryptedIOSNotifications, + prepareEncryptedAndroidNotifications, +}; diff --git a/keyserver/src/push/types.js b/keyserver/src/push/types.js --- a/keyserver/src/push/types.js +++ b/keyserver/src/push/types.js @@ -6,3 +6,11 @@ +notification: apn.Notification, +deviceToken: string, }; + +export type AndroidNotification = { + +data: { + +id?: string, + +badgeOnly?: string, + +[string]: string, + }, +};