From 1b115b1670efbfbfb28a226fbd0c31497c2bb825 Mon Sep 17 00:00:00 2001 From: Rodrigo Date: Wed, 23 Jun 2021 18:25:54 -0500 Subject: [PATCH] Randomize Confirmed Delivery to 25 second max delay --- src/service-worker/ServiceWorker.ts | 12 ++++++++---- test/unit/context/sw/ServiceWorker.ts | 7 ++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/service-worker/ServiceWorker.ts b/src/service-worker/ServiceWorker.ts index 96aef258b..a5a86fcc8 100755 --- a/src/service-worker/ServiceWorker.ts +++ b/src/service-worker/ServiceWorker.ts @@ -27,9 +27,12 @@ import ServiceWorkerHelper from "../helpers/ServiceWorkerHelper"; import { NotificationReceived, NotificationClicked } from "../models/Notification"; import { cancelableTimeout } from "../helpers/sw/CancelableTimeout"; import { DeviceRecord } from '../models/DeviceRecord'; +import { awaitableTimeout } from "../utils/AwaitableTimeout"; declare var self: ServiceWorkerGlobalScope & OSServiceWorkerFields; +const MAX_CONFIRMED_DELIVERY_DELAY = 25; + /** * The main service worker script fetching and displaying notifications to users in the background even when the client * site is not running. The worker is registered via the navigator.serviceWorker.register() call after the user first @@ -354,18 +357,19 @@ export class ServiceWorker { if (!hasRequiredParams) { return null; } - + // JSON.stringify() does not include undefined values // Our response will not contain those fields here which have undefined values const postData = { - player_id : deviceId, + player_id : deviceId, app_id : appId }; - + Log.debug(`Called %csendConfirmedDelivery(${ JSON.stringify(notification, null, 4) })`, Utils.getConsoleStyle('code')); - + + await awaitableTimeout(Math.floor(Math.random() * MAX_CONFIRMED_DELIVERY_DELAY * 1_000)); return await OneSignalApiBase.put(`notifications/${notification.id}/report_received`, postData); } diff --git a/test/unit/context/sw/ServiceWorker.ts b/test/unit/context/sw/ServiceWorker.ts index 813b10b6e..18107efde 100644 --- a/test/unit/context/sw/ServiceWorker.ts +++ b/test/unit/context/sw/ServiceWorker.ts @@ -16,6 +16,7 @@ import { MockPushEvent } from '../../../support/mocks/service-workers/models/Moc import { MockPushMessageData } from '../../../support/mocks/service-workers/models/MockPushMessageData'; import OneSignalUtils from '../../../../src/utils/OneSignalUtils'; import { setupFakePlayerId } from '../../../support/tester/utils'; +import * as awaitableTimeout from '../../../../src/utils/AwaitableTimeout'; declare var self: MockServiceWorkerGlobalScope; @@ -24,7 +25,6 @@ const appConfig = TestEnvironment.getFakeAppConfig(); test.beforeEach(async function() { sandbox = sinon.sandbox.create(); - await TestEnvironment.initializeForServiceWorker(); await Database.setAppConfig(appConfig); @@ -291,6 +291,7 @@ test('onNotificationClicked - notification PUT Before openWindow', async t => { } test('sendConfirmedDelivery - notification is null - feature flag is y', async t => { + sandbox.stub(awaitableTimeout, 'awaitableTimeout'); const notificationId = null; const notificationPutCall = mockNotificationPutCall(notificationId); await fakeSetSubscription(); @@ -300,6 +301,7 @@ test('onNotificationClicked - notification PUT Before openWindow', async t => { }); test('sendConfirmedDelivery - notification is valid - feature flag is y', async t => { + sandbox.stub(awaitableTimeout, 'awaitableTimeout'); const notificationId = Random.getRandomUuid(); const notificationPutCall = mockNotificationPutCall(notificationId); await fakeSetSubscription(); @@ -309,6 +311,7 @@ test('onNotificationClicked - notification PUT Before openWindow', async t => { }); test('sendConfirmedDelivery - notification is valid - feature flag is n', async t => { + sandbox.stub(awaitableTimeout, 'awaitableTimeout'); const notificationId = Random.getRandomUuid(); const notificationPutCall = mockNotificationPutCall(notificationId); await fakeSetSubscription(); @@ -318,6 +321,7 @@ test('onNotificationClicked - notification PUT Before openWindow', async t => { }); test('sendConfirmedDelivery - notification is valid - feature flag is undefined', async t => { + sandbox.stub(awaitableTimeout, 'awaitableTimeout'); const notificationId = Random.getRandomUuid(); const notificationPutCall = mockNotificationPutCall(notificationId); await fakeSetSubscription(); @@ -327,6 +331,7 @@ test('onNotificationClicked - notification PUT Before openWindow', async t => { }); test('sendConfirmedDelivery - notification is valid - feature flag is null', async t => { + sandbox.stub(awaitableTimeout, 'awaitableTimeout'); const notificationId = Random.getRandomUuid(); const notificationPutCall = mockNotificationPutCall(notificationId); await fakeSetSubscription();