From 69ed3638268d1a6a2503417f0ad2a01196691146 Mon Sep 17 00:00:00 2001 From: Cezar Augusto Date: Thu, 2 Mar 2017 00:34:40 -0300 Subject: [PATCH] Update logic for publishers Auditors: @mrose17, @bsclifton Fix #7089 Fix #7429 --- app/renderer/components/publisherToggle.js | 93 ++++++------- js/components/main.js | 1 + js/components/navigationBar.js | 35 +++-- js/lib/urlutil.js | 18 +++ test/unit/app/renderer/publisherToggleTest.js | 125 ++++++++++++------ 5 files changed, 169 insertions(+), 103 deletions(-) diff --git a/app/renderer/components/publisherToggle.js b/app/renderer/components/publisherToggle.js index ea598b48456..29886e61765 100644 --- a/app/renderer/components/publisherToggle.js +++ b/app/renderer/components/publisherToggle.js @@ -3,7 +3,6 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ const React = require('react') -const tldjs = require('tldjs') const ImmutableComponent = require('../../../js/components/immutableComponent') const appActions = require('../../../js/actions/appActions') const settings = require('../../../js/constants/settings') @@ -11,6 +10,8 @@ const getSetting = require('../../../js/settings').getSetting const {StyleSheet, css} = require('aphrodite') const globalStyles = require('./styles/global') const commonStyles = require('./styles/commonStyles') +const {getHostPattern, isHttpOrHttps} = require('../../../js/lib/urlutil') +const {getBaseUrl} = require('../../../js/lib/appUrlUtil') const noFundVerifiedPublisherImage = require('../../extensions/brave/img/urlbar/browser_URL_fund_no_verified.svg') const fundVerifiedPublisherImage = require('../../extensions/brave/img/urlbar/browser_URL_fund_yes_verified.svg') @@ -23,97 +24,89 @@ class PublisherToggle extends ImmutableComponent { this.onAuthorizePublisher = this.onAuthorizePublisher.bind(this) } - get publisherId () { -// @cezaraugusto: publisherIds aren't limited to a domain name -// when this runs, do you think that app/extensions/brave/content/scripts/pageInformation.js has already run? - - return tldjs.getDomain(this.props.url) + get locationId () { + return getBaseUrl(this.props.location) } - get hostPattern () { - return `https?://${this.publisherId}` + get publisherId () { + return this.props.locationInfo.getIn([this.locationId, 'publisher']) } get hostSettings () { - // hostPattern defines it's own identifier for authorized publishers - // sites that do not match criteria would populate siteSettings - // with their default protocol, not hostPattern - return this.props.hostSettings.get(this.hostPattern) + const hostPattern = getHostPattern(this.publisherId) + return this.props.siteSettings.get(hostPattern) } get validPublisherSynopsis () { - // If session is clear then siteSettings is undefined and icon will never be shown, - // but synopsis may not be empty. In such cases let's check if synopsis matches current publisherId - return this.props.synopsis.map(entry => entry.get('site')).includes(this.publisherId) + // If session is clear then siteSettings is undefined and icon + // will never be shown, but synopsis may not be empty. + // In such cases let's check if synopsis matches current publisherId + return !!this.props.synopsis.map(entry => entry.get('site')).includes(this.publisherId) } - get authorizedPublisher () { - // If we can't get ledgerPayments, then it's likely that we are - // on a clean session. Let's then check for publisher's synopsis + get enabledForPaymentsPublisher () { + // All publishers will be enabled by default if AUTO_SUGGEST is ON, + // excluding publishers defined on ledger's exclusion list + const excluded = this.props.locationInfo.getIn([this.locationId, 'exclude']) + const autoSuggestSites = getSetting(settings.AUTO_SUGGEST_SITES) + + // hostSettings is undefined until user hit addFunds button. + // For such cases check autoSuggestSites for eligibility. return this.hostSettings ? this.hostSettings.get('ledgerPayments') !== false - : this.validPublisherSynopsis + : this.validPublisherSynopsis || (autoSuggestSites && !excluded) } get verifiedPublisher () { -// @cezaraugusto: should call `verifiedP` in ledger.js and return the 2nd parameter of the callback - - let verifiedPublisher - this.props.synopsis.map(publisher => { - if (publisher.get('site') === this.publisherId && publisher.get('verified') === true) { - verifiedPublisher = !!publisher - return false - } - return true - }) - return verifiedPublisher + return this.props.locationInfo.getIn([this.locationId, 'verified']) } get visiblePublisher () { - // ledgerPaymentsShown is undefined by default until user decide to permanently hide the publisher + // ledgerPaymentsShown is undefined by default until + // user decide to permanently hide the publisher, // so for icon to be shown it can be everything but false const ledgerPaymentsShown = this.hostSettings && this.hostSettings.get('ledgerPaymentsShown') - return ledgerPaymentsShown !== false + return typeof ledgerPaymentsShown === 'boolean' + ? ledgerPaymentsShown + : true } get shouldShowAddPublisherButton () { - if ((!!this.hostSettings || !!this.validPublisherSynopsis) && this.visiblePublisher) { - // Only show publisher icon if ledger is enabled - return getSetting(settings.PAYMENTS_ENABLED) - } - return false + return getSetting(settings.PAYMENTS_ENABLED) && + isHttpOrHttps(this.props.location) && + this.visiblePublisher } get l10nString () { - if (this.verifiedPublisher && !this.authorizedPublisher) { - return 'verifiedPublisher' - } else if (this.authorizedPublisher) { - return 'enabledPublisher' + let l10nData = 'disabledPublisher' + if (this.verifiedPublisher && !this.enabledForPaymentsPublisher) { + l10nData = 'verifiedPublisher' + } else if (this.enabledForPaymentsPublisher) { + l10nData = 'enabledPublisher' } - return 'disabledPublisher' + return l10nData } onAuthorizePublisher () { - this.authorizedPublisher - ? appActions.changeSiteSetting(this.hostPattern, 'ledgerPayments', false) - : appActions.changeSiteSetting(this.hostPattern, 'ledgerPayments', true) + const hostPattern = getHostPattern(this.publisherId) + appActions.changeSiteSetting(hostPattern, 'ledgerPayments', !this.enabledForPaymentsPublisher) } render () { return this.shouldShowAddPublisherButton ?