Skip to content

Commit

Permalink
Create large notifications in thick threads
Browse files Browse the repository at this point in the history
Summary: This differential creates large notifications in thick threads

Test Plan: Tested in final diff

Reviewers: kamil, tomek

Reviewed By: tomek

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D13469
  • Loading branch information
marcinwasowicz committed Sep 27, 2024
1 parent 0a02874 commit 51312a1
Show file tree
Hide file tree
Showing 9 changed files with 509 additions and 167 deletions.
3 changes: 2 additions & 1 deletion keyserver/src/push/rescind.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ async function prepareAndroidNotification(
devices: $ReadOnlyArray<NotificationTargetDevice>,
): Promise<$ReadOnlyArray<TargetedAndroidNotification>> {
threadID = await validateOutput(platformDetails, tID, threadID);
return await createAndroidNotificationRescind(
const { targetedNotifications } = await createAndroidNotificationRescind(
encryptedNotifUtilsAPI,
{
senderDeviceDescriptor: { keyserverID },
Expand All @@ -379,6 +379,7 @@ async function prepareAndroidNotification(
},
devices,
);
return targetedNotifications;
}

export { rescindPushNotifs };
20 changes: 16 additions & 4 deletions keyserver/src/push/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -1093,11 +1093,12 @@ async function prepareAndroidVisualNotification(
inputData,
);

return createAndroidVisualNotification(
const { targetedNotifications } = await createAndroidVisualNotification(
encryptedNotifUtilsAPI,
convertedData,
devices,
);
return targetedNotifications;
}

async function prepareWebNotification(
Expand All @@ -1110,7 +1111,13 @@ async function prepareWebNotification(
inputData,
);

return createWebNotification(encryptedNotifUtilsAPI, convertedData, devices);
const { targetedNotifications } = await createWebNotification(
encryptedNotifUtilsAPI,
convertedData,
devices,
);

return targetedNotifications;
}

async function prepareWNSNotification(
Expand All @@ -1122,7 +1129,12 @@ async function prepareWNSNotification(
wnsNotifInputDataValidator,
inputData,
);
return createWNSNotification(encryptedNotifUtilsAPI, convertedData, devices);
const { targetedNotifications } = await createWNSNotification(
encryptedNotifUtilsAPI,
convertedData,
devices,
);
return targetedNotifications;
}

type NotificationInfo =
Expand Down Expand Up @@ -1518,7 +1530,7 @@ async function updateBadgeCount(
for (const [versionKey, deviceInfos] of androidVersionsToTokens) {
const { codeVersion, stateVersion } = stringToVersionKey(versionKey);
const preparePromise: Promise<PreparePushResult[]> = (async () => {
const targetedNotifications: $ReadOnlyArray<TargetedAndroidNotification> =
const { targetedNotifications } =
await createAndroidBadgeOnlyNotification(
encryptedNotifUtilsAPI,
{
Expand Down
165 changes: 122 additions & 43 deletions lib/push/android-notif-creators.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import t, { type TInterface } from 'tcomb';
import {
prepareEncryptedAndroidVisualNotifications,
prepareEncryptedAndroidSilentNotifications,
prepareLargeNotifData,
type LargeNotifData,
} from './crypto.js';
import { hasMinCodeVersion } from '../shared/version-utils.js';
import type { PlatformDetails } from '../types/device-types.js';
Expand Down Expand Up @@ -67,7 +69,13 @@ async function createAndroidVisualNotification(
encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI,
inputData: AndroidNotifInputData,
devices: $ReadOnlyArray<NotificationTargetDevice>,
): Promise<$ReadOnlyArray<TargetedAndroidNotification>> {
largeNotifToEncryptionResultPromises?: {
[string]: Promise<LargeNotifData>,
},
): Promise<{
+targetedNotifications: $ReadOnlyArray<TargetedAndroidNotification>,
+largeNotifData?: LargeNotifData,
}> {
const {
senderDeviceDescriptor,
notifTexts,
Expand Down Expand Up @@ -146,11 +154,13 @@ async function createAndroidVisualNotification(
? copyWithMessageInfos
: notification;

return devices.map(({ deliveryID }) => ({
priority,
notification: notificationToSend,
deliveryID,
}));
return {
targetedNotifications: devices.map(({ deliveryID }) => ({
priority,
notification: notificationToSend,
deliveryID,
})),
};
}

const notificationsSizeValidator = (notif: AndroidVisualNotification) => {
Expand All @@ -176,25 +186,61 @@ async function createAndroidVisualNotification(
.map(({ cryptoID, deliveryID }) => ({ cryptoID, deliveryID }));

if (devicesWithExcessiveSizeNoHolders.length === 0) {
return notifsWithMessageInfos.map(
({ notification: notif, deliveryID, encryptionOrder }) => ({
priority,
notification: notif,
deliveryID,
encryptionOrder,
}),
);
return {
targetedNotifications: notifsWithMessageInfos.map(
({ notification: notif, deliveryID, encryptionOrder }) => ({
priority,
notification: notif,
deliveryID,
encryptionOrder,
}),
),
};
}

const canQueryBlobService = hasMinCodeVersion(platformDetails, {
native: 331,
});

let blobHash, blobHolders, encryptionKey, blobUploadError;
if (canQueryBlobService) {
let blobHash,
blobHolders,
encryptionKey,
blobUploadError,
encryptedCopyWithMessageInfos;
const copyWithMessageInfosDataBlob = JSON.stringify(
copyWithMessageInfos.data,
);
if (
canQueryBlobService &&
largeNotifToEncryptionResultPromises &&
largeNotifToEncryptionResultPromises[copyWithMessageInfosDataBlob]
) {
const largeNotifData =
await largeNotifToEncryptionResultPromises[copyWithMessageInfosDataBlob];
blobHash = largeNotifData.blobHash;
encryptionKey = largeNotifData.encryptionKey;
blobHolders = largeNotifData.blobHolders;
encryptedCopyWithMessageInfos =
largeNotifData.encryptedCopyWithMessageInfos;
} else if (canQueryBlobService && largeNotifToEncryptionResultPromises) {
largeNotifToEncryptionResultPromises[copyWithMessageInfosDataBlob] =
prepareLargeNotifData(
copyWithMessageInfosDataBlob,
devicesWithExcessiveSizeNoHolders.length,
encryptedNotifUtilsAPI,
);

const largeNotifData =
await largeNotifToEncryptionResultPromises[copyWithMessageInfosDataBlob];
blobHash = largeNotifData.blobHash;
encryptionKey = largeNotifData.encryptionKey;
blobHolders = largeNotifData.blobHolders;
encryptedCopyWithMessageInfos =
largeNotifData.encryptedCopyWithMessageInfos;
} else if (canQueryBlobService) {
({ blobHash, blobHolders, encryptionKey, blobUploadError } =
await encryptedNotifUtilsAPI.uploadLargeNotifPayload(
JSON.stringify(copyWithMessageInfos.data),
copyWithMessageInfosDataBlob,
devicesWithExcessiveSizeNoHolders.length,
));
}
Expand Down Expand Up @@ -251,10 +297,29 @@ async function createAndroidVisualNotification(
}),
);

return [
const targetedNotifications = [
...targetedNotifsWithMessageInfos,
...targetedNotifsWithoutMessageInfos,
];

if (
!encryptedCopyWithMessageInfos ||
!blobHash ||
!blobHolders ||
!encryptionKey
) {
return { targetedNotifications };
}

return {
targetedNotifications,
largeNotifData: {
blobHash,
blobHolders,
encryptionKey,
encryptedCopyWithMessageInfos,
},
};
}
type AndroidNotificationRescindInputData = {
+senderDeviceDescriptor: SenderDeviceDescriptor,
Expand All @@ -268,7 +333,9 @@ async function createAndroidNotificationRescind(
encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI,
inputData: AndroidNotificationRescindInputData,
devices: $ReadOnlyArray<NotificationTargetDevice>,
): Promise<$ReadOnlyArray<TargetedAndroidNotification>> {
): Promise<{
+targetedNotifications: $ReadOnlyArray<TargetedAndroidNotification>,
}> {
const {
senderDeviceDescriptor,
platformDetails,
Expand Down Expand Up @@ -298,11 +365,13 @@ async function createAndroidNotificationRescind(

const shouldBeEncrypted = hasMinCodeVersion(platformDetails, { native: 233 });
if (!shouldBeEncrypted) {
return devices.map(({ deliveryID }) => ({
notification,
deliveryID,
priority: 'normal',
}));
return {
targetedNotifications: devices.map(({ deliveryID }) => ({
notification,
deliveryID,
priority: 'normal',
})),
};
}

const notifications = await prepareEncryptedAndroidSilentNotifications(
Expand All @@ -312,11 +381,15 @@ async function createAndroidNotificationRescind(
notification,
);

return notifications.map(({ deliveryID, notification: notif }) => ({
deliveryID,
notification: notif,
priority: 'normal',
}));
return {
targetedNotifications: notifications.map(
({ deliveryID, notification: notif }) => ({
deliveryID,
notification: notif,
priority: 'normal',
}),
),
};
}

type SenderDescriptorWithPlatformDetails = {
Expand All @@ -336,7 +409,9 @@ async function createAndroidBadgeOnlyNotification(
encryptedNotifUtilsAPI: EncryptedNotifUtilsAPI,
inputData: AndroidBadgeOnlyNotificationInputData,
devices: $ReadOnlyArray<NotificationTargetDevice>,
): Promise<$ReadOnlyArray<TargetedAndroidNotification>> {
): Promise<{
+targetedNotifications: $ReadOnlyArray<TargetedAndroidNotification>,
}> {
const { senderDeviceDescriptor, platformDetails, badge, threadID } =
inputData;

Expand All @@ -361,11 +436,13 @@ async function createAndroidBadgeOnlyNotification(
const shouldBeEncrypted = hasMinCodeVersion(platformDetails, { native: 222 });

if (!shouldBeEncrypted) {
return devices.map(({ deliveryID }) => ({
notification,
deliveryID,
priority: 'normal',
}));
return {
targetedNotifications: devices.map(({ deliveryID }) => ({
notification,
deliveryID,
priority: 'normal',
})),
};
}

const notifications = await prepareEncryptedAndroidSilentNotifications(
Expand All @@ -375,14 +452,16 @@ async function createAndroidBadgeOnlyNotification(
notification,
);

return notifications.map(
({ notification: notif, deliveryID, encryptionOrder }) => ({
priority: 'normal',
notification: notif,
deliveryID,
encryptionOrder,
}),
);
return {
targetedNotifications: notifications.map(
({ notification: notif, deliveryID, encryptionOrder }) => ({
priority: 'normal',
notification: notif,
deliveryID,
encryptionOrder,
}),
),
};
}

export {
Expand Down
Loading

0 comments on commit 51312a1

Please sign in to comment.