Skip to content

Commit

Permalink
Merge pull request #1236 from brave/contribution-nonverified
Browse files Browse the repository at this point in the history
Adds new contribution flow for unverified publishers
  • Loading branch information
NejcZdovc authored Jan 9, 2019
2 parents c2557ec + bfb30dd commit 8ea4875
Show file tree
Hide file tree
Showing 53 changed files with 783 additions and 179 deletions.
19 changes: 9 additions & 10 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,31 +167,30 @@ ExtensionFunction::ResponseAction BraveRewardsSolveGrantCaptchaFunction::Run() {
return RespondNow(NoArguments());
}

BraveRewardsGetNonVerifiedSettingsFunction::
~BraveRewardsGetNonVerifiedSettingsFunction() {
BraveRewardsGetPendingContributionsTotalFunction::
~BraveRewardsGetPendingContributionsTotalFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsGetNonVerifiedSettingsFunction::Run() {
BraveRewardsGetPendingContributionsTotalFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
RewardsService* rewards_service_ =
RewardsServiceFactory::GetForProfile(profile);
bool non_verified = true;

if (!rewards_service_) {
return RespondNow(OneArgument(
std::make_unique<base::Value>(non_verified)));
std::make_unique<base::Value>(0.0)));
}

rewards_service_->GetPublisherAllowNonVerified(base::Bind(
&BraveRewardsGetNonVerifiedSettingsFunction::OnGetAllowNonVerified,
rewards_service_->GetPendingContributionsTotal(base::Bind(
&BraveRewardsGetPendingContributionsTotalFunction::OnGetPendingTotal,
this));
return RespondLater();
}

void BraveRewardsGetNonVerifiedSettingsFunction::OnGetAllowNonVerified(
bool non_verified) {
Respond(OneArgument(std::make_unique<base::Value>(non_verified)));
void BraveRewardsGetPendingContributionsTotalFunction::OnGetPendingTotal(
double amount) {
Respond(OneArgument(std::make_unique<base::Value>(amount)));
}

} // namespace api
Expand Down
8 changes: 4 additions & 4 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ class BraveRewardsSolveGrantCaptchaFunction : public UIThreadExtensionFunction {
ResponseAction Run() override;
};

class BraveRewardsGetNonVerifiedSettingsFunction : public UIThreadExtensionFunction {
class BraveRewardsGetPendingContributionsTotalFunction : public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getNonVerifiedSettings", UNKNOWN)
DECLARE_EXTENSION_FUNCTION("braveRewards.getPendingContributionsTotal", UNKNOWN)

protected:
~BraveRewardsGetNonVerifiedSettingsFunction() override;
~BraveRewardsGetPendingContributionsTotalFunction() override;

ResponseAction Run() override;

private:
void OnGetAllowNonVerified(bool non_verified);
void OnGetPendingTotal(double amount);
};

} // namespace api
Expand Down
21 changes: 21 additions & 0 deletions browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnAutoContributePropsReady(
std::unique_ptr<brave_rewards::AutoContributeProps> auto_contri_props);
void OnIsWalletCreated(bool created);
void GetPendingContributionsTotal(const base::ListValue* args);
void OnGetPendingContributionsTotal(double amount);

// RewardsServiceObserver implementation
void OnWalletInitialized(brave_rewards::RewardsService* rewards_service,
Expand Down Expand Up @@ -231,6 +233,9 @@ void RewardsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("brave_rewards.setBackupCompleted",
base::BindRepeating(&RewardsDOMHandler::SetBackupCompleted,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.getPendingContributionsTotal",
base::BindRepeating(&RewardsDOMHandler::GetPendingContributionsTotal,
base::Unretained(this)));
}

void RewardsDOMHandler::Init() {
Expand Down Expand Up @@ -843,6 +848,22 @@ void RewardsDOMHandler::SetBackupCompleted(const base::ListValue *args) {
}
}

void RewardsDOMHandler::GetPendingContributionsTotal(
const base::ListValue* args) {
if (rewards_service_) {
rewards_service_->GetPendingContributionsTotal(base::Bind(
&RewardsDOMHandler::OnGetPendingContributionsTotal,
weak_factory_.GetWeakPtr()));
}
}

void RewardsDOMHandler::OnGetPendingContributionsTotal(double amount) {
if (web_ui()->CanCallJavascript()) {
web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.pendingContributionTotal", base::Value(amount));
}
}

} // namespace

BraveRewardsUI::BraveRewardsUI(content::WebUI* web_ui, const std::string& name)
Expand Down
2 changes: 2 additions & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ void CustomizeWebUIHTMLSource(const std::string &name, content::WebUIDataSource*
{ "recurringDonation", IDS_BRAVE_UI_RECURRING_DONATION },
{ "recurringDonations", IDS_BRAVE_UI_RECURRING_DONATIONS },
{ "remove", IDS_BRAVE_UI_REMOVE },
{ "reservedAmountText", IDS_BRAVE_UI_RESERVED_AMOUNT_TEXT },
{ "reservedMoreLink", IDS_BRAVE_UI_RESERVED_MORE_LINK },
{ "restore", IDS_BRAVE_UI_RESTORE },
{ "restoreAll", IDS_BRAVE_UI_RESTORE_ALL },
{ "reviewSitesMsg", IDS_BRAVE_UI_REVIEW_SITE_MSG },
Expand Down
8 changes: 4 additions & 4 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -308,17 +308,17 @@
]
},
{
"name": "getNonVerifiedSettings",
"name": "getPendingContributionsTotal",
"type": "function",
"description": "Gets auto contribute settings",
"description": "Gets pending contributions total",
"parameters": [
{
"type": "function",
"name": "callback",
"parameters": [
{
"name": "nonVerified",
"type": "boolean"
"name": "amount",
"type": "number"
}
]
}
Expand Down
16 changes: 16 additions & 0 deletions components/brave_rewards/browser/pending_contribution.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* 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/. */
#include "brave/components/brave_rewards/browser/pending_contribution.h"

namespace brave_rewards {

PendingContribution::PendingContribution() :
amount(0),
added_date(0),
reconcile_date(0) {
}

PendingContribution::~PendingContribution() { }

} // namespace brave_rewards
27 changes: 27 additions & 0 deletions components/brave_rewards/browser/pending_contribution.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* 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/. */

#ifndef BRAVE_BROWSER_PAYMENTS_PENDING_CONTRIBUTION_
#define BRAVE_BROWSER_PAYMENTS_PENDING_CONTRIBUTION_

#include <string>

namespace brave_rewards {

struct PendingContribution {
PendingContribution();
~PendingContribution();
PendingContribution(const PendingContribution& data) = default;

std::string publisher_key;
double amount = 0;
uint32_t added_date = 0;
uint32_t reconcile_date = 0;
};

using PendingContributionList = std::vector<PendingContribution>;

} // namespace brave_rewards

#endif //BRAVE_BROWSER_PAYMENTS_PENDING_CONTRIBUTION_
119 changes: 114 additions & 5 deletions components/brave_rewards/browser/publisher_info_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace brave_rewards {

namespace {

const int kCurrentVersionNumber = 2;
const int kCurrentVersionNumber = 3;
const int kCompatibleVersionNumber = 1;

} // namespace
Expand Down Expand Up @@ -56,12 +56,14 @@ bool PublisherInfoDatabase::Init() {
!CreateContributionInfoTable() ||
!CreateActivityInfoTable() ||
!CreateMediaPublisherInfoTable() ||
!CreateRecurringDonationTable())
!CreateRecurringDonationTable() ||
!CreatePendingContributionsTable())
return false;

CreateContributionInfoIndex();
CreateActivityInfoIndex();
CreateRecurringDonationIndex();
CreatePendingContributionsIndex();

// Version check.
sql::InitStatus version_status = EnsureCurrentVersion();
Expand Down Expand Up @@ -675,6 +677,97 @@ bool PublisherInfoDatabase::RemoveRecurring(const std::string& publisher_key) {
return statement.Run();
}

bool PublisherInfoDatabase::CreatePendingContributionsTable() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

const char* name = "pending_contribution";
if (GetDB().DoesTableExist(name)) {
return true;
}

std::string sql;
sql.append("CREATE TABLE ");
sql.append(name);
sql.append(
"("
"publisher_id LONGVARCHAR NOT NULL,"
"amount DOUBLE DEFAULT 0 NOT NULL,"
"added_date INTEGER DEFAULT 0 NOT NULL,"
"viewing_id LONGVARCHAR NOT NULL,"
"category INTEGER NOT NULL,"
"CONSTRAINT fk_pending_contribution_publisher_id"
" FOREIGN KEY (publisher_id)"
" REFERENCES publisher_info (publisher_id)"
" ON DELETE CASCADE)");
return GetDB().Execute(sql.c_str());
}

bool PublisherInfoDatabase::CreatePendingContributionsIndex() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

return GetDB().Execute(
"CREATE INDEX IF NOT EXISTS pending_contribution_publisher_id_index "
"ON pending_contribution (publisher_id)");
}

bool PublisherInfoDatabase::InsertPendingContribution
(const ledger::PendingContributionList& list) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

bool initialized = Init();
DCHECK(initialized);

if (!initialized) {
return false;
}

base::Time now = base::Time::Now();
double now_seconds = now.ToDoubleT();

sql::Transaction transaction(&GetDB());
if (!transaction.Begin()) {
return false;
}

for (const auto& item : list.list_) {
sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
"INSERT INTO pending_contribution "
"(publisher_id, amount, added_date, viewing_id, category) "
"VALUES (?, ?, ?, ?, ?)"));

statement.BindString(0, item.publisher_key);
statement.BindDouble(1, item.amount);
statement.BindInt64(2, now_seconds);
statement.BindString(3, item.viewing_id);
statement.BindInt(4, item.category);
statement.Run();
}

return transaction.Commit();
}

double PublisherInfoDatabase::GetReservedAmount() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

bool initialized = Init();
DCHECK(initialized);

double amount = 0.0;

if (!initialized) {
return amount;
}

sql::Statement info_sql(
db_.GetUniqueStatement("SELECT sum(amount) FROM pending_contribution"));

if (info_sql.Step()) {
amount = info_sql.ColumnDouble(0);
}

return amount;
}

// static
int PublisherInfoDatabase::GetCurrentVersion() {
return kCurrentVersionNumber;
Expand Down Expand Up @@ -762,6 +855,16 @@ bool PublisherInfoDatabase::MigrateV1toV2() {
return CreateRecurringDonationIndex();
}

bool PublisherInfoDatabase::MigrateV2toV3() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

if (!CreatePendingContributionsTable()) {
return false;
}

return CreatePendingContributionsIndex();
}

sql::InitStatus PublisherInfoDatabase::EnsureCurrentVersion() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

Expand All @@ -774,15 +877,21 @@ sql::InitStatus PublisherInfoDatabase::EnsureCurrentVersion() {
const int old_version = meta_table_.GetVersionNumber();
const int cur_version = GetCurrentVersion();

// Migration from version 1 to version 2
if (old_version == 1 && cur_version == 2) {
// to version 2
if (old_version < 2 && cur_version < 3) {
if (!MigrateV1toV2()) {
LOG(ERROR) << "DB: Error with MigrateV1toV2";
}
}

meta_table_.SetVersionNumber(cur_version);
// to version 3
if (old_version < 3 && cur_version < 4) {
if (!MigrateV2toV3()) {
LOG(ERROR) << "DB: Error with MigrateV2toV3";
}
}

meta_table_.SetVersionNumber(cur_version);
return sql::INIT_OK;
}

Expand Down
7 changes: 7 additions & 0 deletions components/brave_rewards/browser/publisher_info_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#include "base/memory/memory_pressure_listener.h"
#include "base/sequence_checker.h"
#include "bat/ledger/publisher_info.h"
#include "bat/ledger/pending_contribution.h"
#include "brave/components/brave_rewards/browser/contribution_info.h"
#include "brave/components/brave_rewards/browser/pending_contribution.h"
#include "brave/components/brave_rewards/browser/recurring_donation.h"
#include "build/build_config.h"
#include "sql/database.h"
Expand Down Expand Up @@ -50,6 +52,8 @@ class PublisherInfoDatabase {
void GetRecurringDonations(ledger::PublisherInfoList* list);
void GetTips(ledger::PublisherInfoList* list, ledger::PUBLISHER_MONTH month, int year);
bool RemoveRecurring(const std::string& publisher_key);
bool InsertPendingContribution(const ledger::PendingContributionList& list);
double GetReservedAmount();

// Returns the current version of the publisher info database
static int GetCurrentVersion();
Expand All @@ -72,6 +76,8 @@ class PublisherInfoDatabase {
bool CreateActivityInfoIndex();
bool CreateRecurringDonationTable();
bool CreateRecurringDonationIndex();
bool CreatePendingContributionsTable();
bool CreatePendingContributionsIndex();

std::string BuildClauses(int start,
int limit,
Expand All @@ -84,6 +90,7 @@ class PublisherInfoDatabase {

sql::InitStatus EnsureCurrentVersion();
bool MigrateV1toV2();
bool MigrateV2toV3();

sql::Database db_;
sql::MetaTable meta_table_;
Expand Down
2 changes: 0 additions & 2 deletions components/brave_rewards/browser/recurring_donation.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

#include <string>

#include "brave/components/brave_rewards/browser/content_site.h"

namespace brave_rewards {
struct RecurringDonation {
RecurringDonation();
Expand Down
Loading

0 comments on commit 8ea4875

Please sign in to comment.