Skip to content

Commit

Permalink
Merge pull request #578 from brave/contribution
Browse files Browse the repository at this point in the history
Adds support for auto contribution contribution
  • Loading branch information
NejcZdovc committed Oct 10, 2018
1 parent a505b1c commit a2837af
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 15 deletions.
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ deps = {
"vendor/boto": "https://github.com/boto/boto@f7574aa6cc2c819430c1f05e9a1a1a666ef8169b",
"vendor/python-patch": "https://github.com/svn2github/python-patch@a336a458016ced89aba90dfc3f4c8222ae3b1403",
"vendor/sparkle": "https://github.com/brave/Sparkle.git@c0759cce415d7c0feae45005c8a013b1898711f0",
"vendor/bat-native-ledger": "https://github.com/brave-intl/bat-native-ledger@fe65bbb07d8e3ef237ada14b54dd9390f12fc2ee",
"vendor/bat-native-ledger": "https://github.com/brave-intl/bat-native-ledger@002e2bfa0021bb7b52b2a02de84d56ede4c8b2e7",
"vendor/bat-native-rapidjson": "https://github.com/brave-intl/bat-native-rapidjson.git@86aafe2ef89835ae71c9ed7c2527e3bb3000930e",
"vendor/bip39wally-core-native": "https://github.com/brave-intl/bip39wally-core-native.git@9b119931c702d55be994117eb505d56310720b1d",
"vendor/bat-native-anonize": "https://github.com/brave-intl/bat-native-anonize.git@0559543f458a949b83b58035273ef7f8f1a1b111",
Expand Down
23 changes: 23 additions & 0 deletions browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void ExcludePublisher(const base::ListValue* args);
void RestorePublishers(const base::ListValue* args);
void WalletExists(const base::ListValue* args);
void GetContributionAmount(const base::ListValue* args);

// RewardsServiceObserver implementation
void OnWalletInitialized(brave_rewards::RewardsService* rewards_service,
Expand All @@ -76,6 +77,10 @@ class RewardsDOMHandler : public WebUIMessageHandler,
brave_rewards::Grant grant) override;
void OnContentSiteUpdated(brave_rewards::RewardsService* rewards_service) override;
void OnExcludedSitesChanged(brave_rewards::RewardsService* rewards_service) override;
void OnReconcileComplete(brave_rewards::RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
const std::string& probi) override;

brave_rewards::RewardsService* rewards_service_; // NOT OWNED

Expand Down Expand Up @@ -130,6 +135,9 @@ void RewardsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("brave_rewards.checkWalletExistence",
base::BindRepeating(&RewardsDOMHandler::WalletExists,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.getContributionAmount",
base::BindRepeating(&RewardsDOMHandler::GetContributionAmount,
base::Unretained(this)));
}

void RewardsDOMHandler::Init() {
Expand Down Expand Up @@ -470,6 +478,21 @@ void RewardsDOMHandler::WalletExists(const base::ListValue* args) {
}
}

void RewardsDOMHandler::GetContributionAmount(const base::ListValue* args) {
if (rewards_service_ && web_ui()->CanCallJavascript()) {
double amount = rewards_service_->GetContributionAmount();

web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.contributionAmount", base::Value(amount));
}
}

void RewardsDOMHandler::OnReconcileComplete(brave_rewards::RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
const std::string& probi) {
GetAllBalanceReports();
}

} // namespace

BraveRewardsUI::BraveRewardsUI(content::WebUI* web_ui, const std::string& name)
Expand Down
21 changes: 16 additions & 5 deletions components/brave_rewards/browser/publisher_info_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ bool PublisherInfoDatabase::CreateActivityInfoTable() {
"category INTEGER NOT NULL,"
"month INTEGER NOT NULL,"
"year INTEGER NOT NULL,"
"reconcile_stamp INTEGER DEFAULT 0 NOT NULL,"
"CONSTRAINT fk_activity_info_publisher_id"
" FOREIGN KEY (publisher_id)"
" REFERENCES publisher_info (publisher_id)"
Expand Down Expand Up @@ -226,12 +227,13 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo(
sql::Statement activity_get(
db_.GetUniqueStatement("SELECT publisher_id FROM activity_info WHERE "
"publisher_id=? AND category=? "
"AND month=? AND year=?"));
"AND month=? AND year=? AND reconcile_stamp=?"));

activity_get.BindString(0, info.id);
activity_get.BindInt(1, info.category);
activity_get.BindInt(2, info.month);
activity_get.BindInt(3, info.year);
activity_get.BindInt64(4, info.reconcile_stamp);

if (activity_get.Step()) {
sql::Statement activity_info_update(
Expand All @@ -240,7 +242,7 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo(
"duration=?, score=?, percent=?, "
"weight=? WHERE "
"publisher_id=? AND category=? "
"AND month=? AND year=?"));
"AND month=? AND year=? AND reconcile_stamp=?"));

activity_info_update.BindInt64(0, (int)info.duration);
activity_info_update.BindDouble(1, info.score);
Expand All @@ -250,6 +252,7 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo(
activity_info_update.BindInt(5, info.category);
activity_info_update.BindInt(6, info.month);
activity_info_update.BindInt(7, info.year);
activity_info_update.BindInt64(8, info.reconcile_stamp);

return activity_info_update.Run();
}
Expand All @@ -258,8 +261,8 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo(
GetDB().GetCachedStatement(SQL_FROM_HERE,
"INSERT INTO activity_info "
"(publisher_id, duration, score, percent, "
"weight, category, month, year) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)"));
"weight, category, month, year, reconcile_stamp) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"));

activity_info_insert.BindString(0, info.id);
activity_info_insert.BindInt64(1, (int)info.duration);
Expand All @@ -269,6 +272,7 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo(
activity_info_insert.BindInt(5, info.category);
activity_info_insert.BindInt(6, info.month);
activity_info_insert.BindInt(7, info.year);
activity_info_insert.BindInt64(8, info.reconcile_stamp);

return activity_info_insert.Run();
}
Expand Down Expand Up @@ -340,7 +344,7 @@ bool PublisherInfoDatabase::Find(int start,

std::string query = "SELECT ai.publisher_id, ai.duration, ai.score, ai.percent, "
"ai.weight, pi.verified, pi.excluded, ai.category, ai.month, ai.year, pi.name, "
"pi.url, pi.provider, pi.favIcon "
"pi.url, pi.provider, pi.favIcon, ai.reconcile_stamp "
"FROM activity_info AS ai "
"INNER JOIN publisher_info AS pi ON ai.publisher_id = pi.publisher_id "
"WHERE 1 = 1";
Expand Down Expand Up @@ -368,6 +372,7 @@ bool PublisherInfoDatabase::Find(int start,
info.url = info_sql.ColumnString(11);
info.provider = info_sql.ColumnString(12);
info.favicon_url = info_sql.ColumnString(13);
info.reconcile_stamp = info_sql.ColumnInt64(14);

info.excluded = static_cast<ledger::PUBLISHER_EXCLUDE>(info_sql.ColumnInt(6));
info.category =
Expand Down Expand Up @@ -422,6 +427,9 @@ std::string PublisherInfoDatabase::BuildClauses(int start,
if (filter.year > 0)
clauses += " AND ai.year = ?";

if (filter.reconcile_stamp > 0)
clauses += " AND ai.reconcile_stamp = ?";

if (filter.min_duration > 0)
clauses += " AND ai.duration >= ?";

Expand Down Expand Up @@ -459,6 +467,9 @@ void PublisherInfoDatabase::BindFilter(sql::Statement& statement,
if (filter.year > 0)
statement.BindInt(column++, filter.year);

if (filter.reconcile_stamp > 0)
statement.BindInt64(column++, filter.reconcile_stamp);

if (filter.min_duration > 0)
statement.BindInt(column++, filter.min_duration);

Expand Down
1 change: 1 addition & 0 deletions components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class RewardsService : public KeyedService {
virtual void GetCurrentBalanceReport() = 0;
virtual bool IsWalletCreated() = 0;
virtual void GetPublisherActivityFromUrl(uint64_t windowId, const std::string& url) = 0;
virtual double GetContributionAmount() = 0;

void AddObserver(RewardsServiceObserver* observer);
void RemoveObserver(RewardsServiceObserver* observer);
Expand Down
25 changes: 21 additions & 4 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -489,14 +489,27 @@ void RewardsServiceImpl::OnGrantFinish(ledger::Result result,
const ledger::Grant& grant) {
ledger::BalanceReportInfo report_info;
auto now = base::Time::Now();
ledger_->SetBalanceReportCaptcha(GetPublisherMonth(now), GetPublisherYear(now), grant.probi);
ledger_->SetBalanceReportItem(GetPublisherMonth(now),
GetPublisherYear(now),
ledger::ReportType::GRANT,
grant.probi);
TriggerOnGrantFinish(result, grant);
}

void RewardsServiceImpl::OnReconcileComplete(ledger::Result result,
const std::string& viewing_id) {
LOG(ERROR) << "reconcile complete " << viewing_id;
// TODO - TriggerOnReconcileComplete
const std::string& viewing_id,
const std::string& probi) {
if (result == ledger::Result::LEDGER_OK) {
// TODO add notification service when implemented
auto now = base::Time::Now();
ledger_->SetBalanceReportItem(GetPublisherMonth(now),
GetPublisherYear(now),
ledger::ReportType::AUTO_CONTRIBUTION,
probi);
}

for (auto& observer : observers_)
observer.OnReconcileComplete(this, result, viewing_id, probi);
}

void RewardsServiceImpl::LoadLedgerState(
Expand Down Expand Up @@ -1173,4 +1186,8 @@ void RewardsServiceImpl::OnPublisherActivity(ledger::Result result,
}
}

double RewardsServiceImpl::GetContributionAmount() {
return ledger_->GetContributionAmount();
}

} // namespace brave_rewards
4 changes: 3 additions & 1 deletion components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class RewardsServiceImpl : public RewardsService,
void GetCurrentBalanceReport() override;
bool IsWalletCreated() override;
void GetPublisherActivityFromUrl(uint64_t windowId, const std::string& url) override;
double GetContributionAmount() override;

private:
typedef base::Callback<void(int, const std::string&, const std::map<std::string, std::string>& headers)> FetchCallback;
Expand Down Expand Up @@ -152,7 +153,8 @@ class RewardsServiceImpl : public RewardsService,
double balance,
const std::vector<ledger::Grant>& grants) override;
void OnReconcileComplete(ledger::Result result,
const std::string& viewing_id) override;
const std::string& viewing_id,
const std::string& probi) override;
void OnGrantFinish(ledger::Result result,
const ledger::Grant& grant) override;
void LoadLedgerState(ledger::LedgerCallbackHandler* handler) override;
Expand Down
4 changes: 4 additions & 0 deletions components/brave_rewards/browser/rewards_service_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class RewardsServiceObserver : public base::CheckedObserver {
brave_rewards::Grant grant) {};
virtual void OnContentSiteUpdated(RewardsService* rewards_service) {};
virtual void OnExcludedSitesChanged(RewardsService* rewards_service) {};
virtual void OnReconcileComplete(RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
const std::string& probi) {};
};

} // namespace brave_rewards
Expand Down
4 changes: 4 additions & 0 deletions components/brave_rewards/ui/actions/rewards_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,7 @@ export const restorePublishers = () => action(types.ON_RESTORE_PUBLISHERS)
export const onNumExcludedSites = (num: string) => action(types.ON_NUM_EXCLUDED_SITES, {
num
})

export const onContributionAmount = (amount: number) => action(types.ON_CONTRIBUTION_AMOUNT, {
amount
})
7 changes: 6 additions & 1 deletion components/brave_rewards/ui/brave_rewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ window.cr.define('brave_rewards', function () {
getActions().onWalletExists(exists)
}

function contributionAmount (amount: number) {
getActions().onContributionAmount(amount)
}

return {
initialize,
walletCreated,
Expand All @@ -120,7 +124,8 @@ window.cr.define('brave_rewards', function () {
contributeList,
numExcludedSites,
balanceReports,
walletExists
walletExists,
contributionAmount
}
})

Expand Down
4 changes: 2 additions & 2 deletions components/brave_rewards/ui/components/contributeBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class ContributeBox extends React.Component<Props, State> {
>
{
monthlyList.map((choice: MonthlyChoice) => {
return <div key={`choice-${choice.tokens}`} data-value={choice.tokens.toString()}>
return <div key={`choice-setting-${choice.tokens}`} data-value={choice.tokens.toString()}>
<Tokens
value={choice.tokens}
converted={choice.converted}
Expand Down Expand Up @@ -229,7 +229,7 @@ class ContributeBox extends React.Component<Props, State> {
</Select>
</List>
<List title={getLocale('contributionNextDate')}>
<NextContribution>{new Date(reconcileStamp).toLocaleDateString()}</NextContribution>
<NextContribution>{new Date(reconcileStamp * 1000).toLocaleDateString()}</NextContribution>
</List>
<List title={getLocale('contributionSites')}>
{getLocale('total')} &nbsp;<Tokens
Expand Down
3 changes: 2 additions & 1 deletion components/brave_rewards/ui/constants/rewards_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ export const enum types {
ON_RESTORE_PUBLISHERS = '@@rewards/ON_RESTORE_PUBLISHERS',
CHECK_WALLET_EXISTENCE = '@@rewards/CHECK_WALLET_EXISTENCE',
ON_WALLET_EXISTS = '@@rewards/ON_WALLET_EXISTS',
ON_NUM_EXCLUDED_SITES = '@@rewards/ON_NUM_EXCLUDED_SITES'
ON_NUM_EXCLUDED_SITES = '@@rewards/ON_NUM_EXCLUDED_SITES',
ON_CONTRIBUTION_AMOUNT = '@@rewards/ON_CONTRIBUTION_AMOUNT'
}
7 changes: 7 additions & 0 deletions components/brave_rewards/ui/reducers/wallet_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const createWallet = (state: Rewards.State) => {
chrome.send('brave_rewards.getAddresses', [])
chrome.send('brave_rewards.saveSetting', ['enabledMain', 'true'])
chrome.send('brave_rewards.saveSetting', ['enabledContribute', 'true'])
chrome.send('brave_rewards.getContributionAmount', [])

return state
}
Expand Down Expand Up @@ -180,6 +181,12 @@ const walletReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State,
state = createWallet(state)
break
}
case types.ON_CONTRIBUTION_AMOUNT:
{
state = { ...state }
state.contributionMonthly = action.payload.amount
break
}
}

return state
Expand Down

0 comments on commit a2837af

Please sign in to comment.