Skip to content

Commit

Permalink
Moving ledger into appState part 2
Browse files Browse the repository at this point in the history
Part of brave#11037 PR
  • Loading branch information
NejcZdovc committed Oct 2, 2017
1 parent d660bd8 commit 3064556
Show file tree
Hide file tree
Showing 32 changed files with 2,929 additions and 2,621 deletions.
2,187 changes: 2,187 additions & 0 deletions app/browser/api/ledger.js

Large diffs are not rendered by default.

180 changes: 137 additions & 43 deletions app/browser/reducers/ledgerReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

const Immutable = require('immutable')
const underscore = require('underscore')

// Constants
const appConstants = require('../../../js/constants/appConstants')
Expand All @@ -12,44 +11,31 @@ const settings = require('../../../js/constants/settings')

// State
const ledgerState = require('../../common/state/ledgerState')
const siteSettingsState = require('../../common/state/siteSettingsState')

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

const ledgerReducer = (state, action, immutableAction) => {
action = immutableAction || makeImmutable(action)
switch (action.get('actionType')) {
case appConstants.APP_UPDATE_LEDGER_INFO:
{
state = state.setIn(['ledger', 'info'], action.get('ledgerInfo'))
break
}
// TODO refactor
case appConstants.APP_UPDATE_LOCATION_INFO:
{
state = state.setIn(['ledger', 'locations'], action.get('locationInfo'))
break
}
case appConstants.APP_LEDGER_RECOVERY_STATUS_CHANGED:
{
state = ledgerState.setRecoveryStatus(state, action.get('recoverySucceeded'))
break
}
case appConstants.APP_SET_STATE:
{
state = ledgerUtil.init(state)
state = ledgerApi.migration(state)
state = ledgerApi.init(state)
break
}
case appConstants.APP_BACKUP_KEYS:
{
ledgerUtil.backupKeys(state, action.get('backupAction'))
ledgerApi.backupKeys(state, action.get('backupAction'))
break
}
case appConstants.APP_RECOVER_WALLET:
{
state = ledgerUtil.recoverKeys(
state = ledgerApi.recoverKeys(
state,
action.get('useRecoveryKeyFile'),
action.get('firstRecoveryKey'),
Expand All @@ -59,7 +45,7 @@ const ledgerReducer = (state, action, immutableAction) => {
}
case appConstants.APP_SHUTTING_DOWN:
{
state = ledgerUtil.quit(state)
state = ledgerApi.quit(state)
break
}
case appConstants.APP_ON_CLEAR_BROWSING_DATA:
Expand All @@ -72,49 +58,51 @@ const ledgerReducer = (state, action, immutableAction) => {
}
break
}
// TODO not sure that we use APP_IDLE_STATE_CHANGED anymore
case appConstants.APP_IDLE_STATE_CHANGED:
{
state = ledgerUtil.pageDataChanged(state)
ledgerUtil.addVisit('NOOP', underscore.now(), null)
state = ledgerApi.pageDataChanged(state)
state = ledgerApi.addVisit(state, 'NOOP', new Date().getTime(), null)
break
}
case appConstants.APP_CHANGE_SETTING:
{
switch (action.get('key')) {
case settings.PAYMENTS_ENABLED:
{
state = ledgerUtil.initialize(state, action.get('value'))
state = ledgerApi.initialize(state, action.get('value'))
break
}
case settings.PAYMENTS_CONTRIBUTION_AMOUNT:
{
ledgerUtil.setPaymentInfo(action.get('value'))
ledgerApi.setPaymentInfo(action.get('value'))
break
}
case settings.PAYMENTS_MINIMUM_VISIT_TIME:
{
const value = action.get('value')
if (value <= 0) break
ledgerUtil.synopsis.options.minPublisherDuration = action.value
ledgerApi.saveOptionSynopsis('minPublisherDuration', value)
state = ledgerState.setSynopsisOption(state, 'minPublisherDuration', value)
state = ledgerApi.updatePublisherInfo(state)
break
}
case settings.PAYMENTS_MINIMUM_VISITS:
{
const value = action.get('value')
if (value <= 0) break

ledgerUtil.synopsis.options.minPublisherVisits = value
ledgerApi.saveOptionSynopsis('minPublisherVisits', value)
state = ledgerState.setSynopsisOption(state, 'minPublisherVisits', value)
state = ledgerApi.updatePublisherInfo(state)
break
}

case settings.PAYMENTS_ALLOW_NON_VERIFIED:
{
const value = action.get('value')
ledgerUtil.synopsis.options.showOnlyVerified = value
ledgerApi.saveOptionSynopsis('showOnlyVerified', value)
state = ledgerState.setSynopsisOption(state, 'showOnlyVerified', value)
state = ledgerApi.updatePublisherInfo(state)
break
}
}
Expand All @@ -135,9 +123,9 @@ const ledgerReducer = (state, action, immutableAction) => {
case 'ledgerPaymentsShown':
{
if (action.get('value') === false) {
delete ledgerUtil.synopsis.publishers[publisherKey]
ledgerApi.deleteSynopsis(publisherKey)
state = ledgerState.deletePublishers(state, publisherKey)
state = ledgerUtil.updatePublisherInfo(state)
state = ledgerApi.updatePublisherInfo(state)
}
break
}
Expand All @@ -147,19 +135,19 @@ const ledgerReducer = (state, action, immutableAction) => {
if (publisher.isEmpty()) {
break
}
state = ledgerUtil.updatePublisherInfo(state)
state = ledgerUtil.verifiedP(state, publisherKey)
state = ledgerApi.updatePublisherInfo(state)
state = ledgerApi.verifiedP(state, publisherKey)
break
}
case 'ledgerPinPercentage':
{
const value = action.get('value')
const publisher = ledgerState.getPublisher(state, publisherKey)
if (publisher.isEmpty()) {
if (publisher.isEmpty() || publisher.get('pinPercentage') === value) {
break
}

ledgerUtil.synopsis.publishers[publisherKey].pinPercentage = action.get('value')
state = ledgerUtil.updatePublisherInfo(state, publisherKey)
state = ledgerState.setPublishersProp(state, publisherKey, 'pinPercentage', value)
state = ledgerApi.updatePublisherInfo(state, publisherKey)
break
}
}
Expand All @@ -182,15 +170,13 @@ const ledgerReducer = (state, action, immutableAction) => {
if (publisher.isEmpty()) {
break
}
state = ledgerUtil.updatePublisherInfo(state)
state = ledgerApi.updatePublisherInfo(state)
}
break
}
case appConstants.APP_NETWORK_CONNECTED:
{
setTimeout((state) => {
ledgerUtil.networkConnected(state)
}, 1000, state)
ledgerApi.networkConnected(state)
break
}
case appConstants.APP_NAVIGATOR_HANDLER_REGISTERED:
Expand All @@ -211,7 +197,115 @@ const ledgerReducer = (state, action, immutableAction) => {
case windowConstants.WINDOW_SET_FOCUSED_FRAME:
case windowConstants.WINDOW_GOT_RESPONSE_DETAILS:
{
state = ledgerUtil.pageDataChanged(state)
state = ledgerApi.pageDataChanged(state)
break
}
case appConstants.APP_ON_FAVICON_RECEIVED:
{
state = ledgerState.setPublishersProp(state, action.get('publisherKey'), 'faviconURL', action.get('blob'))
state = ledgerApi.updatePublisherInfo(state)
break
}
case appConstants.APP_ON_EXCLUSION_STATUS:
{
const key = action.get('publisherKey')
const pattern = urlUtil.getHostPattern(key)
const value = action.get('excluded')
ledgerApi.savePublisherOption(key, 'exclude', value)
state = siteSettingsState.setSettingsProp(state, pattern, 'ledgerPayments', value)
state = ledgerState.setPublishersProp(state, key, ['options', 'exclude'], value)
state = ledgerApi.updatePublisherInfo(state)
break
}
case appConstants.APP_ON_LEDGER_LOCATION_UPDATE:
{
const location = action.get('location')
state = ledgerState.setLocationProp(state, location, action.get('prop'), action.get('value'))
break
}
case appConstants.APP_ON_PUBLISHER_OPTION_UPDATE:
{
const value = action.get('value')
const key = action.get('publisherKey')
const prop = action.get('prop')
state = ledgerState.setPublisherOption(state, key, prop, value)

if (action.get('saveIntoSettings')) {
const pattern = urlUtil.getHostPattern(key)
if (prop === 'exclude') {
state = siteSettingsState.setSettingsProp(state, pattern, 'ledgerPayments', value)
}
}
break
}
case appConstants.APP_ON_LEDGER_WALLET_CREATE:
{
ledgerApi.boot()
break
}
case appConstants.APP_ON_BOOT_STATE_FILE:
{
state = ledgerApi.onBootStateFile(state)
break
}
case appConstants.APP_ON_LEDGER_BALANCE_RECEIVED:
{
state = ledgerApi.balanceReceived(state, action.get('unconfirmed'))
break
}
case appConstants.APP_ON_WALLET_PROPERTIES:
{
state = ledgerApi.onWalletProperties(state, action.get('body'))
break
}
case appConstants.APP_LEDGER_PAYMENTS_PRESENT:
{
ledgerApi.paymentPresent(state, action.get('tabId'), action.get('present'))
break
}
case appConstants.APP_ON_ADD_FUNDS_CLOSED:
{
ledgerApi.addFoundClosed(state)
break
}
case appConstants.APP_ON_WALLET_RECOVERY:
{
state = ledgerApi.onWalletRecovery(state, action.get('error'), action.get('result'))
break
}
case appConstants.APP_ON_BRAVERY_PROPERTIES:
{
state = ledgerApi.onBraveryProperties(state, action.get('error'), action.get('result'))
break
}
case appConstants.APP_ON_FIRST_LEDGER_SYNC:
{
state = ledgerApi.onLedgerFirstSync(state, action.get('parsedData'))
break
}
case appConstants.APP_ON_LEDGER_CALLBACK:
{
state = ledgerApi.onCallback(state, action.get('result'), action.get('delayTime'))
break
}
case appConstants.APP_ON_TIME_UNTIL_RECONCILE:
{
state = ledgerApi.onTimeUntilReconcile(state, action.get('stateResult'))
break
}
case appConstants.APP_ON_LEDGER_RUN:
{
ledgerApi.run(state, action.get('delay'))
break
}
case appConstants.APP_ON_NETWORK_CONNECTED:
{
state = ledgerApi.onNetworkConnected(state)
break
}
case appConstants.APP_ON_RESET_RECOVERY_STATUS:
{
state = ledgerState.setRecoveryStatus(state, null)
break
}
}
Expand Down
5 changes: 3 additions & 2 deletions app/browser/reducers/pageDataReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const pageDataState = require('../../common/state/pageDataState')

// Utils
const {makeImmutable} = require('../../common/state/immutableUtil')
const {isSourceAboutUrl} = require('../../../js/lib/appUrlUtil')
const {isSourceAboutUrl, getTargetAboutUrl} = require('../../../js/lib/appUrlUtil')
const {responseHasContent} = require('../../common/lib/httpUtil')

const pageDataReducer = (state, action, immutableAction) => {
Expand Down Expand Up @@ -68,7 +68,8 @@ const pageDataReducer = (state, action, immutableAction) => {
}

const responseCode = action.getIn(['details', 'httpResponseCode'])
if (isSourceAboutUrl(pageUrl) || !responseHasContent(responseCode)) {
const isNewTab = getTargetAboutUrl('about:new-tab') === pageUrl
if (isSourceAboutUrl(pageUrl) || isNewTab || !responseHasContent(responseCode)) {
break
}

Expand Down
21 changes: 14 additions & 7 deletions app/browser/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const bookmarksState = require('../common/state/bookmarksState')
const bookmarkFoldersState = require('../common/state/bookmarkFoldersState')
const historyState = require('../common/state/historyState')
const bookmarkOrderCache = require('../common/cache/bookmarkOrderCache')
const ledgerState = require('../common/state/ledgerState')
const {getWindow} = require('./windows')

let currentPartitionNumber = 0
Expand Down Expand Up @@ -154,8 +155,8 @@ const updateAboutDetails = (tab, tabValue) => {
let location = getBaseUrl(url)

// TODO(bridiver) - refactor these to use state helpers
const ledgerInfo = appState.get('ledgerInfo')
const publisherInfo = appState.get('publisherInfo')
const ledgerInfo = ledgerState.getInfoProps(appState)
const synopsis = appState.getIn(['ledger', 'about'])
const preferencesData = appState.getIn(['about', 'preferences'])
const appSettings = appState.get('settings')
let allSiteSettings = appState.get('siteSettings')
Expand All @@ -176,17 +177,23 @@ const updateAboutDetails = (tab, tabValue) => {
const autofillAddresses = appState.getIn(['autofill', 'addresses'])
const versionInformation = appState.getIn(['about', 'brave', 'versionInformation'])
const aboutDetails = tabValue.get('aboutDetails')
// TODO(bridiver) - convert this to an action

// TODO save this into values into the sate so that we don't call this app action on every state change
// this should be saved in app state when windows will be refactored #11151
/*
if (url === 'about:preferences#payments') {
tab.on('destroyed', () => {
process.emit(messages.LEDGER_PAYMENTS_PRESENT, tabValue.get('tabId'), false)
appActions.ledgerPaymentsPresent(tabValue.get('tabId'), false)
})
process.emit(messages.LEDGER_PAYMENTS_PRESENT, tabValue.get('tabId'), true)
appActions.ledgerPaymentsPresent(tabValue.get('tabId'), false)
} else {
process.emit(messages.LEDGER_PAYMENTS_PRESENT, tabValue.get('tabId'), false)
appActions.ledgerPaymentsPresent(tabValue.get('tabId'), false)
}
*/
if (location === 'about:preferences' || location === 'about:contributions' || location === aboutUrls.get('about:contributions')) {
const ledgerData = ledgerInfo.merge(publisherInfo).merge(preferencesData)
const ledgerData = ledgerInfo
.merge(synopsis)
.merge(preferencesData)
tab.send(messages.LEDGER_UPDATED, ledgerData.toJS())
tab.send(messages.SETTINGS_UPDATED, appSettings.toJS())
tab.send(messages.SITE_SETTINGS_UPDATED, allSiteSettings.toJS())
Expand Down
Loading

0 comments on commit 3064556

Please sign in to comment.