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

Commit b30f016

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

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
@@ -2394,7 +2394,7 @@ const onMediaRequest = (state, xhr, type, tabId) => {
23942394
if (!cache.isEmpty()) {
23952395
const publisherKey = cache.get('publisher')
23962396
const publisher = ledgerState.getPublisher(state, publisherKey)
2397-
if (!publisher.isEmpty()) {
2397+
if (!publisher.isEmpty() && publisher.has('providerName')) {
23982398
return module.exports.saveVisit(state, publisherKey, {
23992399
duration,
24002400
revisited,
@@ -2443,19 +2443,13 @@ const onMediaPublisher = (state, mediaKey, response, duration, revisited) => {
24432443
const publisherURL = response.get('publisherURL')
24442444
const providerName = response.get('providerName')
24452445

2446-
if (publisher.isEmpty()) {
2447-
revisited = false
2446+
if (!synopsis.publishers[publisherKey] || publisher.isEmpty()) {
24482447
synopsis.initPublisher(publisherKey)
24492448

24502449
if (!synopsis.publishers[publisherKey]) {
24512450
return state
24522451
}
24532452

2454-
synopsis.publishers[publisherKey].faviconName = faviconName
2455-
synopsis.publishers[publisherKey].faviconURL = faviconURL
2456-
synopsis.publishers[publisherKey].publisherURL = publisherURL
2457-
synopsis.publishers[publisherKey].providerName = providerName
2458-
24592453
state = ledgerState.setPublisher(state, publisherKey, synopsis.publishers[publisherKey])
24602454

24612455
if (!getSetting(settings.PAYMENTS_SITES_AUTO_SUGGEST)) {
@@ -2467,15 +2461,19 @@ const onMediaPublisher = (state, mediaKey, response, duration, revisited) => {
24672461
savePublisherOption(publisherKey, 'exclude', exclude)
24682462
})
24692463
}
2470-
} else {
2471-
synopsis.publishers[publisherKey].faviconName = faviconName
2472-
synopsis.publishers[publisherKey].faviconURL = faviconURL
2473-
synopsis.publishers[publisherKey].publisherURL = publisherURL
2474-
synopsis.publishers[publisherKey].providerName = providerName
2475-
state = ledgerState.setPublishersProp(state, publisherKey, 'faviconName', faviconName)
2476-
state = ledgerState.setPublishersProp(state, publisherKey, 'faviconURL', faviconURL)
2477-
state = ledgerState.setPublishersProp(state, publisherKey, 'publisherURL', publisherURL)
2478-
state = ledgerState.setPublishersProp(state, publisherKey, 'providerName', providerName)
2464+
}
2465+
2466+
synopsis.publishers[publisherKey].faviconName = faviconName
2467+
synopsis.publishers[publisherKey].faviconURL = faviconURL
2468+
synopsis.publishers[publisherKey].publisherURL = publisherURL
2469+
synopsis.publishers[publisherKey].providerName = providerName
2470+
state = ledgerState.setPublishersProp(state, publisherKey, 'faviconName', faviconName)
2471+
state = ledgerState.setPublishersProp(state, publisherKey, 'faviconURL', faviconURL)
2472+
state = ledgerState.setPublishersProp(state, publisherKey, 'publisherURL', publisherURL)
2473+
state = ledgerState.setPublishersProp(state, publisherKey, 'providerName', providerName)
2474+
2475+
if (publisher.isEmpty()) {
2476+
revisited = false
24792477
}
24802478

24812479
const cacheObject = Immutable.Map()

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

+99-24
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,8 @@ describe('ledger api unit tests', function () {
591591
}))
592592
.setIn(['ledger', 'synopsis', 'publishers', publisherKey], Immutable.fromJS({
593593
visits: 1,
594-
duration: 1000
594+
duration: 1000,
595+
providerName: 'YouTube'
595596
}))
596597

597598
beforeEach(function () {
@@ -698,10 +699,26 @@ describe('ledger api unit tests', function () {
698699
ignoreMinTime: true
699700
}).calledOnce)
700701
})
702+
703+
it('state for this publisher is corrupted, so we need to fetch it again', function () {
704+
const badState = defaultAppState
705+
.setIn(['cache', 'ledgerVideos', videoId], Immutable.fromJS({
706+
publisher: publisherKey
707+
}))
708+
.setIn(['ledger', 'synopsis', 'publishers', publisherKey], Immutable.fromJS({
709+
options: {
710+
excluded: false
711+
}
712+
}))
713+
714+
ledgerApi.onMediaRequest(badState, xhr, ledgerMediaProviders.YOUTUBE, 10)
715+
assert(mediaSpy.calledOnce)
716+
assert(saveVisitSpy.notCalled)
717+
})
701718
})
702719

703720
describe('onMediaPublisher', function () {
704-
let saveVisitSpy, verifiedPStub
721+
let saveVisitSpy, verifiedPStub, setPublisherSpy
705722

706723
const expectedState = Immutable.fromJS({
707724
cache: {
@@ -730,12 +747,18 @@ describe('ledger api unit tests', function () {
730747
migrations: {}
731748
})
732749

733-
before(function () {
734-
verifiedPStub = sinon.stub(ledgerApi, 'verifiedP', (state, publisherKey, fn) => state)
750+
const response = Immutable.fromJS({
751+
publisher: publisherKey,
752+
faviconName: 'Brave',
753+
faviconURL: 'data:image/jpeg;base64,...',
754+
publisherURL: 'https://brave.com',
755+
providerName: 'Youtube'
735756
})
736757

737-
after(function () {
738-
verifiedPStub.restore()
758+
before(function () {
759+
verifiedPStub = sinon.stub(ledgerApi, 'verifiedP', (state, publisherKey, fn) => state)
760+
saveVisitSpy = sinon.spy(ledgerApi, 'saveVisit')
761+
setPublisherSpy = sinon.spy(ledgerState, 'setPublisher')
739762
})
740763

741764
beforeEach(function () {
@@ -748,33 +771,92 @@ describe('ledger api unit tests', function () {
748771
options: {
749772
exclude: true
750773
},
751-
providerName: 'Youtube'
774+
providerName: 'YouTube'
752775
}
753776
}
754777
})
755-
saveVisitSpy = sinon.spy(ledgerApi, 'saveVisit')
778+
})
779+
780+
after(function () {
781+
verifiedPStub.restore()
782+
saveVisitSpy.restore()
783+
setPublisherSpy.restore()
756784
})
757785

758786
afterEach(function () {
759787
ledgerApi.setSynopsis(undefined)
760-
saveVisitSpy.restore()
788+
verifiedPStub.reset()
789+
saveVisitSpy.reset()
790+
setPublisherSpy.reset()
761791
})
762792

763793
it('null case', function () {
764794
const result = ledgerApi.onMediaPublisher(defaultAppState)
765795
assert.deepEqual(result.toJS(), defaultAppState.toJS())
796+
assert(setPublisherSpy.notCalled)
797+
assert(saveVisitSpy.notCalled)
766798
})
767799

768-
it('create publisher if new and add cache', function () {
769-
const response = Immutable.fromJS({
770-
publisher: publisherKey,
771-
faviconName: 'Brave',
772-
faviconURL: 'data:image/jpeg;base64,...',
773-
publisherURL: 'https://brave.com',
774-
providerName: 'Youtube'
800+
it('we do not have publisher in the synopsis', function () {
801+
ledgerApi.setSynopsis({
802+
initPublisher: () => {
803+
ledgerApi.setSynopsis({
804+
initPublisher: () => {},
805+
addPublisher: () => {},
806+
publishers: {
807+
[publisherKey]: {
808+
exclude: false,
809+
options: {
810+
exclude: true
811+
},
812+
providerName: 'YouTube'
813+
}
814+
}
815+
})
816+
},
817+
addPublisher: () => {},
818+
publishers: { }
775819
})
776820

821+
const newState = Immutable.fromJS({
822+
cache: {
823+
ledgerVideos: {
824+
'youtube_kLiLOkzLetE': {
825+
publisher: 'youtube#channel:UCFNTTISby1c_H-rm5Ww5rZg',
826+
faviconName: 'Brave',
827+
providerName: 'Youtube',
828+
faviconURL: 'data:image/jpeg;base64,...',
829+
publisherURL: 'https://brave.com'
830+
}
831+
}
832+
},
833+
ledger: {
834+
synopsis: {
835+
publishers: {
836+
'youtube#channel:UCFNTTISby1c_H-rm5Ww5rZg': {
837+
options: {
838+
exclude: true
839+
},
840+
faviconName: 'old Brave',
841+
faviconURL: 'data:image/jpeg;base64,...',
842+
publisherURL: 'https://brave.io',
843+
providerName: 'Youtube'
844+
}
845+
}
846+
}
847+
},
848+
migrations: {}
849+
})
850+
851+
const state = ledgerApi.onMediaPublisher(newState, videoId, response, 1000, false)
852+
assert(saveVisitSpy.calledOnce)
853+
assert(setPublisherSpy.calledTwice)
854+
assert.deepEqual(state.toJS(), expectedState.toJS())
855+
})
856+
857+
it('create publisher if new and add cache', function () {
777858
const state = ledgerApi.onMediaPublisher(defaultAppState, videoId, response, 1000, false)
859+
assert(setPublisherSpy.calledTwice)
778860
assert(saveVisitSpy.calledOnce)
779861
assert.deepEqual(state.toJS(), expectedState.toJS())
780862
})
@@ -810,15 +892,8 @@ describe('ledger api unit tests', function () {
810892
migrations: {}
811893
})
812894

813-
const response = Immutable.fromJS({
814-
publisher: publisherKey,
815-
faviconName: 'Brave',
816-
faviconURL: 'data:image/jpeg;base64,...',
817-
publisherURL: 'https://brave.com',
818-
providerName: 'Youtube'
819-
})
820-
821895
const state = ledgerApi.onMediaPublisher(newState, videoId, response, 1000, false)
896+
assert(setPublisherSpy.calledOnce)
822897
assert(saveVisitSpy.calledOnce)
823898
assert.deepEqual(state.toJS(), expectedState.toJS())
824899
})

0 commit comments

Comments
 (0)