Skip to content

Commit

Permalink
Fixes brave#14767, making sure browsing activity message goes away wh…
Browse files Browse the repository at this point in the history
…en min time has been accrued
  • Loading branch information
ryanml committed Jul 18, 2018
1 parent c348daf commit d060faa
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 2 deletions.
36 changes: 35 additions & 1 deletion app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ const paymentPresent = (state, tabId, present) => {
}
if (present) {
appActions.onPromotionGet()
appActions.onCheckBrowserActivityTime()

if (togglePromotionTimeoutId) {
clearTimeout(togglePromotionTimeoutId)
}
Expand Down Expand Up @@ -1117,6 +1119,37 @@ const pageDataChanged = (state, viewData = {}, keepInfo = false) => {
return state
}

const checkBrowserActivityTime = (state) => {
// Fuzzing threshold
const minTime = 30 * ledgerUtil.milliseconds.minute
const publishers = ledgerState.getPublishers(state)

if (publishers.isEmpty()) {
return state
}

const ledgerStatus = ledgerState.getAboutProp(state, 'status')
const curBrowsingTime = ledgerState.getAboutProp(state, 'browsingTime') || 0

// Check cached browsing time to avoid unneeded recalculation
if (curBrowsingTime >= minTime && ledgerStatus !== ledgerStatuses.FUZZING) {
return state
}

const browsingTime = publishers.reduce((acc, key, val) => {
return acc + publishers.get(val).get('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')
Expand Down Expand Up @@ -3385,7 +3418,8 @@ const getMethods = () => {
getPublisherInfo,
checkPublisherInfoUpdate,
updatePublishersInfo,
runPublishersUpdate
runPublishersUpdate,
checkBrowserActivityTime
}

let privateMethods = {}
Expand Down
5 changes: 5 additions & 0 deletions app/browser/reducers/ledgerReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ AppStore
},
ledger: {
about: {
browsingTime: number, // A total of publisher visit time
synopsis: Array.Object,
synopsisOptions: Object
},
Expand Down
6 changes: 6 additions & 0 deletions js/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2135,6 +2135,12 @@ const appActions = {
tabId,
index
})
},

onCheckBrowserActivityTime: function () {
dispatch({
actionType: appConstants.APP_ON_CHECK_BROWSER_ACTIVITY_TIME
})
}
}

Expand Down
3 changes: 2 additions & 1 deletion js/constants/appConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
154 changes: 154 additions & 0 deletions test/unit/app/browser/api/ledgerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
})
})

0 comments on commit d060faa

Please sign in to comment.