Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moves pending contributon into mojom definition #2510

Merged
merged 1 commit into from
May 28, 2019
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
34 changes: 17 additions & 17 deletions components/brave_rewards/browser/publisher_info_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -924,17 +924,17 @@ bool PublisherInfoDatabase::InsertPendingContribution
return false;
}

for (const auto& item : list.list_) {
for (const auto& item : 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.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.BindString(3, item->viewing_id);
statement.BindInt(4, item->category);
statement.Run();
}

Expand Down Expand Up @@ -982,20 +982,20 @@ void PublisherInfoDatabase::GetPendingContributions(
"INNER JOIN publisher_info AS pi ON pc.publisher_id = pi.publisher_id"));

while (info_sql.Step()) {
ledger::PendingContributionInfo info;
info.publisher_key = info_sql.ColumnString(0);
info.name = info_sql.ColumnString(1);
info.url = info_sql.ColumnString(2);
info.favicon_url = info_sql.ColumnString(3);
info.verified = info_sql.ColumnBool(4);
info.provider = info_sql.ColumnString(5);
info.amount = info_sql.ColumnDouble(6);
info.added_date = info_sql.ColumnInt64(7);
info.viewing_id = info_sql.ColumnString(8);
info.category =
auto info = ledger::PendingContributionInfo::New();
info->publisher_key = info_sql.ColumnString(0);
info->name = info_sql.ColumnString(1);
info->url = info_sql.ColumnString(2);
info->favicon_url = info_sql.ColumnString(3);
info->verified = info_sql.ColumnBool(4);
info->provider = info_sql.ColumnString(5);
info->amount = info_sql.ColumnDouble(6);
info->added_date = info_sql.ColumnInt64(7);
info->viewing_id = info_sql.ColumnString(8);
info->category =
static_cast<ledger::REWARDS_CATEGORY>(info_sql.ColumnInt(9));

list->push_back(info);
list->push_back(std::move(info));
}
}

Expand Down
121 changes: 61 additions & 60 deletions components/brave_rewards/browser/publisher_info_database_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -624,25 +624,26 @@ TEST_F(PublisherInfoDatabaseTest, InsertPendingContribution) {
base::FilePath db_file;
CreateTempDatabase(&temp_dir, &db_file);

ledger::PendingContribution contribution1;
contribution1.publisher_key = "key1";
contribution1.amount = 10;
contribution1.added_date = 10;
contribution1.viewing_id = "fsodfsdnf23r23rn";
contribution1.category = ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE;

ledger::PendingContribution contribution2;
contribution2.publisher_key = "key2";
contribution2.amount = 20;
contribution2.viewing_id = "aafsofdfsdnf23r23rn";
contribution2.category = ledger::REWARDS_CATEGORY::ONE_TIME_TIP;
auto contribution1 = ledger::PendingContribution::New();
contribution1->publisher_key = "key1";
contribution1->amount = 10;
contribution1->added_date = 10;
contribution1->viewing_id = "fsodfsdnf23r23rn";
contribution1->category = ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE;

auto contribution2 = ledger::PendingContribution::New();
contribution2->publisher_key = "key2";
contribution2->amount = 20;
contribution2->viewing_id = "aafsofdfsdnf23r23rn";
contribution2->category = ledger::REWARDS_CATEGORY::ONE_TIME_TIP;


ledger::PendingContributionList list;
list.list_.push_back(contribution1);
list.list_.push_back(contribution2);
list.push_back(contribution1->Clone());
list.push_back(contribution2->Clone());

bool success = publisher_info_database_->InsertPendingContribution(
list);
std::move(list));
EXPECT_TRUE(success);

std::string query = "SELECT * FROM pending_contribution";
Expand All @@ -652,21 +653,21 @@ TEST_F(PublisherInfoDatabaseTest, InsertPendingContribution) {

// First contribution
EXPECT_TRUE(info_sql.Step());
EXPECT_EQ(info_sql.ColumnString(0), contribution1.publisher_key);
EXPECT_EQ(info_sql.ColumnDouble(1), contribution1.amount);
EXPECT_EQ(info_sql.ColumnString(0), contribution1->publisher_key);
EXPECT_EQ(info_sql.ColumnDouble(1), contribution1->amount);
EXPECT_GE(info_sql.ColumnInt64(2), 20);
EXPECT_EQ(info_sql.ColumnString(3), contribution1.viewing_id);
EXPECT_EQ(info_sql.ColumnString(3), contribution1->viewing_id);
EXPECT_EQ(static_cast<ledger::REWARDS_CATEGORY>(info_sql.ColumnInt(4)),
contribution1.category);
contribution1->category);

// Second contribution
EXPECT_TRUE(info_sql.Step());
EXPECT_EQ(info_sql.ColumnString(0), contribution2.publisher_key);
EXPECT_EQ(info_sql.ColumnDouble(1), contribution2.amount);
EXPECT_EQ(info_sql.ColumnString(0), contribution2->publisher_key);
EXPECT_EQ(info_sql.ColumnDouble(1), contribution2->amount);
EXPECT_GE(info_sql.ColumnInt64(2), 0);
EXPECT_EQ(info_sql.ColumnString(3), contribution2.viewing_id);
EXPECT_EQ(info_sql.ColumnString(3), contribution2->viewing_id);
EXPECT_EQ(static_cast<ledger::REWARDS_CATEGORY>(info_sql.ColumnInt(4)),
contribution2.category);
contribution2->category);
}

TEST_F(PublisherInfoDatabaseTest, GetActivityList) {
Expand Down Expand Up @@ -1043,35 +1044,35 @@ void PublisherInfoDatabaseTest::PreparePendingContributions() {
EXPECT_EQ(CountTableRows("publisher_info"), 4);

// Insert some pending contributions
ledger::PendingContribution contribution1;
contribution1.publisher_key = "key1";
contribution1.amount = 10;
contribution1.viewing_id = "fsodfsdnf23r23rn";
contribution1.category = ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE;

ledger::PendingContribution contribution2;
contribution2.publisher_key = "key2";
contribution2.amount = 20;
contribution2.viewing_id = "aafsoffdffdfsdnf23r23rn";
contribution2.category = ledger::REWARDS_CATEGORY::ONE_TIME_TIP;

ledger::PendingContribution contribution3;
contribution3.publisher_key = "key3";
contribution3.amount = 30;
contribution3.viewing_id = "aafszxfzcofdfsdnf23r23rn";
contribution3.category = ledger::REWARDS_CATEGORY::ONE_TIME_TIP;

ledger::PendingContribution contribution4;
contribution4.publisher_key = "key4";
contribution4.amount = 40;
contribution4.viewing_id = "aafsofdfs12333dnf23r23rn";
contribution4.category = ledger::REWARDS_CATEGORY::ONE_TIME_TIP;
auto contribution1 = ledger::PendingContribution::New();
contribution1->publisher_key = "key1";
contribution1->amount = 10;
contribution1->viewing_id = "fsodfsdnf23r23rn";
contribution1->category = ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE;

auto contribution2 = ledger::PendingContribution::New();
contribution2->publisher_key = "key2";
contribution2->amount = 20;
contribution2->viewing_id = "aafsoffdffdfsdnf23r23rn";
contribution2->category = ledger::REWARDS_CATEGORY::ONE_TIME_TIP;

auto contribution3 = ledger::PendingContribution::New();
contribution3->publisher_key = "key3";
contribution3->amount = 30;
contribution3->viewing_id = "aafszxfzcofdfsdnf23r23rn";
contribution3->category = ledger::REWARDS_CATEGORY::ONE_TIME_TIP;

auto contribution4 = ledger::PendingContribution::New();
contribution4->publisher_key = "key4";
contribution4->amount = 40;
contribution4->viewing_id = "aafsofdfs12333dnf23r23rn";
contribution4->category = ledger::REWARDS_CATEGORY::ONE_TIME_TIP;

ledger::PendingContributionList list;
list.list_.push_back(contribution1);
list.list_.push_back(contribution2);
list.list_.push_back(contribution3);
list.list_.push_back(contribution4);
list.push_back(std::move(contribution1));
list.push_back(std::move(contribution2));
list.push_back(std::move(contribution3));
list.push_back(std::move(contribution4));

success = publisher_info_database_->InsertPendingContribution(
list);
Expand All @@ -1093,12 +1094,12 @@ TEST_F(PublisherInfoDatabaseTest, GetPendingContributions) {
publisher_info_database_->GetPendingContributions(&select_list);
EXPECT_EQ(static_cast<int>(select_list.size()), 4);

EXPECT_EQ(select_list.at(0).publisher_key, "key1");
EXPECT_EQ(select_list.at(1).publisher_key, "key2");
EXPECT_EQ(select_list.at(2).publisher_key, "key3");
EXPECT_EQ(select_list.at(3).publisher_key, "key4");
EXPECT_EQ(select_list.at(0)->publisher_key, "key1");
EXPECT_EQ(select_list.at(1)->publisher_key, "key2");
EXPECT_EQ(select_list.at(2)->publisher_key, "key3");
EXPECT_EQ(select_list.at(3)->publisher_key, "key4");

EXPECT_EQ(select_list.at(0).url, "https://key1.com");
EXPECT_EQ(select_list.at(0)->url, "https://key1.com");
}

TEST_F(PublisherInfoDatabaseTest, RemovePendingContributions) {
Expand All @@ -1113,20 +1114,20 @@ TEST_F(PublisherInfoDatabaseTest, RemovePendingContributions) {
*/
ledger::PendingContributionInfoList select_list;
publisher_info_database_->GetPendingContributions(&select_list);
EXPECT_EQ(select_list.at(0).publisher_key, "key1");
EXPECT_EQ(select_list.at(0)->publisher_key, "key1");
bool success = publisher_info_database_->RemovePendingContributions(
"key1",
"fsodfsdnf23r23rn",
select_list.at(0).added_date);
select_list.at(0)->added_date);
EXPECT_TRUE(success);

ledger::PendingContributionInfoList list;
publisher_info_database_->GetPendingContributions(&list);
EXPECT_EQ(static_cast<int>(list.size()), 3);

EXPECT_EQ(list.at(0).publisher_key, "key2");
EXPECT_EQ(list.at(1).publisher_key, "key3");
EXPECT_EQ(list.at(2).publisher_key, "key4");
EXPECT_EQ(list.at(0)->publisher_key, "key2");
EXPECT_EQ(list.at(1)->publisher_key, "key3");
EXPECT_EQ(list.at(2)->publisher_key, "key4");

/**
* Trying to delete not existing row
Expand Down
40 changes: 19 additions & 21 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2751,13 +2751,13 @@ void RewardsServiceImpl::OnSavePendingContribution(ledger::Result result) {
}

void RewardsServiceImpl::SavePendingContribution(
const ledger::PendingContributionList& list) {
ledger::PendingContributionList list) {
base::PostTaskAndReplyWithResult(
file_task_runner_.get(),
FROM_HERE,
base::Bind(&SavePendingContributionOnFileTaskRunner,
publisher_info_backend_.get(),
list),
std::move(list)),
base::Bind(&RewardsServiceImpl::OnSavePendingContribution,
AsWeakPtr()));
}
Expand Down Expand Up @@ -3034,32 +3034,30 @@ ledger::PendingContributionInfoList PendingContributionsOnFileTaskRunner(
}

PendingContributionInfo PendingContributionLedgerToRewards(
const ledger::PendingContributionInfo& contribution) {
const ledger::PendingContributionInfoPtr contribution) {
PendingContributionInfo info;
info.publisher_key = contribution.publisher_key;
info.category = contribution.category;
info.verified = contribution.verified;
info.name = contribution.name;
info.url = contribution.url;
info.provider = contribution.provider;
info.favicon_url = contribution.favicon_url;
info.amount = contribution.amount;
info.added_date = contribution.added_date;
info.viewing_id = contribution.viewing_id;
info.expiration_date = contribution.expiration_date;
info.publisher_key = contribution->publisher_key;
info.category = contribution->category;
info.verified = contribution->verified;
info.name = contribution->name;
info.url = contribution->url;
info.provider = contribution->provider;
info.favicon_url = contribution->favicon_url;
info.amount = contribution->amount;
info.added_date = contribution->added_date;
info.viewing_id = contribution->viewing_id;
info.expiration_date = contribution->expiration_date;
return info;
}

void RewardsServiceImpl::OnGetPendingContributionsUI(
GetPendingContributionsCallback callback,
const std::vector<std::string>& json_list) {
ledger::PendingContributionInfoList list) {
tmancey marked this conversation as resolved.
Show resolved Hide resolved
std::unique_ptr<brave_rewards::PendingContributionInfoList> new_list(
new brave_rewards::PendingContributionInfoList);
for (auto &json_contribution : json_list) {
ledger::PendingContributionInfo contribution;
contribution.loadFromJson(json_contribution);
for (auto &item : list) {
brave_rewards::PendingContributionInfo new_contribution =
PendingContributionLedgerToRewards(contribution);
PendingContributionLedgerToRewards(std::move(item));
new_list->push_back(new_contribution);
}

Expand All @@ -3076,12 +3074,12 @@ void RewardsServiceImpl::GetPendingContributionsUI(

void RewardsServiceImpl::OnGetPendingContributions(
const ledger::PendingContributionInfoListCallback& callback,
const ledger::PendingContributionInfoList& list) {
ledger::PendingContributionInfoList list) {
if (!Connected()) {
return;
}

callback(list);
callback(std::move(list));
}

void RewardsServiceImpl::GetPendingContributions(
Expand Down
6 changes: 3 additions & 3 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,11 @@ class RewardsServiceImpl : public RewardsService,

void OnGetPendingContributionsUI(
GetPendingContributionsCallback callback,
const std::vector<std::string>& json_list);
ledger::PendingContributionInfoList list);

void OnGetPendingContributions(
const ledger::PendingContributionInfoListCallback& callback,
const ledger::PendingContributionInfoList& list);
ledger::PendingContributionInfoList list);

void OnGetExcludedPublishersNumberDB(
ledger::GetExcludedPublishersNumberDBCallback callback,
Expand Down Expand Up @@ -499,7 +499,7 @@ class RewardsServiceImpl : public RewardsService,
ledger::PublisherInfoPtr publisher_info);

void SavePendingContribution(
const ledger::PendingContributionList& list) override;
ledger::PendingContributionList list) override;

void OnSavePendingContribution(ledger::Result result);

Expand Down
18 changes: 5 additions & 13 deletions components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -568,11 +568,11 @@ std::string BatLedgerClientMojoProxy::URIEncode(const std::string& value) {
}

void BatLedgerClientMojoProxy::SavePendingContribution(
const ledger::PendingContributionList& list) {
ledger::PendingContributionList list) {
if (!Connected())
return;

bat_ledger_client_->SavePendingContribution(list.ToJson());
bat_ledger_client_->SavePendingContribution(std::move(list));
}

void OnLoadActivityInfo(
Expand Down Expand Up @@ -730,22 +730,14 @@ void BatLedgerClientMojoProxy::GetExcludedPublishersNumberDB(

void OnGetPendingContributions(
const ledger::PendingContributionInfoListCallback& callback,
const std::vector<std::string>& info_list) {
ledger::PendingContributionInfoList list;

for (const auto& info : info_list) {
ledger::PendingContributionInfo new_info;
new_info.loadFromJson(info);
list.push_back(new_info);
}

callback(list);
ledger::PendingContributionInfoList list) {
callback(std::move(list));
}

void BatLedgerClientMojoProxy::GetPendingContributions(
const ledger::PendingContributionInfoListCallback& callback) {
if (!Connected()) {
callback(std::vector<ledger::PendingContributionInfo>());
callback(ledger::PendingContributionInfoList());
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient,
std::string URIEncode(const std::string& value) override;

void SavePendingContribution(
const ledger::PendingContributionList& list) override;
ledger::PendingContributionList list) override;

void LoadActivityInfo(ledger::ActivityInfoFilter filter,
ledger::PublisherInfoCallback callback) override;
Expand Down
Loading