Skip to content

Commit

Permalink
Implement target conditions on rollouts. (#2889)
Browse files Browse the repository at this point in the history
  • Loading branch information
sammacbeth authored Jan 9, 2025
1 parent 9f24233 commit 067acc9
Show file tree
Hide file tree
Showing 4 changed files with 393 additions and 2 deletions.
19 changes: 18 additions & 1 deletion shared/js/background/components/remote-config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/**
* @typedef {import('../settings.js')} Settings
* @typedef {import('./tds').default} TDSStorage
* @typedef {import('@duckduckgo/privacy-configuration/schema/config.js').GenericV4Config} Config
* @typedef {import('@duckduckgo/privacy-configuration/schema/config.ts').GenericV4Config} Config
* @typedef {{
* localeCountry: string;
* localeLanguage: string;
* }} TargetEnvironment
*/

import { getUserLocaleCountry, getUserLocale } from '../i18n';
import { getFeatureSettings, isFeatureEnabled, satisfiesMinVersion } from '../utils';
import { getExtensionVersion, getFromSessionStorage } from '../wrapper';
import ResourceLoader from './resource-loader';
Expand Down Expand Up @@ -42,6 +47,11 @@ export default class RemoteConfig extends ResourceLoader {
this.onUpdate(async (_, etag, v) => {
this.updateConfig(v);
});
/** @type {TargetEnvironment} */
this.targetEnvironment = {
localeCountry: getUserLocaleCountry(),
localeLanguage: getUserLocale(),
};
}

/**
Expand Down Expand Up @@ -88,6 +98,13 @@ export default class RemoteConfig extends ResourceLoader {
_processRawConfig(configValue) {
Object.entries(configValue.features).forEach(([featureName, feature]) => {
Object.entries(feature.features || {}).forEach(([name, subfeature]) => {
if (subfeature.targets && subfeature.state === 'enabled') {
// Targets: subfeature should only be enabled if a matching target is found.
const match = subfeature.targets.some((t) => Object.entries(t).every(([k, v]) => v === this.targetEnvironment[k]));
if (!match) {
subfeature.state = 'disabled';
}
}
if (subfeature.rollout && subfeature.state === 'enabled') {
/* Handle a rollout: Dice roll is stored in settings and used that to decide
* whether the feature is set as 'enabled' or not.
Expand Down
8 changes: 8 additions & 0 deletions shared/js/background/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ export function getFullUserLocale() {

return browser.i18n.getUILanguage();
}

export function getUserLocaleCountry() {
try {
return getFullUserLocale().split('-')[1];
} catch (e) {
return '';
}
}
Loading

0 comments on commit 067acc9

Please sign in to comment.