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

Payments features 1.0 #7642

Merged
merged 5 commits into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 14 additions & 0 deletions app/extensions/brave/img/ledger/icon_pin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions app/extensions/brave/img/ledger/icon_remove.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion app/extensions/brave/locales/en-US/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ done=Done
off=off
on=on
ok=Ok
minimumPercentage=Hide sites with less than 1% usage
autoSuggestSites=auto-include
notifications=Show notifications
moneyAdd=Use your debit/credit card
Expand Down Expand Up @@ -152,6 +151,7 @@ site=Site
views=Views
timeSpent=Time Spent
include=Include
actions=Actions
percentage=%
remove=Remove
bravery=Bravery
Expand Down Expand Up @@ -358,3 +358,5 @@ useTorrentViewer=Enable Torrent Viewer *
dashboardSettingsTitle=Dashboard
dashboardShowImages=Show images
requiresRestart=* Requires browser restart
showAll=Show all
hideLower=Hide lower
184 changes: 117 additions & 67 deletions app/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,6 @@ const doAction = (action) => {
updatePublisherInfo()
break

case settings.MINIMUM_PERCENTAGE:
updatePublisherInfo()
break

default:
break
}
Expand All @@ -213,6 +209,10 @@ const doAction = (action) => {
if (publisherInfo._internal.verboseP) console.log('\nupdating ' + publisher + ' stickyP=' + action.value)
updatePublisherInfo()
verifiedP(publisher)
} else if (action.key === 'ledgerPinPercentage') {
if (!synopsis.publishers[publisher]) break
synopsis.publishers[publisher].pinPercentage = action.value
updatePublisherInfo()
}
break

Expand Down Expand Up @@ -1000,7 +1000,7 @@ var stickyP = (publisher) => {
delete synopsis.publishers[publisher].options.stickyP
}

return (result || false)
return (result === undefined || result)
}

var eligibleP = (publisher) => {
Expand All @@ -1022,92 +1022,135 @@ var contributeP = (publisher) => {
}

var synopsisNormalizer = () => {
var i, duration, minP, n, pct, publisher, results, total
var data = []
var scorekeeper = synopsis.options.scorekeeper
// courtesy of https://stackoverflow.com/questions/13483430/how-to-make-rounded-percentages-add-up-to-100#13485888
const roundToTarget = (l, target, property) => {
let off = target - underscore.reduce(l, (acc, x) => { return acc + Math.round(x[property]) }, 0)

results = []
underscore.keys(synopsis.publishers).forEach((publisher) => {
if (!visibleP(publisher)) return
return underscore.sortBy(l, (x) => Math.round(x[property]) - x[property])
.map((x, i) => {
x[property] = Math.round(x[property]) + (off > i) - (i >= (l.length + off))
return x
})
}

results.push(underscore.extend({ publisher: publisher }, underscore.omit(synopsis.publishers[publisher], 'window')))
}, synopsis)
results = underscore.sortBy(results, (entry) => { return -entry.scores[scorekeeper] })
n = results.length

total = 0
for (i = 0; i < n; i++) { total += results[i].scores[scorekeeper] }
if (total === 0) return data

pct = []
for (i = 0; i < n; i++) {
publisher = synopsis.publishers[results[i].publisher]
duration = results[i].duration

data[i] = {
rank: i + 1,
verified: results[i].options.verified || false,
site: results[i].publisher,
views: results[i].visits,
const normalizePinned = (dataPinned, total, target) => dataPinned.map((publisher) => {
const floatNumber = (publisher.pinPercentage / total) * target
let newPer = Math.floor(floatNumber)
if (newPer < 1) {
newPer = 1
}

publisher.weight = floatNumber
publisher.pinPercentage = newPer
return publisher
})

const getPublisherData = (result) => {
let duration = result.duration

let data = {
verified: result.options.verified || false,
site: result.publisher,
views: result.visits,
duration: duration,
daysSpent: 0,
hoursSpent: 0,
minutesSpent: 0,
secondsSpent: 0,
faviconURL: publisher.faviconURL,
score: results[i].scores[scorekeeper]
faviconURL: result.faviconURL,
score: result.scores[scorekeeper],
pinPercentage: result.pinPercentage,
weight: result.pinPercentage
}
// HACK: Protocol is sometimes blank here, so default to http:// so we can
// still generate publisherURL.
data[i].publisherURL = (results[i].protocol || 'http:') + '//' + results[i].publisher

pct[i] = Math.round((results[i].scores[scorekeeper] * 100) / total)
data.publisherURL = (result.protocol || 'http:') + '//' + result.publisher

if (duration >= msecs.day) {
data[i].daysSpent = Math.max(Math.round(duration / msecs.day), 1)
data.daysSpent = Math.max(Math.round(duration / msecs.day), 1)
} else if (duration >= msecs.hour) {
data[i].hoursSpent = Math.max(Math.floor(duration / msecs.hour), 1)
data[i].minutesSpent = Math.round((duration % msecs.hour) / msecs.minute)
data.hoursSpent = Math.max(Math.floor(duration / msecs.hour), 1)
data.minutesSpent = Math.round((duration % msecs.hour) / msecs.minute)
} else if (duration >= msecs.minute) {
data[i].minutesSpent = Math.max(Math.round(duration / msecs.minute), 1)
data[i].secondsSpent = Math.round((duration % msecs.minute) / msecs.second)
data.minutesSpent = Math.max(Math.round(duration / msecs.minute), 1)
data.secondsSpent = Math.round((duration % msecs.minute) / msecs.second)
} else {
data[i].secondsSpent = Math.max(Math.round(duration / msecs.second), 1)
data.secondsSpent = Math.max(Math.round(duration / msecs.second), 1)
}

return data
}

// courtesy of https://stackoverflow.com/questions/13483430/how-to-make-rounded-percentages-add-up-to-100#13485888
var foo = (l, target) => {
var off = target - underscore.reduce(l, (acc, x) => { return acc + Math.round(x) }, 0)
let results
let dataPinned = []
let dataUnPinned = []
let dataExcluded = []
let pinnedTotal = 0
let unPinnedTotal = 0
const scorekeeper = synopsis.options.scorekeeper

return underscore.chain(l)
.sortBy((x) => { return Math.round(x) - x })
.map((x, i) => { return Math.round(x) + (off > i) - (i >= (l.length + off)) })
.value()
}
results = []
underscore.keys(synopsis.publishers).forEach((publisher) => {
if (!visibleP(publisher)) return

minP = getSetting(settings.MINIMUM_PERCENTAGE)
pct = foo(pct, 100)
total = 0
for (i = 0; i < n; i++) {
if (pct[i] < 0) pct[i] = 0
if ((minP) && (pct[i] < 1)) {
data = data.slice(0, i)
break
results.push(underscore.extend({ publisher: publisher }, underscore.omit(synopsis.publishers[publisher], 'window')))
}, synopsis)
results = underscore.sortBy(results, (entry) => { return -entry.scores[scorekeeper] })

// move publisher to the correct array and get totals
results.forEach((result) => {
if (result.pinPercentage && result.pinPercentage > 0) {
// pinned
pinnedTotal += result.pinPercentage
dataPinned.push(getPublisherData(result))
} else if (stickyP(result.publisher)) {
// unpinned
unPinnedTotal += result.scores[scorekeeper]
dataUnPinned.push(result)
} else {
// excluded
let publisher = getPublisherData(result)
publisher.percentage = 0
publisher.weight = 0
dataExcluded.push(publisher)
}
})

data[i].percentage = pct[i]
total += pct[i]
}
// round if over 100% of pinned publishers
if (pinnedTotal > 100) {
dataPinned = roundToTarget(normalizePinned(dataPinned, pinnedTotal, 100), 100, 'pinPercentage')
dataUnPinned = dataUnPinned.map((result) => {
let publisher = getPublisherData(result)
publisher.percentage = 0
publisher.weight = 0
return publisher
})

for (i = data.length - 1; (total > 100) && (i >= 0); i--) {
if (data[i].percentage < 2) continue
// sync app store
appActions.changeLedgerPinnedPercentages(dataPinned)
} else {
// unpinned publishers
dataUnPinned = dataUnPinned.map((result) => {
let publisher = getPublisherData(result)
const floatNumber = (publisher.score / unPinnedTotal) * (100 - pinnedTotal)
publisher.percentage = Math.round(floatNumber)
publisher.weight = floatNumber
return publisher
})

data[i].percentage--
total--
// normalize unpinned values
dataUnPinned = roundToTarget(dataUnPinned, (100 - pinnedTotal), 'percentage')
}

return data
const newData = dataPinned.concat(dataUnPinned, dataExcluded)

// sync synopsis
newData.forEach((item) => {
synopsis.publishers[item.site].weight = item.weight
synopsis.publishers[item.site].pinPercentage = item.pinPercentage
})

return newData
}

/*
Expand Down Expand Up @@ -1649,9 +1692,16 @@ var run = (delayTime) => {

if ((typeof delayTime === 'undefined') || (!client)) return

var active, state
var active, state, weights, winners
var ballots = client.ballots()
var winners = ((synopsis) && (ballots > 0) && (synopsis.winners(ballots))) || []
var data = (synopsis) && (ballots > 0) && synopsisNormalizer()

if (data) {
weights = []
data.forEach((datum) => { weights.push({ publisher: datum.site, weight: datum.weight / 100.0 }) })
winners = synopsis.winners(ballots, weights)
}
if (!winners) winners = []

try {
winners.forEach((winner) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,6 @@ class AdvancedSettingsContent extends ImmutableComponent {
<SettingsList className={css(commonStyles.noMarginBottom)}
listClassName={css(styles.list)}
>
<SettingCheckbox
dataL10nId='minimumPercentage'
prefKey={settings.MINIMUM_PERCENTAGE}
settings={this.props.settings}
onChangeSetting={this.props.onChangeSetting}
className={css(styles.listItem)}
switchClassName={css(styles.checkboxSwitch)}
labelClassName={css(commonStyles.noMarginBottom)}
/>
<SettingCheckbox
dataL10nId='notifications'
prefKey={settings.PAYMENTS_NOTIFICATIONS}
Expand Down
Loading