Skip to content

Instantly share code, notes, and snippets.

@marcinwasowicz
Created November 7, 2023 14:33
Show Gist options
  • Save marcinwasowicz/a62c3e2c571d791c52538c5dcd5e730f to your computer and use it in GitHub Desktop.
Save marcinwasowicz/a62c3e2c571d791c52538c5dcd5e730f to your computer and use it in GitHub Desktop.
Sort notifications to match encryption order.
diff --git a/keyserver/src/push/crypto.js b/keyserver/src/push/crypto.js
index c4c1b6aae..e8ca7b733 100644
--- a/keyserver/src/push/crypto.js
+++ b/keyserver/src/push/crypto.js
@@ -221,24 +221,27 @@ async function encryptAndroidNotificationRescind(
async function encryptWebNotification(
cookieID: string,
notification: PlainTextWebNotification,
-): Promise<WebNotification> {
+): Promise<{ notif: WebNotification, +version?: number }> {
const { id, ...payloadSansId } = notification;
const unencryptedSerializedPayload = JSON.stringify(payloadSansId);
try {
const {
encryptedMessages: { serializedPayload },
+ version,
} = await encryptAndUpdateOlmSession(cookieID, 'notifications', {
serializedPayload: unencryptedSerializedPayload,
});
- return { id, encryptedPayload: serializedPayload.body };
+ return { notif: { id, encryptedPayload: serializedPayload.body }, version };
} catch (e) {
console.log('Notification encryption failed: ' + e);
return {
- id,
- encryptionFailed: '1',
- ...payloadSansId,
+ notif: {
+ id,
+ encryptionFailed: '1',
+ ...payloadSansId,
+ },
};
}
}
@@ -349,12 +352,16 @@ function prepareEncryptedWebNotifications(
$ReadOnlyArray<{
+deviceToken: string,
+notification: WebNotification,
+ +version?: number,
}>,
> {
const notificationPromises = devices.map(
async ({ deviceToken, cookieID }) => {
- const notif = await encryptWebNotification(cookieID, notification);
- return { notification: notif, deviceToken };
+ const { notif, version } = await encryptWebNotification(
+ cookieID,
+ notification,
+ );
+ return { notification: notif, deviceToken, version };
},
);
return Promise.all(notificationPromises);
diff --git a/keyserver/src/push/send.js b/keyserver/src/push/send.js
index c75f2c248..44ea265e8 100644
--- a/keyserver/src/push/send.js
+++ b/keyserver/src/push/send.js
@@ -361,15 +361,24 @@ async function sendPushNotif(input: {
};
const deliveryPromise: Promise<PushResult> = (async () => {
- const targetedNotifications = await prepareWebNotification(
- {
- notifTexts,
- threadID: threadInfo.id,
- unreadCount,
- platformDetails,
- },
- devices,
- );
+ const notificationsPromises = [];
+ for (let i = 0; i < 100; i++) {
+ notificationsPromises.push(
+ prepareWebNotification(
+ {
+ notifTexts: { ...notifTexts, body: notifTexts.body + i },
+ threadID: threadInfo.id,
+ unreadCount,
+ platformDetails,
+ },
+ devices,
+ ),
+ );
+ }
+
+ const targetedNotifications = (
+ await Promise.all(notificationsPromises)
+ ).flat();
return await sendWebNotifications(targetedNotifications, {
...notificationInfo,
diff --git a/keyserver/src/push/types.js b/keyserver/src/push/types.js
index 2b0e819cc..65a2aab05 100644
--- a/keyserver/src/push/types.js
+++ b/keyserver/src/push/types.js
@@ -60,6 +60,7 @@ export type TargetedAndroidNotification = {
export type TargetedWebNotification = {
+notification: WebNotification,
+deviceToken: string,
+ +version?: number,
};
export type NotificationTargetDevice = {
diff --git a/keyserver/src/push/utils.js b/keyserver/src/push/utils.js
index 465a57805..be672150b 100644
--- a/keyserver/src/push/utils.js
+++ b/keyserver/src/push/utils.js
@@ -233,9 +233,21 @@ async function webPush(
): Promise<WebPushResult> {
await ensureWebPushInitialized();
- const pushResults = await Promise.all(
- targetedNotifications.map(
- async ({ notification, deviceToken: deviceTokenString }) => {
+ const orderedTargetedNotifications = targetedNotifications
+ .map(({ notification, deviceToken, version }) => ({
+ notification,
+ deviceToken,
+ version: version ? version : -1,
+ }))
+ .sort(({ version: v1 }, { version: v2 }) => (v1 > v2 ? 1 : -1));
+
+ const pushResults = [];
+ for (const {
+ notification,
+ deviceToken: deviceTokenString,
+ } of orderedTargetedNotifications) {
+ pushResults.push(
+ await (async () => {
const deviceToken: PushSubscriptionJSON = JSON.parse(deviceTokenString);
const notificationString = JSON.stringify(notification);
try {
@@ -244,9 +256,9 @@ async function webPush(
return { error };
}
return {};
- },
- ),
- );
+ })(),
+ );
+ }
const errors = [];
const invalidTokens = [];
diff --git a/keyserver/src/updaters/olm-session-updater.js b/keyserver/src/updaters/olm-session-updater.js
index 218b8a986..ce7d37f2a 100644
--- a/keyserver/src/updaters/olm-session-updater.js
+++ b/keyserver/src/updaters/olm-session-updater.js
@@ -15,6 +15,7 @@ const olmSessionUpdateRetryDelay = 50;
type OlmEncryptionResult = {
+encryptedMessages: { +[string]: EncryptResult },
+dbPersistConditionViolated?: boolean,
+ +version?: number,
};
async function encryptAndUpdateOlmSession(
@@ -54,7 +55,7 @@ async function encryptAndUpdateOlmSession(
}
if (dbPersistCondition && !dbPersistCondition(encryptedMessages)) {
- return { encryptedMessages, dbPersistConditionViolated: true };
+ return { encryptedMessages, dbPersistConditionViolated: true, version };
}
const updatedSession = session.pickle(picklingKey);
@@ -86,7 +87,7 @@ async function encryptAndUpdateOlmSession(
const [{ versionOnUpdateAttempt }] = selectResult;
if (version === versionOnUpdateAttempt) {
- return { encryptedMessages };
+ return { encryptedMessages, version };
}
await sleep(olmSessionUpdateRetryDelay);
diff --git a/lib/shared/version-utils.js b/lib/shared/version-utils.js
index e3fc8978e..f559163e3 100644
--- a/lib/shared/version-utils.js
+++ b/lib/shared/version-utils.js
@@ -6,7 +6,7 @@ import { type PlatformDetails, isWebPlatform } from '../types/device-types.js';
* A code version used for features that are waiting to be released
* and we're not sure in which version they will be available.
*/
-const FUTURE_CODE_VERSION = 1000000;
+const FUTURE_CODE_VERSION = 0;
/**
* A code version used for features that are waiting to be included
diff --git a/web/push-notif/service-worker.js b/web/push-notif/service-worker.js
index 9041e708c..2d44bd036 100644
--- a/web/push-notif/service-worker.js
+++ b/web/push-notif/service-worker.js
@@ -45,7 +45,7 @@ function buildDecryptionErrorNotification(
},
};
- if (decryptionError.displayErrorMessage && decryptionError.error) {
+ if (decryptionError.error) {
return {
body: decryptionError.error,
...baseErrorPayload,
@@ -104,9 +104,11 @@ self.addEventListener('push', (event: PushEvent) => {
'Comm notification',
decryptionErrorNotification,
);
+ console.log(decryptionResult.error);
return;
} else if (decryptionResult && decryptionResult.body) {
plainTextData = decryptionResult;
+ console.log('correct');
} else if (data.body) {
plainTextData = data;
} else {
diff --git a/web/selectors/tunnelbroker-selectors.js b/web/selectors/tunnelbroker-selectors.js
index a83868b9d..e39d3b0c1 100644
--- a/web/selectors/tunnelbroker-selectors.js
+++ b/web/selectors/tunnelbroker-selectors.js
@@ -8,7 +8,7 @@ import type { AppState } from '../redux/redux-setup.js';
export const createTunnelbrokerInitMessage: AppState => ?ConnectionInitializationMessage =
createSelector(
- state => state.cryptoStore.primaryIdentityKeys?.ed25519,
+ state => state.cryptoStore?.primaryIdentityKeys.ed25519,
state => state.commServicesAccessToken,
state => state.currentUserInfo?.id,
(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment