From 29f2689035efb646a92717ec90c959e78030ea9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20W=C4=85sowicz?= Date: Fri, 6 Sep 2024 19:00:14 +0200 Subject: [PATCH] One check for minimum code version supporting e2ee notifs Summary: This differential ensures that e2ee notifs are sent to devices supporting code version that will be determined during staff e2ee dm's release Test Plan: Ensure that e2ee notifs work if and only if FUTURE_CODE_VERSION is 0 Reviewers: tomek Reviewed By: tomek Subscribers: ashoat Differential Revision: https://phab.comm.dev/D13261 --- lib/push/android-notif-creators.js | 17 +--------- lib/push/apns-notif-creators.js | 16 +-------- lib/push/send-utils.js | 53 +++++++++++++++++++++++++++--- 3 files changed, 50 insertions(+), 36 deletions(-) diff --git a/lib/push/android-notif-creators.js b/lib/push/android-notif-creators.js index 38e151f9cc..7ab2aae3c0 100644 --- a/lib/push/android-notif-creators.js +++ b/lib/push/android-notif-creators.js @@ -7,10 +7,7 @@ import { prepareEncryptedAndroidVisualNotifications, prepareEncryptedAndroidSilentNotifications, } from './crypto.js'; -import { - hasMinCodeVersion, - FUTURE_CODE_VERSION, -} from '../shared/version-utils.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 { @@ -288,12 +285,6 @@ async function createAndroidNotificationRescind( }, }; - invariant( - (rescindID && badge) || - hasMinCodeVersion(platformDetails, { native: FUTURE_CODE_VERSION }), - 'thick thread rescind not support for this client version', - ); - if (rescindID && badge) { notification = { ...notification, @@ -349,12 +340,6 @@ async function createAndroidBadgeOnlyNotification( const { senderDeviceDescriptor, platformDetails, badge, threadID } = inputData; - invariant( - (!threadID && badge) || - hasMinCodeVersion(platformDetails, { native: FUTURE_CODE_VERSION }), - 'thick thread badge updates not support for this client version', - ); - let notificationData = { badgeOnly: '1' }; if (badge) { notificationData = { diff --git a/lib/push/apns-notif-creators.js b/lib/push/apns-notif-creators.js index 9f93cb56cc..eb48b88832 100644 --- a/lib/push/apns-notif-creators.js +++ b/lib/push/apns-notif-creators.js @@ -12,10 +12,7 @@ import { prepareEncryptedAPNsSilentNotifications, } from './crypto.js'; import { getAPNsNotificationTopic } from '../shared/notif-utils.js'; -import { - hasMinCodeVersion, - FUTURE_CODE_VERSION, -} from '../shared/version-utils.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 { @@ -331,12 +328,6 @@ async function createAPNsNotificationRescind( senderDeviceDescriptor, } = inputData; - invariant( - (rescindID && badge !== null && badge !== undefined) || - hasMinCodeVersion(platformDetails, { native: FUTURE_CODE_VERSION }), - 'thick thread rescind not support for this client version', - ); - const apnsTopic = getAPNsNotificationTopic(platformDetails); let notification; @@ -433,11 +424,6 @@ async function createAPNsBadgeOnlyNotification( ): Promise<$ReadOnlyArray> { const { senderDeviceDescriptor, platformDetails, threadID, badge } = inputData; - invariant( - (!threadID && badge !== undefined && badge !== null) || - hasMinCodeVersion(platformDetails, { native: FUTURE_CODE_VERSION }), - 'thick thread badge updates not support for this client version', - ); const shouldBeEncrypted = hasMinCodeVersion(platformDetails, { native: 222, diff --git a/lib/push/send-utils.js b/lib/push/send-utils.js index 8854e1a3a6..7ad680c30a 100644 --- a/lib/push/send-utils.js +++ b/lib/push/send-utils.js @@ -38,6 +38,10 @@ import { isMemberActive, threadInfoFromRawThreadInfo, } from '../shared/thread-utils.js'; +import { + NEXT_CODE_VERSION, + hasMinCodeVersion, +} from '../shared/version-utils.js'; import type { AuxUserInfos } from '../types/aux-user-types.js'; import type { PlatformDetails, Platform } from '../types/device-types.js'; import { @@ -997,6 +1001,38 @@ async function createOlmSessionWithDevices( } } +function filterDevicesSupportingDMNotifs< + T: { +devices: $ReadOnlyArray, ... }, +>(devicesContainer: T): T { + return { + ...devicesContainer, + devices: devicesContainer.devices.filter(({ platformDetails }) => + hasMinCodeVersion(platformDetails, { + native: NEXT_CODE_VERSION, + web: NEXT_CODE_VERSION, + majorDesktop: NEXT_CODE_VERSION, + }), + ), + }; +} + +function filterDevicesSupportingDMNotifsForUsers< + T: { +devices: $ReadOnlyArray, ... }, +>(userToDevicesContainer: { +[userID: string]: T }): { +[userID: string]: T } { + const result: { [userID: string]: T } = {}; + for (const userID in userToDevicesContainer) { + const devicesContainer = userToDevicesContainer[userID]; + const filteredDevicesContainer = + filterDevicesSupportingDMNotifs(devicesContainer); + if (filteredDevicesContainer.devices.length === 0) { + continue; + } + result[userID] = filteredDevicesContainer; + } + + return result; +} + type PreparePushNotifsInputData = { +encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI, +senderDeviceDescriptor: SenderDeviceDescriptor, @@ -1037,9 +1073,11 @@ async function preparePushNotifs( return null; } + const filteredPushInfos = filterDevicesSupportingDMNotifsForUsers(pushInfos); + const deviceIDsToUserIDs: { [string]: string } = {}; - for (const userID in pushInfos) { - for (const device of pushInfos[userID].devices) { + for (const userID in filteredPushInfos) { + for (const device of filteredPushInfos[userID].devices) { deviceIDsToUserIDs[device.cryptoID] = userID; } } @@ -1049,7 +1087,7 @@ async function preparePushNotifs( return await buildNotifsFromPushInfo({ encryptedNotifUtilsAPI, senderDeviceDescriptor, - pushInfo: pushInfos, + pushInfo: filteredPushInfos, thickRawThreadInfos, userInfos, getENSNames, @@ -1084,15 +1122,20 @@ async function prepareOwnDevicesPushNotifs( return null; } + const filteredownDevicesPushInfos = + filterDevicesSupportingDMNotifs(ownDevicesPushInfo); + const { senderUserID, senderDeviceDescriptor } = senderInfo; const deviceIDsToUserIDs: { [string]: string } = {}; - for (const device of ownDevicesPushInfo.devices) { + for (const device of filteredownDevicesPushInfos.devices) { deviceIDsToUserIDs[device.cryptoID] = senderUserID; } await createOlmSessionWithDevices(deviceIDsToUserIDs, olmSessionCreator); - const devicesByPlatform = getDevicesByPlatform(ownDevicesPushInfo.devices); + const devicesByPlatform = getDevicesByPlatform( + filteredownDevicesPushInfos.devices, + ); if (rescindData) { return await buildRescindsForOwnDevices(