From ae059730f40bb21dede8610089a11a18ad34b137 Mon Sep 17 00:00:00 2001 From: ryanml Date: Tue, 17 Jul 2018 15:44:44 -0700 Subject: [PATCH] Fixes #14767, making sure browsing activity message goes away when min time has been accrued --- app/browser/api/ledger.js | 35 +++++- app/browser/reducers/ledgerReducer.js | 5 + docs/state.md | 1 + js/actions/appActions.js | 6 + js/constants/appConstants.js | 3 +- test/unit/app/browser/api/ledgerTest.js | 154 ++++++++++++++++++++++++ 6 files changed, 202 insertions(+), 2 deletions(-) diff --git a/app/browser/api/ledger.js b/app/browser/api/ledger.js index afd2fed00d6..54cd95b6410 100644 --- a/app/browser/api/ledger.js +++ b/app/browser/api/ledger.js @@ -190,6 +190,8 @@ const paymentPresent = (state, tabId, present) => { } if (present) { appActions.onPromotionGet() + appActions.onCheckBrowserActivityTime() + if (togglePromotionTimeoutId) { clearTimeout(togglePromotionTimeoutId) } @@ -1117,6 +1119,36 @@ const pageDataChanged = (state, viewData = {}, keepInfo = false) => { return state } +const checkBrowserActivityTime = (state) => { + // Fuzzing threshold + const minTime = 30 * ledgerUtil.milliseconds.minute + const publisherKeys = Object.keys(synopsis.publishers) + + if (publisherKeys.length === 0) { + return state + } + + // Check cached browsing time to avoid unneeded recalculation + const ledgerStatus = ledgerState.getAboutProp(state, 'status') + const curBrowsingTime = ledgerState.getAboutProp(state, 'browsingTime') || 0 + if (curBrowsingTime >= minTime && ledgerStatus !== ledgerStatuses.FUZZING) { + return state + } + + const browsingTime = publisherKeys.reduce((acc, key) => { + return acc + synopsis.publishers[key]['duration'] + }, 0) + + // Cache browsing time + state = ledgerState.setAboutProp(state, 'browsingTime', browsingTime) + + if (browsingTime >= minTime && ledgerStatus === ledgerStatuses.FUZZING) { + state = ledgerState.setAboutProp(state, 'status', '') + } + + return state +} + const backupKeys = (state, backupAction) => { const date = format(new Date(), 'MM/DD/YYYY') const passphrase = ledgerState.getInfoProp(state, 'passphrase') @@ -3385,7 +3417,8 @@ const getMethods = () => { getPublisherInfo, checkPublisherInfoUpdate, updatePublishersInfo, - runPublishersUpdate + runPublishersUpdate, + checkBrowserActivityTime } let privateMethods = {} diff --git a/app/browser/reducers/ledgerReducer.js b/app/browser/reducers/ledgerReducer.js index d5dbfbcca6d..3b648541ef6 100644 --- a/app/browser/reducers/ledgerReducer.js +++ b/app/browser/reducers/ledgerReducer.js @@ -604,6 +604,11 @@ const ledgerReducer = (state, action, immutableAction) => { ) break } + case appConstants.APP_ON_CHECK_BROWSER_ACTIVITY_TIME: + { + state = ledgerApi.checkBrowserActivityTime(state) + break + } } return state } diff --git a/docs/state.md b/docs/state.md index 2660510d764..6de20eaa35d 100644 --- a/docs/state.md +++ b/docs/state.md @@ -189,6 +189,7 @@ AppStore }, ledger: { about: { + browsingTime: number, // A total of publisher visit time synopsis: Array.Object, synopsisOptions: Object }, diff --git a/js/actions/appActions.js b/js/actions/appActions.js index 52d61b771ca..c27acb6cede 100644 --- a/js/actions/appActions.js +++ b/js/actions/appActions.js @@ -2135,6 +2135,12 @@ const appActions = { tabId, index }) + }, + + onCheckBrowserActivityTime: function () { + dispatch({ + actionType: appConstants.APP_ON_CHECK_BROWSER_ACTIVITY_TIME + }) } } diff --git a/js/constants/appConstants.js b/js/constants/appConstants.js index 858490b0c8b..2edc284fc18 100644 --- a/js/constants/appConstants.js +++ b/js/constants/appConstants.js @@ -218,7 +218,8 @@ const appConstants = { APP_ON_NOTIFICATION_RESPONSE: _, APP_ON_PUBLISHERS_INFO_RECEIVED: _, APP_ON_PUBLISHERS_INFO_WRITE: _, - APP_ON_PUBLISHERS_INFO_READ: _ + APP_ON_PUBLISHERS_INFO_READ: _, + APP_ON_CHECK_BROWSER_ACTIVITY_TIME: _ } module.exports = mapValuesByKeys(appConstants) diff --git a/test/unit/app/browser/api/ledgerTest.js b/test/unit/app/browser/api/ledgerTest.js index 0d6b6bb36e0..775dadfb274 100644 --- a/test/unit/app/browser/api/ledgerTest.js +++ b/test/unit/app/browser/api/ledgerTest.js @@ -4302,4 +4302,158 @@ describe('ledger api unit tests', function () { assert.equal(true, synopsis.publishers['clifton.io'].options.verified) }) }) + + describe('checkBrowserActivityTime', function () { + afterEach(function () { + ledgerApi.setSynopsis(undefined) + }) + + it('returns state when there are no publishers', function () { + const synopsis = { + options: {}, + publishers: {} + } + const state = defaultAppState + .setIn(['ledger', 'synopsis'], Immutable.fromJS(synopsis)) + + ledgerApi.setSynopsis(synopsis) + + const result = ledgerApi.checkBrowserActivityTime(state) + assert.deepEqual(result.toJS(), state.toJS()) + }) + + it('returns state if browsingTime is cached at >= 30 minutes, and ledger doesn\'t have a fuzzed status', function () { + const synopsis = { + options: {}, + publishers: { + 'brave.com': { + visits: 2, + duration: 1080000 + }, + 'clifton.io': { + visits: 3, + duration: 1200000 + } + } + } + const state = defaultAppState + .setIn(['ledger', 'synopsis'], Immutable.fromJS(synopsis)) + .setIn(['ledger', 'about', 'browsingTime'], 2280000) + + ledgerApi.setSynopsis(synopsis) + + const result = ledgerApi.checkBrowserActivityTime(state) + assert.deepEqual(result.toJS(), state.toJS()) + }) + + it('unsets status if browsingTime is cached at >= 30 minutes, and ledger has a fuzzed status', function () { + const synopsis = { + options: {}, + publishers: { + 'brave.com': { + visits: 2, + duration: 1080000 + }, + 'clifton.io': { + visits: 3, + duration: 1200000 + } + } + } + const state = defaultAppState + .setIn(['ledger', 'synopsis'], Immutable.fromJS(synopsis)) + .setIn(['ledger', 'about', 'browsingTime'], 2280000) + .setIn(['ledger', 'about', 'status'], ledgerStatuses.FUZZING) + + const expectedState = state + .setIn(['ledger', 'about', 'status'], '') + + ledgerApi.setSynopsis(synopsis) + + const result = ledgerApi.checkBrowserActivityTime(state) + assert.deepEqual(result.toJS(), expectedState.toJS()) + }) + + it('does not unset status if browsing time is less than 30 minutes, and ledger has a fuzzed status', function () { + const synopsis = { + options: {}, + publishers: { + 'brave.com': { + visits: 2, + duration: 20000 + }, + 'clifton.io': { + visits: 3, + duration: 40000 + } + } + } + const state = defaultAppState + .setIn(['ledger', 'synopsis'], Immutable.fromJS(synopsis)) + .setIn(['ledger', 'about', 'status'], ledgerStatuses.FUZZING) + + const expectedState = state + .setIn(['ledger', 'about', 'browsingTime'], 60000) + + ledgerApi.setSynopsis(synopsis) + + const result = ledgerApi.checkBrowserActivityTime(state) + assert.deepEqual(result.toJS(), expectedState.toJS()) + }) + + it('caches browsing time and unsets status if browsing time is greater than 30 minutes', function () { + const synopsis = { + options: {}, + publishers: { + 'brave.com': { + visits: 2, + duration: 1080000 + }, + 'clifton.io': { + visits: 3, + duration: 1200000 + } + } + } + const state = defaultAppState + .setIn(['ledger', 'synopsis'], Immutable.fromJS(synopsis)) + .setIn(['ledger', 'about', 'status'], ledgerStatuses.FUZZING) + + const expectedState = state + .setIn(['ledger', 'about', 'status'], '') + .setIn(['ledger', 'about', 'browsingTime'], 2280000) + + ledgerApi.setSynopsis(synopsis) + + const result = ledgerApi.checkBrowserActivityTime(state) + assert.deepEqual(result.toJS(), expectedState.toJS()) + }) + + it('caches browsing time and does not unset status if ledgerStatus is not fuzzing', function () { + const synopsis = { + options: {}, + publishers: { + 'brave.com': { + visits: 2, + duration: 1080000 + }, + 'clifton.io': { + visits: 3, + duration: 1200000 + } + } + } + const state = defaultAppState + .setIn(['ledger', 'synopsis'], Immutable.fromJS(synopsis)) + .setIn(['ledger', 'about', 'status'], ledgerStatuses.CORRUPTED_SEED) + + const expectedState = state + .setIn(['ledger', 'about', 'browsingTime'], 2280000) + + ledgerApi.setSynopsis(synopsis) + + const result = ledgerApi.checkBrowserActivityTime(state) + assert.deepEqual(result.toJS(), expectedState.toJS()) + }) + }) })