@@ -50,6 +50,10 @@ const ledgerVideoCache = require('../../common/cache/ledgerVideoCache')
50
50
const updater = require ( '../../updater' )
51
51
const promoCodeFirstRunStorage = require ( '../../promoCodeFirstRunStorage' )
52
52
const appUrlUtil = require ( '../../../js/lib/appUrlUtil' )
53
+ const urlutil = require ( '../../../js/lib/urlutil' )
54
+ const windowState = require ( '../../common/state/windowState' )
55
+ const { makeImmutable, makeJS, isList} = require ( '../../common/state/immutableUtil' )
56
+ const siteHacks = require ( '../../siteHacks' )
53
57
54
58
// Caching
55
59
let locationDefault = 'NOOP'
@@ -99,7 +103,7 @@ const clientOptions = {
99
103
environment : process . env . LEDGER_ENVIRONMENT || 'production'
100
104
}
101
105
102
- var platforms = {
106
+ const platforms = {
103
107
'darwin' : 'osx' ,
104
108
'win32x64' : 'winx64' ,
105
109
'win32ia32' : 'winia32' ,
@@ -2087,7 +2091,7 @@ const onReferralInit = (err, response, body) => {
2087
2091
}
2088
2092
2089
2093
if ( body && body . download_id ) {
2090
- appActions . onReferralCodeRead ( body . download_id , body . referral_code )
2094
+ appActions . onReferralCodeRead ( body )
2091
2095
promoCodeFirstRunStorage
2092
2096
. removePromoCode ( )
2093
2097
. catch ( error => {
@@ -2103,6 +2107,60 @@ const onReferralInit = (err, response, body) => {
2103
2107
}
2104
2108
}
2105
2109
2110
+ const onReferralRead = ( state , body , activeWindowId ) => {
2111
+ body = makeImmutable ( body )
2112
+
2113
+ if ( body . has ( 'offer_page_url' ) ) {
2114
+ const url = body . get ( 'offer_page_url' )
2115
+ if ( urlutil . isURL ( url ) ) {
2116
+ if ( activeWindowId === windowState . WINDOW_ID_NONE ) {
2117
+ state = updateState . setUpdateProp ( state , 'referralPage' , url )
2118
+ } else {
2119
+ appActions . createTabRequested ( {
2120
+ url,
2121
+ windowId : activeWindowId
2122
+ } )
2123
+ state = updateState . setUpdateProp ( state , 'referralPage' , null )
2124
+ }
2125
+ }
2126
+ }
2127
+
2128
+ if ( body . has ( 'headers' ) ) {
2129
+ const headers = body . get ( 'headers' )
2130
+ state = updateState . setUpdateProp ( state , 'referralHeaders' , headers )
2131
+ siteHacks . setReferralHeaders ( headers )
2132
+ }
2133
+
2134
+ state = updateState . setUpdateProp ( state , 'referralDownloadId' , body . get ( 'download_id' ) )
2135
+ state = updateState . setUpdateProp ( state , 'referralPromoCode' , body . get ( 'referral_code' ) )
2136
+
2137
+ return state
2138
+ }
2139
+
2140
+ const fetchReferralHeaders = ( ) => {
2141
+ module . exports . roundtrip ( {
2142
+ server : referralServer ,
2143
+ method : 'GET' ,
2144
+ path : '/promo/custom-headers'
2145
+ } , { } , appActions . onFetchReferralHeaders )
2146
+ }
2147
+
2148
+ const onFetchReferralHeaders = ( state , err , response , body ) => {
2149
+ if ( err ) {
2150
+ if ( clientOptions . verboseP ) {
2151
+ console . error ( makeJS ( err ) )
2152
+ }
2153
+ return state
2154
+ }
2155
+
2156
+ if ( body && isList ( body ) ) {
2157
+ state = updateState . setUpdateProp ( state , 'referralHeaders' , body )
2158
+ siteHacks . setReferralHeaders ( body )
2159
+ }
2160
+
2161
+ return state
2162
+ }
2163
+
2106
2164
const initialize = ( state , paymentsEnabled ) => {
2107
2165
let fs
2108
2166
@@ -2138,9 +2196,15 @@ const initialize = (state, paymentsEnabled) => {
2138
2196
if ( clientOptions . verboseP ) {
2139
2197
console . error ( 'read error: ' + error . toString ( ) )
2140
2198
}
2199
+ fetchReferralHeaders ( )
2141
2200
} )
2201
+ } else {
2202
+ fetchReferralHeaders ( )
2142
2203
}
2143
2204
2205
+ // Get referral headers every day
2206
+ setInterval ( ( ) => fetchReferralHeaders , ( 24 * ledgerUtil . milliseconds . hour ) )
2207
+
2144
2208
if ( ! paymentsEnabled ) {
2145
2209
client = null
2146
2210
return ledgerState . resetInfo ( state , true )
@@ -2984,7 +3048,9 @@ const getMethods = () => {
2984
3048
checkReferralActivity,
2985
3049
setPublishersOptions,
2986
3050
referralCheck,
2987
- roundtrip
3051
+ roundtrip,
3052
+ onFetchReferralHeaders,
3053
+ onReferralRead
2988
3054
}
2989
3055
2990
3056
let privateMethods = { }
0 commit comments