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

Add notification to suggest users use Brave Payments #4141

Merged
merged 2 commits into from
Sep 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/extensions/brave/locales/en-US/app.properties
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,12 @@ notificationPasswordWithUserName=Would you like Brave to remember the password f
notificationPassword=Would you like Brave to remember your password on {{origin}}?
notificationPasswordSettings=[Password settings]
notificationPaymentDone=Your contribution of {{amount}} {{currency}} has been processed. Thanks for supporting your favorite websites!
notificationTryPayments=Are you ready to support the sites you use most?
notificationTryPaymentsYes=Sure, I'll try
prefsRestart=Do you want to restart now?
yes=Yes
no=No
noThanks=No thanks
neverForThisSite=Never for this site
browserHistory=Browser history
downloadHistory=Download history
Expand Down
73 changes: 57 additions & 16 deletions app/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const underscore = require('underscore')
const uuid = require('node-uuid')

const appActions = require('../js/actions/appActions')
const appConfig = require('../js/constants/appConfig')
const appConstants = require('../js/constants/appConstants')
const appDispatcher = require('../js/dispatcher/appDispatcher')
const messages = require('../js/constants/messages')
Expand Down Expand Up @@ -100,6 +101,7 @@ const msecs = { year: 365 * 24 * 60 * 60 * 1000,
let addFundsMessage
let reconciliationMessage
let notificationPaymentDoneMessage
let notificationTryPaymentsMessage
let suppressNotifications = false
let reconciliationNotificationShown = false
let notificationTimeout = null
Expand Down Expand Up @@ -252,6 +254,13 @@ if (ipc) {
setTimeout(() => { reconciliationNotificationShown = false }, 1 * msecs.day)
} else if (message === notificationPaymentDoneMessage) {
appActions.hideMessageBox(message)
} else if (message === notificationTryPaymentsMessage) {
appActions.hideMessageBox(message)
if (buttonIndex === 1 && win) {
win.webContents.send(messages.SHORTCUT_NEW_FRAME,
'about:preferences#payments', { singleFrame: true })
}
appActions.changeSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED, true)
}
})

Expand Down Expand Up @@ -383,6 +392,12 @@ eventStore.addChangeListener(() => {
var initialize = (onoff) => {
enable(onoff)

// Check if relevant browser notifications should be shown every 15 minutes
if (notificationTimeout) {
clearInterval(notificationTimeout)
}
notificationTimeout = setInterval(showNotifications, 15 * msecs.minute)

if (!onoff) {
client = null
return appActions.updateLedgerInfo({})
Expand Down Expand Up @@ -430,12 +445,12 @@ var initialize = (onoff) => {
}

var enable = (onoff) => {
if (onoff && !getSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED)) {
appActions.changeSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED, true)
}

if (!onoff) {
synopsis = null
if (notificationTimeout) {
clearInterval(notificationTimeout)
notificationTimeout = null
}
return updatePublisherInfo()
}

Expand Down Expand Up @@ -475,9 +490,6 @@ var enable = (onoff) => {
})
updatePublisherInfo()

// Check if relevant browser notifications should be shown every 15 minutes
notificationTimeout = setInterval(showNotifications, 15 * msecs.minute)

fs.readFile(pathName(publisherPath), (err, data) => {
if (err) {
if (err.code !== 'ENOENT') console.log('publisherPath read error: ' + err.toString())
Expand Down Expand Up @@ -1290,17 +1302,46 @@ var pathName = (name) => {
* UI controller functionality
*/

/**
* Show message that it's time to add funds if reconciliation is less than
* a day in the future and balance is too low.
* 24 hours prior to reconciliation, show message asking user to review
* their votes.
*/
const showNotifications = () => {
if (!getSetting(settings.PAYMENTS_ENABLED) ||
!getSetting(settings.PAYMENTS_NOTIFICATIONS) || suppressNotifications) {
return
if (getSetting(settings.PAYMENTS_ENABLED) &&
getSetting(settings.PAYMENTS_NOTIFICATIONS) &&
!suppressNotifications) {
showEnabledNotifications()
} else if (!getSetting(settings.PAYMENTS_ENABLED)) {
showDisabledNotifications()
}
}

// When Payments is disabled
const showDisabledNotifications = () => {
if (!getSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED)) {
const firstRunTimestamp = appStore.getState().get('firstRunTimestamp')
if (new Date().getTime() - firstRunTimestamp < appConfig.payments.delayNotificationTryPayments) {
return
}
notificationTryPaymentsMessage = locale.translation('notificationTryPayments')
appActions.showMessageBox({
greeting: locale.translation('updateHello'),
message: notificationTryPaymentsMessage,
buttons: [
{text: locale.translation('noThanks')},
{text: locale.translation('notificationTryPaymentsYes'), className: 'primary'}
],
options: {
style: 'greetingStyle',
persist: false
}
})
}
}

/**
* Show message that it's time to add funds if reconciliation is less than
* a day in the future and balance is too low.
* 24 hours prior to reconciliation, show message asking user to review
* their votes.
*/
const showEnabledNotifications = () => {
const reconcileStamp = ledgerInfo.reconcileStamp
const balance = Number(ledgerInfo.balance || 0)
const unconfirmed = Number(ledgerInfo.unconfirmed || 0)
Expand Down
3 changes: 3 additions & 0 deletions app/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,12 @@ var rendererIdentifiers = function () {
'notificationPassword',
'notificationPasswordSettings',
'notificationPaymentDone',
'notificationTryPayments',
'notificationTryPaymentsYes',
'prefsRestart',
'yes',
'no',
'noThanks',
'neverForThisSite',
'passwordsManager',
'downloadItemPause',
Expand Down
3 changes: 3 additions & 0 deletions app/sessionStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ module.exports.loadAppState = () => {
module.exports.cleanAppData(data, false)
}
data = Object.assign(module.exports.defaultAppState(), data)
if (!data.firstRunTimestamp) {
data.firstRunTimestamp = new Date().getTime()
}
data.cleanedOnShutdown = false
// Always recalculate the update status
if (data.updates) {
Expand Down
1 change: 1 addition & 0 deletions docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ AppStore

```javascript
{
firstRunTimestamp: integer,
extensions: {
[id]: { // the unique id of the extension
id: string,
Expand Down
4 changes: 4 additions & 0 deletions js/constants/appConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ module.exports = {
crashes: {
crashSubmitUrl: crashURL
},
payments: {
delayNotificationTryPayments: 1000 * 60 * 60 * 24 * 10 // 10 days (from firstRunTimestamp)
},
updates: {
// Check for front end updates every hour
appUpdateCheckFrequency: 1000 * 60 * 60,
Expand Down Expand Up @@ -103,6 +106,7 @@ module.exports = {
'bookmarks.toolbar.showOnlyFavicon': false,
'payments.enabled': false,
'payments.notifications': false,
'payments.notificationTryPaymentsDismissed': false, // True if you dismiss the message or enable Payments
'payments.contribution-amount': 5, // USD
'privacy.autofill-enabled': true,
'privacy.do-not-track': false,
Expand Down
1 change: 1 addition & 0 deletions js/constants/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const settings = {
// Payments Tab
PAYMENTS_ENABLED: 'payments.enabled',
PAYMENTS_NOTIFICATIONS: 'payments.notifications',
PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED: 'payments.notificationTryPaymentsDismissed',
PAYMENTS_CONTRIBUTION_AMOUNT: 'payments.contribution-amount',
// Advanced settings
HARDWARE_ACCELERATION_ENABLED: 'advanced.hardware-acceleration-enabled',
Expand Down
41 changes: 41 additions & 0 deletions test/app/sessionStoreTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,45 @@ describe('sessionStore', function () {
yield Brave.app.client.waitForExist(navigatorBookmarked)
})
})

describe('firstRunTimestamp', function () {
Brave.beforeAllServerSetup(this)
before(function * () {
// AY: Why do I need to start, stop and start for this to work properly?
yield Brave.startApp()
yield setup(Brave.app.client)
yield Brave.stopApp(false)
yield Brave.startApp()
yield setup(Brave.app.client)
})

after(function * () {
yield Brave.stopApp()
})

it('sets it once', function * () {
const timestamp = new Date().getTime()
let firstRunTimestamp
yield Brave.app.client
.waitUntil(function () {
return this.getAppState().then((val) => {
firstRunTimestamp = val.value.firstRunTimestamp
return (
firstRunTimestamp > (timestamp - 30 * 1000) &&
firstRunTimestamp <= timestamp
)
})
})

yield Brave.stopApp(false)
yield Brave.startApp()
yield setup(Brave.app.client)
yield Brave.app.client
.waitUntil(function () {
return this.getAppState().then((val) => {
return (val.value.firstRunTimestamp === firstRunTimestamp)
})
})
})
})
})