From ac6d8b4d079bab10a69571ff6c15edb67aad9bf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CCezar=20Augusto=E2=80=9C?= Date: Fri, 13 Jan 2017 05:17:31 -0200 Subject: [PATCH] Add auto-suggest sites option Close #6261 Auditors: @mrose17, @bsclifton --- .../brave/locales/en-US/app.properties | 2 + .../locales/en-US/preferences.properties | 4 +- app/ledger.js | 6 +- app/renderer/components/publisherToggle.js | 96 ++++++++++++++++ app/renderer/components/urlBar.js | 5 +- js/about/preferences.js | 19 ++- js/components/main.js | 2 + js/components/navigationBar.js | 12 +- js/constants/appConfig.js | 1 + js/constants/settings.js | 1 + less/navigationBar.less | 3 +- test/unit/app/renderer/publisherToggleTest.js | 108 ++++++++++++++++++ 12 files changed, 252 insertions(+), 7 deletions(-) create mode 100644 app/renderer/components/publisherToggle.js create mode 100644 test/unit/app/renderer/publisherToggleTest.js diff --git a/app/extensions/brave/locales/en-US/app.properties b/app/extensions/brave/locales/en-US/app.properties index de5e0370aa4..3071e13f17b 100644 --- a/app/extensions/brave/locales/en-US/app.properties +++ b/app/extensions/brave/locales/en-US/app.properties @@ -55,6 +55,8 @@ reloadButton.title=Reload page homeButton.title=Open Homepage stopButton.title=Stop loading noScriptButton.title=Allow scripts +enablePublisher.title=Enable publisher for payments +disablePublisher.title=Disable publisher for payments braveMenu.title=Open Brave Shields Panel muteTab=Mute tab unmuteTab=Unmute tab diff --git a/app/extensions/brave/locales/en-US/preferences.properties b/app/extensions/brave/locales/en-US/preferences.properties index 4efd53726cc..5701db3f706 100644 --- a/app/extensions/brave/locales/en-US/preferences.properties +++ b/app/extensions/brave/locales/en-US/preferences.properties @@ -21,6 +21,7 @@ paymentsWelcomeText5=Note: Brave Payments uses a country-lookup service in order paymentsWelcomeText6=Need more info? paymentsWelcomeText7=for Brave Payments… paymentsWelcomeLink=View the FAQ +paymentsFAQLink.title=View the FAQ paymentsSidebarText1=Our Partners paymentsSidebarText2=All transaction IP addresses are anonymized with technology from: paymentsSidebarText3=Brave Bitcoin Wallets are provided through a partnership with: @@ -52,7 +53,7 @@ viewPaymentHistory=View Payment History… paymentHistoryTitle=Your Payment History paymentHistoryFooterText=Your next payment contribution is {{reconcileDate}}. paymentHistoryOKText=OK -hideExcluded=hide excluded sites +hideExcluded=Only show included sites bravePayments=Brave Payments beta=beta contributionDate=Contribution Date @@ -85,6 +86,7 @@ off=off on=on ok=Ok minimumPercentage=Hide sites with less than 1% usage +autoSuggestSites=auto-suggest sites notifications=Show notifications moneyAdd=Use your debit/credit card moneyAddSubTitle=No Bitcoin needed! diff --git a/app/ledger.js b/app/ledger.js index a7bc54ec716..18142e1ade6 100644 --- a/app/ledger.js +++ b/app/ledger.js @@ -519,7 +519,7 @@ eventStore.addChangeListener(() => { // NB: in theory we have already seen every element in info except for (perhaps) the last one... underscore.rest(info, info.length - 1).forEach((page) => { - var entry, faviconURL, publisher, siteSetting + var entry, faviconURL, pattern, publisher, siteSetting var location = page.url if ((location.match(/^about/)) || ((locations[location]) && (locations[location].publisher))) return @@ -540,6 +540,10 @@ eventStore.addChangeListener(() => { if (!page.publisher) return publisher = page.publisher + pattern = `https?://${publisher}` + if ((!synopsis.publishers[publisher]) && (!getSetting(settings.AUTO_SUGGEST_SITES))) { + appActions.changeSiteSetting(pattern, 'ledgerPayments', false) + } synopsis.initPublisher(publisher) entry = synopsis.publishers[publisher] if ((page.protocol) && (!entry.protocol)) entry.protocol = page.protocol diff --git a/app/renderer/components/publisherToggle.js b/app/renderer/components/publisherToggle.js new file mode 100644 index 00000000000..ee37326117f --- /dev/null +++ b/app/renderer/components/publisherToggle.js @@ -0,0 +1,96 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * 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') +const getSetting = require('../../../js/settings').getSetting +const cx = require('../../../js/lib/classSet') +const Button = require('../../../js/components/button') + +class PublisherToggle extends ImmutableComponent { + constructor () { + super() + this.onAuthorizePublisher = this.onAuthorizePublisher.bind(this) + } + + get domain () { + return tldjs.getDomain(this.props.url) + } + + get hostPattern () { + return `https?://${this.domain}` + } + + 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) + } + + 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 domain + return this.props.synopsis.map(entry => entry.get('site')).includes(this.domain) + } + + get enabledPublisher () { + // 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 + return this.hostSettings + ? this.hostSettings.get('ledgerPayments') !== false + : this.validPublisherSynopsis + } + + get visiblePublisher () { + // 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 === 'undefined' || ledgerPaymentsShown !== false + } + + get shouldShowAddPublisherButton () { + if ((!!this.hostSettings || !!this.validPublisherSynopsis) && this.visiblePublisher) { + // Only show publisher icon if autoSuggest option is OFF + return !getSetting(settings.AUTO_SUGGEST_SITES) + } + return false + } + + onAuthorizePublisher () { + // if payments disabled, enable it + if (!getSetting(settings.AUTO_SUGGEST_SITES)) { + appActions.changeSetting(settings.PAYMENTS_ENABLED, true) + } + + this.enabledPublisher + ? appActions.changeSiteSetting(this.hostPattern, 'ledgerPayments', false) + : appActions.changeSiteSetting(this.hostPattern, 'ledgerPayments', true) + } + + render () { + return this.shouldShowAddPublisherButton + ? +