Skip to content
This repository was archived by the owner on Dec 11, 2019. It is now read-only.

Commit 7370c81

Browse files
committed
Merge pull request #14771 from ryanml/browsing-time-fix
Dismissing browser activity notice when browsing time has met/surpassed 30 minutes
1 parent a38be75 commit 7370c81

6 files changed

Lines changed: 200 additions & 2 deletions

File tree

app/browser/api/ledger.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ const paymentPresent = (state, tabId, present) => {
190190
}
191191
if (present) {
192192
appActions.onPromotionGet()
193+
193194
if (togglePromotionTimeoutId) {
194195
clearTimeout(togglePromotionTimeoutId)
195196
}
@@ -1113,6 +1114,38 @@ const pageDataChanged = (state, viewData = {}, keepInfo = false) => {
11131114
}
11141115

11151116
state = addNewLocation(state, location, tabId, keepInfo)
1117+
appActions.onCheckBrowserActivityTime()
1118+
1119+
return state
1120+
}
1121+
1122+
const checkBrowserActivityTime = (state) => {
1123+
// Fuzzing threshold
1124+
const minTime = 30 * ledgerUtil.milliseconds.minute
1125+
const publishers = ledgerState.getPublishers(state)
1126+
1127+
if (publishers.isEmpty()) {
1128+
return state
1129+
}
1130+
1131+
const ledgerStatus = ledgerState.getAboutProp(state, 'status')
1132+
const curBrowsingTime = ledgerState.getAboutProp(state, 'browsingTime') || 0
1133+
1134+
// Check cached browsing time to avoid unneeded recalculation
1135+
if (curBrowsingTime >= minTime || ledgerStatus !== ledgerStatuses.FUZZING) {
1136+
return state
1137+
}
1138+
1139+
const browsingTime = publishers.reduce((acc, publisher) => {
1140+
return acc + publisher.get('duration')
1141+
}, 0)
1142+
1143+
// Cache browsing time
1144+
state = ledgerState.setAboutProp(state, 'browsingTime', browsingTime)
1145+
1146+
if (browsingTime >= minTime && ledgerStatus === ledgerStatuses.FUZZING) {
1147+
state = ledgerState.setAboutProp(state, 'status', '')
1148+
}
11161149

11171150
return state
11181151
}
@@ -3371,7 +3404,8 @@ const getMethods = () => {
33713404
getPublisherInfo,
33723405
checkPublisherInfoUpdate,
33733406
updatePublishersInfo,
3374-
runPublishersUpdate
3407+
runPublishersUpdate,
3408+
checkBrowserActivityTime
33753409
}
33763410

33773411
let privateMethods = {}

app/browser/reducers/ledgerReducer.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,11 @@ const ledgerReducer = (state, action, immutableAction) => {
604604
)
605605
break
606606
}
607+
case appConstants.APP_ON_CHECK_BROWSER_ACTIVITY_TIME:
608+
{
609+
state = ledgerApi.checkBrowserActivityTime(state)
610+
break
611+
}
607612
}
608613
return state
609614
}

docs/state.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ AppStore
189189
},
190190
ledger: {
191191
about: {
192+
browsingTime: number, // A total of publisher visit time
192193
synopsis: Array.Object,
193194
synopsisOptions: Object
194195
},

js/actions/appActions.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,6 +2109,12 @@ const appActions = {
21092109
tabId,
21102110
index
21112111
})
2112+
},
2113+
2114+
onCheckBrowserActivityTime: function () {
2115+
dispatch({
2116+
actionType: appConstants.APP_ON_CHECK_BROWSER_ACTIVITY_TIME
2117+
})
21122118
}
21132119
}
21142120

js/constants/appConstants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ const appConstants = {
217217
APP_ON_NOTIFICATION_RESPONSE: _,
218218
APP_ON_PUBLISHERS_INFO_RECEIVED: _,
219219
APP_ON_PUBLISHERS_INFO_WRITE: _,
220-
APP_ON_PUBLISHERS_INFO_READ: _
220+
APP_ON_PUBLISHERS_INFO_READ: _,
221+
APP_ON_CHECK_BROWSER_ACTIVITY_TIME: _
221222
}
222223

223224
module.exports = mapValuesByKeys(appConstants)

test/unit/app/browser/api/ledgerTest.js

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4211,4 +4211,155 @@ describe('ledger api unit tests', function () {
42114211
assert.equal(true, synopsis.publishers['clifton.io'].options.verified)
42124212
})
42134213
})
4214+
4215+
describe('checkBrowserActivityTime', function () {
4216+
afterEach(function () {
4217+
ledgerApi.setSynopsis(undefined)
4218+
})
4219+
4220+
it('returns state when there are no publishers', function () {
4221+
const synopsis = {
4222+
options: {},
4223+
publishers: {}
4224+
}
4225+
const state = defaultAppState
4226+
.setIn(['ledger', 'synopsis'], Immutable.fromJS(synopsis))
4227+
4228+
ledgerApi.setSynopsis(synopsis)
4229+
4230+
const result = ledgerApi.checkBrowserActivityTime(state)
4231+
assert.deepEqual(result.toJS(), state.toJS())
4232+
})
4233+
4234+
it('returns state if browsingTime is cached at >= 30 minutes, and ledger doesn\'t have a fuzzed status', function () {
4235+
const synopsis = {
4236+
options: {},
4237+
publishers: {
4238+
'brave.com': {
4239+
visits: 2,
4240+
duration: 1080000
4241+
},
4242+
'clifton.io': {
4243+
visits: 3,
4244+
duration: 1200000
4245+
}
4246+
}
4247+
}
4248+
const state = defaultAppState
4249+
.setIn(['ledger', 'synopsis'], Immutable.fromJS(synopsis))
4250+
.setIn(['ledger', 'about', 'browsingTime'], 2280000)
4251+
4252+
ledgerApi.setSynopsis(synopsis)
4253+
4254+
const result = ledgerApi.checkBrowserActivityTime(state)
4255+
assert.deepEqual(result.toJS(), state.toJS())
4256+
})
4257+
4258+
it('return state if cached time is less than 30 minutes and status is not fuzzed', function () {
4259+
const synopsis = {
4260+
options: {},
4261+
publishers: {
4262+
'brave.com': {
4263+
visits: 2,
4264+
duration: 420000
4265+
},
4266+
'clifton.io': {
4267+
visits: 3,
4268+
duration: 425000
4269+
}
4270+
}
4271+
}
4272+
const state = defaultAppState
4273+
.setIn(['ledger', 'synopsis'], Immutable.fromJS(synopsis))
4274+
.setIn(['ledger', 'about', 'status'], ledgerStatuses.CORRUPTED_SEED)
4275+
4276+
ledgerApi.setSynopsis(synopsis)
4277+
4278+
const result = ledgerApi.checkBrowserActivityTime(state)
4279+
assert.deepEqual(result.toJS(), state.toJS())
4280+
})
4281+
4282+
it('does not unset status if browsing time is less than 30 minutes, and ledger has a fuzzed status', function () {
4283+
const synopsis = {
4284+
options: {},
4285+
publishers: {
4286+
'brave.com': {
4287+
visits: 2,
4288+
duration: 20000
4289+
},
4290+
'clifton.io': {
4291+
visits: 3,
4292+
duration: 40000
4293+
}
4294+
}
4295+
}
4296+
const state = defaultAppState
4297+
.setIn(['ledger', 'synopsis'], Immutable.fromJS(synopsis))
4298+
.setIn(['ledger', 'about', 'status'], ledgerStatuses.FUZZING)
4299+
4300+
const expectedState = state
4301+
.setIn(['ledger', 'about', 'browsingTime'], 60000)
4302+
4303+
ledgerApi.setSynopsis(synopsis)
4304+
4305+
const result = ledgerApi.checkBrowserActivityTime(state)
4306+
assert.deepEqual(result.toJS(), expectedState.toJS())
4307+
})
4308+
4309+
it('unsets status if browsingTime is >= 30 minutes and ledgerStatus is fuzzing', function () {
4310+
const synopsis = {
4311+
options: {},
4312+
publishers: {
4313+
'brave.com': {
4314+
visits: 2,
4315+
duration: 1080000
4316+
},
4317+
'clifton.io': {
4318+
visits: 3,
4319+
duration: 1200000
4320+
}
4321+
}
4322+
}
4323+
const state = defaultAppState
4324+
.setIn(['ledger', 'synopsis'], Immutable.fromJS(synopsis))
4325+
.setIn(['ledger', 'about', 'status'], ledgerStatuses.FUZZING)
4326+
4327+
const expectedState = state
4328+
.setIn(['ledger', 'about', 'browsingTime'], 2280000)
4329+
.setIn(['ledger', 'about', 'status'], '')
4330+
4331+
ledgerApi.setSynopsis(synopsis)
4332+
4333+
const result = ledgerApi.checkBrowserActivityTime(state)
4334+
assert.deepEqual(result.toJS(), expectedState.toJS())
4335+
})
4336+
4337+
it('caches browsing time and unsets status if browsing time is greater than 30 minutes', function () {
4338+
const synopsis = {
4339+
options: {},
4340+
publishers: {
4341+
'brave.com': {
4342+
visits: 2,
4343+
duration: 1080000
4344+
},
4345+
'clifton.io': {
4346+
visits: 3,
4347+
duration: 1200000
4348+
}
4349+
}
4350+
}
4351+
const state = defaultAppState
4352+
.setIn(['ledger', 'synopsis'], Immutable.fromJS(synopsis))
4353+
.setIn(['ledger', 'about', 'status'], ledgerStatuses.FUZZING)
4354+
4355+
const expectedState = state
4356+
.setIn(['ledger', 'about', 'status'], '')
4357+
.setIn(['ledger', 'about', 'browsingTime'], 2280000)
4358+
4359+
ledgerApi.setSynopsis(synopsis)
4360+
4361+
const result = ledgerApi.checkBrowserActivityTime(state)
4362+
assert.deepEqual(result.toJS(), expectedState.toJS())
4363+
})
4364+
})
42144365
})

0 commit comments

Comments
 (0)