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

Commit 9654ef4

Browse files
committed
Merge pull request #12706 from NejcZdovc/hotfix/#12591-error
Fixes undefined synopsis for media publishers
1 parent fbbfbe0 commit 9654ef4

File tree

2 files changed

+114
-41
lines changed

2 files changed

+114
-41
lines changed

app/browser/api/ledger.js

+15-17
Original file line numberDiff line numberDiff line change
@@ -2351,7 +2351,7 @@ const onMediaRequest = (state, xhr, type, tabId) => {
23512351
if (!cache.isEmpty()) {
23522352
const publisherKey = cache.get('publisher')
23532353
const publisher = ledgerState.getPublisher(state, publisherKey)
2354-
if (!publisher.isEmpty()) {
2354+
if (!publisher.isEmpty() && publisher.has('providerName')) {
23552355
return module.exports.saveVisit(state, publisherKey, {
23562356
duration,
23572357
revisited,
@@ -2400,19 +2400,13 @@ const onMediaPublisher = (state, mediaKey, response, duration, revisited) => {
24002400
const publisherURL = response.get('publisherURL')
24012401
const providerName = response.get('providerName')
24022402

2403-
if (publisher.isEmpty()) {
2404-
revisited = false
2403+
if (!synopsis.publishers[publisherKey] || publisher.isEmpty()) {
24052404
synopsis.initPublisher(publisherKey)
24062405

24072406
if (!synopsis.publishers[publisherKey]) {
24082407
return state
24092408
}
24102409

2411-
synopsis.publishers[publisherKey].faviconName = faviconName
2412-
synopsis.publishers[publisherKey].faviconURL = faviconURL
2413-
synopsis.publishers[publisherKey].publisherURL = publisherURL
2414-
synopsis.publishers[publisherKey].providerName = providerName
2415-
24162410
state = ledgerState.setPublisher(state, publisherKey, synopsis.publishers[publisherKey])
24172411

24182412
if (!getSetting(settings.PAYMENTS_SITES_AUTO_SUGGEST)) {
@@ -2424,15 +2418,19 @@ const onMediaPublisher = (state, mediaKey, response, duration, revisited) => {
24242418
savePublisherOption(publisherKey, 'exclude', exclude)
24252419
})
24262420
}
2427-
} else {
2428-
synopsis.publishers[publisherKey].faviconName = faviconName
2429-
synopsis.publishers[publisherKey].faviconURL = faviconURL
2430-
synopsis.publishers[publisherKey].publisherURL = publisherURL
2431-
synopsis.publishers[publisherKey].providerName = providerName
2432-
state = ledgerState.setPublishersProp(state, publisherKey, 'faviconName', faviconName)
2433-
state = ledgerState.setPublishersProp(state, publisherKey, 'faviconURL', faviconURL)
2434-
state = ledgerState.setPublishersProp(state, publisherKey, 'publisherURL', publisherURL)
2435-
state = ledgerState.setPublishersProp(state, publisherKey, 'providerName', providerName)
2421+
}
2422+
2423+
synopsis.publishers[publisherKey].faviconName = faviconName
2424+
synopsis.publishers[publisherKey].faviconURL = faviconURL
2425+
synopsis.publishers[publisherKey].publisherURL = publisherURL
2426+
synopsis.publishers[publisherKey].providerName = providerName
2427+
state = ledgerState.setPublishersProp(state, publisherKey, 'faviconName', faviconName)
2428+
state = ledgerState.setPublishersProp(state, publisherKey, 'faviconURL', faviconURL)
2429+
state = ledgerState.setPublishersProp(state, publisherKey, 'publisherURL', publisherURL)
2430+
state = ledgerState.setPublishersProp(state, publisherKey, 'providerName', providerName)
2431+
2432+
if (publisher.isEmpty()) {
2433+
revisited = false
24362434
}
24372435

24382436
const cacheObject = Immutable.Map()

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

+99-24
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,8 @@ describe('ledger api unit tests', function () {
529529
}))
530530
.setIn(['ledger', 'synopsis', 'publishers', publisherKey], Immutable.fromJS({
531531
visits: 1,
532-
duration: 1000
532+
duration: 1000,
533+
providerName: 'YouTube'
533534
}))
534535

535536
beforeEach(function () {
@@ -636,10 +637,26 @@ describe('ledger api unit tests', function () {
636637
ignoreMinTime: true
637638
}).calledOnce)
638639
})
640+
641+
it('state for this publisher is corrupted, so we need to fetch it again', function () {
642+
const badState = defaultAppState
643+
.setIn(['cache', 'ledgerVideos', videoId], Immutable.fromJS({
644+
publisher: publisherKey
645+
}))
646+
.setIn(['ledger', 'synopsis', 'publishers', publisherKey], Immutable.fromJS({
647+
options: {
648+
excluded: false
649+
}
650+
}))
651+
652+
ledgerApi.onMediaRequest(badState, xhr, ledgerMediaProviders.YOUTUBE, 10)
653+
assert(mediaSpy.calledOnce)
654+
assert(saveVisitSpy.notCalled)
655+
})
639656
})
640657

641658
describe('onMediaPublisher', function () {
642-
let saveVisitSpy, verifiedPStub
659+
let saveVisitSpy, verifiedPStub, setPublisherSpy
643660

644661
const expectedState = Immutable.fromJS({
645662
cache: {
@@ -668,12 +685,18 @@ describe('ledger api unit tests', function () {
668685
migrations: {}
669686
})
670687

671-
before(function () {
672-
verifiedPStub = sinon.stub(ledgerApi, 'verifiedP', (state, publisherKey, fn) => state)
688+
const response = Immutable.fromJS({
689+
publisher: publisherKey,
690+
faviconName: 'Brave',
691+
faviconURL: 'data:image/jpeg;base64,...',
692+
publisherURL: 'https://brave.com',
693+
providerName: 'Youtube'
673694
})
674695

675-
after(function () {
676-
verifiedPStub.restore()
696+
before(function () {
697+
verifiedPStub = sinon.stub(ledgerApi, 'verifiedP', (state, publisherKey, fn) => state)
698+
saveVisitSpy = sinon.spy(ledgerApi, 'saveVisit')
699+
setPublisherSpy = sinon.spy(ledgerState, 'setPublisher')
677700
})
678701

679702
beforeEach(function () {
@@ -686,33 +709,92 @@ describe('ledger api unit tests', function () {
686709
options: {
687710
exclude: true
688711
},
689-
providerName: 'Youtube'
712+
providerName: 'YouTube'
690713
}
691714
}
692715
})
693-
saveVisitSpy = sinon.spy(ledgerApi, 'saveVisit')
716+
})
717+
718+
after(function () {
719+
verifiedPStub.restore()
720+
saveVisitSpy.restore()
721+
setPublisherSpy.restore()
694722
})
695723

696724
afterEach(function () {
697725
ledgerApi.setSynopsis(undefined)
698-
saveVisitSpy.restore()
726+
verifiedPStub.reset()
727+
saveVisitSpy.reset()
728+
setPublisherSpy.reset()
699729
})
700730

701731
it('null case', function () {
702732
const result = ledgerApi.onMediaPublisher(defaultAppState)
703733
assert.deepEqual(result.toJS(), defaultAppState.toJS())
734+
assert(setPublisherSpy.notCalled)
735+
assert(saveVisitSpy.notCalled)
704736
})
705737

706-
it('create publisher if new and add cache', function () {
707-
const response = Immutable.fromJS({
708-
publisher: publisherKey,
709-
faviconName: 'Brave',
710-
faviconURL: 'data:image/jpeg;base64,...',
711-
publisherURL: 'https://brave.com',
712-
providerName: 'Youtube'
738+
it('we do not have publisher in the synopsis', function () {
739+
ledgerApi.setSynopsis({
740+
initPublisher: () => {
741+
ledgerApi.setSynopsis({
742+
initPublisher: () => {},
743+
addPublisher: () => {},
744+
publishers: {
745+
[publisherKey]: {
746+
exclude: false,
747+
options: {
748+
exclude: true
749+
},
750+
providerName: 'YouTube'
751+
}
752+
}
753+
})
754+
},
755+
addPublisher: () => {},
756+
publishers: { }
713757
})
714758

759+
const newState = Immutable.fromJS({
760+
cache: {
761+
ledgerVideos: {
762+
'youtube_kLiLOkzLetE': {
763+
publisher: 'youtube#channel:UCFNTTISby1c_H-rm5Ww5rZg',
764+
faviconName: 'Brave',
765+
providerName: 'Youtube',
766+
faviconURL: 'data:image/jpeg;base64,...',
767+
publisherURL: 'https://brave.com'
768+
}
769+
}
770+
},
771+
ledger: {
772+
synopsis: {
773+
publishers: {
774+
'youtube#channel:UCFNTTISby1c_H-rm5Ww5rZg': {
775+
options: {
776+
exclude: true
777+
},
778+
faviconName: 'old Brave',
779+
faviconURL: 'data:image/jpeg;base64,...',
780+
publisherURL: 'https://brave.io',
781+
providerName: 'Youtube'
782+
}
783+
}
784+
}
785+
},
786+
migrations: {}
787+
})
788+
789+
const state = ledgerApi.onMediaPublisher(newState, videoId, response, 1000, false)
790+
assert(saveVisitSpy.calledOnce)
791+
assert(setPublisherSpy.calledTwice)
792+
assert.deepEqual(state.toJS(), expectedState.toJS())
793+
})
794+
795+
it('create publisher if new and add cache', function () {
715796
const state = ledgerApi.onMediaPublisher(defaultAppState, videoId, response, 1000, false)
797+
assert(setPublisherSpy.calledTwice)
716798
assert(saveVisitSpy.calledOnce)
717799
assert.deepEqual(state.toJS(), expectedState.toJS())
718800
})
@@ -748,15 +830,8 @@ describe('ledger api unit tests', function () {
748830
migrations: {}
749831
})
750832

751-
const response = Immutable.fromJS({
752-
publisher: publisherKey,
753-
faviconName: 'Brave',
754-
faviconURL: 'data:image/jpeg;base64,...',
755-
publisherURL: 'https://brave.com',
756-
providerName: 'Youtube'
757-
})
758-
759833
const state = ledgerApi.onMediaPublisher(newState, videoId, response, 1000, false)
834+
assert(setPublisherSpy.calledOnce)
760835
assert(saveVisitSpy.calledOnce)
761836
assert.deepEqual(state.toJS(), expectedState.toJS())
762837
})

0 commit comments

Comments
 (0)