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

Commit c44ea1e

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

File tree

9 files changed

+551
-87
lines changed

9 files changed

+551
-87
lines changed

app/browser/api/ledger.js

+69-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',
@@ -2087,7 +2091,7 @@ const onReferralInit = (err, response, body) => {
20872091
}
20882092

20892093
if (body && body.download_id) {
2090-
appActions.onReferralCodeRead(body.download_id, body.referral_code)
2094+
appActions.onReferralCodeRead(body)
20912095
promoCodeFirstRunStorage
20922096
.removePromoCode()
20932097
.catch(error => {
@@ -2103,6 +2107,60 @@ const onReferralInit = (err, response, body) => {
21032107
}
21042108
}
21052109

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+
21062164
const initialize = (state, paymentsEnabled) => {
21072165
let fs
21082166

@@ -2138,9 +2196,15 @@ const initialize = (state, paymentsEnabled) => {
21382196
if (clientOptions.verboseP) {
21392197
console.error('read error: ' + error.toString())
21402198
}
2199+
fetchReferralHeaders()
21412200
})
2201+
} else {
2202+
fetchReferralHeaders()
21422203
}
21432204

2205+
// Get referral headers every day
2206+
setInterval(() => fetchReferralHeaders, (24 * ledgerUtil.milliseconds.hour))
2207+
21442208
if (!paymentsEnabled) {
21452209
client = null
21462210
return ledgerState.resetInfo(state, true)
@@ -2984,7 +3048,9 @@ const getMethods = () => {
29843048
checkReferralActivity,
29853049
setPublishersOptions,
29863050
referralCheck,
2987-
roundtrip
3051+
roundtrip,
3052+
onFetchReferralHeaders,
3053+
onReferralRead
29883054
}
29893055

29903056
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)