From 6decbaa69eca721064b27977e6e98f2b1e9af643 Mon Sep 17 00:00:00 2001 From: Brian Clifton Date: Mon, 20 Nov 2017 08:55:05 -0700 Subject: [PATCH] Merge pull request #12004 from NejcZdovc/hotfix/#11997-mintime Fixes time spent min visit time --- app/browser/api/ledger.js | 35 ++++++++++++++-------- test/unit/app/browser/api/ledgerTest.js | 40 ++++++++++++------------- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/app/browser/api/ledger.js b/app/browser/api/ledger.js index 7c2ea26c107..995acfc4879 100644 --- a/app/browser/api/ledger.js +++ b/app/browser/api/ledger.js @@ -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) @@ -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') } @@ -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 + }) } } @@ -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 } diff --git a/test/unit/app/browser/api/ledgerTest.js b/test/unit/app/browser/api/ledgerTest.js index 8015359e409..c96ff0b084e 100644 --- a/test/unit/app/browser/api/ledgerTest.js +++ b/test/unit/app/browser/api/ledgerTest.js @@ -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 () { @@ -601,32 +605,24 @@ 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 () { @@ -634,7 +630,11 @@ describe('ledger api unit tests', function () { 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) }) })