diff --git a/keyserver/src/push/crypto.js b/keyserver/src/push/crypto.js index 6011c0762..51936c2f2 100644 --- a/keyserver/src/push/crypto.js +++ b/keyserver/src/push/crypto.js @@ -1,582 +1,218 @@ // @flow import apn from '@parse/node-apn'; import crypto from 'crypto'; import invariant from 'invariant'; import _cloneDeep from 'lodash/fp/cloneDeep.js'; import type { - PlainTextWebNotification, - PlainTextWebNotificationPayload, - WebNotification, - PlainTextWNSNotification, - WNSNotification, - AndroidVisualNotification, - AndroidVisualNotificationPayload, - AndroidBadgeOnlyNotification, - AndroidNotificationRescind, NotificationTargetDevice, SenderDeviceDescriptor, EncryptedNotifUtilsAPI, } from 'lib/types/notif-types.js'; import { toBase64URL } from 'lib/utils/base64.js'; import { encrypt, generateKey } from '../utils/aes-crypto-utils.js'; import { getOlmUtility } from '../utils/olm-utils.js'; async function encryptAPNsNotification( encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, cookieID: string, senderDeviceDescriptor: SenderDeviceDescriptor, notification: apn.Notification, codeVersion?: ?number, notificationSizeValidator?: apn.Notification => boolean, blobHolder?: ?string, ): Promise<{ +notification: apn.Notification, +payloadSizeExceeded: boolean, +encryptedPayloadHash?: string, +encryptionOrder?: number, }> { invariant( !notification.collapseId, `Collapse ID can't be directly stored in apn.Notification object due ` + `to security reasons. Please put it in payload property`, ); const encryptedNotification = new apn.Notification(); encryptedNotification.id = notification.id; encryptedNotification.payload.id = notification.id; if (blobHolder) { encryptedNotification.payload.blobHolder = blobHolder; } encryptedNotification.payload.keyserverID = notification.payload.keyserverID; encryptedNotification.topic = notification.topic; encryptedNotification.sound = notification.aps.sound; encryptedNotification.pushType = 'alert'; encryptedNotification.mutableContent = true; const { id, ...payloadSansUnencryptedData } = notification.payload; const unencryptedPayload = { ...payloadSansUnencryptedData, badge: notification.aps.badge.toString(), merged: notification.body, }; try { const unencryptedSerializedPayload = JSON.stringify(unencryptedPayload); let dbPersistCondition; if (notificationSizeValidator) { dbPersistCondition = (serializedPayload: string) => { const notifCopy = _cloneDeep(encryptedNotification); notifCopy.payload.encryptedPayload = serializedPayload; return notificationSizeValidator(notifCopy); }; } const { encryptedData: serializedPayload, sizeLimitViolated: dbPersistConditionViolated, encryptionOrder, } = await encryptedNotifUtilsAPI.encryptSerializedNotifPayload( cookieID, unencryptedSerializedPayload, dbPersistCondition, ); encryptedNotification.payload.encryptedPayload = serializedPayload.body; encryptedNotification.payload = { ...encryptedNotification.payload, ...senderDeviceDescriptor, }; if (codeVersion && codeVersion >= 254 && codeVersion % 2 === 0) { encryptedNotification.aps = { alert: { body: 'ENCRYPTED' }, ...encryptedNotification.aps, }; } const encryptedPayloadHash = getOlmUtility().sha256(serializedPayload.body); return { notification: encryptedNotification, payloadSizeExceeded: !!dbPersistConditionViolated, encryptedPayloadHash, encryptionOrder, }; } catch (e) { console.log('Notification encryption failed: ' + e); encryptedNotification.body = notification.body; encryptedNotification.threadId = notification.payload.threadID; invariant( typeof notification.aps.badge === 'number', 'Unencrypted notification must have badge as a number', ); encryptedNotification.badge = notification.aps.badge; encryptedNotification.payload = { ...encryptedNotification.payload, ...notification.payload, encryptionFailed: 1, }; return { notification: encryptedNotification, payloadSizeExceeded: notificationSizeValidator ? notificationSizeValidator(_cloneDeep(encryptedNotification)) : false, }; } } -async function encryptAndroidNotificationPayload( - encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, - cookieID: string, - senderDeviceDescriptor: SenderDeviceDescriptor, - unencryptedPayload: T, - payloadSizeValidator?: ( - T | $ReadOnly<{ ...SenderDeviceDescriptor, +encryptedPayload: string }>, - ) => boolean, -): Promise<{ - +resultPayload: - | T - | $ReadOnly<{ ...SenderDeviceDescriptor, +encryptedPayload: string }>, - +payloadSizeExceeded: boolean, - +encryptionOrder?: number, -}> { - try { - const unencryptedSerializedPayload = JSON.stringify(unencryptedPayload); - if (!unencryptedSerializedPayload) { - return { - resultPayload: unencryptedPayload, - payloadSizeExceeded: payloadSizeValidator - ? payloadSizeValidator(unencryptedPayload) - : false, - }; - } - - let dbPersistCondition; - if (payloadSizeValidator) { - dbPersistCondition = (serializedPayload: string) => - payloadSizeValidator({ - encryptedPayload: serializedPayload, - ...senderDeviceDescriptor, - }); - } - - const { - encryptedData: serializedPayload, - sizeLimitViolated: dbPersistConditionViolated, - encryptionOrder, - } = await encryptedNotifUtilsAPI.encryptSerializedNotifPayload( - cookieID, - unencryptedSerializedPayload, - dbPersistCondition, - ); - - return { - resultPayload: { - encryptedPayload: serializedPayload.body, - ...senderDeviceDescriptor, - }, - payloadSizeExceeded: !!dbPersistConditionViolated, - encryptionOrder, - }; - } catch (e) { - console.log('Notification encryption failed: ' + e); - const resultPayload = { - encryptionFailed: '1', - ...unencryptedPayload, - }; - return { - resultPayload, - payloadSizeExceeded: payloadSizeValidator - ? payloadSizeValidator(resultPayload) - : false, - }; - } -} - -async function encryptAndroidVisualNotification( - encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, - senderDeviceDescriptor: SenderDeviceDescriptor, - cookieID: string, - notification: AndroidVisualNotification, - notificationSizeValidator?: AndroidVisualNotification => boolean, - blobHolder?: ?string, -): Promise<{ - +notification: AndroidVisualNotification, - +payloadSizeExceeded: boolean, - +encryptionOrder?: number, -}> { - const { id, ...rest } = notification.data; - - let unencryptedData = {}; - if (id) { - unencryptedData = { id }; - } - - let unencryptedPayload = rest; - if (blobHolder) { - unencryptedPayload = { ...unencryptedPayload, blobHolder }; - } - - let payloadSizeValidator; - if (notificationSizeValidator) { - payloadSizeValidator = ( - payload: - | AndroidVisualNotificationPayload - | $ReadOnly<{ ...SenderDeviceDescriptor, +encryptedPayload: string }>, - ) => { - return notificationSizeValidator({ - data: { ...unencryptedData, ...payload }, - }); - }; - } - const { resultPayload, payloadSizeExceeded, encryptionOrder } = - await encryptAndroidNotificationPayload( - encryptedNotifUtilsAPI, - cookieID, - senderDeviceDescriptor, - unencryptedPayload, - payloadSizeValidator, - ); - return { - notification: { - data: { - ...unencryptedData, - ...resultPayload, - }, - }, - payloadSizeExceeded, - encryptionOrder, - }; -} - -async function encryptAndroidSilentNotification( - encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, - cookieID: string, - senderDeviceDescriptor: SenderDeviceDescriptor, - notification: AndroidNotificationRescind | AndroidBadgeOnlyNotification, -): Promise { - // We don't validate payload size for rescind - // since they are expected to be small and - // never exceed any FCM limit - const { ...unencryptedPayload } = notification.data; - const { resultPayload } = await encryptAndroidNotificationPayload( - encryptedNotifUtilsAPI, - cookieID, - senderDeviceDescriptor, - unencryptedPayload, - ); - if (resultPayload.encryptedPayload) { - return { - data: { ...resultPayload }, - }; - } - - if (resultPayload.rescind) { - return { - data: { ...resultPayload }, - }; - } - - return { - data: { - ...resultPayload, - }, - }; -} - -async function encryptBasicPayload( - encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, - cookieID: string, - senderDeviceDescriptor: SenderDeviceDescriptor, - basicPayload: T, -): Promise< - | $ReadOnly<{ - ...SenderDeviceDescriptor, - +encryptedPayload: string, - +encryptionOrder?: number, - }> - | { ...T, +encryptionFailed: '1' }, -> { - const unencryptedSerializedPayload = JSON.stringify(basicPayload); - - if (!unencryptedSerializedPayload) { - return { ...basicPayload, encryptionFailed: '1' }; - } - - try { - const { encryptedData: serializedPayload, encryptionOrder } = - await encryptedNotifUtilsAPI.encryptSerializedNotifPayload( - cookieID, - unencryptedSerializedPayload, - ); - - return { - ...senderDeviceDescriptor, - encryptedPayload: serializedPayload.body, - encryptionOrder, - }; - } catch (e) { - console.log('Notification encryption failed: ' + e); - return { - ...basicPayload, - encryptionFailed: '1', - }; - } -} - -async function encryptWebNotification( - encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, - cookieID: string, - senderDeviceDescriptor: SenderDeviceDescriptor, - notification: PlainTextWebNotification, -): Promise<{ +notification: WebNotification, +encryptionOrder?: number }> { - const { id, ...payloadSansId } = notification; - const { encryptionOrder, ...encryptionResult } = - await encryptBasicPayload( - encryptedNotifUtilsAPI, - cookieID, - senderDeviceDescriptor, - payloadSansId, - ); - - return { - notification: { id, ...encryptionResult }, - encryptionOrder, - }; -} - -async function encryptWNSNotification( - encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, - cookieID: string, - senderDeviceDescriptor: SenderDeviceDescriptor, - notification: PlainTextWNSNotification, -): Promise<{ +notification: WNSNotification, +encryptionOrder?: number }> { - const { encryptionOrder, ...encryptionResult } = - await encryptBasicPayload( - encryptedNotifUtilsAPI, - cookieID, - senderDeviceDescriptor, - notification, - ); - return { - notification: { ...encryptionResult }, - encryptionOrder, - }; -} - function prepareEncryptedAPNsNotifications( encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, senderDeviceDescriptor: SenderDeviceDescriptor, devices: $ReadOnlyArray, notification: apn.Notification, codeVersion?: ?number, notificationSizeValidator?: apn.Notification => boolean, ): Promise< $ReadOnlyArray<{ +cookieID: string, +deviceToken: string, +notification: apn.Notification, +payloadSizeExceeded: boolean, +encryptedPayloadHash?: string, +encryptionOrder?: number, }>, > { const notificationPromises = devices.map( async ({ cookieID, deviceToken, blobHolder }) => { const notif = await encryptAPNsNotification( encryptedNotifUtilsAPI, cookieID, senderDeviceDescriptor, notification, codeVersion, notificationSizeValidator, blobHolder, ); return { cookieID, deviceToken, ...notif }; }, ); return Promise.all(notificationPromises); } function prepareEncryptedIOSNotificationRescind( encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, senderDeviceDescriptor: SenderDeviceDescriptor, devices: $ReadOnlyArray, notification: apn.Notification, codeVersion?: ?number, ): Promise< $ReadOnlyArray<{ +cookieID: string, +deviceToken: string, +notification: apn.Notification, }>, > { const notificationPromises = devices.map( async ({ deviceToken, cookieID }) => { const { notification: notif } = await encryptAPNsNotification( encryptedNotifUtilsAPI, cookieID, senderDeviceDescriptor, notification, codeVersion, ); return { deviceToken, cookieID, notification: notif }; }, ); return Promise.all(notificationPromises); } -function prepareEncryptedAndroidVisualNotifications( - encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, - senderDeviceDescriptor: SenderDeviceDescriptor, - devices: $ReadOnlyArray, - notification: AndroidVisualNotification, - notificationSizeValidator?: ( - notification: AndroidVisualNotification, - ) => boolean, -): Promise< - $ReadOnlyArray<{ - +cookieID: string, - +deviceToken: string, - +notification: AndroidVisualNotification, - +payloadSizeExceeded: boolean, - +encryptionOrder?: number, - }>, -> { - const notificationPromises = devices.map( - async ({ deviceToken, cookieID, blobHolder }) => { - const notif = await encryptAndroidVisualNotification( - encryptedNotifUtilsAPI, - senderDeviceDescriptor, - cookieID, - notification, - notificationSizeValidator, - blobHolder, - ); - return { deviceToken, cookieID, ...notif }; - }, - ); - return Promise.all(notificationPromises); -} - -function prepareEncryptedAndroidSilentNotifications( - encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, - senderDeviceDescriptor: SenderDeviceDescriptor, - devices: $ReadOnlyArray, - notification: AndroidNotificationRescind | AndroidBadgeOnlyNotification, -): Promise< - $ReadOnlyArray<{ - +cookieID: string, - +deviceToken: string, - +notification: AndroidNotificationRescind | AndroidBadgeOnlyNotification, - +encryptionOrder?: number, - }>, -> { - const notificationPromises = devices.map( - async ({ deviceToken, cookieID }) => { - const notif = await encryptAndroidSilentNotification( - encryptedNotifUtilsAPI, - cookieID, - senderDeviceDescriptor, - notification, - ); - return { deviceToken, cookieID, notification: notif }; - }, - ); - return Promise.all(notificationPromises); -} - -function prepareEncryptedWebNotifications( - encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, - senderDeviceDescriptor: SenderDeviceDescriptor, - devices: $ReadOnlyArray, - notification: PlainTextWebNotification, -): Promise< - $ReadOnlyArray<{ - +deviceToken: string, - +notification: WebNotification, - +encryptionOrder?: number, - }>, -> { - const notificationPromises = devices.map( - async ({ deviceToken, cookieID }) => { - const notif = await encryptWebNotification( - encryptedNotifUtilsAPI, - cookieID, - senderDeviceDescriptor, - notification, - ); - return { ...notif, deviceToken }; - }, - ); - return Promise.all(notificationPromises); -} - -function prepareEncryptedWNSNotifications( - encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, - senderDeviceDescriptor: SenderDeviceDescriptor, - devices: $ReadOnlyArray, - notification: PlainTextWNSNotification, -): Promise< - $ReadOnlyArray<{ - +deviceToken: string, - +notification: WNSNotification, - +encryptionOrder?: number, - }>, -> { - const notificationPromises = devices.map( - async ({ deviceToken, cookieID }) => { - const notif = await encryptWNSNotification( - encryptedNotifUtilsAPI, - cookieID, - senderDeviceDescriptor, - notification, - ); - return { ...notif, deviceToken }; - }, - ); - return Promise.all(notificationPromises); -} - async function encryptBlobPayload(payload: string): Promise<{ +encryptionKey: string, +encryptedPayload: Blob, +encryptedPayloadHash: string, }> { const encryptionKey = await generateKey(); const encryptedPayload = await encrypt( encryptionKey, new TextEncoder().encode(payload), ); const encryptedPayloadBuffer = Buffer.from(encryptedPayload); const blobHashBase64 = await crypto .createHash('sha256') .update(encryptedPayloadBuffer) .digest('base64'); const blobHash = toBase64URL(blobHashBase64); const payloadBlob = new Blob([encryptedPayloadBuffer]); const encryptionKeyString = Buffer.from(encryptionKey).toString('base64'); return { encryptionKey: encryptionKeyString, encryptedPayload: payloadBlob, encryptedPayloadHash: blobHash, }; } export { prepareEncryptedAPNsNotifications, prepareEncryptedIOSNotificationRescind, - prepareEncryptedAndroidVisualNotifications, - prepareEncryptedAndroidSilentNotifications, - prepareEncryptedWebNotifications, - prepareEncryptedWNSNotifications, encryptBlobPayload, }; diff --git a/keyserver/src/push/encrypted-notif-utils-api.js b/keyserver/src/push/encrypted-notif-utils-api.js index 24be39bb8..efe57e1a2 100644 --- a/keyserver/src/push/encrypted-notif-utils-api.js +++ b/keyserver/src/push/encrypted-notif-utils-api.js @@ -1,49 +1,56 @@ // @flow import type { EncryptResult } from '@commapp/olm'; import type { EncryptedNotifUtilsAPI } from 'lib/types/notif-types.js'; import { blobServiceUpload } from './utils.js'; import { encryptAndUpdateOlmSession } from '../updaters/olm-session-updater.js'; const encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI = { encryptSerializedNotifPayload: async ( cryptoID: string, unencryptedPayload: string, - encryptedPayloadSizeValidator?: (encryptedPayload: string) => boolean, + encryptedPayloadSizeValidator?: ( + encryptedPayload: string, + type: '1' | '0', + ) => boolean, ) => { let dbPersistCondition; if (encryptedPayloadSizeValidator) { dbPersistCondition = ({ serializedPayload, }: { +[string]: EncryptResult, - }) => encryptedPayloadSizeValidator(serializedPayload.body); + }) => + encryptedPayloadSizeValidator( + serializedPayload.body, + serializedPayload.type ? '1' : '0', + ); } const { encryptedMessages: { serializedPayload }, dbPersistConditionViolated, encryptionOrder, } = await encryptAndUpdateOlmSession( cryptoID, 'notifications', { serializedPayload: unencryptedPayload, }, dbPersistCondition, ); return { encryptedData: serializedPayload, sizeLimitViolated: dbPersistConditionViolated, encryptionOrder, }; }, uploadLargeNotifPayload: blobServiceUpload, getNotifByteSize: (serializedPayload: string) => Buffer.byteLength(serializedPayload), }; export default encryptedNotifUtilsAPI; diff --git a/keyserver/src/push/rescind.js b/keyserver/src/push/rescind.js index 19cb556cc..3f22c5dff 100644 --- a/keyserver/src/push/rescind.js +++ b/keyserver/src/push/rescind.js @@ -1,405 +1,403 @@ // @flow import apn from '@parse/node-apn'; import type { ResponseFailure } from '@parse/node-apn'; import type { FirebaseError } from 'firebase-admin'; import invariant from 'invariant'; +import { prepareEncryptedAndroidSilentNotifications } from 'lib/push/crypto.js'; import type { PlatformDetails } from 'lib/types/device-types.js'; import type { NotificationTargetDevice, TargetedAndroidNotification, SenderDeviceDescriptor, EncryptedNotifUtilsAPI, } from 'lib/types/notif-types.js'; import { threadSubscriptions } from 'lib/types/subscription-types.js'; import { threadPermissions } from 'lib/types/thread-permission-types.js'; import { promiseAll } from 'lib/utils/promises.js'; import { tID } from 'lib/utils/validation-utils.js'; -import { - prepareEncryptedAndroidSilentNotifications, - prepareEncryptedIOSNotificationRescind, -} from './crypto.js'; +import { prepareEncryptedIOSNotificationRescind } from './crypto.js'; import encryptedNotifUtilsAPI from './encrypted-notif-utils-api.js'; import { getAPNsNotificationTopic } from './providers.js'; import type { TargetedAPNsNotification } from './types.js'; import { apnPush, fcmPush, type APNPushResult, type FCMPushResult, } from './utils.js'; import createIDs from '../creators/id-creator.js'; import { dbQuery, SQL } from '../database/database.js'; import type { SQLStatementType } from '../database/types.js'; import { thisKeyserverID } from '../user/identity.js'; import { validateOutput } from '../utils/validation-utils.js'; type ParsedDelivery = { +platform: 'ios' | 'macos' | 'android', +codeVersion: ?number, +stateVersion: ?number, +notificationID: string, +deviceTokens: $ReadOnlyArray, }; type RescindDelivery = { source: 'rescind', rescindedID: string, errors?: | $ReadOnlyArray | $ReadOnlyArray, }; async function rescindPushNotifs( notifCondition: SQLStatementType, inputCountCondition?: SQLStatementType, ) { const notificationExtractString = `$.${threadSubscriptions.home}`; const visPermissionExtractString = `$.${threadPermissions.VISIBLE}.value`; const fetchQuery = SQL` SELECT n.id, n.user, n.thread, n.message, n.delivery, n.collapse_key, COUNT( `; fetchQuery.append(inputCountCondition ? inputCountCondition : SQL`m.thread`); fetchQuery.append(SQL` ) AS unread_count FROM notifications n LEFT JOIN memberships m ON m.user = n.user AND m.last_message > m.last_read_message AND m.role > 0 AND JSON_EXTRACT(subscription, ${notificationExtractString}) AND JSON_EXTRACT(permissions, ${visPermissionExtractString}) WHERE n.rescinded = 0 AND `); fetchQuery.append(notifCondition); fetchQuery.append(SQL` GROUP BY n.id, m.user`); const [[fetchResult], keyserverID] = await Promise.all([ dbQuery(fetchQuery), thisKeyserverID(), ]); const allDeviceTokens = new Set(); const parsedDeliveries: { [string]: $ReadOnlyArray } = {}; for (const row of fetchResult) { const rawDelivery = JSON.parse(row.delivery); const deliveries = Array.isArray(rawDelivery) ? rawDelivery : [rawDelivery]; const id = row.id.toString(); const rowParsedDeliveries = []; for (const delivery of deliveries) { if ( delivery.iosID || delivery.deviceType === 'ios' || delivery.deviceType === 'macos' ) { const deviceTokens = delivery.iosDeviceTokens ?? delivery.deviceTokens; rowParsedDeliveries.push({ notificationID: delivery.iosID, codeVersion: delivery.codeVersion, stateVersion: delivery.stateVersion, platform: delivery.deviceType ?? 'ios', deviceTokens, }); deviceTokens.forEach(deviceToken => allDeviceTokens.add(deviceToken)); } else if (delivery.androidID || delivery.deviceType === 'android') { const deviceTokens = delivery.androidDeviceTokens ?? delivery.deviceTokens; rowParsedDeliveries.push({ notificationID: row.collapse_key ? row.collapse_key : id, codeVersion: delivery.codeVersion, stateVersion: delivery.stateVersion, platform: 'android', deviceTokens, }); deviceTokens.forEach(deviceToken => allDeviceTokens.add(deviceToken)); } } parsedDeliveries[id] = rowParsedDeliveries; } const deviceTokenToCookieID = await getDeviceTokenToCookieID(allDeviceTokens); const deliveryPromises: { [string]: Promise | Promise, } = {}; const notifInfo = {}; const rescindedIDs = []; for (const row of fetchResult) { const id = row.id.toString(); const threadID = row.thread.toString(); notifInfo[id] = { userID: row.user.toString(), threadID, messageID: row.message.toString(), }; for (const delivery of parsedDeliveries[id]) { let platformDetails: PlatformDetails = { platform: delivery.platform }; if (delivery.codeVersion) { platformDetails = { ...platformDetails, codeVersion: delivery.codeVersion, }; } if (delivery.stateVersion) { platformDetails = { ...platformDetails, stateVersion: delivery.stateVersion, }; } if (delivery.platform === 'ios') { const devices = delivery.deviceTokens.map(deviceToken => ({ deviceToken, cookieID: deviceTokenToCookieID[deviceToken], })); const deliveryPromise = (async () => { const targetedNotifications = await prepareIOSNotification( keyserverID, delivery.notificationID, row.unread_count, threadID, platformDetails, devices, ); return await apnPush({ targetedNotifications, platformDetails: { platform: 'ios', codeVersion: delivery.codeVersion, }, }); })(); deliveryPromises[id] = deliveryPromise; } else if (delivery.platform === 'android') { const devices = delivery.deviceTokens.map(deviceToken => ({ deviceToken, cookieID: deviceTokenToCookieID[deviceToken], })); const deliveryPromise = (async () => { const targetedNotifications = await prepareAndroidNotification( keyserverID, delivery.notificationID, row.unread_count, threadID, platformDetails, devices, ); return await fcmPush({ targetedNotifications, codeVersion: delivery.codeVersion, }); })(); deliveryPromises[id] = deliveryPromise; } } rescindedIDs.push(id); } const numRescinds = Object.keys(deliveryPromises).length; const dbIDsPromise: Promise> = (async () => { if (numRescinds === 0) { return undefined; } return await createIDs('notifications', numRescinds); })(); const rescindPromise: Promise = (async () => { if (rescindedIDs.length === 0) { return undefined; } const rescindQuery = SQL` UPDATE notifications SET rescinded = 1 WHERE id IN (${rescindedIDs}) `; return await dbQuery(rescindQuery); })(); const [deliveryResults, dbIDs] = await Promise.all([ promiseAll(deliveryPromises), dbIDsPromise, rescindPromise, ]); const newNotifRows = []; if (numRescinds > 0) { invariant(dbIDs, 'dbIDs should be set'); for (const rescindedID in deliveryResults) { const delivery: RescindDelivery = { source: 'rescind', rescindedID, }; const { errors } = deliveryResults[rescindedID]; if (errors) { delivery.errors = errors; } const dbID = dbIDs.shift(); const { userID, threadID, messageID } = notifInfo[rescindedID]; newNotifRows.push([ dbID, userID, threadID, messageID, null, JSON.stringify([delivery]), 1, ]); } } if (newNotifRows.length > 0) { const insertQuery = SQL` INSERT INTO notifications (id, user, thread, message, collapse_key, delivery, rescinded) VALUES ${newNotifRows} `; await dbQuery(insertQuery); } } async function getDeviceTokenToCookieID( deviceTokens: Set, ): Promise<{ +[string]: string }> { if (deviceTokens.size === 0) { return {}; } const deviceTokenToCookieID = {}; const fetchCookiesQuery = SQL` SELECT id, device_token FROM cookies WHERE device_token IN (${[...deviceTokens]}) `; const [fetchResult] = await dbQuery(fetchCookiesQuery); for (const row of fetchResult) { deviceTokenToCookieID[row.device_token.toString()] = row.id.toString(); } return deviceTokenToCookieID; } async function conditionallyEncryptNotification( encryptedNotifUtilsAPIInstance: EncryptedNotifUtilsAPI, senderDeviceDescriptor: SenderDeviceDescriptor, notification: T, codeVersion: ?number, devices: $ReadOnlyArray, encryptCallback: ( encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, senderDeviceDescriptor: SenderDeviceDescriptor, devices: $ReadOnlyArray, notification: T, codeVersion?: ?number, ) => Promise< $ReadOnlyArray<{ +notification: T, +cookieID: string, +deviceToken: string, +encryptionOrder?: number, }>, >, ): Promise<$ReadOnlyArray<{ +deviceToken: string, +notification: T }>> { const shouldBeEncrypted = codeVersion && codeVersion >= 233; if (!shouldBeEncrypted) { return devices.map(({ deviceToken }) => ({ notification, deviceToken, })); } const notifications = await encryptCallback( encryptedNotifUtilsAPI, senderDeviceDescriptor, devices, notification, codeVersion, ); return notifications.map(({ deviceToken, notification: notif }) => ({ deviceToken, notification: notif, })); } async function prepareIOSNotification( keyserverID: string, iosID: string, unreadCount: number, threadID: string, platformDetails: PlatformDetails, devices: $ReadOnlyArray, ): Promise<$ReadOnlyArray> { threadID = await validateOutput(platformDetails, tID, threadID); const { codeVersion } = platformDetails; const notification = new apn.Notification(); notification.topic = getAPNsNotificationTopic({ platform: 'ios', codeVersion, }); if (codeVersion && codeVersion > 198) { notification.mutableContent = true; notification.pushType = 'alert'; notification.badge = unreadCount; } else { notification.priority = 5; notification.contentAvailable = true; notification.pushType = 'background'; } notification.payload = codeVersion && codeVersion > 135 ? { backgroundNotifType: 'CLEAR', notificationId: iosID, setUnreadStatus: true, threadID, keyserverID, } : { managedAps: { action: 'CLEAR', notificationId: iosID, }, }; return await conditionallyEncryptNotification( encryptedNotifUtilsAPI, { keyserverID }, notification, codeVersion, devices, prepareEncryptedIOSNotificationRescind, ); } async function prepareAndroidNotification( keyserverID: string, notifID: string, unreadCount: number, threadID: string, platformDetails: PlatformDetails, devices: $ReadOnlyArray, ): Promise<$ReadOnlyArray> { threadID = await validateOutput(platformDetails, tID, threadID); const { codeVersion } = platformDetails; const notification = { data: { badge: unreadCount.toString(), rescind: 'true', rescindID: notifID, setUnreadStatus: 'true', threadID, }, }; const targetedRescinds = await conditionallyEncryptNotification( encryptedNotifUtilsAPI, { keyserverID }, notification, codeVersion, devices, prepareEncryptedAndroidSilentNotifications, ); return targetedRescinds.map(targetedRescind => ({ ...targetedRescind, priority: 'normal', })); } export { rescindPushNotifs }; diff --git a/keyserver/src/push/send.js b/keyserver/src/push/send.js index 641b6a803..879be795b 100644 --- a/keyserver/src/push/send.js +++ b/keyserver/src/push/send.js @@ -1,1975 +1,1720 @@ // @flow import type { ResponseFailure } from '@parse/node-apn'; import apn from '@parse/node-apn'; import invariant from 'invariant'; import _cloneDeep from 'lodash/fp/cloneDeep.js'; import _flow from 'lodash/fp/flow.js'; import _groupBy from 'lodash/fp/groupBy.js'; import _mapValues from 'lodash/fp/mapValues.js'; import _pickBy from 'lodash/fp/pickBy.js'; import type { QueryResults } from 'mysql'; import t from 'tcomb'; import uuidv4 from 'uuid/v4.js'; +import { + type AndroidNotifInputData, + androidNotifInputDataValidator, + createAndroidVisualNotification, +} from 'lib/push/android-notif-creators.js'; +import { prepareEncryptedAndroidSilentNotifications } from 'lib/push/crypto.js'; +import { + type WebNotifInputData, + webNotifInputDataValidator, + createWebNotification, +} from 'lib/push/web-notif-creators.js'; +import { + type WNSNotifInputData, + wnsNotifInputDataValidator, + createWNSNotification, +} from 'lib/push/wns-notif-creators.js'; import { oldValidUsernameRegex } from 'lib/shared/account-utils.js'; import { isUserMentioned } from 'lib/shared/mention-utils.js'; import { createMessageInfo, shimUnsupportedRawMessageInfos, sortMessageInfoList, } from 'lib/shared/message-utils.js'; import { messageSpecs } from 'lib/shared/messages/message-specs.js'; import { notifTextsForMessageInfo } from 'lib/shared/notif-utils.js'; import { rawThreadInfoFromServerThreadInfo, threadInfoFromRawThreadInfo, } from 'lib/shared/thread-utils.js'; import { hasMinCodeVersion } from 'lib/shared/version-utils.js'; import type { Platform, PlatformDetails } from 'lib/types/device-types.js'; import { messageTypes } from 'lib/types/message-types-enum.js'; import { type MessageData, type RawMessageInfo, rawMessageInfoValidator, } from 'lib/types/message-types.js'; import type { ThreadInfo } from 'lib/types/minimally-encoded-thread-permissions-types.js'; import type { - AndroidVisualNotification, NotificationTargetDevice, TargetedAndroidNotification, TargetedWebNotification, TargetedWNSNotification, ResolvedNotifTexts, } from 'lib/types/notif-types.js'; import { resolvedNotifTextsValidator } from 'lib/types/notif-types.js'; import type { ServerThreadInfo } from 'lib/types/thread-types.js'; import { updateTypes } from 'lib/types/update-types-enum.js'; import { type GlobalUserInfo } from 'lib/types/user-types.js'; import { values } from 'lib/utils/objects.js'; import { tID, tPlatformDetails, tShape } from 'lib/utils/validation-utils.js'; -import { - prepareEncryptedAndroidVisualNotifications, - prepareEncryptedAndroidSilentNotifications, - prepareEncryptedAPNsNotifications, - prepareEncryptedWebNotifications, - prepareEncryptedWNSNotifications, -} from './crypto.js'; +import { prepareEncryptedAPNsNotifications } from './crypto.js'; import encryptedNotifUtilsAPI from './encrypted-notif-utils-api.js'; import { getAPNsNotificationTopic } from './providers.js'; import { rescindPushNotifs } from './rescind.js'; import type { TargetedAPNsNotification } from './types.js'; import { apnMaxNotificationPayloadByteSize, apnPush, - fcmMaxNotificationPayloadByteSize, fcmPush, getUnreadCounts, webPush, type WebPushError, - wnsMaxNotificationPayloadByteSize, wnsPush, type WNSPushError, } from './utils.js'; import createIDs from '../creators/id-creator.js'; import { createUpdates } from '../creators/update-creator.js'; import { dbQuery, mergeOrConditions, SQL } from '../database/database.js'; import type { CollapsableNotifInfo } from '../fetchers/message-fetchers.js'; import { fetchCollapsableNotifs } from '../fetchers/message-fetchers.js'; import { fetchServerThreadInfos } from '../fetchers/thread-fetchers.js'; import { fetchUserInfos } from '../fetchers/user-fetchers.js'; import type { Viewer } from '../session/viewer.js'; import { thisKeyserverID } from '../user/identity.js'; import { getENSNames } from '../utils/ens-cache.js'; import { getFCNames } from '../utils/fc-cache.js'; import { validateOutput } from '../utils/validation-utils.js'; export type Device = { +platform: Platform, +deviceToken: string, +cookieID: string, +codeVersion: ?number, +stateVersion: ?number, +majorDesktopVersion: ?number, }; export type PushUserInfo = { +devices: Device[], // messageInfos and messageDatas have the same key +messageInfos: RawMessageInfo[], +messageDatas: MessageData[], }; type Delivery = PushDelivery | { collapsedInto: string }; type NotificationRow = { +dbID: string, +userID: string, +threadID?: ?string, +messageID?: ?string, +collapseKey?: ?string, +deliveries: Delivery[], }; export type PushInfo = { [userID: string]: PushUserInfo }; async function sendPushNotifs(pushInfo: PushInfo) { if (Object.keys(pushInfo).length === 0) { return; } const keyserverID = await thisKeyserverID(); const [ unreadCounts, { usersToCollapsableNotifInfo, serverThreadInfos, userInfos }, dbIDs, ] = await Promise.all([ getUnreadCounts(Object.keys(pushInfo)), fetchInfos(pushInfo), createDBIDs(pushInfo), ]); const preparePromises: Array>> = []; const notifications: Map = new Map(); for (const userID in usersToCollapsableNotifInfo) { const threadInfos = _flow( _mapValues((serverThreadInfo: ServerThreadInfo) => { const rawThreadInfo = rawThreadInfoFromServerThreadInfo( serverThreadInfo, userID, { minimallyEncodePermissions: true }, ); if (!rawThreadInfo) { return null; } invariant( rawThreadInfo.minimallyEncoded, 'rawThreadInfo from rawThreadInfoFromServerThreadInfo must be ' + 'minimallyEncoded when minimallyEncodePermissions option is set', ); return threadInfoFromRawThreadInfo(rawThreadInfo, userID, userInfos); }), _pickBy(threadInfo => threadInfo), )(serverThreadInfos); for (const notifInfo of usersToCollapsableNotifInfo[userID]) { preparePromises.push( preparePushNotif({ keyserverID, notifInfo, userID, pushUserInfo: pushInfo[userID], unreadCount: unreadCounts[userID], threadInfos, userInfos, dbIDs, rowsToSave: notifications, }), ); } } const prepareResults = await Promise.all(preparePromises); const flattenedPrepareResults = prepareResults.filter(Boolean).flat(); const deliveryResults = await deliverPushNotifsInEncryptionOrder( flattenedPrepareResults, ); const cleanUpPromise = (async () => { if (dbIDs.length === 0) { return; } const query = SQL`DELETE FROM ids WHERE id IN (${dbIDs})`; await dbQuery(query); })(); await Promise.all([ cleanUpPromise, saveNotifResults(deliveryResults, notifications, true), ]); } type PreparePushResult = { +platform: Platform, +notificationInfo: NotificationInfo, +notification: | TargetedAPNsNotification | TargetedAndroidNotification | TargetedWebNotification | TargetedWNSNotification, }; async function preparePushNotif(input: { keyserverID: string, notifInfo: CollapsableNotifInfo, userID: string, pushUserInfo: PushUserInfo, unreadCount: number, threadInfos: { +[threadID: string]: ThreadInfo, }, userInfos: { +[userID: string]: GlobalUserInfo }, dbIDs: string[], // mutable rowsToSave: Map, // mutable }): Promise> { const { keyserverID, notifInfo, userID, pushUserInfo, unreadCount, threadInfos, userInfos, dbIDs, rowsToSave, } = input; const hydrateMessageInfo = (rawMessageInfo: RawMessageInfo) => createMessageInfo(rawMessageInfo, userID, userInfos, threadInfos); const newMessageInfos = []; const newRawMessageInfos = []; for (const newRawMessageInfo of notifInfo.newMessageInfos) { const newMessageInfo = hydrateMessageInfo(newRawMessageInfo); if (newMessageInfo) { newMessageInfos.push(newMessageInfo); newRawMessageInfos.push(newRawMessageInfo); } } if (newMessageInfos.length === 0) { return null; } const existingMessageInfos = notifInfo.existingMessageInfos .map(hydrateMessageInfo) .filter(Boolean); const allMessageInfos = sortMessageInfoList([ ...newMessageInfos, ...existingMessageInfos, ]); const [firstNewMessageInfo, ...remainingNewMessageInfos] = newMessageInfos; const { threadID } = firstNewMessageInfo; const threadInfo = threadInfos[threadID]; const parentThreadInfo = threadInfo.parentThreadID ? threadInfos[threadInfo.parentThreadID] : null; const updateBadge = threadInfo.currentUser.subscription.home; const displayBanner = threadInfo.currentUser.subscription.pushNotifs; const username = userInfos[userID] && userInfos[userID].username; let resolvedUsername; if (getENSNames) { const userInfosWithENSNames = await getENSNames([userInfos[userID]]); resolvedUsername = userInfosWithENSNames[0].username; } const userWasMentioned = username && threadInfo.currentUser.role && oldValidUsernameRegex.test(username) && newMessageInfos.some(newMessageInfo => { const unwrappedMessageInfo = newMessageInfo.type === messageTypes.SIDEBAR_SOURCE ? newMessageInfo.sourceMessage : newMessageInfo; return ( unwrappedMessageInfo.type === messageTypes.TEXT && (isUserMentioned(username, unwrappedMessageInfo.text) || (resolvedUsername && isUserMentioned(resolvedUsername, unwrappedMessageInfo.text))) ); }); if (!updateBadge && !displayBanner && !userWasMentioned) { return null; } const badgeOnly = !displayBanner && !userWasMentioned; const notifTargetUserInfo = { id: userID, username }; const notifTexts = await notifTextsForMessageInfo( allMessageInfos, threadInfo, parentThreadInfo, notifTargetUserInfo, getENSNames, getFCNames, ); if (!notifTexts) { return null; } const dbID = dbIDs.shift(); invariant(dbID, 'should have sufficient DB IDs'); const byPlatform = getDevicesByPlatform(pushUserInfo.devices); const firstMessageID = firstNewMessageInfo.id; invariant(firstMessageID, 'RawMessageInfo.id should be set on server'); const notificationInfo = { source: 'new_message', dbID, userID, threadID, messageID: firstMessageID, collapseKey: notifInfo.collapseKey, }; const preparePromises: Array>> = []; const iosVersionsToTokens = byPlatform.get('ios'); if (iosVersionsToTokens) { for (const [versionKey, devices] of iosVersionsToTokens) { const { codeVersion, stateVersion } = stringToVersionKey(versionKey); const platformDetails: PlatformDetails = { platform: 'ios', codeVersion, stateVersion, }; const shimmedNewRawMessageInfos = shimUnsupportedRawMessageInfos( newRawMessageInfos, platformDetails, ); const preparePromise: Promise<$ReadOnlyArray> = (async () => { const targetedNotifications = await prepareAPNsNotification( { keyserverID, notifTexts, newRawMessageInfos: shimmedNewRawMessageInfos, threadID: threadInfo.id, collapseKey: notifInfo.collapseKey, badgeOnly, unreadCount, platformDetails, }, devices, ); return targetedNotifications.map(notification => ({ notification, platform: 'ios', notificationInfo: { ...notificationInfo, codeVersion, stateVersion, }, })); })(); preparePromises.push(preparePromise); } } const androidVersionsToTokens = byPlatform.get('android'); if (androidVersionsToTokens) { for (const [versionKey, devices] of androidVersionsToTokens) { const { codeVersion, stateVersion } = stringToVersionKey(versionKey); const platformDetails = { platform: 'android', codeVersion, stateVersion, }; const shimmedNewRawMessageInfos = shimUnsupportedRawMessageInfos( newRawMessageInfos, platformDetails, ); const preparePromise: Promise<$ReadOnlyArray> = (async () => { const targetedNotifications = await prepareAndroidVisualNotification( { - keyserverID, + senderDeviceDescriptor: { keyserverID }, notifTexts, newRawMessageInfos: shimmedNewRawMessageInfos, threadID: threadInfo.id, collapseKey: notifInfo.collapseKey, badgeOnly, unreadCount, platformDetails, - dbID, + notifID: dbID, }, devices, ); return targetedNotifications.map(notification => ({ notification, platform: 'android', notificationInfo: { ...notificationInfo, codeVersion, stateVersion, }, })); })(); preparePromises.push(preparePromise); } } const webVersionsToTokens = byPlatform.get('web'); if (webVersionsToTokens) { for (const [versionKey, devices] of webVersionsToTokens) { const { codeVersion, stateVersion } = stringToVersionKey(versionKey); const platformDetails = { platform: 'web', codeVersion, stateVersion, }; const preparePromise: Promise<$ReadOnlyArray> = (async () => { const targetedNotifications = await prepareWebNotification( { notifTexts, threadID: threadInfo.id, - keyserverID, + senderDeviceDescriptor: { keyserverID }, unreadCount, platformDetails, + id: uuidv4(), }, devices, ); return targetedNotifications.map(notification => ({ notification, platform: 'web', notificationInfo: { ...notificationInfo, codeVersion, stateVersion, }, })); })(); preparePromises.push(preparePromise); } } const macosVersionsToTokens = byPlatform.get('macos'); if (macosVersionsToTokens) { for (const [versionKey, devices] of macosVersionsToTokens) { const { codeVersion, stateVersion, majorDesktopVersion } = stringToVersionKey(versionKey); const platformDetails = { platform: 'macos', codeVersion, stateVersion, majorDesktopVersion, }; const shimmedNewRawMessageInfos = shimUnsupportedRawMessageInfos( newRawMessageInfos, platformDetails, ); const preparePromise: Promise<$ReadOnlyArray> = (async () => { const targetedNotifications = await prepareAPNsNotification( { keyserverID, notifTexts, newRawMessageInfos: shimmedNewRawMessageInfos, threadID: threadInfo.id, collapseKey: notifInfo.collapseKey, badgeOnly, unreadCount, platformDetails, }, devices, ); return targetedNotifications.map(notification => ({ notification, platform: 'macos', notificationInfo: { ...notificationInfo, codeVersion, stateVersion, }, })); })(); preparePromises.push(preparePromise); } } const windowsVersionsToTokens = byPlatform.get('windows'); if (windowsVersionsToTokens) { for (const [versionKey, devices] of windowsVersionsToTokens) { const { codeVersion, stateVersion, majorDesktopVersion } = stringToVersionKey(versionKey); const platformDetails = { platform: 'windows', codeVersion, stateVersion, majorDesktopVersion, }; const preparePromise: Promise<$ReadOnlyArray> = (async () => { const targetedNotifications = await prepareWNSNotification(devices, { notifTexts, threadID: threadInfo.id, - keyserverID, + senderDeviceDescriptor: { keyserverID }, unreadCount, platformDetails, }); return targetedNotifications.map(notification => ({ notification, platform: 'windows', notificationInfo: { ...notificationInfo, codeVersion, stateVersion, }, })); })(); preparePromises.push(preparePromise); } } for (const newMessageInfo of remainingNewMessageInfos) { const newDBID = dbIDs.shift(); invariant(newDBID, 'should have sufficient DB IDs'); const messageID = newMessageInfo.id; invariant(messageID, 'RawMessageInfo.id should be set on server'); rowsToSave.set(newDBID, { dbID: newDBID, userID, threadID: newMessageInfo.threadID, messageID, collapseKey: notifInfo.collapseKey, deliveries: [{ collapsedInto: dbID }], }); } const prepareResults = await Promise.all(preparePromises); return prepareResults.flat(); } // For better readability we don't differentiate between // encrypted and unencrypted notifs and order them together function compareEncryptionOrder( pushNotif1: PreparePushResult, pushNotif2: PreparePushResult, ): number { const order1 = pushNotif1.notification.encryptionOrder ?? 0; const order2 = pushNotif2.notification.encryptionOrder ?? 0; return order1 - order2; } async function deliverPushNotifsInEncryptionOrder( preparedPushNotifs: $ReadOnlyArray, ): Promise<$ReadOnlyArray> { const deliveryPromises: Array>> = []; const groupedByDevice = _groupBy( preparedPushNotif => preparedPushNotif.deviceToken, )(preparedPushNotifs); for (const preparedPushNotifsForDevice of values(groupedByDevice)) { const orderedPushNotifsForDevice = preparedPushNotifsForDevice.sort( compareEncryptionOrder, ); const deviceDeliveryPromise = (async () => { const deliveries = []; for (const preparedPushNotif of orderedPushNotifsForDevice) { const { platform, notification, notificationInfo } = preparedPushNotif; let delivery: PushResult; if (platform === 'ios' || platform === 'macos') { delivery = await sendAPNsNotification( platform, [notification], notificationInfo, ); } else if (platform === 'android') { delivery = await sendAndroidNotification( [notification], notificationInfo, ); } else if (platform === 'web') { delivery = await sendWebNotifications( [notification], notificationInfo, ); } else if (platform === 'windows') { delivery = await sendWNSNotification( [notification], notificationInfo, ); } if (delivery) { deliveries.push(delivery); } } return deliveries; })(); deliveryPromises.push(deviceDeliveryPromise); } const deliveryResults = await Promise.all(deliveryPromises); return deliveryResults.flat(); } async function sendRescindNotifs(rescindInfo: PushInfo) { if (Object.keys(rescindInfo).length === 0) { return; } const usersToCollapsableNotifInfo = await fetchCollapsableNotifs(rescindInfo); const promises = []; for (const userID in usersToCollapsableNotifInfo) { for (const notifInfo of usersToCollapsableNotifInfo[userID]) { for (const existingMessageInfo of notifInfo.existingMessageInfos) { const rescindCondition = SQL` n.user = ${userID} AND n.thread = ${existingMessageInfo.threadID} AND n.message = ${existingMessageInfo.id} `; promises.push(rescindPushNotifs(rescindCondition)); } } } await Promise.all(promises); } // The results in deliveryResults will be combined with the rows // in rowsToSave and then written to the notifications table async function saveNotifResults( deliveryResults: $ReadOnlyArray, inputRowsToSave: Map, rescindable: boolean, ) { const rowsToSave = new Map(inputRowsToSave); const allInvalidTokens = []; for (const deliveryResult of deliveryResults) { const { info, delivery, invalidTokens } = deliveryResult; const { dbID, userID } = info; const curNotifRow = rowsToSave.get(dbID); if (curNotifRow) { curNotifRow.deliveries.push(delivery); } else { // Ternary expressions for Flow const threadID = info.threadID ? info.threadID : null; const messageID = info.messageID ? info.messageID : null; const collapseKey = info.collapseKey ? info.collapseKey : null; rowsToSave.set(dbID, { dbID, userID, threadID, messageID, collapseKey, deliveries: [delivery], }); } if (invalidTokens) { allInvalidTokens.push({ userID, tokens: invalidTokens, }); } } const notificationRows = []; for (const notification of rowsToSave.values()) { notificationRows.push([ notification.dbID, notification.userID, notification.threadID, notification.messageID, notification.collapseKey, JSON.stringify(notification.deliveries), Number(!rescindable), ]); } const dbPromises: Array> = []; if (allInvalidTokens.length > 0) { dbPromises.push(removeInvalidTokens(allInvalidTokens)); } if (notificationRows.length > 0) { const query = SQL` INSERT INTO notifications (id, user, thread, message, collapse_key, delivery, rescinded) VALUES ${notificationRows} `; dbPromises.push(dbQuery(query)); } if (dbPromises.length > 0) { await Promise.all(dbPromises); } } async function fetchInfos(pushInfo: PushInfo) { const usersToCollapsableNotifInfo = await fetchCollapsableNotifs(pushInfo); const threadIDs = new Set(); const threadWithChangedNamesToMessages = new Map>(); const addThreadIDsFromMessageInfos = (rawMessageInfo: RawMessageInfo) => { const threadID = rawMessageInfo.threadID; threadIDs.add(threadID); const messageSpec = messageSpecs[rawMessageInfo.type]; if (messageSpec.threadIDs) { for (const id of messageSpec.threadIDs(rawMessageInfo)) { threadIDs.add(id); } } if ( rawMessageInfo.type === messageTypes.CHANGE_SETTINGS && rawMessageInfo.field === 'name' ) { const messages = threadWithChangedNamesToMessages.get(threadID); if (messages) { messages.push(rawMessageInfo.id); } else { threadWithChangedNamesToMessages.set(threadID, [rawMessageInfo.id]); } } }; for (const userID in usersToCollapsableNotifInfo) { for (const notifInfo of usersToCollapsableNotifInfo[userID]) { for (const rawMessageInfo of notifInfo.existingMessageInfos) { addThreadIDsFromMessageInfos(rawMessageInfo); } for (const rawMessageInfo of notifInfo.newMessageInfos) { addThreadIDsFromMessageInfos(rawMessageInfo); } } } // These threadInfos won't have currentUser set const threadPromise = fetchServerThreadInfos({ threadIDs }); const oldNamesPromise: Promise = (async () => { if (threadWithChangedNamesToMessages.size === 0) { return undefined; } const typesThatAffectName = [ messageTypes.CHANGE_SETTINGS, messageTypes.CREATE_THREAD, ]; const oldNameQuery = SQL` SELECT IF( JSON_TYPE(JSON_EXTRACT(m.content, "$.name")) = 'NULL', "", JSON_UNQUOTE(JSON_EXTRACT(m.content, "$.name")) ) AS name, m.thread FROM ( SELECT MAX(id) AS id FROM messages WHERE type IN (${typesThatAffectName}) AND JSON_EXTRACT(content, "$.name") IS NOT NULL AND`; const threadClauses = []; for (const [threadID, messages] of threadWithChangedNamesToMessages) { threadClauses.push( SQL`(thread = ${threadID} AND id NOT IN (${messages}))`, ); } oldNameQuery.append(mergeOrConditions(threadClauses)); oldNameQuery.append(SQL` GROUP BY thread ) x LEFT JOIN messages m ON m.id = x.id `); return await dbQuery(oldNameQuery); })(); const [threadResult, oldNames] = await Promise.all([ threadPromise, oldNamesPromise, ]); const serverThreadInfos = { ...threadResult.threadInfos }; if (oldNames) { const [result] = oldNames; for (const row of result) { const threadID = row.thread.toString(); serverThreadInfos[threadID] = { ...serverThreadInfos[threadID], name: row.name, }; } } const userInfos = await fetchNotifUserInfos( serverThreadInfos, usersToCollapsableNotifInfo, ); return { usersToCollapsableNotifInfo, serverThreadInfos, userInfos }; } async function fetchNotifUserInfos( serverThreadInfos: { +[threadID: string]: ServerThreadInfo }, usersToCollapsableNotifInfo: { +[userID: string]: CollapsableNotifInfo[] }, ) { const missingUserIDs = new Set(); for (const threadID in serverThreadInfos) { const serverThreadInfo = serverThreadInfos[threadID]; for (const member of serverThreadInfo.members) { missingUserIDs.add(member.id); } } const addUserIDsFromMessageInfos = (rawMessageInfo: RawMessageInfo) => { missingUserIDs.add(rawMessageInfo.creatorID); const userIDs = messageSpecs[rawMessageInfo.type].userIDs?.(rawMessageInfo) ?? []; for (const userID of userIDs) { missingUserIDs.add(userID); } }; for (const userID in usersToCollapsableNotifInfo) { missingUserIDs.add(userID); for (const notifInfo of usersToCollapsableNotifInfo[userID]) { for (const rawMessageInfo of notifInfo.existingMessageInfos) { addUserIDsFromMessageInfos(rawMessageInfo); } for (const rawMessageInfo of notifInfo.newMessageInfos) { addUserIDsFromMessageInfos(rawMessageInfo); } } } return await fetchUserInfos([...missingUserIDs]); } async function createDBIDs(pushInfo: PushInfo): Promise { let numIDsNeeded = 0; for (const userID in pushInfo) { numIDsNeeded += pushInfo[userID].messageInfos.length; } return await createIDs('notifications', numIDsNeeded); } type VersionKey = { +codeVersion: number, +stateVersion: number, +majorDesktopVersion?: number, }; const versionKeyRegex: RegExp = new RegExp(/^-?\d+\|-?\d+(\|-?\d+)?$/); function versionKeyToString(versionKey: VersionKey): string { const baseStringVersionKey = `${versionKey.codeVersion}|${versionKey.stateVersion}`; if (!versionKey.majorDesktopVersion) { return baseStringVersionKey; } return `${baseStringVersionKey}|${versionKey.majorDesktopVersion}`; } function stringToVersionKey(versionKeyString: string): VersionKey { invariant( versionKeyRegex.test(versionKeyString), 'should pass correct version key string', ); const [codeVersion, stateVersion, majorDesktopVersion] = versionKeyString .split('|') .map(Number); return { codeVersion, stateVersion, majorDesktopVersion }; } function getDevicesByPlatform( devices: $ReadOnlyArray, ): Map>> { const byPlatform = new Map< Platform, Map>, >(); for (const device of devices) { let innerMap = byPlatform.get(device.platform); if (!innerMap) { innerMap = new Map>(); byPlatform.set(device.platform, innerMap); } const codeVersion: number = device.codeVersion !== null && device.codeVersion !== undefined ? device.codeVersion : -1; const stateVersion: number = device.stateVersion ?? -1; let versionsObject = { codeVersion, stateVersion }; if (device.majorDesktopVersion) { versionsObject = { ...versionsObject, majorDesktopVersion: device.majorDesktopVersion, }; } const versionKey = versionKeyToString(versionsObject); let innerMostArrayTmp: ?Array = innerMap.get(versionKey); if (!innerMostArrayTmp) { innerMostArrayTmp = []; innerMap.set(versionKey, innerMostArrayTmp); } const innerMostArray = innerMostArrayTmp; innerMostArray.push({ cookieID: device.cookieID, deviceToken: device.deviceToken, }); } return byPlatform; } type CommonNativeNotifInputData = { +keyserverID: string, +notifTexts: ResolvedNotifTexts, +newRawMessageInfos: RawMessageInfo[], +threadID: string, +collapseKey: ?string, +badgeOnly: boolean, +unreadCount: number, +platformDetails: PlatformDetails, }; const commonNativeNotifInputDataValidator = tShape({ keyserverID: t.String, notifTexts: resolvedNotifTextsValidator, newRawMessageInfos: t.list(rawMessageInfoValidator), threadID: tID, collapseKey: t.maybe(t.String), badgeOnly: t.Boolean, unreadCount: t.Number, platformDetails: tPlatformDetails, }); async function prepareAPNsNotification( inputData: CommonNativeNotifInputData, devices: $ReadOnlyArray, ): Promise<$ReadOnlyArray> { const convertedData = await validateOutput( inputData.platformDetails, commonNativeNotifInputDataValidator, inputData, ); const { keyserverID, notifTexts, newRawMessageInfos, threadID, collapseKey, badgeOnly, unreadCount, platformDetails, } = convertedData; const canDecryptNonCollapsibleTextIOSNotifs = platformDetails.codeVersion && platformDetails.codeVersion > 222; const isNonCollapsibleTextNotification = newRawMessageInfos.every( newRawMessageInfo => newRawMessageInfo.type === messageTypes.TEXT, ) && !collapseKey; const canDecryptAllIOSNotifs = platformDetails.codeVersion && platformDetails.codeVersion >= 267; const canDecryptIOSNotif = platformDetails.platform === 'ios' && (canDecryptAllIOSNotifs || (isNonCollapsibleTextNotification && canDecryptNonCollapsibleTextIOSNotifs)); const canDecryptMacOSNotifs = platformDetails.platform === 'macos' && hasMinCodeVersion(platformDetails, { web: 47, majorDesktop: 9, }); const shouldBeEncrypted = canDecryptIOSNotif || canDecryptMacOSNotifs; const uniqueID = uuidv4(); const notification = new apn.Notification(); notification.topic = getAPNsNotificationTopic(platformDetails); const { merged, ...rest } = notifTexts; // We don't include alert's body on macos because we // handle displaying the notification ourselves and // we don't want macOS to display it automatically. if (!badgeOnly && platformDetails.platform !== 'macos') { notification.body = merged; notification.sound = 'default'; } notification.payload = { ...notification.payload, ...rest, }; notification.badge = unreadCount; notification.threadId = threadID; notification.id = uniqueID; notification.pushType = 'alert'; notification.payload.id = uniqueID; notification.payload.threadID = threadID; if (platformDetails.codeVersion && platformDetails.codeVersion > 198) { notification.mutableContent = true; } if (collapseKey && (canDecryptAllIOSNotifs || canDecryptMacOSNotifs)) { notification.payload.collapseID = collapseKey; } else if (collapseKey) { notification.collapseId = collapseKey; } const messageInfos = JSON.stringify(newRawMessageInfos); // We make a copy before checking notification's length, because calling // length compiles the notification and makes it immutable. Further // changes to its properties won't be reflected in the final plaintext // data that is sent. const copyWithMessageInfos = _cloneDeep(notification); copyWithMessageInfos.payload = { ...copyWithMessageInfos.payload, messageInfos, }; const notificationSizeValidator = (notif: apn.Notification) => notif.length() <= apnMaxNotificationPayloadByteSize; if (!shouldBeEncrypted) { const notificationToSend = notificationSizeValidator( _cloneDeep(copyWithMessageInfos), ) ? copyWithMessageInfos : notification; return devices.map(({ deviceToken }) => ({ notification: notificationToSend, deviceToken, })); } // The `messageInfos` field in notification payload is // not used on MacOS so we can return early. if (platformDetails.platform === 'macos') { const macOSNotifsWithoutMessageInfos = await prepareEncryptedAPNsNotifications( encryptedNotifUtilsAPI, { keyserverID }, devices, notification, platformDetails.codeVersion, ); return macOSNotifsWithoutMessageInfos.map( ({ notification: notif, deviceToken }) => ({ notification: notif, deviceToken, }), ); } const notifsWithMessageInfos = await prepareEncryptedAPNsNotifications( encryptedNotifUtilsAPI, { keyserverID }, devices, copyWithMessageInfos, platformDetails.codeVersion, notificationSizeValidator, ); const devicesWithExcessiveSizeNoHolders = notifsWithMessageInfos .filter(({ payloadSizeExceeded }) => payloadSizeExceeded) .map(({ deviceToken, cookieID }) => ({ deviceToken, cookieID, })); if (devicesWithExcessiveSizeNoHolders.length === 0) { return notifsWithMessageInfos.map( ({ notification: notif, deviceToken, encryptedPayloadHash, encryptionOrder, }) => ({ notification: notif, deviceToken, encryptedPayloadHash, encryptionOrder, }), ); } const canQueryBlobService = hasMinCodeVersion(platformDetails, { native: 331, }); let blobHash, blobHolders, encryptionKey, blobUploadError; if (canQueryBlobService) { ({ blobHash, blobHolders, encryptionKey, blobUploadError } = await encryptedNotifUtilsAPI.uploadLargeNotifPayload( copyWithMessageInfos.compile(), devicesWithExcessiveSizeNoHolders.length, )); } if (blobUploadError) { console.warn( `Failed to upload payload of notification: ${uniqueID} ` + `due to error: ${blobUploadError}`, ); } let devicesWithExcessiveSize = devicesWithExcessiveSizeNoHolders; if ( blobHash && encryptionKey && blobHolders && blobHolders.length === devicesWithExcessiveSize.length ) { notification.payload = { ...notification.payload, blobHash, encryptionKey, }; devicesWithExcessiveSize = blobHolders.map((holder, idx) => ({ ...devicesWithExcessiveSize[idx], blobHolder: holder, })); } const notifsWithoutMessageInfos = await prepareEncryptedAPNsNotifications( encryptedNotifUtilsAPI, { keyserverID }, devicesWithExcessiveSize, notification, platformDetails.codeVersion, ); const targetedNotifsWithMessageInfos = notifsWithMessageInfos .filter(({ payloadSizeExceeded }) => !payloadSizeExceeded) .map( ({ notification: notif, deviceToken, encryptedPayloadHash, encryptionOrder, }) => ({ notification: notif, deviceToken, encryptedPayloadHash, encryptionOrder, }), ); const targetedNotifsWithoutMessageInfos = notifsWithoutMessageInfos.map( ({ notification: notif, deviceToken, encryptedPayloadHash, encryptionOrder, }) => ({ notification: notif, deviceToken, encryptedPayloadHash, encryptionOrder, }), ); return [ ...targetedNotifsWithMessageInfos, ...targetedNotifsWithoutMessageInfos, ]; } -type AndroidNotifInputData = { - ...CommonNativeNotifInputData, - +dbID: string, -}; -const androidNotifInputDataValidator = tShape({ - ...commonNativeNotifInputDataValidator.meta.props, - dbID: t.String, -}); async function prepareAndroidVisualNotification( inputData: AndroidNotifInputData, devices: $ReadOnlyArray, ): Promise<$ReadOnlyArray> { const convertedData = await validateOutput( inputData.platformDetails, androidNotifInputDataValidator, inputData, ); - const { - keyserverID, - notifTexts, - newRawMessageInfos, - threadID, - collapseKey, - badgeOnly, - unreadCount, - platformDetails, - dbID, - } = convertedData; - - const canDecryptNonCollapsibleTextNotifs = hasMinCodeVersion( - platformDetails, - { native: 228 }, - ); - const isNonCollapsibleTextNotif = - newRawMessageInfos.every( - newRawMessageInfo => newRawMessageInfo.type === messageTypes.TEXT, - ) && !collapseKey; - - const canDecryptAllNotifTypes = hasMinCodeVersion(platformDetails, { - native: 267, - }); - - const shouldBeEncrypted = - canDecryptAllNotifTypes || - (canDecryptNonCollapsibleTextNotifs && isNonCollapsibleTextNotif); - - const { merged, ...rest } = notifTexts; - const notification = { - data: { - badge: unreadCount.toString(), - ...rest, - threadID, - }, - }; - - let notifID; - if (collapseKey && canDecryptAllNotifTypes) { - notifID = dbID; - notification.data = { - ...notification.data, - collapseKey, - }; - } else if (collapseKey) { - notifID = collapseKey; - } else { - notifID = dbID; - } - - notification.data = { - ...notification.data, - id: notifID, - badgeOnly: badgeOnly ? '1' : '0', - }; - - const messageInfos = JSON.stringify(newRawMessageInfos); - const copyWithMessageInfos = { - ...notification, - data: { ...notification.data, messageInfos }, - }; - - const priority = 'high'; - if (!shouldBeEncrypted) { - const notificationToSend = - encryptedNotifUtilsAPI.getNotifByteSize( - JSON.stringify(copyWithMessageInfos), - ) <= fcmMaxNotificationPayloadByteSize - ? copyWithMessageInfos - : notification; - - return devices.map(({ deviceToken }) => ({ - priority, - notification: notificationToSend, - deviceToken, - })); - } - - const notificationsSizeValidator = (notif: AndroidVisualNotification) => { - const serializedNotif = JSON.stringify(notif); - return ( - !serializedNotif || - encryptedNotifUtilsAPI.getNotifByteSize(serializedNotif) <= - fcmMaxNotificationPayloadByteSize - ); - }; - - const notifsWithMessageInfos = - await prepareEncryptedAndroidVisualNotifications( - encryptedNotifUtilsAPI, - { keyserverID }, - devices, - copyWithMessageInfos, - notificationsSizeValidator, - ); - - const devicesWithExcessiveSizeNoHolders = notifsWithMessageInfos - .filter(({ payloadSizeExceeded }) => payloadSizeExceeded) - .map(({ cookieID, deviceToken }) => ({ cookieID, deviceToken })); - - if (devicesWithExcessiveSizeNoHolders.length === 0) { - return notifsWithMessageInfos.map( - ({ notification: notif, deviceToken, encryptionOrder }) => ({ - priority, - notification: notif, - deviceToken, - encryptionOrder, - }), - ); - } - - const canQueryBlobService = hasMinCodeVersion(platformDetails, { - native: 331, - }); - - let blobHash, blobHolders, encryptionKey, blobUploadError; - if (canQueryBlobService) { - ({ blobHash, blobHolders, encryptionKey, blobUploadError } = - await encryptedNotifUtilsAPI.uploadLargeNotifPayload( - JSON.stringify(copyWithMessageInfos.data), - devicesWithExcessiveSizeNoHolders.length, - )); - } - - if (blobUploadError) { - console.warn( - `Failed to upload payload of notification: ${notifID} ` + - `due to error: ${blobUploadError}`, - ); - } - - let devicesWithExcessiveSize = devicesWithExcessiveSizeNoHolders; - if ( - blobHash && - encryptionKey && - blobHolders && - blobHolders.length === devicesWithExcessiveSizeNoHolders.length - ) { - notification.data = { - ...notification.data, - blobHash, - encryptionKey, - }; - - devicesWithExcessiveSize = blobHolders.map((holder, idx) => ({ - ...devicesWithExcessiveSize[idx], - blobHolder: holder, - })); - } - const notifsWithoutMessageInfos = - await prepareEncryptedAndroidVisualNotifications( - encryptedNotifUtilsAPI, - { keyserverID }, - devicesWithExcessiveSize, - notification, - ); - - const targetedNotifsWithMessageInfos = notifsWithMessageInfos - .filter(({ payloadSizeExceeded }) => !payloadSizeExceeded) - .map(({ notification: notif, deviceToken, encryptionOrder }) => ({ - priority, - notification: notif, - deviceToken, - encryptionOrder, - })); - - const targetedNotifsWithoutMessageInfos = notifsWithoutMessageInfos.map( - ({ notification: notif, deviceToken, encryptionOrder }) => ({ - priority, - notification: notif, - deviceToken, - encryptionOrder, - }), + return createAndroidVisualNotification( + encryptedNotifUtilsAPI, + convertedData, + devices, ); - - return [ - ...targetedNotifsWithMessageInfos, - ...targetedNotifsWithoutMessageInfos, - ]; } -type WebNotifInputData = { - +notifTexts: ResolvedNotifTexts, - +threadID: string, - +keyserverID: string, - +unreadCount: number, - +platformDetails: PlatformDetails, -}; -const webNotifInputDataValidator = tShape({ - notifTexts: resolvedNotifTextsValidator, - threadID: tID, - keyserverID: t.String, - unreadCount: t.Number, - platformDetails: tPlatformDetails, -}); async function prepareWebNotification( inputData: WebNotifInputData, devices: $ReadOnlyArray, ): Promise<$ReadOnlyArray> { const convertedData = await validateOutput( inputData.platformDetails, webNotifInputDataValidator, inputData, ); - const { notifTexts, threadID, unreadCount, keyserverID } = convertedData; - const id = uuidv4(); - const { merged, ...rest } = notifTexts; - const notification = { - ...rest, - unreadCount, - id, - threadID, - }; - - const shouldBeEncrypted = hasMinCodeVersion(convertedData.platformDetails, { - web: 43, - }); - - if (!shouldBeEncrypted) { - return devices.map(({ deviceToken }) => ({ deviceToken, notification })); - } - return prepareEncryptedWebNotifications( - encryptedNotifUtilsAPI, - { keyserverID }, - devices, - notification, - ); + return createWebNotification(encryptedNotifUtilsAPI, convertedData, devices); } -type WNSNotifInputData = { - +notifTexts: ResolvedNotifTexts, - +threadID: string, - +keyserverID: string, - +unreadCount: number, - +platformDetails: PlatformDetails, -}; -const wnsNotifInputDataValidator = tShape({ - notifTexts: resolvedNotifTextsValidator, - threadID: tID, - keyserverID: t.String, - unreadCount: t.Number, - platformDetails: tPlatformDetails, -}); async function prepareWNSNotification( devices: $ReadOnlyArray, inputData: WNSNotifInputData, ): Promise<$ReadOnlyArray> { const convertedData = await validateOutput( inputData.platformDetails, wnsNotifInputDataValidator, inputData, ); - const { notifTexts, threadID, unreadCount, keyserverID } = convertedData; - const { merged, ...rest } = notifTexts; - const notification = { - ...rest, - unreadCount, - threadID, - }; - - if ( - encryptedNotifUtilsAPI.getNotifByteSize(JSON.stringify(notification)) > - wnsMaxNotificationPayloadByteSize - ) { - console.warn('WNS notification exceeds size limit'); - } - - const shouldBeEncrypted = hasMinCodeVersion(inputData.platformDetails, { - majorDesktop: 10, - }); - - if (!shouldBeEncrypted) { - return devices.map(({ deviceToken }) => ({ - deviceToken, - notification, - })); - } - return await prepareEncryptedWNSNotifications( - encryptedNotifUtilsAPI, - { keyserverID }, - devices, - notification, - ); + return createWNSNotification(encryptedNotifUtilsAPI, convertedData, devices); } type NotificationInfo = | { +source: 'new_message', +dbID: string, +userID: string, +threadID: string, +messageID: string, +collapseKey: ?string, +codeVersion: number, +stateVersion: number, } | { +source: 'mark_as_unread' | 'mark_as_read' | 'activity_update', +dbID: string, +userID: string, +codeVersion: number, +stateVersion: number, }; type APNsDelivery = { +source: $PropertyType, +deviceType: 'ios' | 'macos', +iosID: string, +deviceTokens: $ReadOnlyArray, +codeVersion: number, +stateVersion: number, +errors?: $ReadOnlyArray, +encryptedPayloadHashes?: $ReadOnlyArray, +deviceTokensToPayloadHash?: { +[deviceToken: string]: string, }, }; type APNsResult = { info: NotificationInfo, delivery: APNsDelivery, invalidTokens?: $ReadOnlyArray, }; async function sendAPNsNotification( platform: 'ios' | 'macos', targetedNotifications: $ReadOnlyArray, notificationInfo: NotificationInfo, ): Promise { const { source, codeVersion, stateVersion } = notificationInfo; const response = await apnPush({ targetedNotifications, platformDetails: { platform, codeVersion }, }); invariant( new Set(targetedNotifications.map(({ notification }) => notification.id)) .size === 1, 'Encrypted versions of the same notification must share id value', ); const iosID = targetedNotifications[0].notification.id; const deviceTokens = targetedNotifications.map( ({ deviceToken }) => deviceToken, ); let delivery: APNsDelivery = { source, deviceType: platform, iosID, deviceTokens, codeVersion, stateVersion, }; if (response.errors) { delivery = { ...delivery, errors: response.errors, }; } const deviceTokensToPayloadHash: { [string]: string } = {}; for (const targetedNotification of targetedNotifications) { if (targetedNotification.encryptedPayloadHash) { deviceTokensToPayloadHash[targetedNotification.deviceToken] = targetedNotification.encryptedPayloadHash; } } if (Object.keys(deviceTokensToPayloadHash).length !== 0) { delivery = { ...delivery, deviceTokensToPayloadHash, }; } const result: APNsResult = { info: notificationInfo, delivery, }; if (response.invalidTokens) { result.invalidTokens = response.invalidTokens; } return result; } type PushResult = AndroidResult | APNsResult | WebResult | WNSResult; type PushDelivery = AndroidDelivery | APNsDelivery | WebDelivery | WNSDelivery; type AndroidDelivery = { source: $PropertyType, deviceType: 'android', androidIDs: $ReadOnlyArray, deviceTokens: $ReadOnlyArray, codeVersion: number, stateVersion: number, errors?: $ReadOnlyArray, }; type AndroidResult = { info: NotificationInfo, delivery: AndroidDelivery, invalidTokens?: $ReadOnlyArray, }; async function sendAndroidNotification( targetedNotifications: $ReadOnlyArray, notificationInfo: NotificationInfo, ): Promise { const collapseKey = notificationInfo.collapseKey ? notificationInfo.collapseKey : null; // for Flow... const { source, codeVersion, stateVersion } = notificationInfo; const response = await fcmPush({ targetedNotifications, collapseKey, codeVersion, }); const deviceTokens = targetedNotifications.map( ({ deviceToken }) => deviceToken, ); const androidIDs = response.fcmIDs ? response.fcmIDs : []; const delivery: AndroidDelivery = { source, deviceType: 'android', androidIDs, deviceTokens, codeVersion, stateVersion, }; if (response.errors) { delivery.errors = response.errors; } const result: AndroidResult = { info: notificationInfo, delivery, }; if (response.invalidTokens) { result.invalidTokens = response.invalidTokens; } return result; } type WebDelivery = { +source: $PropertyType, +deviceType: 'web', +deviceTokens: $ReadOnlyArray, +codeVersion?: number, +stateVersion: number, +errors?: $ReadOnlyArray, }; type WebResult = { +info: NotificationInfo, +delivery: WebDelivery, +invalidTokens?: $ReadOnlyArray, }; async function sendWebNotifications( targetedNotifications: $ReadOnlyArray, notificationInfo: NotificationInfo, ): Promise { const { source, codeVersion, stateVersion } = notificationInfo; const response = await webPush(targetedNotifications); const deviceTokens = targetedNotifications.map( ({ deviceToken }) => deviceToken, ); const delivery: WebDelivery = { source, deviceType: 'web', deviceTokens, codeVersion, errors: response.errors, stateVersion, }; const result: WebResult = { info: notificationInfo, delivery, invalidTokens: response.invalidTokens, }; return result; } type WNSDelivery = { +source: $PropertyType, +deviceType: 'windows', +wnsIDs: $ReadOnlyArray, +deviceTokens: $ReadOnlyArray, +codeVersion?: number, +stateVersion: number, +errors?: $ReadOnlyArray, }; type WNSResult = { +info: NotificationInfo, +delivery: WNSDelivery, +invalidTokens?: $ReadOnlyArray, }; async function sendWNSNotification( targetedNotifications: $ReadOnlyArray, notificationInfo: NotificationInfo, ): Promise { const { source, codeVersion, stateVersion } = notificationInfo; const response = await wnsPush(targetedNotifications); const deviceTokens = targetedNotifications.map( ({ deviceToken }) => deviceToken, ); const wnsIDs = response.wnsIDs ?? []; const delivery: WNSDelivery = { source, deviceType: 'windows', wnsIDs, deviceTokens, codeVersion, errors: response.errors, stateVersion, }; const result: WNSResult = { info: notificationInfo, delivery, invalidTokens: response.invalidTokens, }; return result; } type InvalidToken = { +userID: string, +tokens: $ReadOnlyArray, }; async function removeInvalidTokens( invalidTokens: $ReadOnlyArray, ): Promise { const sqlTuples = invalidTokens.map( invalidTokenUser => SQL`( user = ${invalidTokenUser.userID} AND device_token IN (${invalidTokenUser.tokens}) )`, ); const sqlCondition = mergeOrConditions(sqlTuples); const selectQuery = SQL` SELECT id, user, device_token FROM cookies WHERE `; selectQuery.append(sqlCondition); const [result] = await dbQuery(selectQuery); const userCookiePairsToInvalidDeviceTokens = new Map>(); for (const row of result) { const userCookiePair = `${row.user}|${row.id}`; const existing = userCookiePairsToInvalidDeviceTokens.get(userCookiePair); if (existing) { existing.add(row.device_token); } else { userCookiePairsToInvalidDeviceTokens.set( userCookiePair, new Set([row.device_token]), ); } } const time = Date.now(); const promises: Array> = []; for (const entry of userCookiePairsToInvalidDeviceTokens) { const [userCookiePair, deviceTokens] = entry; const [userID, cookieID] = userCookiePair.split('|'); const updateDatas = [...deviceTokens].map(deviceToken => ({ type: updateTypes.BAD_DEVICE_TOKEN, userID, time, deviceToken, targetCookie: cookieID, })); promises.push(createUpdates(updateDatas)); } const updateQuery = SQL` UPDATE cookies SET device_token = NULL WHERE `; updateQuery.append(sqlCondition); promises.push(dbQuery(updateQuery)); await Promise.all(promises); } async function updateBadgeCount( viewer: Viewer, source: 'mark_as_unread' | 'mark_as_read' | 'activity_update', ) { const { userID } = viewer; const deviceTokenQuery = SQL` SELECT platform, device_token, versions, id FROM cookies WHERE user = ${userID} AND device_token IS NOT NULL `; if (viewer.data.cookieID) { deviceTokenQuery.append(SQL`AND id != ${viewer.cookieID} `); } const [unreadCounts, [deviceTokenResult], [dbID], keyserverID] = await Promise.all([ getUnreadCounts([userID]), dbQuery(deviceTokenQuery), createIDs('notifications', 1), thisKeyserverID(), ]); const unreadCount = unreadCounts[userID]; const devices = deviceTokenResult.map(row => { const versions = JSON.parse(row.versions); return { platform: row.platform, cookieID: row.id, deviceToken: row.device_token, codeVersion: versions?.codeVersion, stateVersion: versions?.stateVersion, }; }); const byPlatform = getDevicesByPlatform(devices); const preparePromises: Array>> = []; const iosVersionsToTokens = byPlatform.get('ios'); if (iosVersionsToTokens) { for (const [versionKey, deviceInfos] of iosVersionsToTokens) { const { codeVersion, stateVersion } = stringToVersionKey(versionKey); const notification = new apn.Notification(); notification.topic = getAPNsNotificationTopic({ platform: 'ios', codeVersion, stateVersion, }); notification.badge = unreadCount; notification.pushType = 'alert'; const preparePromise: Promise = (async () => { let targetedNotifications: $ReadOnlyArray; if (codeVersion > 222) { const notificationsArray = await prepareEncryptedAPNsNotifications( encryptedNotifUtilsAPI, { keyserverID }, deviceInfos, notification, codeVersion, ); targetedNotifications = notificationsArray.map( ({ notification: notif, deviceToken, encryptionOrder }) => ({ notification: notif, deviceToken, encryptionOrder, }), ); } else { targetedNotifications = deviceInfos.map(({ deviceToken }) => ({ notification, deviceToken, })); } return targetedNotifications.map(targetedNotification => ({ notification: targetedNotification, platform: 'ios', notificationInfo: { source, dbID, userID, codeVersion, stateVersion, }, })); })(); preparePromises.push(preparePromise); } } const androidVersionsToTokens = byPlatform.get('android'); if (androidVersionsToTokens) { for (const [versionKey, deviceInfos] of androidVersionsToTokens) { const { codeVersion, stateVersion } = stringToVersionKey(versionKey); const notificationData = { badge: unreadCount.toString(), badgeOnly: '1', }; const notification = { data: { ...notificationData }, }; const preparePromise: Promise = (async () => { let targetedNotifications: $ReadOnlyArray; const priority = 'normal'; if (codeVersion > 222) { const notificationsArray = await prepareEncryptedAndroidSilentNotifications( encryptedNotifUtilsAPI, { keyserverID }, deviceInfos, notification, ); targetedNotifications = notificationsArray.map( ({ notification: notif, deviceToken, encryptionOrder }) => ({ priority, notification: notif, deviceToken, encryptionOrder, }), ); } else { targetedNotifications = deviceInfos.map(({ deviceToken }) => ({ priority, deviceToken, notification, })); } return targetedNotifications.map(targetedNotification => ({ notification: targetedNotification, platform: 'android', notificationInfo: { source, dbID, userID, codeVersion, stateVersion, }, })); })(); preparePromises.push(preparePromise); } } const macosVersionsToTokens = byPlatform.get('macos'); if (macosVersionsToTokens) { for (const [versionKey, deviceInfos] of macosVersionsToTokens) { const { codeVersion, stateVersion, majorDesktopVersion } = stringToVersionKey(versionKey); const notification = new apn.Notification(); notification.topic = getAPNsNotificationTopic({ platform: 'macos', codeVersion, stateVersion, majorDesktopVersion, }); notification.badge = unreadCount; notification.pushType = 'alert'; notification.payload.keyserverID = keyserverID; const preparePromise: Promise = (async () => { const shouldBeEncrypted = hasMinCodeVersion(viewer.platformDetails, { web: 47, majorDesktop: 9, }); let targetedNotifications: $ReadOnlyArray; if (shouldBeEncrypted) { const notificationsArray = await prepareEncryptedAPNsNotifications( encryptedNotifUtilsAPI, { keyserverID }, deviceInfos, notification, codeVersion, ); targetedNotifications = notificationsArray.map( ({ notification: notif, deviceToken, encryptionOrder }) => ({ notification: notif, deviceToken, encryptionOrder, }), ); } else { targetedNotifications = deviceInfos.map(({ deviceToken }) => ({ deviceToken, notification, })); } return targetedNotifications.map(targetedNotification => ({ notification: targetedNotification, platform: 'macos', notificationInfo: { source, dbID, userID, codeVersion, stateVersion, }, })); })(); preparePromises.push(preparePromise); } } const prepareResults = await Promise.all(preparePromises); const flattenedPrepareResults = prepareResults.filter(Boolean).flat(); const deliveryResults = await deliverPushNotifsInEncryptionOrder( flattenedPrepareResults, ); await saveNotifResults(deliveryResults, new Map(), false); } export { sendPushNotifs, sendRescindNotifs, updateBadgeCount }; diff --git a/keyserver/src/push/utils.js b/keyserver/src/push/utils.js index af7f5b42f..d7d353040 100644 --- a/keyserver/src/push/utils.js +++ b/keyserver/src/push/utils.js @@ -1,451 +1,452 @@ // @flow import type { ResponseFailure } from '@parse/node-apn'; import type { FirebaseApp, FirebaseError } from 'firebase-admin'; import invariant from 'invariant'; import nodeFetch from 'node-fetch'; import type { Response } from 'node-fetch'; import uuid from 'uuid'; import webpush from 'web-push'; +import { fcmMaxNotificationPayloadByteSize } from 'lib/push/android-notif-creators.js'; +import { wnsMaxNotificationPayloadByteSize } from 'lib/push/wns-notif-creators.js'; import type { PlatformDetails } from 'lib/types/device-types.js'; import type { TargetedAndroidNotification, TargetedWebNotification, TargetedWNSNotification, } from 'lib/types/notif-types.js'; import { threadSubscriptions } from 'lib/types/subscription-types.js'; import { threadPermissions } from 'lib/types/thread-permission-types.js'; import { encryptBlobPayload } from './crypto.js'; import { getAPNPushProfileForCodeVersion, getFCMPushProfileForCodeVersion, getAPNProvider, getFCMProvider, ensureWebPushInitialized, getWNSToken, } from './providers.js'; import type { TargetedAPNsNotification } from './types.js'; import { dbQuery, SQL } from '../database/database.js'; import { upload, assignHolder } from '../services/blob.js'; const fcmTokenInvalidationErrors = new Set([ 'messaging/registration-token-not-registered', 'messaging/invalid-registration-token', ]); -const fcmMaxNotificationPayloadByteSize = 4000; + const apnTokenInvalidationErrorCode = 410; const apnBadRequestErrorCode = 400; const apnBadTokenErrorString = 'BadDeviceToken'; const apnMaxNotificationPayloadByteSize = 4096; const webInvalidTokenErrorCodes = [404, 410]; const wnsInvalidTokenErrorCodes = [404, 410]; -const wnsMaxNotificationPayloadByteSize = 5000; export type APNPushResult = | { +success: true } | { +errors: $ReadOnlyArray, +invalidTokens?: $ReadOnlyArray, }; async function apnPush({ targetedNotifications, platformDetails, }: { +targetedNotifications: $ReadOnlyArray, +platformDetails: PlatformDetails, }): Promise { const pushProfile = getAPNPushProfileForCodeVersion(platformDetails); const apnProvider = await getAPNProvider(pushProfile); if (!apnProvider && process.env.NODE_ENV === 'development') { console.log(`no keyserver/secrets/${pushProfile}.json so ignoring notifs`); return { success: true }; } invariant(apnProvider, `keyserver/secrets/${pushProfile}.json should exist`); const results = await Promise.all( targetedNotifications.map(({ notification, deviceToken }) => { return apnProvider.send(notification, deviceToken); }), ); const errors: Array = []; for (const result of results) { errors.push(...result.failed); } const invalidTokens: Array = []; for (const error of errors) { /* eslint-disable eqeqeq */ if ( error.status == apnTokenInvalidationErrorCode || (error.status == apnBadRequestErrorCode && error.response.reason === apnBadTokenErrorString) ) { invalidTokens.push(error.device); } /* eslint-enable eqeqeq */ } if (invalidTokens.length > 0) { return { errors, invalidTokens }; } else if (errors.length > 0) { return { errors }; } else { return { success: true }; } } type WritableFCMPushResult = { success?: true, fcmIDs?: $ReadOnlyArray, errors?: $ReadOnlyArray, invalidTokens?: $ReadOnlyArray, }; export type FCMPushResult = $ReadOnly; async function fcmPush({ targetedNotifications, collapseKey, codeVersion, }: { +targetedNotifications: $ReadOnlyArray, +codeVersion: ?number, +collapseKey?: ?string, }): Promise { const pushProfile = getFCMPushProfileForCodeVersion(codeVersion); const fcmProvider = await getFCMProvider(pushProfile); if (!fcmProvider && process.env.NODE_ENV === 'development') { console.log(`no keyserver/secrets/${pushProfile}.json so ignoring notifs`); return { success: true }; } invariant(fcmProvider, `keyserver/secrets/${pushProfile}.json should exist`); const options: Object = {}; if (collapseKey) { options.collapseKey = collapseKey; } // firebase-admin is extremely barebones and has a lot of missing or poorly // thought-out functionality. One of the issues is that if you send a // multicast messages and one of the device tokens is invalid, the resultant // won't explain which of the device tokens is invalid. So we're forced to // avoid the multicast functionality and call it once per deviceToken. const results = await Promise.all( targetedNotifications.map(({ notification, deviceToken, priority }) => { return fcmSinglePush(fcmProvider, notification, deviceToken, { ...options, priority, }); }), ); const errors = []; const ids = []; const invalidTokens = []; for (let i = 0; i < results.length; i++) { const pushResult = results[i]; for (const error of pushResult.errors) { errors.push(error.error); const errorCode = error.type === 'firebase_error' ? error.error.errorInfo.code : undefined; if (errorCode && fcmTokenInvalidationErrors.has(errorCode)) { invalidTokens.push(targetedNotifications[i].deviceToken); } } for (const id of pushResult.fcmIDs) { ids.push(id); } } const result: WritableFCMPushResult = {}; if (ids.length > 0) { result.fcmIDs = ids; } if (errors.length > 0) { result.errors = errors; } else { result.success = true; } if (invalidTokens.length > 0) { result.invalidTokens = invalidTokens; } return result; } type FCMSinglePushError = | { +type: 'firebase_error', +error: FirebaseError } | { +type: 'exception', +error: mixed }; type FCMSinglePushResult = { +fcmIDs: $ReadOnlyArray, +errors: $ReadOnlyArray, }; async function fcmSinglePush( provider: FirebaseApp, notification: Object, deviceToken: string, options: Object, ): Promise { try { const deliveryResult = await provider .messaging() .sendToDevice(deviceToken, notification, options); const errors = []; const ids = []; for (const fcmResult of deliveryResult.results) { if (fcmResult.error) { errors.push({ type: 'firebase_error', error: fcmResult.error }); } else if (fcmResult.messageId) { ids.push(fcmResult.messageId); } } return { fcmIDs: ids, errors }; } catch (e) { return { fcmIDs: [], errors: [{ type: 'exception', error: e }] }; } } async function getUnreadCounts( userIDs: string[], ): Promise<{ [userID: string]: number }> { const visPermissionExtractString = `$.${threadPermissions.VISIBLE}.value`; const notificationExtractString = `$.${threadSubscriptions.home}`; const query = SQL` SELECT user, COUNT(thread) AS unread_count FROM memberships WHERE user IN (${userIDs}) AND last_message > last_read_message AND role > 0 AND JSON_EXTRACT(permissions, ${visPermissionExtractString}) AND JSON_EXTRACT(subscription, ${notificationExtractString}) GROUP BY user `; const [result] = await dbQuery(query); const usersToUnreadCounts: { [string]: number } = {}; for (const row of result) { usersToUnreadCounts[row.user.toString()] = row.unread_count; } for (const userID of userIDs) { if (usersToUnreadCounts[userID] === undefined) { usersToUnreadCounts[userID] = 0; } } return usersToUnreadCounts; } export type WebPushError = { +statusCode: number, +headers: { +[string]: string }, +body: string, }; type WritableWebPushResult = { success?: true, errors?: $ReadOnlyArray, invalidTokens?: $ReadOnlyArray, }; type WebPushResult = $ReadOnly; type WebPushAttempt = { +error?: WebPushError, }; async function webPush( targetedNotifications: $ReadOnlyArray, ): Promise { await ensureWebPushInitialized(); const pushResults: $ReadOnlyArray = await Promise.all( targetedNotifications.map( async ({ notification, deviceToken: deviceTokenString }) => { const deviceToken: PushSubscriptionJSON = JSON.parse(deviceTokenString); const notificationString = JSON.stringify(notification); try { await webpush.sendNotification(deviceToken, notificationString); } catch (error) { return ({ error }: WebPushAttempt); } return {}; }, ), ); const errors = []; const invalidTokens = []; const deviceTokens = targetedNotifications.map( ({ deviceToken }) => deviceToken, ); for (let i = 0; i < pushResults.length; i++) { const pushResult = pushResults[i]; const { error } = pushResult; if (error) { errors.push(error); if (webInvalidTokenErrorCodes.includes(error.statusCode)) { invalidTokens.push(deviceTokens[i]); } } } const result: WritableWebPushResult = {}; if (errors.length > 0) { result.errors = errors; } else { result.success = true; } if (invalidTokens.length > 0) { result.invalidTokens = invalidTokens; } return result; } export type WNSPushError = any | string | Response; type WritableWNSPushResult = { success?: true, wnsIDs?: $ReadOnlyArray, errors?: $ReadOnlyArray, invalidTokens?: $ReadOnlyArray, }; type WNSPushResult = $ReadOnly; async function wnsPush( targetedNotifications: $ReadOnlyArray, ): Promise { const token = await getWNSToken(); if (!token && process.env.NODE_ENV === 'development') { console.log(`no keyserver/secrets/wns_config.json so ignoring notifs`); return { success: true }; } invariant(token, `keyserver/secrets/wns_config.json should exist`); const pushResults = targetedNotifications.map(async targetedNotification => { const notificationString = JSON.stringify( targetedNotification.notification, ); try { return await wnsSinglePush( token, notificationString, targetedNotification.deviceToken, ); } catch (error) { return { error }; } }); const errors = []; const notifIDs = []; const invalidTokens = []; const deviceTokens = targetedNotifications.map( ({ deviceToken }) => deviceToken, ); for (let i = 0; i < pushResults.length; i++) { const pushResult = await pushResults[i]; if (pushResult.error) { errors.push(pushResult.error); if ( pushResult.error === 'invalidDomain' || wnsInvalidTokenErrorCodes.includes(pushResult.error?.status) ) { invalidTokens.push(deviceTokens[i]); } } else { notifIDs.push(pushResult.wnsID); } } const result: WritableWNSPushResult = {}; if (notifIDs.length > 0) { result.wnsIDs = notifIDs; } if (errors.length > 0) { result.errors = errors; } else { result.success = true; } if (invalidTokens.length > 0) { result.invalidTokens = invalidTokens; } return result; } async function wnsSinglePush(token: string, notification: string, url: string) { const parsedURL = new URL(url); const domain = parsedURL.hostname.split('.').slice(-3); if ( domain[0] !== 'notify' || domain[1] !== 'windows' || domain[2] !== 'com' ) { return { error: 'invalidDomain' }; } try { const result = await nodeFetch(url, { method: 'POST', headers: { 'Content-Type': 'application/octet-stream', 'X-WNS-Type': 'wns/raw', 'Authorization': `Bearer ${token}`, }, body: notification, }); if (!result.ok) { return { error: result }; } const wnsID = result.headers.get('X-WNS-MSG-ID'); invariant(wnsID, 'Missing WNS ID'); return { wnsID }; } catch (err) { return { error: err }; } } async function blobServiceUpload( payload: string, numberOfHolders: number, ): Promise< | { +blobHolders: $ReadOnlyArray, +blobHash: string, +encryptionKey: string, } | { +blobUploadError: string }, > { const blobHolders = Array.from({ length: numberOfHolders }, () => uuid.v4()); try { const { encryptionKey, encryptedPayload, encryptedPayloadHash } = await encryptBlobPayload(payload); const [blobHolder, ...additionalHolders] = blobHolders; const uploadPromise = upload(encryptedPayload, { hash: encryptedPayloadHash, holder: blobHolder, }); const additionalHoldersPromises = additionalHolders.map(holder => assignHolder({ hash: encryptedPayloadHash, holder }), ); await Promise.all([uploadPromise, Promise.all(additionalHoldersPromises)]); return { blobHash: encryptedPayloadHash, blobHolders, encryptionKey, }; } catch (e) { return { blobUploadError: e.message, }; } } export { apnPush, blobServiceUpload, fcmPush, webPush, wnsPush, getUnreadCounts, apnMaxNotificationPayloadByteSize, fcmMaxNotificationPayloadByteSize, wnsMaxNotificationPayloadByteSize, }; diff --git a/lib/push/android-notif-creators.js b/lib/push/android-notif-creators.js new file mode 100644 index 000000000..684ba5f9f --- /dev/null +++ b/lib/push/android-notif-creators.js @@ -0,0 +1,254 @@ +// @flow + +import t, { type TInterface } from 'tcomb'; + +import { prepareEncryptedAndroidVisualNotifications } from './crypto.js'; +import { hasMinCodeVersion } from '../shared/version-utils.js'; +import type { PlatformDetails } from '../types/device-types.js'; +import { messageTypes } from '../types/message-types-enum.js'; +import { + type RawMessageInfo, + rawMessageInfoValidator, +} from '../types/message-types.js'; +import { + type AndroidVisualNotification, + type NotificationTargetDevice, + type TargetedAndroidNotification, + type ResolvedNotifTexts, + resolvedNotifTextsValidator, + type SenderDeviceDescriptor, + senderDeviceDescriptorValidator, + type EncryptedNotifUtilsAPI, +} from '../types/notif-types.js'; +import { tID, tPlatformDetails, tShape } from '../utils/validation-utils.js'; + +export const fcmMaxNotificationPayloadByteSize = 4000; + +type CommonNativeNotifInputData = $ReadOnly<{ + +senderDeviceDescriptor: SenderDeviceDescriptor, + +notifTexts: ResolvedNotifTexts, + +newRawMessageInfos: RawMessageInfo[], + +threadID: string, + +collapseKey: ?string, + +badgeOnly: boolean, + +unreadCount?: number, + +platformDetails: PlatformDetails, +}>; + +const commonNativeNotifInputDataValidator = tShape({ + senderDeviceDescriptor: senderDeviceDescriptorValidator, + notifTexts: resolvedNotifTextsValidator, + newRawMessageInfos: t.list(rawMessageInfoValidator), + threadID: tID, + collapseKey: t.maybe(t.String), + badgeOnly: t.Boolean, + unreadCount: t.maybe(t.Number), + platformDetails: tPlatformDetails, +}); + +export type AndroidNotifInputData = { + ...CommonNativeNotifInputData, + +notifID: string, +}; + +export const androidNotifInputDataValidator: TInterface = + tShape({ + ...commonNativeNotifInputDataValidator.meta.props, + notifID: t.String, + }); + +async function createAndroidVisualNotification( + encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, + inputData: AndroidNotifInputData, + devices: $ReadOnlyArray, +): Promise<$ReadOnlyArray> { + const { + senderDeviceDescriptor, + notifTexts, + newRawMessageInfos, + threadID, + collapseKey, + badgeOnly, + unreadCount, + platformDetails, + notifID, + } = inputData; + + const canDecryptNonCollapsibleTextNotifs = hasMinCodeVersion( + platformDetails, + { native: 228 }, + ); + const isNonCollapsibleTextNotif = + newRawMessageInfos.every( + newRawMessageInfo => newRawMessageInfo.type === messageTypes.TEXT, + ) && !collapseKey; + + const canDecryptAllNotifTypes = hasMinCodeVersion(platformDetails, { + native: 267, + }); + + const shouldBeEncrypted = + canDecryptAllNotifTypes || + (canDecryptNonCollapsibleTextNotifs && isNonCollapsibleTextNotif); + + const { merged, ...rest } = notifTexts; + const notification = { + data: { + ...rest, + threadID, + }, + }; + + if (unreadCount) { + notification.data = { + ...notification.data, + badge: unreadCount.toString(), + }; + } + + let id; + if (collapseKey && canDecryptAllNotifTypes) { + id = notifID; + notification.data = { + ...notification.data, + collapseKey, + }; + } else if (collapseKey) { + id = collapseKey; + } else { + id = notifID; + } + + notification.data = { + ...notification.data, + id, + badgeOnly: badgeOnly ? '1' : '0', + }; + + const messageInfos = JSON.stringify(newRawMessageInfos); + const copyWithMessageInfos = { + ...notification, + data: { ...notification.data, messageInfos }, + }; + + const priority = 'high'; + if (!shouldBeEncrypted) { + const notificationToSend = + encryptedNotifUtilsAPI.getNotifByteSize( + JSON.stringify(copyWithMessageInfos), + ) <= fcmMaxNotificationPayloadByteSize + ? copyWithMessageInfos + : notification; + + return devices.map(({ deviceToken }) => ({ + priority, + notification: notificationToSend, + deviceToken, + })); + } + + const notificationsSizeValidator = (notif: AndroidVisualNotification) => { + const serializedNotif = JSON.stringify(notif); + return ( + !serializedNotif || + encryptedNotifUtilsAPI.getNotifByteSize(serializedNotif) <= + fcmMaxNotificationPayloadByteSize + ); + }; + + const notifsWithMessageInfos = + await prepareEncryptedAndroidVisualNotifications( + encryptedNotifUtilsAPI, + senderDeviceDescriptor, + devices, + copyWithMessageInfos, + notificationsSizeValidator, + ); + + const devicesWithExcessiveSizeNoHolders = notifsWithMessageInfos + .filter(({ payloadSizeExceeded }) => payloadSizeExceeded) + .map(({ cookieID, deviceToken }) => ({ cookieID, deviceToken })); + + if (devicesWithExcessiveSizeNoHolders.length === 0) { + return notifsWithMessageInfos.map( + ({ notification: notif, deviceToken, encryptionOrder }) => ({ + priority, + notification: notif, + deviceToken, + encryptionOrder, + }), + ); + } + + const canQueryBlobService = hasMinCodeVersion(platformDetails, { + native: 331, + }); + + let blobHash, blobHolders, encryptionKey, blobUploadError; + if (canQueryBlobService) { + ({ blobHash, blobHolders, encryptionKey, blobUploadError } = + await encryptedNotifUtilsAPI.uploadLargeNotifPayload( + JSON.stringify(copyWithMessageInfos.data), + devicesWithExcessiveSizeNoHolders.length, + )); + } + + if (blobUploadError) { + console.warn( + `Failed to upload payload of notification: ${notifID} ` + + `due to error: ${blobUploadError}`, + ); + } + + let devicesWithExcessiveSize = devicesWithExcessiveSizeNoHolders; + if ( + blobHash && + encryptionKey && + blobHolders && + blobHolders.length === devicesWithExcessiveSizeNoHolders.length + ) { + notification.data = { + ...notification.data, + blobHash, + encryptionKey, + }; + + devicesWithExcessiveSize = blobHolders.map((holder, idx) => ({ + ...devicesWithExcessiveSize[idx], + blobHolder: holder, + })); + } + + const notifsWithoutMessageInfos = + await prepareEncryptedAndroidVisualNotifications( + encryptedNotifUtilsAPI, + senderDeviceDescriptor, + devicesWithExcessiveSize, + notification, + ); + + const targetedNotifsWithMessageInfos = notifsWithMessageInfos + .filter(({ payloadSizeExceeded }) => !payloadSizeExceeded) + .map(({ notification: notif, deviceToken, encryptionOrder }) => ({ + priority, + notification: notif, + deviceToken, + encryptionOrder, + })); + + const targetedNotifsWithoutMessageInfos = notifsWithoutMessageInfos.map( + ({ notification: notif, deviceToken, encryptionOrder }) => ({ + priority, + notification: notif, + deviceToken, + encryptionOrder, + }), + ); + + return [ + ...targetedNotifsWithMessageInfos, + ...targetedNotifsWithoutMessageInfos, + ]; +} + +export { createAndroidVisualNotification }; diff --git a/keyserver/src/push/crypto.js b/lib/push/crypto.js similarity index 59% copy from keyserver/src/push/crypto.js copy to lib/push/crypto.js index 6011c0762..e15d14e26 100644 --- a/keyserver/src/push/crypto.js +++ b/lib/push/crypto.js @@ -1,582 +1,391 @@ // @flow -import apn from '@parse/node-apn'; -import crypto from 'crypto'; -import invariant from 'invariant'; -import _cloneDeep from 'lodash/fp/cloneDeep.js'; - import type { PlainTextWebNotification, PlainTextWebNotificationPayload, WebNotification, PlainTextWNSNotification, WNSNotification, AndroidVisualNotification, AndroidVisualNotificationPayload, AndroidBadgeOnlyNotification, AndroidNotificationRescind, NotificationTargetDevice, SenderDeviceDescriptor, EncryptedNotifUtilsAPI, -} from 'lib/types/notif-types.js'; -import { toBase64URL } from 'lib/utils/base64.js'; - -import { encrypt, generateKey } from '../utils/aes-crypto-utils.js'; -import { getOlmUtility } from '../utils/olm-utils.js'; - -async function encryptAPNsNotification( - encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, - cookieID: string, - senderDeviceDescriptor: SenderDeviceDescriptor, - notification: apn.Notification, - codeVersion?: ?number, - notificationSizeValidator?: apn.Notification => boolean, - blobHolder?: ?string, -): Promise<{ - +notification: apn.Notification, - +payloadSizeExceeded: boolean, - +encryptedPayloadHash?: string, - +encryptionOrder?: number, -}> { - invariant( - !notification.collapseId, - `Collapse ID can't be directly stored in apn.Notification object due ` + - `to security reasons. Please put it in payload property`, - ); - - const encryptedNotification = new apn.Notification(); - - encryptedNotification.id = notification.id; - encryptedNotification.payload.id = notification.id; - - if (blobHolder) { - encryptedNotification.payload.blobHolder = blobHolder; - } - - encryptedNotification.payload.keyserverID = notification.payload.keyserverID; - encryptedNotification.topic = notification.topic; - encryptedNotification.sound = notification.aps.sound; - encryptedNotification.pushType = 'alert'; - encryptedNotification.mutableContent = true; - - const { id, ...payloadSansUnencryptedData } = notification.payload; - const unencryptedPayload = { - ...payloadSansUnencryptedData, - badge: notification.aps.badge.toString(), - merged: notification.body, - }; - - try { - const unencryptedSerializedPayload = JSON.stringify(unencryptedPayload); - - let dbPersistCondition; - if (notificationSizeValidator) { - dbPersistCondition = (serializedPayload: string) => { - const notifCopy = _cloneDeep(encryptedNotification); - notifCopy.payload.encryptedPayload = serializedPayload; - return notificationSizeValidator(notifCopy); - }; - } - const { - encryptedData: serializedPayload, - sizeLimitViolated: dbPersistConditionViolated, - encryptionOrder, - } = await encryptedNotifUtilsAPI.encryptSerializedNotifPayload( - cookieID, - unencryptedSerializedPayload, - dbPersistCondition, - ); - - encryptedNotification.payload.encryptedPayload = serializedPayload.body; - encryptedNotification.payload = { - ...encryptedNotification.payload, - ...senderDeviceDescriptor, - }; - - if (codeVersion && codeVersion >= 254 && codeVersion % 2 === 0) { - encryptedNotification.aps = { - alert: { body: 'ENCRYPTED' }, - ...encryptedNotification.aps, - }; - } - - const encryptedPayloadHash = getOlmUtility().sha256(serializedPayload.body); - return { - notification: encryptedNotification, - payloadSizeExceeded: !!dbPersistConditionViolated, - encryptedPayloadHash, - encryptionOrder, - }; - } catch (e) { - console.log('Notification encryption failed: ' + e); - - encryptedNotification.body = notification.body; - encryptedNotification.threadId = notification.payload.threadID; - invariant( - typeof notification.aps.badge === 'number', - 'Unencrypted notification must have badge as a number', - ); - encryptedNotification.badge = notification.aps.badge; - - encryptedNotification.payload = { - ...encryptedNotification.payload, - ...notification.payload, - encryptionFailed: 1, - }; - return { - notification: encryptedNotification, - payloadSizeExceeded: notificationSizeValidator - ? notificationSizeValidator(_cloneDeep(encryptedNotification)) - : false, - }; - } -} +} from '../types/notif-types.js'; async function encryptAndroidNotificationPayload( encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, cookieID: string, senderDeviceDescriptor: SenderDeviceDescriptor, unencryptedPayload: T, payloadSizeValidator?: ( - T | $ReadOnly<{ ...SenderDeviceDescriptor, +encryptedPayload: string }>, + | T + | $ReadOnly<{ + ...SenderDeviceDescriptor, + +encryptedPayload: string, + +type: '1' | '0', + }>, ) => boolean, ): Promise<{ +resultPayload: | T - | $ReadOnly<{ ...SenderDeviceDescriptor, +encryptedPayload: string }>, + | $ReadOnly<{ + ...SenderDeviceDescriptor, + +encryptedPayload: string, + +type: '1' | '0', + }>, +payloadSizeExceeded: boolean, +encryptionOrder?: number, }> { try { const unencryptedSerializedPayload = JSON.stringify(unencryptedPayload); if (!unencryptedSerializedPayload) { return { resultPayload: unencryptedPayload, payloadSizeExceeded: payloadSizeValidator ? payloadSizeValidator(unencryptedPayload) : false, }; } let dbPersistCondition; if (payloadSizeValidator) { - dbPersistCondition = (serializedPayload: string) => + dbPersistCondition = (serializedPayload: string, type: '1' | '0') => payloadSizeValidator({ encryptedPayload: serializedPayload, + type, ...senderDeviceDescriptor, }); } const { encryptedData: serializedPayload, sizeLimitViolated: dbPersistConditionViolated, encryptionOrder, } = await encryptedNotifUtilsAPI.encryptSerializedNotifPayload( cookieID, unencryptedSerializedPayload, dbPersistCondition, ); return { resultPayload: { - encryptedPayload: serializedPayload.body, ...senderDeviceDescriptor, + encryptedPayload: serializedPayload.body, + type: serializedPayload.type ? '1' : '0', }, payloadSizeExceeded: !!dbPersistConditionViolated, encryptionOrder, }; } catch (e) { console.log('Notification encryption failed: ' + e); const resultPayload = { encryptionFailed: '1', ...unencryptedPayload, }; return { resultPayload, payloadSizeExceeded: payloadSizeValidator ? payloadSizeValidator(resultPayload) : false, }; } } async function encryptAndroidVisualNotification( encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, senderDeviceDescriptor: SenderDeviceDescriptor, cookieID: string, notification: AndroidVisualNotification, notificationSizeValidator?: AndroidVisualNotification => boolean, blobHolder?: ?string, ): Promise<{ +notification: AndroidVisualNotification, +payloadSizeExceeded: boolean, +encryptionOrder?: number, }> { const { id, ...rest } = notification.data; let unencryptedData = {}; if (id) { unencryptedData = { id }; } let unencryptedPayload = rest; if (blobHolder) { unencryptedPayload = { ...unencryptedPayload, blobHolder }; } let payloadSizeValidator; if (notificationSizeValidator) { payloadSizeValidator = ( payload: | AndroidVisualNotificationPayload - | $ReadOnly<{ ...SenderDeviceDescriptor, +encryptedPayload: string }>, + | $ReadOnly<{ + ...SenderDeviceDescriptor, + +encryptedPayload: string, + +type: '0' | '1', + }>, ) => { return notificationSizeValidator({ data: { ...unencryptedData, ...payload }, }); }; } const { resultPayload, payloadSizeExceeded, encryptionOrder } = await encryptAndroidNotificationPayload( encryptedNotifUtilsAPI, cookieID, senderDeviceDescriptor, unencryptedPayload, payloadSizeValidator, ); return { notification: { data: { ...unencryptedData, ...resultPayload, }, }, payloadSizeExceeded, encryptionOrder, }; } async function encryptAndroidSilentNotification( encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, cookieID: string, senderDeviceDescriptor: SenderDeviceDescriptor, notification: AndroidNotificationRescind | AndroidBadgeOnlyNotification, ): Promise { // We don't validate payload size for rescind // since they are expected to be small and // never exceed any FCM limit const { ...unencryptedPayload } = notification.data; const { resultPayload } = await encryptAndroidNotificationPayload( encryptedNotifUtilsAPI, cookieID, senderDeviceDescriptor, unencryptedPayload, ); if (resultPayload.encryptedPayload) { return { data: { ...resultPayload }, }; } if (resultPayload.rescind) { return { data: { ...resultPayload }, }; } return { data: { ...resultPayload, }, }; } async function encryptBasicPayload( encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, cookieID: string, senderDeviceDescriptor: SenderDeviceDescriptor, basicPayload: T, ): Promise< | $ReadOnly<{ ...SenderDeviceDescriptor, +encryptedPayload: string, + +type: '1' | '0', +encryptionOrder?: number, }> | { ...T, +encryptionFailed: '1' }, > { const unencryptedSerializedPayload = JSON.stringify(basicPayload); if (!unencryptedSerializedPayload) { return { ...basicPayload, encryptionFailed: '1' }; } try { const { encryptedData: serializedPayload, encryptionOrder } = await encryptedNotifUtilsAPI.encryptSerializedNotifPayload( cookieID, unencryptedSerializedPayload, ); return { ...senderDeviceDescriptor, encryptedPayload: serializedPayload.body, + type: serializedPayload.type ? '1' : '0', encryptionOrder, }; } catch (e) { console.log('Notification encryption failed: ' + e); return { ...basicPayload, encryptionFailed: '1', }; } } async function encryptWebNotification( encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, cookieID: string, senderDeviceDescriptor: SenderDeviceDescriptor, notification: PlainTextWebNotification, ): Promise<{ +notification: WebNotification, +encryptionOrder?: number }> { const { id, ...payloadSansId } = notification; const { encryptionOrder, ...encryptionResult } = await encryptBasicPayload( encryptedNotifUtilsAPI, cookieID, senderDeviceDescriptor, payloadSansId, ); return { notification: { id, ...encryptionResult }, encryptionOrder, }; } async function encryptWNSNotification( encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, cookieID: string, senderDeviceDescriptor: SenderDeviceDescriptor, notification: PlainTextWNSNotification, ): Promise<{ +notification: WNSNotification, +encryptionOrder?: number }> { const { encryptionOrder, ...encryptionResult } = await encryptBasicPayload( encryptedNotifUtilsAPI, cookieID, senderDeviceDescriptor, notification, ); return { notification: { ...encryptionResult }, encryptionOrder, }; } -function prepareEncryptedAPNsNotifications( - encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, - senderDeviceDescriptor: SenderDeviceDescriptor, - devices: $ReadOnlyArray, - notification: apn.Notification, - codeVersion?: ?number, - notificationSizeValidator?: apn.Notification => boolean, -): Promise< - $ReadOnlyArray<{ - +cookieID: string, - +deviceToken: string, - +notification: apn.Notification, - +payloadSizeExceeded: boolean, - +encryptedPayloadHash?: string, - +encryptionOrder?: number, - }>, -> { - const notificationPromises = devices.map( - async ({ cookieID, deviceToken, blobHolder }) => { - const notif = await encryptAPNsNotification( - encryptedNotifUtilsAPI, - cookieID, - senderDeviceDescriptor, - notification, - codeVersion, - notificationSizeValidator, - blobHolder, - ); - return { cookieID, deviceToken, ...notif }; - }, - ); - return Promise.all(notificationPromises); -} - -function prepareEncryptedIOSNotificationRescind( - encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, - senderDeviceDescriptor: SenderDeviceDescriptor, - devices: $ReadOnlyArray, - notification: apn.Notification, - codeVersion?: ?number, -): Promise< - $ReadOnlyArray<{ - +cookieID: string, - +deviceToken: string, - +notification: apn.Notification, - }>, -> { - const notificationPromises = devices.map( - async ({ deviceToken, cookieID }) => { - const { notification: notif } = await encryptAPNsNotification( - encryptedNotifUtilsAPI, - cookieID, - senderDeviceDescriptor, - notification, - codeVersion, - ); - return { deviceToken, cookieID, notification: notif }; - }, - ); - return Promise.all(notificationPromises); -} - function prepareEncryptedAndroidVisualNotifications( encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, senderDeviceDescriptor: SenderDeviceDescriptor, devices: $ReadOnlyArray, notification: AndroidVisualNotification, notificationSizeValidator?: ( notification: AndroidVisualNotification, ) => boolean, ): Promise< $ReadOnlyArray<{ +cookieID: string, +deviceToken: string, +notification: AndroidVisualNotification, +payloadSizeExceeded: boolean, +encryptionOrder?: number, }>, > { const notificationPromises = devices.map( async ({ deviceToken, cookieID, blobHolder }) => { const notif = await encryptAndroidVisualNotification( encryptedNotifUtilsAPI, senderDeviceDescriptor, cookieID, notification, notificationSizeValidator, blobHolder, ); return { deviceToken, cookieID, ...notif }; }, ); return Promise.all(notificationPromises); } function prepareEncryptedAndroidSilentNotifications( encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, senderDeviceDescriptor: SenderDeviceDescriptor, devices: $ReadOnlyArray, notification: AndroidNotificationRescind | AndroidBadgeOnlyNotification, ): Promise< $ReadOnlyArray<{ +cookieID: string, +deviceToken: string, +notification: AndroidNotificationRescind | AndroidBadgeOnlyNotification, +encryptionOrder?: number, }>, > { const notificationPromises = devices.map( async ({ deviceToken, cookieID }) => { const notif = await encryptAndroidSilentNotification( encryptedNotifUtilsAPI, cookieID, senderDeviceDescriptor, notification, ); return { deviceToken, cookieID, notification: notif }; }, ); return Promise.all(notificationPromises); } function prepareEncryptedWebNotifications( encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, senderDeviceDescriptor: SenderDeviceDescriptor, devices: $ReadOnlyArray, notification: PlainTextWebNotification, ): Promise< $ReadOnlyArray<{ +deviceToken: string, +notification: WebNotification, +encryptionOrder?: number, }>, > { const notificationPromises = devices.map( async ({ deviceToken, cookieID }) => { const notif = await encryptWebNotification( encryptedNotifUtilsAPI, cookieID, senderDeviceDescriptor, notification, ); return { ...notif, deviceToken }; }, ); return Promise.all(notificationPromises); } function prepareEncryptedWNSNotifications( encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, senderDeviceDescriptor: SenderDeviceDescriptor, devices: $ReadOnlyArray, notification: PlainTextWNSNotification, ): Promise< $ReadOnlyArray<{ +deviceToken: string, +notification: WNSNotification, +encryptionOrder?: number, }>, > { const notificationPromises = devices.map( async ({ deviceToken, cookieID }) => { const notif = await encryptWNSNotification( encryptedNotifUtilsAPI, cookieID, senderDeviceDescriptor, notification, ); return { ...notif, deviceToken }; }, ); return Promise.all(notificationPromises); } -async function encryptBlobPayload(payload: string): Promise<{ - +encryptionKey: string, - +encryptedPayload: Blob, - +encryptedPayloadHash: string, -}> { - const encryptionKey = await generateKey(); - const encryptedPayload = await encrypt( - encryptionKey, - new TextEncoder().encode(payload), - ); - const encryptedPayloadBuffer = Buffer.from(encryptedPayload); - const blobHashBase64 = await crypto - .createHash('sha256') - .update(encryptedPayloadBuffer) - .digest('base64'); - const blobHash = toBase64URL(blobHashBase64); - - const payloadBlob = new Blob([encryptedPayloadBuffer]); - const encryptionKeyString = Buffer.from(encryptionKey).toString('base64'); - return { - encryptionKey: encryptionKeyString, - encryptedPayload: payloadBlob, - encryptedPayloadHash: blobHash, - }; -} - export { - prepareEncryptedAPNsNotifications, - prepareEncryptedIOSNotificationRescind, prepareEncryptedAndroidVisualNotifications, prepareEncryptedAndroidSilentNotifications, prepareEncryptedWebNotifications, prepareEncryptedWNSNotifications, - encryptBlobPayload, }; diff --git a/lib/push/web-notif-creators.js b/lib/push/web-notif-creators.js new file mode 100644 index 000000000..5ccca392d --- /dev/null +++ b/lib/push/web-notif-creators.js @@ -0,0 +1,70 @@ +// @flow + +import t, { type TInterface } from 'tcomb'; + +import { prepareEncryptedWebNotifications } from './crypto.js'; +import { hasMinCodeVersion } from '../shared/version-utils.js'; +import type { PlatformDetails } from '../types/device-types.js'; +import { + type NotificationTargetDevice, + type TargetedWebNotification, + type ResolvedNotifTexts, + resolvedNotifTextsValidator, + type SenderDeviceDescriptor, + senderDeviceDescriptorValidator, + type EncryptedNotifUtilsAPI, +} from '../types/notif-types.js'; +import { tID, tPlatformDetails, tShape } from '../utils/validation-utils.js'; + +export type WebNotifInputData = { + +id: string, + +notifTexts: ResolvedNotifTexts, + +threadID: string, + +senderDeviceDescriptor: SenderDeviceDescriptor, + +unreadCount: number, + +platformDetails: PlatformDetails, +}; + +export const webNotifInputDataValidator: TInterface = + tShape({ + id: t.String, + notifTexts: resolvedNotifTextsValidator, + threadID: tID, + senderDeviceDescriptor: senderDeviceDescriptorValidator, + unreadCount: t.Number, + platformDetails: tPlatformDetails, + }); + +async function createWebNotification( + encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, + inputData: WebNotifInputData, + devices: $ReadOnlyArray, +): Promise<$ReadOnlyArray> { + const { id, notifTexts, threadID, unreadCount, senderDeviceDescriptor } = + inputData; + + const { merged, ...rest } = notifTexts; + const notification = { + ...rest, + unreadCount, + id, + threadID, + }; + + const shouldBeEncrypted = hasMinCodeVersion(inputData.platformDetails, { + web: 43, + }); + + if (!shouldBeEncrypted) { + return devices.map(({ deviceToken }) => ({ deviceToken, notification })); + } + + return prepareEncryptedWebNotifications( + encryptedNotifUtilsAPI, + senderDeviceDescriptor, + devices, + notification, + ); +} + +export { createWebNotification }; diff --git a/lib/push/wns-notif-creators.js b/lib/push/wns-notif-creators.js new file mode 100644 index 000000000..51a235ba2 --- /dev/null +++ b/lib/push/wns-notif-creators.js @@ -0,0 +1,77 @@ +// @flow + +import t, { type TInterface } from 'tcomb'; + +import { prepareEncryptedWNSNotifications } from './crypto.js'; +import { hasMinCodeVersion } from '../shared/version-utils.js'; +import type { PlatformDetails } from '../types/device-types.js'; +import { + type NotificationTargetDevice, + type TargetedWNSNotification, + type ResolvedNotifTexts, + resolvedNotifTextsValidator, + type SenderDeviceDescriptor, + senderDeviceDescriptorValidator, + type EncryptedNotifUtilsAPI, +} from '../types/notif-types.js'; +import { tID, tPlatformDetails, tShape } from '../utils/validation-utils.js'; + +export const wnsMaxNotificationPayloadByteSize = 5000; + +export type WNSNotifInputData = { + +notifTexts: ResolvedNotifTexts, + +threadID: string, + +senderDeviceDescriptor: SenderDeviceDescriptor, + +unreadCount: number, + +platformDetails: PlatformDetails, +}; + +export const wnsNotifInputDataValidator: TInterface = + tShape({ + notifTexts: resolvedNotifTextsValidator, + threadID: tID, + senderDeviceDescriptor: senderDeviceDescriptorValidator, + unreadCount: t.Number, + platformDetails: tPlatformDetails, + }); + +async function createWNSNotification( + encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, + inputData: WNSNotifInputData, + devices: $ReadOnlyArray, +): Promise<$ReadOnlyArray> { + const { notifTexts, threadID, unreadCount, senderDeviceDescriptor } = + inputData; + const { merged, ...rest } = notifTexts; + const notification = { + ...rest, + unreadCount, + threadID, + }; + + if ( + encryptedNotifUtilsAPI.getNotifByteSize(JSON.stringify(notification)) > + wnsMaxNotificationPayloadByteSize + ) { + console.warn('WNS notification exceeds size limit'); + } + + const shouldBeEncrypted = hasMinCodeVersion(inputData.platformDetails, { + majorDesktop: 10, + }); + + if (!shouldBeEncrypted) { + return devices.map(({ deviceToken }) => ({ + deviceToken, + notification, + })); + } + return await prepareEncryptedWNSNotifications( + encryptedNotifUtilsAPI, + senderDeviceDescriptor, + devices, + notification, + ); +} + +export { createWNSNotification }; diff --git a/lib/types/notif-types.js b/lib/types/notif-types.js index 67cd8d403..86a2ea3ac 100644 --- a/lib/types/notif-types.js +++ b/lib/types/notif-types.js @@ -1,212 +1,221 @@ // @flow import type { EncryptResult } from '@commapp/olm'; -import t, { type TInterface } from 'tcomb'; +import t, { type TInterface, type TUnion } from 'tcomb'; import type { EntityText, ThreadEntity } from '../utils/entity-text.js'; import { tShape } from '../utils/validation-utils.js'; export type NotifTexts = { +merged: string | EntityText, +body: string | EntityText, +title: string | ThreadEntity, +prefix?: string | EntityText, }; export type ResolvedNotifTexts = { +merged: string, +body: string, +title: string, +prefix?: string, }; export const resolvedNotifTextsValidator: TInterface = tShape({ merged: t.String, body: t.String, title: t.String, prefix: t.maybe(t.String), }); export type SenderDeviceDescriptor = | { +keyserverID: string } | { +senderDeviceID: string }; +export const senderDeviceDescriptorValidator: TUnion = + t.union([ + tShape({ keyserverID: t.String }), + tShape({ senderDeviceID: t.String }), + ]); + export type PlainTextWebNotificationPayload = { +body: string, +prefix?: string, +title: string, +unreadCount: number, +threadID: string, +encryptionFailed?: '1', }; export type PlainTextWebNotification = $ReadOnly<{ +id: string, ...PlainTextWebNotificationPayload, }>; export type EncryptedWebNotification = $ReadOnly<{ ...SenderDeviceDescriptor, +id: string, +encryptedPayload: string, - +type?: '0' | '1', + +type: '0' | '1', }>; export type WebNotification = | PlainTextWebNotification | EncryptedWebNotification; export type PlainTextWNSNotification = { +body: string, +prefix?: string, +title: string, +unreadCount: number, +threadID: string, +encryptionFailed?: '1', }; export type EncryptedWNSNotification = $ReadOnly<{ ...SenderDeviceDescriptor, +encryptedPayload: string, - +type?: '0' | '1', + +type: '0' | '1', }>; export type WNSNotification = | PlainTextWNSNotification | EncryptedWNSNotification; export type AndroidVisualNotificationPayloadBase = $ReadOnly<{ - +badge: string, + +badge?: string, +body: string, +title: string, +prefix?: string, +threadID: string, +collapseKey?: string, +badgeOnly?: '0', +encryptionFailed?: '1', }>; type AndroidSmallVisualNotificationPayload = $ReadOnly<{ ...AndroidVisualNotificationPayloadBase, +messageInfos?: string, }>; type AndroidLargeVisualNotificationPayload = $ReadOnly<{ ...AndroidVisualNotificationPayloadBase, +blobHash: string, +encryptionKey: string, }>; export type AndroidVisualNotificationPayload = | AndroidSmallVisualNotificationPayload | AndroidLargeVisualNotificationPayload; type EncryptedThinThreadPayload = { +keyserverID: string, +encryptedPayload: string, - +type?: '0' | '1', + +type: '0' | '1', }; type EncryptedThickThreadPayload = { +senderDeviceID: string, +encryptedPayload: string, - +type?: '0' | '1', + +type: '0' | '1', }; export type AndroidVisualNotification = { +data: $ReadOnly<{ +id?: string, ... | AndroidSmallVisualNotificationPayload | AndroidLargeVisualNotificationPayload | EncryptedThinThreadPayload | EncryptedThickThreadPayload, }>, }; export type AndroidNotificationRescind = { +data: $ReadOnly<{ ... | { +badge: string, +rescind: 'true', +rescindID: string, +setUnreadStatus: 'true', +threadID: string, +encryptionFailed?: string, } | EncryptedThinThreadPayload | EncryptedThickThreadPayload, }>, }; export type AndroidBadgeOnlyNotification = { +data: $ReadOnly<{ ... | { +badge: string, +badgeOnly: '1', +encryptionFailed?: string, } | EncryptedThinThreadPayload | EncryptedThickThreadPayload, }>, }; type AndroidNotificationWithPriority = | { +notification: AndroidVisualNotification, +priority: 'high', } | { +notification: AndroidBadgeOnlyNotification | AndroidNotificationRescind, +priority: 'normal', }; export type TargetedAndroidNotification = $ReadOnly<{ ...AndroidNotificationWithPriority, +deviceToken: string, +encryptionOrder?: number, }>; export type TargetedWebNotification = { +notification: WebNotification, +deviceToken: string, +encryptionOrder?: number, }; export type TargetedWNSNotification = { +notification: WNSNotification, +deviceToken: string, +encryptionOrder?: number, }; export type NotificationTargetDevice = { +cookieID: string, +deviceToken: string, +blobHolder?: string, }; export type EncryptedNotifUtilsAPI = { +encryptSerializedNotifPayload: ( cryptoID: string, unencryptedPayload: string, - encryptedPayloadSizeValidator?: (encryptedPayload: string) => boolean, + encryptedPayloadSizeValidator?: ( + encryptedPayload: string, + type: '1' | '0', + ) => boolean, ) => Promise<{ +encryptedData: EncryptResult, +sizeLimitViolated?: boolean, +encryptionOrder?: number, }>, +uploadLargeNotifPayload: ( payload: string, numberOfHolders: number, ) => Promise< | { +blobHolders: $ReadOnlyArray, +blobHash: string, +encryptionKey: string, } | { +blobUploadError: string }, >, +getNotifByteSize: (serializedNotification: string) => number, };