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

Commit 98ddc15

Browse files
committed
Adds promo
Auditors: Test Plan:
1 parent ca517c8 commit 98ddc15

File tree

9 files changed

+557
-97
lines changed

9 files changed

+557
-97
lines changed

app/browser/api/ledger.js

+70-3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ const ledgerVideoCache = require('../../common/cache/ledgerVideoCache')
5050
const updater = require('../../updater')
5151
const promoCodeFirstRunStorage = require('../../promoCodeFirstRunStorage')
5252
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')
5357

5458
// Caching
5559
let locationDefault = 'NOOP'
@@ -99,7 +103,7 @@ const clientOptions = {
99103
environment: process.env.LEDGER_ENVIRONMENT || 'production'
100104
}
101105

102-
var platforms = {
106+
const platforms = {
103107
'darwin': 'osx',
104108
'win32x64': 'winx64',
105109
'win32ia32': 'winia32',
@@ -2059,6 +2063,7 @@ const onCallback = (state, result, delayTime) => {
20592063

20602064
const onReferralCodeRead = (code) => {
20612065
if (!code) {
2066+
fetchReferralHeaders()
20622067
return
20632068
}
20642069

@@ -2087,7 +2092,7 @@ const onReferralInit = (err, response, body) => {
20872092
}
20882093

20892094
if (body && body.download_id) {
2090-
appActions.onReferralCodeRead(body.download_id, body.referral_code)
2095+
appActions.onReferralCodeRead(body)
20912096
promoCodeFirstRunStorage
20922097
.removePromoCode()
20932098
.catch(error => {
@@ -2103,6 +2108,60 @@ const onReferralInit = (err, response, body) => {
21032108
}
21042109
}
21052110

2111+
const onReferralRead = (state, body, activeWindowId) => {
2112+
body = makeImmutable(body)
2113+
2114+
if (body.has('offer_page_url')) {
2115+
const url = body.get('offer_page_url')
2116+
if (urlutil.isURL(url)) {
2117+
if (activeWindowId === windowState.WINDOW_ID_NONE) {
2118+
state = updateState.setUpdateProp(state, 'referralPage', url)
2119+
} else {
2120+
appActions.createTabRequested({
2121+
url,
2122+
windowId: activeWindowId
2123+
})
2124+
state = updateState.setUpdateProp(state, 'referralPage', null)
2125+
}
2126+
}
2127+
}
2128+
2129+
if (body.has('headers')) {
2130+
const headers = body.get('headers')
2131+
state = updateState.setUpdateProp(state, 'referralHeaders', headers)
2132+
siteHacks.setReferralHeaders(headers)
2133+
}
2134+
2135+
state = updateState.setUpdateProp(state, 'referralDownloadId', body.get('download_id'))
2136+
state = updateState.setUpdateProp(state, 'referralPromoCode', body.get('referral_code'))
2137+
2138+
return state
2139+
}
2140+
2141+
const fetchReferralHeaders = () => {
2142+
module.exports.roundtrip({
2143+
server: referralServer,
2144+
method: 'GET',
2145+
path: '/promo/custom-headers'
2146+
}, {}, appActions.onFetchReferralHeaders)
2147+
}
2148+
2149+
const onFetchReferralHeaders = (state, err, response, body) => {
2150+
if (err) {
2151+
if (clientOptions.verboseP) {
2152+
console.error(makeJS(err))
2153+
}
2154+
return state
2155+
}
2156+
2157+
if (body && isList(body)) {
2158+
state = updateState.setUpdateProp(state, 'referralHeaders', body)
2159+
siteHacks.setReferralHeaders(body)
2160+
}
2161+
2162+
return state
2163+
}
2164+
21062165
const initialize = (state, paymentsEnabled) => {
21072166
let fs
21082167

@@ -2138,9 +2197,15 @@ const initialize = (state, paymentsEnabled) => {
21382197
if (clientOptions.verboseP) {
21392198
console.error('read error: ' + error.toString())
21402199
}
2200+
fetchReferralHeaders()
21412201
})
2202+
} else {
2203+
fetchReferralHeaders()
21422204
}
21432205

2206+
// Get referral headers every day
2207+
setInterval(() => fetchReferralHeaders, (24 * ledgerUtil.milliseconds.hour))
2208+
21442209
if (!paymentsEnabled) {
21452210
client = null
21462211
return ledgerState.resetInfo(state, true)
@@ -2984,7 +3049,9 @@ const getMethods = () => {
29843049
checkReferralActivity,
29853050
setPublishersOptions,
29863051
referralCheck,
2987-
roundtrip
3052+
roundtrip,
3053+
onFetchReferralHeaders,
3054+
onReferralRead
29883055
}
29893056

29903057
let privateMethods = {}

app/browser/reducers/ledgerReducer.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const pageDataState = require('../../common/state/pageDataState')
1818
const updateState = require('../../common/state/updateState')
1919

2020
// Utils
21+
const windows = require('../windows')
2122
const ledgerApi = require('../../browser/api/ledger')
2223
const ledgerNotifications = require('../../browser/api/ledgerNotifications')
2324
const {makeImmutable} = require('../../common/state/immutableUtil')
@@ -460,8 +461,7 @@ const ledgerReducer = (state, action, immutableAction) => {
460461
}
461462
case appConstants.APP_ON_REFERRAL_CODE_READ:
462463
{
463-
state = updateState.setUpdateProp(state, 'referralDownloadId', action.get('downloadId'))
464-
state = updateState.setUpdateProp(state, 'referralPromoCode', action.get('promoCode'))
464+
state = ledgerApi.onReferralRead(state, action.get('body'), windows.getActiveWindowId())
465465
break
466466
}
467467
case appConstants.APP_ON_REFERRAL_CODE_FAIL:
@@ -474,6 +474,11 @@ const ledgerReducer = (state, action, immutableAction) => {
474474
state = ledgerApi.checkReferralActivity(state)
475475
break
476476
}
477+
case appConstants.APP_ON_FETCH_REFERRAL_HEADERS:
478+
{
479+
state = ledgerApi.onFetchReferralHeaders(state, action.get('error'), action.get('response'), action.get('body'))
480+
break
481+
}
477482
case appConstants.APP_ON_REFERRAL_ACTIVITY:
478483
{
479484
state = updateState.setUpdateProp(state, 'referralTimestamp', new Date().getTime())

app/browser/reducers/tabsReducer.js

+29-10
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,34 @@
44

55
'use strict'
66

7-
const appConfig = require('../../../js/constants/appConfig')
8-
const appConstants = require('../../../js/constants/appConstants')
9-
const tabs = require('../tabs')
10-
const windows = require('../windows')
11-
const {getWebContents} = require('../webContentsCache')
127
const {BrowserWindow} = require('electron')
8+
const Immutable = require('immutable')
9+
10+
// Actions
11+
const windowActions = require('../../../js/actions/windowActions')
12+
13+
// State
1314
const tabState = require('../../common/state/tabState')
1415
const siteSettings = require('../../../js/state/siteSettings')
1516
const siteSettingsState = require('../../common/state/siteSettingsState')
17+
const {frameOptsFromFrame} = require('../../../js/state/frameStateUtil')
18+
const updateState = require('../../common/state/updateState')
19+
20+
// Constants
21+
const appConfig = require('../../../js/constants/appConfig')
22+
const appConstants = require('../../../js/constants/appConstants')
1623
const windowConstants = require('../../../js/constants/windowConstants')
17-
const windowActions = require('../../../js/actions/windowActions')
24+
const dragTypes = require('../../../js/constants/dragTypes')
25+
const tabActionConsts = require('../../common/constants/tabAction')
26+
27+
// Utils
28+
const tabs = require('../tabs')
29+
const windows = require('../windows')
30+
const {getWebContents} = require('../webContentsCache')
1831
const {makeImmutable} = require('../../common/state/immutableUtil')
1932
const {getFlashResourceId} = require('../../../js/flash')
2033
const {l10nErrorText} = require('../../common/lib/httpUtil')
21-
const Immutable = require('immutable')
22-
const dragTypes = require('../../../js/constants/dragTypes')
23-
const tabActionConsts = require('../../common/constants/tabAction')
2434
const flash = require('../../../js/flash')
25-
const {frameOptsFromFrame} = require('../../../js/state/frameStateUtil')
2635
const {isSourceAboutUrl, isTargetAboutUrl, isNavigatableAboutPage} = require('../../../js/lib/appUrlUtil')
2736
const {shouldDebugTabEvents} = require('../../cmdLine')
2837

@@ -396,6 +405,16 @@ const tabsReducer = (state, action, immutableAction) => {
396405
// We only need to run welcome screen once
397406
state = state.setIn(['about', 'welcome', 'showOnLoad'], false)
398407
}
408+
409+
// Show promotion
410+
const page = updateState.getUpdateProp(state, 'referralPage') || null
411+
if (page) {
412+
setImmediate(() => tabs.create({
413+
url: page,
414+
windowId
415+
}))
416+
state = updateState.setUpdateProp(state, 'referralPage', null)
417+
}
399418
}
400419
break
401420
}

0 commit comments

Comments
 (0)