mediawiki-extensions-Popups/resources/ext.popups/userSettings.js
Sam Smith b7effdc000 Include user's preview count in BOOT action
Action changes:
* Include the user's preview count in the user property of the action.

Reducer changes:
* Make the eventLogging reducer add the bucketed user's preview count to
  the state tree.

Changes:
* Extract mw.popups.UserSettings#getPreviewCount and #setPreviewCount
  and associated tests from the ext.popups.core module.

Bug: T152225
Change-Id: I6b7afef31311be8fede685deb536f577845cb9cf
2016-12-12 13:01:44 +00:00

106 lines
2.5 KiB
JavaScript

( function ( mw ) {
/**
* @typedef {Object} ext.popups.UserSettings
*/
var IS_ENABLED_KEY = 'mwe-popups-enabled',
PREVIEW_COUNT_KEY = 'ext.popups.core.previewCount';
/**
* Given the global state of the application, creates an object whose methods
* encapsulate all interactions with the given User Agent's storage.
*
* @param {mw.storage} storage The `mw.storage` singleton instance
* @param {mw.user} user The `mw.user` singleton instance
*
* @return {ext.popups.UserSettings}
*/
mw.popups.createUserSettings = function ( storage, user ) {
return {
/**
* Gets whether or not the user has previously enabled Page Previews.
*
* N.B. that if the user hasn't previously enabled or disabled Page
* Previews, i.e. mw.popups.userSettings.setIsEnabled(true), then they
* are treated as if they have enabled them.
*
* @return {Boolean}
*/
getIsEnabled: function () {
return storage.get( IS_ENABLED_KEY ) !== '0';
},
/**
* Sets whether or not the user has enabled Page Previews.
*
* @param {Boolean} isEnabled
*/
setIsEnabled: function ( isEnabled ) {
storage.set( IS_ENABLED_KEY, isEnabled ? '1' : '0' );
},
/**
* Gets whether or not the user has previously enabled **or disabled**
* Page Previews.
*
* @return {Boolean}
*/
hasIsEnabled: function () {
return storage.get( IS_ENABLED_KEY, undefined ) !== undefined;
},
/**
* Gets the user's Page Previews token.
*
* If the storage doesn't contain a token, then one is generated and
* persisted to the storage before being returned.
*
* @return {String}
*/
getToken: function () {
var key = 'PopupsExperimentID',
id = storage.get( key );
if ( !id ) {
id = user.generateRandomSessionId();
storage.set( key, id );
}
return id;
},
/**
* Gets the number of Page Previews that the user has seen.
*
* If the storage isn't available, then -1 is returned.
*
* @return {Number}
*/
getPreviewCount: function () {
var result = storage.get( PREVIEW_COUNT_KEY );
if ( result === false ) {
return -1;
} else if ( result === null ) {
return 0;
}
return parseInt( result, 10 );
},
/**
* Sets the number of Page Previews that the user has seen.
*
* @param {Number} count
*/
setPreviewCount: function ( count ) {
storage.set( PREVIEW_COUNT_KEY, count.toString() );
}
};
};
}( mediaWiki ) );