Skip to content

Commit

Permalink
Merge pull request #996 from OneSignal/user-model/fix-privacy-consent…
Browse files Browse the repository at this point in the history
…-block

User model/fix privacy consent block
  • Loading branch information
rgomezp authored Feb 25, 2023
2 parents 53437c6 + a194fe3 commit 8f99d22
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/core/executors/ExecutorBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { logMethodCall } from "../../shared/utils/utils";
import { SupportedModel } from "../models/SupportedModels";
import ExecutorResult from "./ExecutorResult";
import Log from "../../shared/libraries/Log";
import Database from "../../shared/services/Database";
import LocalStorage from "../../shared/utils/LocalStorage";

const RETRY_AFTER = 5_000;

Expand Down Expand Up @@ -105,6 +107,13 @@ export default abstract class ExecutorBase {
abstract getOperationsFromCache(): Promise<Operation<SupportedModel>[]>;

protected async _processOperationQueue(): Promise<void> {
const consentRequired = OneSignal.config.userConfig.requiresUserPrivacyConsent || LocalStorage.getConsentRequired();
const consentGiven = await Database.getConsentGiven();

if (consentRequired && !consentGiven) {
return;
}

const cachedOperations = await this.getOperationsFromCache();
this._operationQueue = [...cachedOperations, ...this._operationQueue];

Expand Down
10 changes: 9 additions & 1 deletion src/onesignal/OneSignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ export default class OneSignal {
static async login(externalId: string, token?: string): Promise<void> {
logMethodCall('login', { externalId, token });

const consentRequired = OneSignal.config?.userConfig.requiresUserPrivacyConsent || LocalStorage.getConsentRequired();
const consentGiven = await Database.getConsentGiven();

if (consentRequired && !consentGiven) {
throw new OneSignalError('Login: Consent required but not given, skipping login');
}

try {
// before, logging in, process anything waiting in the delta queue so it's not lost
this.coreDirector.forceDeltaQueueProcessingOnAllExecutors();
Expand Down Expand Up @@ -219,6 +226,8 @@ export default class OneSignal {
return;
}

await OneSignal._initializeCoreModuleAndUserNamespace();

if (OneSignal.config.userConfig.requiresUserPrivacyConsent || LocalStorage.getConsentRequired()) {
const providedConsent = await Database.getConsentGiven();
if (!providedConsent) {
Expand All @@ -231,7 +240,6 @@ export default class OneSignal {
}

private static async _delayedInit(): Promise<void> {
await OneSignal._initializeCoreModuleAndUserNamespace();
OneSignal.pendingInit = false;
// Ignore Promise as doesn't return until the service worker becomes active.
OneSignal.context.workerMessenger.listen();
Expand Down

0 comments on commit 8f99d22

Please sign in to comment.