-
Notifications
You must be signed in to change notification settings - Fork 972
Update logic for publishers #7439
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,14 +3,15 @@ | |
* 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 {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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do all hostSettings entries have a ledgerPayments field? because comparing !== false will be true if that field is undefined. you might want to change this to something like:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep that's intentional. Ledger only recognizes |
||
: 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 | ||
? <span | ||
data-test-id='publisherButton' | ||
data-test-authorized={this.authorizedPublisher} | ||
data-test-authorized={this.enabledForPaymentsPublisher} | ||
data-test-verified={this.verifiedPublisher} | ||
className={css(styles.addPublisherButtonContainer)}> | ||
<button | ||
className={ | ||
css( | ||
commonStyles.browserButton, | ||
!this.authorizedPublisher && this.verifiedPublisher && styles.noFundVerified, | ||
this.authorizedPublisher && this.verifiedPublisher && styles.fundVerified, | ||
!this.authorizedPublisher && !this.verifiedPublisher && styles.noFundUnverified, | ||
this.authorizedPublisher && !this.verifiedPublisher && styles.fundUnverified | ||
!this.enabledForPaymentsPublisher && this.verifiedPublisher && styles.noFundVerified, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we wanted to make this cleaner, you could do the && for the two conditions above and store in a const, like
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree. Will take this for next refactors and just updated Wiki for future reference. Thanks |
||
this.enabledForPaymentsPublisher && this.verifiedPublisher && styles.fundVerified, | ||
!this.enabledForPaymentsPublisher && !this.verifiedPublisher && styles.noFundUnverified, | ||
this.enabledForPaymentsPublisher && !this.verifiedPublisher && styles.fundUnverified | ||
) | ||
} | ||
data-l10n-id={this.l10nString} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -558,6 +558,15 @@ WindowStore | |
top: number // the top position of the popup window | ||
}, | ||
previewFrameKey: number, | ||
locationInfo: { | ||
[url]: { | ||
publisher: string, // url of the publisher in question | ||
verified: boolean, // wheter or not site is a verified publisher | ||
exclude: boolean, // wheter or not site is in the excluded list | ||
stickyP: boolean, // wheter or not site was added using addFunds urlbar toggle | ||
timestamp: number // timestamp in milliseconds | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. super small nitpick- would be nice to alphabetize these fields. Sublime has this sorting built in; I believe you'd need an add-on for VS Code (cc @NejcZdovc) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we should keep all properties in the |
||
}, | ||
publisherInfo: { | ||
synopsis: [{ | ||
daysSpent: number, // e.g., 1 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason this changes from the regular
.includes()
to a double negate using!!
? The double negate is great for making something is truthy (when you don't care about it's value), but in this caseincludes()
should already be returning a bool