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

Commit

Permalink
Merge pull request #12004 from NejcZdovc/hotfix/#11997-mintime
Browse files Browse the repository at this point in the history
Fixes time spent min visit time
  • Loading branch information
bsclifton committed Nov 20, 2017
1 parent 8f12cdf commit 6decbaa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
35 changes: 22 additions & 13 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,19 +672,26 @@ const addSiteVisit = (state, timestamp, location, tabId) => {
}
}

return saveVisit(state, publisherKey, duration, revisitP)
return saveVisit(state, publisherKey, {
duration,
revisited: revisitP
})
}

const saveVisit = (state, publisherKey, duration, revisited) => {
if (!synopsis || !publisherKey) {
const saveVisit = (state, publisherKey, options) => {
if (!synopsis || !publisherKey || !options) {
return state
}

if (_internal.verboseP) {
console.log('\nadd publisher ' + publisherKey + ': ' + (duration / 1000) + ' sec' + ' revisitP=' + revisited)
console.log('\nadd publisher ' + publisherKey + ': ' + (options.duration / 1000) + ' sec' + ' revisitP=' + options.revisited)
}

synopsis.addPublisher(publisherKey, {duration: duration, revisitP: revisited})
synopsis.addPublisher(publisherKey, {
duration: options.duration,
revisitP: options.revisited,
ignoreMinTime: options.ignoreMinTime || false
})
state = ledgerState.setPublisher(state, publisherKey, synopsis.publishers[publisherKey])
state = updatePublisherInfo(state)
state = checkVerifiedStatus(state, publisherKey)
Expand Down Expand Up @@ -2230,12 +2237,6 @@ const onMediaRequest = (state, xhr, type, tabId) => {
return state
}

const minDuration = parseInt(getSetting(settings.PAYMENTS_MINIMUM_VISIT_TIME))
duration = parseInt(duration)
if (duration > 0 && duration < minDuration) {
duration = minDuration
}

if (!ledgerPublisher) {
ledgerPublisher = require('bat-publisher')
}
Expand All @@ -2253,7 +2254,11 @@ const onMediaRequest = (state, xhr, type, tabId) => {
const publisherKey = cache.get('publisher')
const publisher = ledgerState.getPublisher(state, publisherKey)
if (!publisher.isEmpty()) {
return module.exports.saveVisit(state, publisherKey, duration, revisited)
return module.exports.saveVisit(state, publisherKey, {
duration,
revisited,
ignoreMinTime: true
})
}
}

Expand Down Expand Up @@ -2338,7 +2343,11 @@ const onMediaPublisher = (state, mediaKey, response, duration, revisited) => {
// Add to cache
state = ledgerVideoCache.setCacheByVideoId(state, mediaKey, cacheObject)

state = module.exports.saveVisit(state, publisherKey, duration, revisited)
state = module.exports.saveVisit(state, publisherKey, {
duration,
revisited,
ignoreMinTime: true
})

return state
}
Expand Down
40 changes: 20 additions & 20 deletions test/unit/app/browser/api/ledgerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,11 @@ describe('ledger api unit tests', function () {
it('get data from cache, if we have publisher in synopsis', function () {
ledgerApi.onMediaRequest(cacheAppState, xhr, ledgerMediaProviders.YOUTUBE, 1)
assert(publisherFromMediaPropsSpy.notCalled)
assert(saveVisitSpy.withArgs(cacheAppState, publisherKey, 10001, false).calledOnce)
assert(saveVisitSpy.withArgs(cacheAppState, publisherKey, {
duration: 10001,
revisited: false,
ignoreMinTime: true
}).calledOnce)
})

it('get data from server if we have cache, but we do not have publisher in synopsis', function () {
Expand All @@ -601,40 +605,36 @@ describe('ledger api unit tests', function () {
assert(saveVisitSpy.notCalled)
})

it('min duration is set to minimum visit time if below that threshold', function () {
const xhr2 = 'https://www.youtube.com/api/stats/watchtime?docid=kLiLOkzLetE&st=20.338&et=21.339'
ledgerApi.onMediaRequest(cacheAppState, xhr2, ledgerMediaProviders.YOUTUBE, 1)
assert(publisherFromMediaPropsSpy.notCalled)
assert(saveVisitSpy.withArgs(cacheAppState, publisherKey, paymentsMinVisitTime, false).calledOnce)
})

it('min duration is set to minimum visit time if below that threshold (string setting)', function () {
paymentsMinVisitTime = '5000'
const xhr2 = 'https://www.youtube.com/api/stats/watchtime?docid=kLiLOkzLetE&st=20.338&et=21.339'
ledgerApi.onMediaRequest(cacheAppState, xhr2, ledgerMediaProviders.YOUTUBE, 1)
assert(publisherFromMediaPropsSpy.notCalled)
assert(saveVisitSpy.withArgs(cacheAppState, publisherKey, 5000, false).calledOnce)
paymentsMinVisitTime = 5000
})

it('revisited if visiting the same media in the same tab', function () {
// first call, revisit false
ledgerApi.onMediaRequest(cacheAppState, xhr, ledgerMediaProviders.YOUTUBE, 1)
assert.equal(ledgerApi.getCurrentMediaKey(), videoId)
assert(saveVisitSpy.withArgs(cacheAppState, publisherKey, 10001, false).calledOnce)
assert(saveVisitSpy.withArgs(cacheAppState, publisherKey, {
duration: 10001,
revisited: false,
ignoreMinTime: true
}).calledOnce)

// second call, revisit true
ledgerApi.onMediaRequest(cacheAppState, xhr, ledgerMediaProviders.YOUTUBE, 1)
assert(publisherFromMediaPropsSpy.notCalled)
assert(saveVisitSpy.withArgs(cacheAppState, publisherKey, 10001, true).calledOnce)
assert(saveVisitSpy.withArgs(cacheAppState, publisherKey, {
duration: 10001,
revisited: false,
ignoreMinTime: true
}).calledOnce)
})

it('revisited if visiting media in the background tab', function () {
// first call, revisit false
ledgerApi.setCurrentMediaKey('11')
ledgerApi.onMediaRequest(cacheAppState, xhr, ledgerMediaProviders.YOUTUBE, 10)
assert.equal(ledgerApi.getCurrentMediaKey(), '11')
assert(saveVisitSpy.withArgs(cacheAppState, publisherKey, 10001, true).calledOnce)
assert(saveVisitSpy.withArgs(cacheAppState, publisherKey, {
duration: 10001,
revisited: true,
ignoreMinTime: true
}).calledOnce)
})
})

Expand Down

0 comments on commit 6decbaa

Please sign in to comment.