@@ -89,13 +89,17 @@ let balanceTimeoutId = false
8989let runTimeoutId
9090let promotionTimeoutId
9191let togglePromotionTimeoutId
92- let verifiedTimeoutId = false
92+ let publisherInfoTimeoutId = false
9393
9494// Database
9595let v2RulesetDB
9696const v2RulesetPath = 'ledger-rulesV2.leveldb'
9797const statePath = 'ledger-state.json'
9898
99+ // Publisher info
100+ const publisherInfoPath = 'publisher-data.json'
101+ const publisherInfoUpdateInterval = ledgerUtil . milliseconds . day * 2
102+
99103// Definitions
100104const clientOptions = {
101105 debugP : process . env . LEDGER_DEBUG ,
@@ -191,7 +195,7 @@ const paymentPresent = (state, tabId, present) => {
191195 appActions . onPromotionGet ( )
192196
193197 state = checkSeed ( state )
194- getPublisherTimestamp ( true )
198+ runPublishersUpdate ( state )
195199 } else if ( balanceTimeoutId ) {
196200 clearTimeout ( balanceTimeoutId )
197201 balanceTimeoutId = false
@@ -226,20 +230,83 @@ const checkSeed = (state) => {
226230 return state
227231}
228232
229- const getPublisherTimestamp = ( updateList ) => {
233+ const getPublisherInfo = ( ) => {
230234 if ( ! client ) {
231235 return
232236 }
233237
234- client . publisherTimestamp ( ( err , result ) => {
238+ client . fetchPublisherInfo ( ( err , result ) => {
235239 if ( err ) {
236- console . error ( 'Error while retrieving publisher timestamp ' , err . toString ( ) )
240+ console . error ( 'Error while retrieving verified publishers ' , err . toString ( ) )
237241 return
238242 }
239- appActions . onPublisherTimestamp ( result . timestamp , updateList )
243+ appActions . onPublishersInfoReceived ( result )
240244 } )
241245}
242246
247+ const checkPublisherInfoUpdate = ( state ) => {
248+ const verifiedPTimestamp = updateState . getUpdateProp ( state , 'verifiedPublishersTimestamp' ) || null
249+
250+ if (
251+ verifiedPTimestamp == null ||
252+ ( ( new Date ( ) . getTime ( ) - verifiedPTimestamp ) >= publisherInfoUpdateInterval )
253+ ) {
254+ if ( publisherInfoTimeoutId ) {
255+ clearTimeout ( publisherInfoTimeoutId )
256+ }
257+
258+ // Startup and boot delay
259+ const delay = ( ! bootP || ! client ) ? ( ledgerUtil . milliseconds . second * 15 ) : 0
260+
261+ publisherInfoTimeoutId = setTimeout ( ( ) => {
262+ module . exports . getPublisherInfo ( )
263+ } , delay )
264+ }
265+ }
266+
267+ const updatePublishersInfo = ( state , publisherKeys , publisherData , updateStamp ) => {
268+ if ( publisherData == null || publisherKeys == null ) {
269+ return state
270+ }
271+
272+ let updateData = [ ]
273+
274+ if ( ! Array . isArray ( publisherKeys ) ) {
275+ publisherKeys = [ publisherKeys ]
276+ }
277+
278+ const publishers = publisherData . filter ( p => publisherKeys . indexOf ( p [ 0 ] ) > - 1 )
279+ publishers . forEach ( ( publisher ) => {
280+ const verified = ! ! publisher [ 1 ]
281+ const publisherKey = publisher [ 0 ]
282+
283+ savePublisherOption ( publisherKey , 'verified' , verified )
284+ savePublisherOption ( publisherKey , 'verifiedTimestamp' , updateStamp )
285+
286+ updateData . push ( {
287+ verified,
288+ publisherKey,
289+ verifiedTimestamp : updateStamp
290+ } )
291+ } )
292+
293+ if ( updateData . length > 0 ) {
294+ appActions . onPublishersOptionUpdate ( updateData )
295+ }
296+
297+ if ( process . env . NODE_ENV === 'test' ) {
298+ [ 'brianbondy.com' , 'clifton.io' ] . forEach ( ( key ) => {
299+ if ( ledgerState . hasPublisher ( state , key ) ) {
300+ state = ledgerState . setPublisherOption ( state , key , 'verified' , true )
301+ savePublisherOption ( key , 'verified' , true )
302+ }
303+ } )
304+ state = updatePublisherInfo ( state )
305+ }
306+
307+ return state
308+ }
309+
243310const addFoundClosed = ( state ) => {
244311 if ( balanceTimeoutId ) {
245312 clearTimeout ( balanceTimeoutId )
@@ -270,8 +337,6 @@ const onBootStateFile = (state) => {
270337 try {
271338 clientprep ( )
272339 client = ledgerClient ( null , underscore . extend ( { roundtrip : module . exports . roundtrip } , clientOptions ) , null )
273-
274- getPublisherTimestamp ( )
275340 } catch ( ex ) {
276341 state = ledgerState . resetInfo ( state )
277342 bootP = false
@@ -620,38 +685,35 @@ const inspectP = (db, path, publisher, property, key, callback) => {
620685 } )
621686}
622687
623- // TODO rename function name
624- const verifiedP = ( state , publisherKey , callback , lastUpdate ) => {
625- const clientCallback = ( err , result ) => {
688+ const runPublishersUpdate = ( state ) => {
689+ const publishers = ledgerState . getPublishers ( state )
690+
691+ if ( publishers . isEmpty ( ) ) {
692+ return
693+ }
694+
695+ module . exports . updatePublishers ( state , Array . from ( publishers . keys ( ) ) )
696+ }
697+
698+ const updatePublishers = ( state , publisherKeys ) => {
699+ const fs = require ( 'fs' )
700+
701+ fs . readFile ( pathName ( publisherInfoPath ) , ( err , data ) => {
626702 if ( err ) {
627- console . error ( ` Error verifying publisher ${ publisherKey } : ` , err . toString ( ) )
703+ console . error ( ' Error: Could not read from publishers file' )
628704 return
629705 }
630706
631- if ( callback ) {
632- if ( result ) {
633- callback ( null , result , lastUpdate )
634- } else {
635- callback ( err , { } )
636- }
637- }
638- }
639-
640- if ( Array . isArray ( publisherKey ) ) {
641- client . publishersInfo ( publisherKey , clientCallback )
642- } else {
643- client . publisherInfo ( publisherKey , clientCallback )
644- }
707+ let result
708+ const updateStamp = new Date ( ) . getTime ( )
645709
646- if ( process . env . NODE_ENV === 'test' ) {
647- [ 'brianbondy.com' , 'clifton.io' ] . forEach ( ( key ) => {
648- if ( ledgerState . hasPublisher ( state , key ) ) {
649- state = ledgerState . setPublisherOption ( state , key , 'verified' , true )
650- savePublisherOption ( publisherKey , 'verified' , true )
651- }
652- } )
653- state = updatePublisherInfo ( state )
654- }
710+ try {
711+ result = JSON . parse ( data )
712+ appActions . onPublishersInfoRead ( publisherKeys , result , updateStamp )
713+ } catch ( err ) {
714+ console . error ( `Error: Could not parse data from publishers file` )
715+ }
716+ } )
655717
656718 return state
657719}
@@ -790,48 +852,7 @@ const saveVisit = (state, publisherKey, options) => {
790852 return state
791853}
792854
793- const onVerifiedPStatus = ( error , result , lastUpdate ) => {
794- if ( error || result == null ) {
795- return
796- }
797-
798- if ( ! Array . isArray ( result ) ) {
799- result = [ result ]
800- }
801-
802- if ( result . length === 0 ) {
803- return
804- }
805-
806- const data = result . reduce ( ( publishers , item ) => {
807- if ( item . err ) {
808- return publishers
809- }
810-
811- const publisherKey = item . publisher
812- let verified = false
813- if ( item && item . properties ) {
814- verified = ! ! item . properties . verified
815- savePublisherOption ( publisherKey , 'verified' , verified )
816- }
817-
818- savePublisherOption ( publisherKey , 'verifiedTimestamp' , lastUpdate )
819-
820- publishers . push ( {
821- publisherKey,
822- verified,
823- verifiedTimestamp : lastUpdate
824- } )
825-
826- return publishers
827- } , [ ] )
828-
829- if ( data && data . length > 0 ) {
830- appActions . onPublishersOptionUpdate ( data )
831- }
832- }
833-
834- const checkVerifiedStatus = ( state , publisherKeys , publisherTimestamp ) => {
855+ const checkVerifiedStatus = ( state , publisherKeys ) => {
835856 if ( publisherKeys == null ) {
836857 return state
837858 }
@@ -840,23 +861,7 @@ const checkVerifiedStatus = (state, publisherKeys, publisherTimestamp) => {
840861 publisherKeys = [ publisherKeys ]
841862 }
842863
843- const lastUpdate = parseInt ( publisherTimestamp || ledgerState . getLedgerValue ( state , 'publisherTimestamp' ) )
844- const checkKeys = publisherKeys . reduce ( ( init , key ) => {
845- const lastPublisherUpdate = parseInt ( ledgerState . getPublisherOption ( state , key , 'verifiedTimestamp' ) || 0 )
846-
847- if ( lastUpdate > lastPublisherUpdate ) {
848- init . push ( key )
849- }
850-
851- return init
852- } , [ ] )
853-
854- if ( checkKeys . length === 0 ) {
855- return state
856- }
857-
858- state = module . exports . verifiedP ( state , checkKeys , onVerifiedPStatus , lastUpdate )
859-
864+ state = module . exports . updatePublishers ( state , publisherKeys )
860865 return state
861866}
862867
@@ -1583,7 +1588,7 @@ const roundtrip = (params, options, callback) => {
15831588 parts = underscore . extend ( underscore . pick ( parts , [ 'protocol' , 'hostname' , 'port' ] ) ,
15841589 underscore . omit ( params , [ 'headers' , 'payload' , 'timeout' ] ) )
15851590
1586- // TBD: let the user configure this via preferences [MTR]
1591+ // TBD: let the user configure this via preferences [MTR]
15871592 if ( params . useProxy ) {
15881593 if ( parts . hostname === 'ledger.brave.com' ) {
15891594 parts . hostname = 'ledger-proxy.privateinternetaccess.com'
@@ -1592,6 +1597,9 @@ const roundtrip = (params, options, callback) => {
15921597 }
15931598 }
15941599
1600+ // Use alternate hostname if it's provided
1601+ parts . hostname = params . altHostname || parts . hostname
1602+
15951603 const i = parts . path . indexOf ( '?' )
15961604 if ( i !== - 1 ) {
15971605 parts . pathname = parts . path . substring ( 0 , i )
@@ -2325,10 +2333,6 @@ const initialize = (state, paymentsEnabled) => {
23252333
23262334 ledgerNotifications . init ( )
23272335
2328- if ( verifiedTimeoutId ) {
2329- clearInterval ( verifiedTimeoutId )
2330- }
2331-
23322336 if ( ! userAgent ) {
23332337 const versionInformation = state . getIn ( [ 'about' , 'brave' , 'versionInformation' ] )
23342338 if ( versionInformation ) {
@@ -2361,13 +2365,20 @@ const initialize = (state, paymentsEnabled) => {
23612365 // Get referral headers every day
23622366 setInterval ( ( ) => fetchReferralHeaders , ( 24 * ledgerUtil . milliseconds . hour ) )
23632367
2368+ if ( paymentsEnabled ) {
2369+ // First run
2370+ module . exports . checkPublisherInfoUpdate ( state )
2371+ // Check again every other day
2372+ setInterval ( ( ) => {
2373+ module . exports . checkPublisherInfoUpdate ( state )
2374+ } , publisherInfoUpdateInterval )
2375+ }
2376+
23642377 if ( ! paymentsEnabled ) {
23652378 client = null
23662379 return ledgerState . resetInfo ( state , true )
23672380 }
23682381
2369- verifiedTimeoutId = setInterval ( getPublisherTimestamp , 1 * ledgerUtil . milliseconds . hour )
2370-
23712382 if ( client ) {
23722383 return state
23732384 }
@@ -2463,7 +2474,7 @@ const onInitRead = (state, parsedData) => {
24632474 underscore . extend ( parsedData . options , { roundtrip : module . exports . roundtrip } , options ) ,
24642475 parsedData )
24652476
2466- getPublisherTimestamp ( true )
2477+ runPublishersUpdate ( state )
24672478
24682479 // Scenario: User enables Payments, disables it, waits 30+ days, then
24692480 // enables it again -> reconcileStamp is in the past.
@@ -3148,17 +3159,26 @@ const onPromotionResponse = (state, status) => {
31483159 return state
31493160}
31503161
3151- const onPublisherTimestamp = ( state , oldTimestamp , newTimestamp ) => {
3152- if ( oldTimestamp === newTimestamp ) {
3153- return
3162+ const onPublishersInfo = ( state , result ) => {
3163+ if ( ! result ) {
3164+ return state
31543165 }
31553166
3156- const publishers = ledgerState . getPublishers ( state )
3157- if ( publishers . isEmpty ( ) ) {
3158- return
3159- }
3167+ const fs = require ( 'fs' )
3168+ const path = pathName ( publisherInfoPath )
31603169
3161- module . exports . checkVerifiedStatus ( state , Array . from ( publishers . keys ( ) ) , newTimestamp )
3170+ fs . writeFile ( path , JSON . stringify ( result ) , ( err ) => {
3171+ if ( err ) {
3172+ console . error ( `Error: Could not write file at ${ path } ` )
3173+ return
3174+ }
3175+ if ( clientOptions . verboseP ) {
3176+ console . log ( `Publishers file written successfully at ${ path } ` )
3177+ }
3178+ appActions . onPublishersInfoWrite ( )
3179+ } )
3180+
3181+ return state
31623182}
31633183
31643184const checkReferralActivity = ( state ) => {
@@ -3301,7 +3321,7 @@ const getMethods = () => {
33013321 setPaymentInfo,
33023322 updatePublisherInfo,
33033323 networkConnected,
3304- verifiedP ,
3324+ updatePublishers ,
33053325 boot,
33063326 onBootStateFile,
33073327 onWalletProperties,
@@ -3333,7 +3353,6 @@ const getMethods = () => {
33333353 getBalance,
33343354 fileRecoveryKeys,
33353355 getPromotion,
3336- onPublisherTimestamp,
33373356 checkVerifiedStatus,
33383357 checkReferralActivity,
33393358 setPublishersOptions,
@@ -3355,7 +3374,12 @@ const getMethods = () => {
33553374 cacheRuleSet,
33563375 disablePayments,
33573376 runPromotionCheck,
3358- onRunPromotionCheck
3377+ onRunPromotionCheck,
3378+ onPublishersInfo,
3379+ getPublisherInfo,
3380+ checkPublisherInfoUpdate,
3381+ updatePublishersInfo,
3382+ runPublishersUpdate
33593383 }
33603384
33613385 let privateMethods = { }
@@ -3396,7 +3420,6 @@ const getMethods = () => {
33963420 onReferralInit,
33973421 roundTripFromWindow,
33983422 onReferralCodeRead,
3399- onVerifiedPStatus,
34003423 onFuzzing,
34013424 checkSeed,
34023425 shouldTrackTab
0 commit comments