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

Commit

Permalink
Merge pull request #11874 from NejcZdovc/feature/#11100-ugp
Browse files Browse the repository at this point in the history
Adds UGP support
  • Loading branch information
bsclifton authored Nov 28, 2017
2 parents 9da47f4 + 7f6103b commit 0147755
Show file tree
Hide file tree
Showing 24 changed files with 2,373 additions and 473 deletions.
203 changes: 154 additions & 49 deletions app/browser/api/ledger.js

Large diffs are not rendered by default.

147 changes: 122 additions & 25 deletions app/browser/api/ledgerNotifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,9 @@ const appActions = require('../../../js/actions/appActions')

// Utils
const locale = require('../../locale')
const ledgerUtil = require('../../common/lib/ledgerUtil')
const getSetting = require('../../../js/settings').getSetting

const miliseconds = {
year: 365 * 24 * 60 * 60 * 1000,
week: 7 * 24 * 60 * 60 * 1000,
day: 24 * 60 * 60 * 1000,
hour: 60 * 60 * 1000,
minute: 60 * 1000,
second: 1000
}

const text = {
hello: locale.translation('updateHello'),
paymentDone: undefined,
Expand All @@ -39,13 +31,13 @@ const text = {
walletConvertedToBat: locale.translation('walletConvertedToBat')
}

const pollingInterval = 15 * miliseconds.minute // 15 * minutes
const pollingInterval = 15 * ledgerUtil.milliseconds.minute // 15 * minutes
let intervalTimeout
const displayOptions = {
style: 'greetingStyle',
persist: false
}
const nextAddFundsTime = 3 * miliseconds.day
const nextAddFundsTime = 3 * ledgerUtil.milliseconds.day

const sufficientBalanceToReconcile = (state) => {
const balance = Number(ledgerState.getInfoProp(state, 'balance') || 0)
Expand All @@ -68,14 +60,14 @@ const shouldShowNotificationAddFunds = () => {
return !nextTime || (new Date().getTime() > nextTime)
}

const init = (state) => {
const init = () => {
// Check if relevant browser notifications should be shown every 15 minutes
if (intervalTimeout) {
clearInterval(intervalTimeout)
}
intervalTimeout = setInterval((state) => {
module.exports.onInterval(state)
}, pollingInterval, state)
intervalTimeout = setInterval(() => {
appActions.onLedgerNotificationInterval()
}, pollingInterval)
}

const onLaunch = (state) => {
Expand Down Expand Up @@ -114,11 +106,34 @@ const onLaunch = (state) => {
const onInterval = (state) => {
if (getSetting(settings.PAYMENTS_ENABLED)) {
if (getSetting(settings.PAYMENTS_NOTIFICATIONS)) {
showEnabledNotifications(state)
module.exports.showEnabledNotifications(state)
}
} else {
showDisabledNotifications(state)
module.exports.showDisabledNotifications(state)
}

if (getSetting(settings.PAYMENTS_NOTIFICATIONS)) {
state = module.exports.onIntervalDynamic(state)
}

return state
}

const onIntervalDynamic = (state) => {
const promotion = ledgerState.getPromotion(state)
const time = new Date().getTime()

if (promotion.isEmpty()) {
return state
}

const timestamp = promotion.get('remindTimestamp')
if (timestamp && timestamp !== -1 && time > timestamp) {
state = ledgerState.setPromotionProp(state, 'remindTimestamp', -1)
module.exports.showPromotionNotification(state)
}

return state
}

const onResponse = (message, buttonIndex, activeWindow) => {
Expand All @@ -139,7 +154,7 @@ const onResponse = (message, buttonIndex, activeWindow) => {
break

case text.reconciliation:
// buttonIndex === 1 is Dismiss
// buttonIndex === 1 is Dismiss
if (buttonIndex === 0) {
appActions.changeSetting(settings.PAYMENTS_NOTIFICATIONS, false)
} else if (buttonIndex === 2 && activeWindow) {
Expand Down Expand Up @@ -183,6 +198,32 @@ const onResponse = (message, buttonIndex, activeWindow) => {
appActions.hideNotification(message)
}

const onDynamicResponse = (message, actionId, activeWindow) => {
if (!message) {
return
}

switch (actionId) {
case 'optInPromotion':
{
if (activeWindow) {
appActions.createTabRequested({
url: 'about:preferences#payments',
windowId: activeWindow.id
})
}
break
}
case 'remindLater':
{
appActions.onPromotionRemind()
break
}
}

appActions.hideNotification(message)
}

/**
* Show message that it's time to add funds if reconciliation is less than
* a day in the future and balance is too low.
Expand All @@ -195,18 +236,18 @@ const showEnabledNotifications = (state) => {
return
}

if (reconcileStamp - new Date().getTime() < miliseconds.day) {
if (reconcileStamp - new Date().getTime() < ledgerUtil.milliseconds.day) {
if (sufficientBalanceToReconcile(state)) {
if (shouldShowNotificationReviewPublishers()) {
const reconcileFrequency = ledgerState.getInfoProp(state, 'reconcileFrequency')
showReviewPublishers(reconcileStamp + ((reconcileFrequency - 2) * miliseconds.day))
showReviewPublishers(reconcileStamp + ((reconcileFrequency - 2) * ledgerUtil.milliseconds.day))
}
} else if (shouldShowNotificationAddFunds()) {
showAddFunds()
}
} else if (reconcileStamp - new Date().getTime() < 2 * miliseconds.day) {
} else if (reconcileStamp - new Date().getTime() < 2 * ledgerUtil.milliseconds.day) {
if (sufficientBalanceToReconcile(state) && (shouldShowNotificationReviewPublishers())) {
showReviewPublishers(new Date().getTime() + miliseconds.day)
showReviewPublishers(new Date().getTime() + ledgerUtil.milliseconds.day)
}
}
}
Expand Down Expand Up @@ -307,8 +348,57 @@ const showBraveWalletUpdated = () => {
})
}

const onPromotionReceived = (state) => {
const promotion = ledgerState.getPromotionNotification(state)

if (!promotion.isEmpty() && !promotion.has('firstShowTimestamp')) {
state = ledgerState.setPromotionNotificationProp(state, 'firstShowTimestamp', new Date().getTime())
showPromotionNotification(state)
}

return state
}

const showPromotionNotification = (state) => {
const notification = ledgerState.getPromotionNotification(state)

if (
notification.isEmpty() ||
(
getSetting(settings.PAYMENTS_ENABLED) &&
!getSetting(settings.PAYMENTS_NOTIFICATIONS)
)
) {
return
}

const data = notification.toJS()
data.from = 'ledger'

appActions.showNotification(data)
}

const removePromotionNotification = (state) => {
const notification = ledgerState.getPromotionNotification(state)

if (notification.isEmpty()) {
return
}

appActions.hideNotification(notification.get('message'))
}

if (ipc) {
ipc.on(messages.NOTIFICATION_RESPONSE, (e, message, buttonIndex) => {
ipc.on(messages.NOTIFICATION_RESPONSE, (e, message, buttonIndex, checkbox, index, buttonActionId) => {
if (buttonActionId) {
onDynamicResponse(
message,
buttonActionId,
electron.BrowserWindow.getActiveWindow()
)
return
}

onResponse(
message,
buttonIndex,
Expand All @@ -323,7 +413,13 @@ const getMethods = () => {
init,
onLaunch,
showBraveWalletUpdated,
onInterval
onInterval,
onPromotionReceived,
removePromotionNotification,
showDisabledNotifications,
showEnabledNotifications,
onIntervalDynamic,
showPromotionNotification
}

let privateMethods = {}
Expand All @@ -338,7 +434,8 @@ const getMethods = () => {
},
getPollingInterval: () => {
return pollingInterval
}
},
onDynamicResponse
}
}

Expand Down
37 changes: 37 additions & 0 deletions app/browser/reducers/ledgerReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const migrationState = require('../../common/state/migrationState')

// Utils
const ledgerApi = require('../../browser/api/ledger')
const ledgerNotifications = require('../../browser/api/ledgerNotifications')
const {makeImmutable} = require('../../common/state/immutableUtil')
const getSetting = require('../../../js/settings').getSetting

Expand Down Expand Up @@ -404,6 +405,22 @@ const ledgerReducer = (state, action, immutableAction) => {
state = ledgerState.setLedgerValue(state, 'publisherTimestamp', action.get('timestamp'))
break
}
case appConstants.APP_SAVE_LEDGER_PROMOTION:
{
state = ledgerState.savePromotion(state, action.get('promotion'))
state = ledgerNotifications.onPromotionReceived(state)
break
}
case appConstants.APP_ON_PROMOTION_CLAIM:
{
ledgerApi.claimPromotion(state)
break
}
case appConstants.APP_ON_PROMOTION_REMIND:
{
state = ledgerState.remindMeLater(state)
break
}
case appConstants.APP_ON_LEDGER_MEDIA_DATA:
{
state = ledgerApi.onMediaRequest(state, action.get('url'), action.get('type'), action.get('tabId'))
Expand All @@ -420,6 +437,26 @@ const ledgerReducer = (state, action, immutableAction) => {
state = ledgerState.saveSynopsis(state, publishers)
break
}
case appConstants.APP_ON_PROMOTION_RESPONSE:
{
state = ledgerApi.onPromotionResponse(state)
break
}
case appConstants.APP_ON_PROMOTION_REMOVAL:
{
state = ledgerState.removePromotion(state)
break
}
case appConstants.APP_ON_LEDGER_NOTIFICATION_INTERVAL:
{
state = ledgerNotifications.onInterval(state)
break
}
case appConstants.APP_ON_PROMOTION_GET:
{
ledgerApi.getPromotion(state)
break
}
case appConstants.APP_ON_LEDGER_MEDIA_PUBLISHER:
{
state = ledgerApi.onMediaPublisher(
Expand Down
1 change: 1 addition & 0 deletions app/browser/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ const updateAboutDetails = (tabId) => {
.merge(preferencesData)
.set('wizardData', wizardData)
.set('migration', migration)
.set('promotion', ledgerState.getAboutPromotion(appState))
sendAboutDetails(tabId, messages.LEDGER_UPDATED, ledgerData)
} else if (url === 'about:preferences#sync' || location === 'about:contributions' || onPaymentsPage) {
const sync = appState.get('sync', Immutable.Map())
Expand Down
12 changes: 11 additions & 1 deletion app/common/lib/ledgerUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,15 @@ const getMediaProvider = (url) => {
return provider
}

const milliseconds = {
year: 365 * 24 * 60 * 60 * 1000,
week: 7 * 24 * 60 * 60 * 1000,
day: 24 * 60 * 60 * 1000,
hour: 60 * 60 * 1000,
minute: 60 * 1000,
second: 1000
}

const getMethods = () => {
const publicMethods = {
shouldTrackView,
Expand All @@ -309,7 +318,8 @@ const getMethods = () => {
getMediaDuration,
getMediaProvider,
getMediaData,
getMediaKey
getMediaKey,
milliseconds
}

let privateMethods = {}
Expand Down
Loading

0 comments on commit 0147755

Please sign in to comment.