Skip to content

Commit

Permalink
Merge pull request #867 from OneSignal/fix/custom-link-user-gesture-r…
Browse files Browse the repository at this point in the history
…equirement

Fix custom link user gesture requirement [custom-link] [safari]
  • Loading branch information
rgomezp authored Dec 11, 2021
2 parents 70ce416 + 4671d5a commit 20247bc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 47 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"build:dev-stag": "./build/scripts/build.sh -f development -t staging"
},
"config": {
"sdkVersion": "151510"
"sdkVersion": "151511"
},
"repository": {
"type": "git",
Expand Down
27 changes: 14 additions & 13 deletions src/managers/CustomLinkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ResourceLoadState } from "../services/DynamicResourceLoader";
import { CUSTOM_LINK_CSS_CLASSES, CUSTOM_LINK_CSS_SELECTORS } from "../slidedown/constants";
import { addCssClass } from "../utils";
import { AppUserConfigCustomLinkOptions } from "../models/Prompts";
import LocalStorage from "../utils/LocalStorage";

export class CustomLinkManager {
private config: AppUserConfigCustomLinkOptions | undefined;
Expand All @@ -23,7 +24,7 @@ export class CustomLinkManager {

Log.info("OneSignal: initializing customlink");

if (!this.config?.unsubscribeEnabled && await CustomLinkManager.isPushEnabled()) {
if (!this.config?.unsubscribeEnabled && CustomLinkManager.isPushEnabled()) {
this.hideCustomLinkContainers();
return;
}
Expand Down Expand Up @@ -57,7 +58,7 @@ export class CustomLinkManager {
addCssClass(explanation, this.config.size);
}

if (await CustomLinkManager.isPushEnabled()) {
if (CustomLinkManager.isPushEnabled()) {
addCssClass(explanation, CUSTOM_LINK_CSS_CLASSES.state.subscribed);
} else {
addCssClass(explanation, CUSTOM_LINK_CSS_CLASSES.state.unsubscribed);
Expand Down Expand Up @@ -86,7 +87,7 @@ export class CustomLinkManager {
addCssClass(subscribeButton, this.config.style);
}

if (await CustomLinkManager.isPushEnabled()) {
if (CustomLinkManager.isPushEnabled()) {
addCssClass(subscribeButton, CUSTOM_LINK_CSS_CLASSES.state.subscribed);
} else {
addCssClass(subscribeButton, CUSTOM_LINK_CSS_CLASSES.state.unsubscribed);
Expand Down Expand Up @@ -128,37 +129,37 @@ export class CustomLinkManager {
}

private async handleClick(element: HTMLElement): Promise<void> {
if (await CustomLinkManager.isPushEnabled()) {
if (CustomLinkManager.isPushEnabled()) {
await OneSignal.setSubscription(false);
await this.setTextFromPushStatus(element);
} else {
if (!await CustomLinkManager.isOptedOut()) {
if (!CustomLinkManager.isOptedOut()) {
const autoAccept = !OneSignal.environmentInfo.requiresUserInteraction;
const options: RegisterOptions = { autoAccept };
await OneSignal.registerForPushNotifications(options);
// once subscribed, prevent unsubscribe by hiding customlinks
if (!this.config?.unsubscribeEnabled && await CustomLinkManager.isPushEnabled()) {
if (!this.config?.unsubscribeEnabled && CustomLinkManager.isPushEnabled()) {
this.hideCustomLinkContainers();
}
return;
}
await OneSignal.setSubscription(true);
// once subscribed, prevent unsubscribe by hiding customlinks
if (!this.config?.unsubscribeEnabled && await CustomLinkManager.isPushEnabled()) {
if (!this.config?.unsubscribeEnabled && CustomLinkManager.isPushEnabled()) {
this.hideCustomLinkContainers();
}
}
}

private async setTextFromPushStatus(element: HTMLElement): Promise<void> {
if (this.config?.text?.subscribe) {
if (!await CustomLinkManager.isPushEnabled()) {
if (!CustomLinkManager.isPushEnabled()) {
element.textContent = this.config.text.subscribe;
}
}

if (this.config?.text?.unsubscribe) {
if (await CustomLinkManager.isPushEnabled()) {
if (CustomLinkManager.isPushEnabled()) {
element.textContent = this.config.text.unsubscribe;
}
}
Expand All @@ -182,11 +183,11 @@ export class CustomLinkManager {
return Array.prototype.slice.call(containers);
}

static async isPushEnabled(): Promise<boolean> {
return await OneSignal.privateIsPushNotificationsEnabled();
static isPushEnabled(): boolean {
return LocalStorage.getIsPushNotificationsEnabled();
}

static async isOptedOut(): Promise<boolean> {
return await OneSignal.internalIsOptedOut();
static isOptedOut(): boolean {
return LocalStorage.getIsOptedOut();
}
}
66 changes: 33 additions & 33 deletions test/unit/prompts/CustomLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ test('customlink: container: not render if disabled', async t => {
});

test('customlink: container: render if enabled, explanation present', async t => {
sandbox.stub(CustomLinkManager, "isPushEnabled").resolves(true);
sandbox.stub(CustomLinkManager, "isOptedOut").resolves(false);
sandbox.stub(CustomLinkManager, "isPushEnabled").returns(true);
sandbox.stub(CustomLinkManager, "isOptedOut").returns(false);

await new CustomLinkManager(config).initialize();

Expand All @@ -76,8 +76,8 @@ test('customlink: container: render if enabled, explanation present', async t =>
});

test('customlink: container: render if enabled, no explanation', async t => {
sandbox.stub(CustomLinkManager, "isPushEnabled").resolves(true);
sandbox.stub(CustomLinkManager, "isOptedOut").resolves(false);
sandbox.stub(CustomLinkManager, "isPushEnabled").returns(true);
sandbox.stub(CustomLinkManager, "isOptedOut").returns(false);

config.text.explanation = "";
await new CustomLinkManager(config).initialize();
Expand All @@ -92,7 +92,7 @@ test('customlink: container: render if enabled, no explanation', async t => {

test("customlink: push enabled text and state", async t => {
sandbox.stub(CustomLinkManager, "isPushEnabled").returns(true);
sandbox.stub(CustomLinkManager, "isOptedOut").resolves(false);
sandbox.stub(CustomLinkManager, "isOptedOut").returns(false);

const manager = new CustomLinkManager(config);
await manager.initialize();
Expand All @@ -104,8 +104,8 @@ test("customlink: push enabled text and state", async t => {
});

test('customlink: push disabled text and state', async t => {
sandbox.stub(CustomLinkManager, "isPushEnabled").resolves(false);
sandbox.stub(CustomLinkManager, "isOptedOut").resolves(false);
sandbox.stub(CustomLinkManager, "isPushEnabled").returns(false);
sandbox.stub(CustomLinkManager, "isOptedOut").returns(false);

await new CustomLinkManager(config).initialize();

Expand All @@ -117,8 +117,8 @@ test('customlink: push disabled text and state', async t => {
});

test('customlink: subscribe: intitialized, push enabled', async t => {
sandbox.stub(CustomLinkManager, "isPushEnabled").resolves(true);
sandbox.stub(CustomLinkManager, "isOptedOut").resolves(false);
sandbox.stub(CustomLinkManager, "isPushEnabled").returns(true);
sandbox.stub(CustomLinkManager, "isOptedOut").returns(false);

await new CustomLinkManager(config).initialize();

Expand All @@ -131,8 +131,8 @@ test('customlink: subscribe: intitialized, push enabled', async t => {
});

test('customlink: subscribe: intitialized, push disabled', async t => {
sandbox.stub(CustomLinkManager, "isPushEnabled").resolves(false);
sandbox.stub(CustomLinkManager, "isOptedOut").resolves(false);
sandbox.stub(CustomLinkManager, "isPushEnabled").returns(false);
sandbox.stub(CustomLinkManager, "isOptedOut").returns(false);

await new CustomLinkManager(config).initialize();

Expand All @@ -145,8 +145,8 @@ test('customlink: subscribe: intitialized, push disabled', async t => {
});

test('customlink: subscribe: unsubscribe disabled', async t => {
sandbox.stub(CustomLinkManager, "isPushEnabled").resolves(true);
sandbox.stub(CustomLinkManager, "isOptedOut").resolves(false);
sandbox.stub(CustomLinkManager, "isPushEnabled").returns(true);
sandbox.stub(CustomLinkManager, "isOptedOut").returns(false);

config.unsubscribeEnabled = false;
await new CustomLinkManager(config).initialize();
Expand All @@ -158,8 +158,8 @@ test('customlink: subscribe: unsubscribe disabled', async t => {
});

test('customlink: subscribe: unsubscribe enabled', async t => {
sandbox.stub(CustomLinkManager, "isPushEnabled").resolves(true);
sandbox.stub(CustomLinkManager, "isOptedOut").resolves(false);
sandbox.stub(CustomLinkManager, "isPushEnabled").returns(true);
sandbox.stub(CustomLinkManager, "isOptedOut").returns(false);

await new CustomLinkManager(config).initialize();

Expand All @@ -175,8 +175,8 @@ test('customlink: subscribe: unsubscribe enabled', async t => {
});

test('customlink: subscribe: button', async t => {
sandbox.stub(CustomLinkManager, "isPushEnabled").resolves(true);
sandbox.stub(CustomLinkManager, "isOptedOut").resolves(false);
sandbox.stub(CustomLinkManager, "isPushEnabled").returns(true);
sandbox.stub(CustomLinkManager, "isOptedOut").returns(false);

config.style = "button";
await new CustomLinkManager(config).initialize();
Expand All @@ -191,8 +191,8 @@ test('customlink: subscribe: button', async t => {
});

test('customlink: subscribe: link', async t => {
sandbox.stub(CustomLinkManager, "isPushEnabled").resolves(true);
sandbox.stub(CustomLinkManager, "isOptedOut").resolves(false);
sandbox.stub(CustomLinkManager, "isPushEnabled").returns(true);
sandbox.stub(CustomLinkManager, "isOptedOut").returns(false);

config.style = "link";
await new CustomLinkManager(config).initialize();
Expand All @@ -207,9 +207,9 @@ test('customlink: subscribe: link', async t => {
});

test('customlink: reinitialize', async t => {
sandbox.stub(OneSignal, 'privateIsPushNotificationsEnabled').returns(false);
sandbox.stub(CustomLinkManager, "isPushEnabled").resolves(false);
sandbox.stub(CustomLinkManager, "isOptedOut").resolves(false);
sandbox.stub(OneSignal, 'privateIsPushNotificationsEnabled').resolves(false);
sandbox.stub(CustomLinkManager, "isPushEnabled").returns(false);
sandbox.stub(CustomLinkManager, "isOptedOut").returns(false);

await new CustomLinkManager(config).initialize();
await new CustomLinkManager(config).initialize();
Expand All @@ -220,8 +220,8 @@ test('customlink: reinitialize', async t => {
test('customlink: subscribe: clicked: subscribed -> unsubscribed', async t => {
const subscriptionPromise = EventsTestHelper.getSubscriptionPromise();
new EventsTestHelper(sandbox).simulateSubscribingAfterNativeAllow();
sandbox.stub(CustomLinkManager, "isPushEnabled").resolves(true);
sandbox.stub(CustomLinkManager, "isOptedOut").resolves(false);
sandbox.stub(CustomLinkManager, "isPushEnabled").returns(true);
sandbox.stub(CustomLinkManager, "isOptedOut").returns(false);

const subscriptionSpy = sandbox.stub(OneSignal, 'setSubscription').callsFake(async () => {
await setSubscriptionStub();
Expand Down Expand Up @@ -250,14 +250,14 @@ test('customlink: subscribe: clicked: subscribed -> unsubscribed', async t => {

test('customlink: subscribe: clicked: unsubscribed -> subscribed. https. opted out', async t => {
const subscriptionPromise = EventsTestHelper.getSubscriptionPromise();
sandbox.stub(CustomLinkManager, "isPushEnabled").resolves(false);
sandbox.stub(CustomLinkManager, "isPushEnabled").returns(false);
sandbox.stub(OneSignalUtils, 'isUsingSubscriptionWorkaround').returns(false);
const subscriptionSpy = sandbox.stub(OneSignal, 'setSubscription').callsFake(async () => {
await setSubscriptionStub();
});
// TODO: why is this called in custom link
sandbox.stub(DismissHelper, 'wasPromptOfTypeDismissed').returns(false);
sandbox.stub(CustomLinkManager, "isOptedOut").resolves(true);
sandbox.stub(CustomLinkManager, "isOptedOut").returns(true);
sandbox.stub(OneSignal.context.subscriptionManager, 'getSubscriptionState').returns({
subscribed: true,
optedOut: true,
Expand All @@ -282,12 +282,12 @@ test('customlink: subscribe: clicked: unsubscribed -> subscribed. https. opted o
test('customlink: subscribe: clicked: unsubscribed -> subscribed. https. never subscribed.', async t => {
TestEnvironment.overrideEnvironmentInfo({ requiresUserInteraction: false });
const subscriptionPromise = EventsTestHelper.getSubscriptionPromise();
sandbox.stub(CustomLinkManager, "isPushEnabled").resolves(false);
sandbox.stub(CustomLinkManager, "isPushEnabled").returns(false);
const subscriptionSpy = sandbox.stub(OneSignal, 'registerForPushNotifications').callsFake(async () => {
await setSubscriptionStub();
});
sandbox.stub(OneSignalUtils, 'isUsingSubscriptionWorkaround').returns(false);
sandbox.stub(CustomLinkManager, "isOptedOut").resolves(false);
sandbox.stub(CustomLinkManager, "isOptedOut").returns(false);

sandbox.stub(OneSignal.context.subscriptionManager, 'getSubscriptionState').returns({
subscribed: false,
Expand All @@ -310,12 +310,12 @@ test('customlink: subscribe: clicked: unsubscribed -> subscribed. https. never s
test('customlink: subscribe: clicked: unsubscribed -> subscribed. http. never subscribed.', async t => {
const subscriptionPromise = EventsTestHelper.getSubscriptionPromise();
TestEnvironment.overrideEnvironmentInfo({ requiresUserInteraction: false });
sandbox.stub(CustomLinkManager, "isPushEnabled").resolves(false);
sandbox.stub(CustomLinkManager, "isPushEnabled").returns(false);
const registerSpy = sandbox.stub(OneSignal, 'registerForPushNotifications').callsFake(async () => {
await setSubscriptionStub();
});
sandbox.stub(OneSignalUtils, 'isUsingSubscriptionWorkaround').returns(true);
sandbox.stub(CustomLinkManager, "isOptedOut").resolves(false);
sandbox.stub(CustomLinkManager, "isOptedOut").returns(false);


await new CustomLinkManager(config).initialize();
Expand All @@ -337,10 +337,10 @@ test('customlink: subscribe: clicked: unsubscribed -> subscribed. http. never su

test('customlink: subscribe: clicked: unsubscribed -> subscribed. http. opted out.', async t => {
const subscriptionPromise = EventsTestHelper.getSubscriptionPromise();
sandbox.stub(PermissionManager.prototype, 'getReportedNotificationPermission').resolves(false);
sandbox.stub(PermissionManager.prototype, 'getReportedNotificationPermission').returns(false);
sandbox.stub(OneSignalUtils, 'isUsingSubscriptionWorkaround').returns(true);
sandbox.stub(CustomLinkManager, "isPushEnabled").resolves(false);
sandbox.stub(CustomLinkManager, "isOptedOut").resolves(true);
sandbox.stub(CustomLinkManager, "isPushEnabled").returns(false);
sandbox.stub(CustomLinkManager, "isOptedOut").returns(true);
const subscriptionSpy = sandbox.stub(OneSignal, 'setSubscription').callsFake(async () => {
await setSubscriptionStub();
});
Expand Down

0 comments on commit 20247bc

Please sign in to comment.