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

Adds support for min visits #1399

Merged
merged 1 commit into from
Feb 4, 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
1 change: 1 addition & 0 deletions browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ void RewardsDOMHandler::OnAutoContributePropsReady(
props->contribution_min_time,
props->reconcile_stamp,
props->contribution_non_verified,
props->contribution_min_visits,
base::Bind(&RewardsDOMHandler::OnContentSiteList,
weak_factory_.GetWeakPtr()));
}
Expand Down
15 changes: 12 additions & 3 deletions components/brave_rewards/browser/publisher_info_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ bool PublisherInfoDatabase::InsertOrUpdateActivityInfo(
activity_info_insert.BindInt(5, info.month);
activity_info_insert.BindInt(6, info.year);
activity_info_insert.BindInt64(7, info.reconcile_stamp);
activity_info_insert.BindInt64(8, info.visits);
activity_info_insert.BindInt(8, info.visits);

return activity_info_insert.Run();
}
Expand Down Expand Up @@ -481,7 +481,7 @@ bool PublisherInfoDatabase::GetActivityList(
std::string query = "SELECT ai.publisher_id, ai.duration, ai.score, "
"ai.percent, ai.weight, pi.verified, pi.excluded, "
"ai.month, ai.year, pi.name, pi.url, pi.provider, "
"pi.favIcon, ai.reconcile_stamp "
"pi.favIcon, ai.reconcile_stamp, ai.visits "
"FROM activity_info AS ai "
"INNER JOIN publisher_info AS pi "
"ON ai.publisher_id = pi.publisher_id "
Expand Down Expand Up @@ -522,6 +522,10 @@ bool PublisherInfoDatabase::GetActivityList(
query += " AND ai.percent >= ?";
}

if (filter.min_visits > 0) {
query += " AND ai.visits >= ?";
}

if (!filter.non_verified) {
query += " AND pi.verified = 1";
}
Expand Down Expand Up @@ -577,6 +581,10 @@ bool PublisherInfoDatabase::GetActivityList(
info_sql.BindInt(column++, filter.percent);
}

if (filter.min_visits > 0) {
info_sql.BindInt(column++, filter.min_visits);
}

while (info_sql.Step()) {
std::string id(info_sql.ColumnString(0));
ledger::ACTIVITY_MONTH month(
Expand All @@ -595,14 +603,15 @@ bool PublisherInfoDatabase::GetActivityList(
info.provider = info_sql.ColumnString(11);
info.favicon_url = info_sql.ColumnString(12);
info.reconcile_stamp = info_sql.ColumnInt64(13);
info.visits = info_sql.ColumnInt(14);

info.excluded = static_cast<ledger::PUBLISHER_EXCLUDE>(
info_sql.ColumnInt(6));

list->push_back(info);
}

return list;
return true;
}

/**
Expand Down
198 changes: 195 additions & 3 deletions components/brave_rewards/browser/publisher_info_database_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdateActivityInfo) {

ledger::PublisherInfo info;
info.id = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
info.verified = false;
info.verified = true;
info.excluded = ledger::PUBLISHER_EXCLUDE::DEFAULT;
info.name = "name";
info.url = "https://brave.com";
info.provider = "";
info.favicon_url = "";
info.provider = "youtube";
info.favicon_url = "favicon.ico";
info.duration = 10;
info.score = 1.1;
info.percent = 100;
Expand Down Expand Up @@ -220,6 +220,22 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdateActivityInfo) {
EXPECT_EQ(static_cast<uint64_t>(info_sql.ColumnInt64(8)),
info.reconcile_stamp);

query = "SELECT * FROM publisher_info WHERE publisher_id=?";
sql::Statement info_sql_0(GetDB().GetUniqueStatement(query.c_str()));

info_sql_0.BindString(0, info.id);

EXPECT_TRUE(info_sql_0.Step());
EXPECT_EQ(CountTableRows("publisher_info"), 1);
EXPECT_EQ(info_sql_0.ColumnString(0), info.id);
EXPECT_EQ(info_sql_0.ColumnBool(1), info.verified);
EXPECT_EQ(static_cast<ledger::PUBLISHER_EXCLUDE>(info_sql_0.ColumnInt(2)),
info.excluded);
EXPECT_EQ(info_sql_0.ColumnString(3), info.name);
EXPECT_EQ(info_sql_0.ColumnString(4), info.favicon_url);
EXPECT_EQ(info_sql_0.ColumnString(5), info.url);
EXPECT_EQ(info_sql_0.ColumnString(6), info.provider);

/**
* Make sure that second insert is update and not insert,
* month, year and stamp is unique key
Expand Down Expand Up @@ -502,7 +518,183 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdateActivityInfos) {
}

TEST_F(PublisherInfoDatabaseTest, InsertPendingContribution) {
/**
* Good path
*/
base::ScopedTempDir temp_dir;
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::DIRECT_DONATION;

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

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

std::string query = "SELECT * FROM pending_contribution";
sql::Statement info_sql(GetDB().GetUniqueStatement(query.c_str()));

EXPECT_EQ(CountTableRows("pending_contribution"), 2);

// 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_GE(info_sql.ColumnInt64(2), 20);
EXPECT_EQ(info_sql.ColumnString(3), contribution1.viewing_id);
EXPECT_EQ(static_cast<ledger::REWARDS_CATEGORY>(info_sql.ColumnInt(4)),
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_GE(info_sql.ColumnInt64(2), 0);
EXPECT_EQ(info_sql.ColumnString(3), contribution2.viewing_id);
EXPECT_EQ(static_cast<ledger::REWARDS_CATEGORY>(info_sql.ColumnInt(4)),
contribution2.category);
}

TEST_F(PublisherInfoDatabaseTest, GetActivityList) {
base::ScopedTempDir temp_dir;
base::FilePath db_file;
CreateTempDatabase(&temp_dir, &db_file);

// first entry publisher
ledger::PublisherInfo info;
info.id = "publisher_1";
info.name = "publisher_name_1";
info.url = "https://publisher1.com";
info.excluded = ledger::PUBLISHER_EXCLUDE::DEFAULT;
info.duration = 0;
info.verified = false;
info.visits = 0;
info.month = ledger::ACTIVITY_MONTH::JANUARY;
info.year = 1970;
info.reconcile_stamp = 1;
EXPECT_TRUE(publisher_info_database_->InsertOrUpdateActivityInfo(info));

// with duration
info.id = "publisher_2";
info.name = "publisher_name_2";
info.url = "https://publisher2.com";
info.excluded = ledger::PUBLISHER_EXCLUDE::DEFAULT;
info.duration = 100;
info.verified = false;
info.visits = 1;
EXPECT_TRUE(publisher_info_database_->InsertOrUpdateActivityInfo(info));

// verified publisher
info.id = "publisher_3";
info.name = "publisher_name_3";
info.url = "https://publisher3.com";
info.excluded = ledger::PUBLISHER_EXCLUDE::DEFAULT;
info.duration = 1;
info.verified = true;
info.visits = 1;
EXPECT_TRUE(publisher_info_database_->InsertOrUpdateActivityInfo(info));

// excluded publisher
info.id = "publisher_4";
info.name = "publisher_name_4";
info.url = "https://publisher4.com";
info.excluded = ledger::PUBLISHER_EXCLUDE::EXCLUDED;
info.duration = 1;
info.verified = false;
info.visits = 1;
EXPECT_TRUE(publisher_info_database_->InsertOrUpdateActivityInfo(info));

// with visits
info.id = "publisher_5";
info.name = "publisher_name_5";
info.url = "https://publisher5.com";
info.excluded = ledger::PUBLISHER_EXCLUDE::DEFAULT;
info.duration = 1;
info.verified = false;
info.visits = 10;
EXPECT_TRUE(publisher_info_database_->InsertOrUpdateActivityInfo(info));

// full
info.id = "publisher_6";
info.name = "publisher_name_6";
info.url = "https://publisher6.com";
info.excluded = ledger::PUBLISHER_EXCLUDE::INCLUDED;
info.duration = 5000;
info.verified = true;
info.visits = 10;
EXPECT_TRUE(publisher_info_database_->InsertOrUpdateActivityInfo(info));

EXPECT_EQ(CountTableRows("activity_info"), 6);
EXPECT_EQ(CountTableRows("publisher_info"), 6);

/**
* Get publisher with min_duration
*/
ledger::PublisherInfoList list_1;
ledger::ActivityInfoFilter filter_1;
filter_1.min_duration = 50;
filter_1.excluded = ledger::EXCLUDE_FILTER::FILTER_ALL;
EXPECT_TRUE(publisher_info_database_->GetActivityList(0, 0, filter_1, &list_1));
EXPECT_EQ(static_cast<int>(list_1.size()), 2);

EXPECT_EQ(list_1.at(0).id, "publisher_2");
EXPECT_EQ(list_1.at(1).id, "publisher_6");

/**
* Get verified publishers
*/
ledger::PublisherInfoList list_2;
ledger::ActivityInfoFilter filter_2;
filter_2.non_verified = false;
filter_2.excluded = ledger::EXCLUDE_FILTER::FILTER_ALL;
EXPECT_TRUE(publisher_info_database_->GetActivityList(0, 0, filter_2, &list_2));
EXPECT_EQ(static_cast<int>(list_2.size()), 2);

EXPECT_EQ(list_2.at(0).id, "publisher_3");
EXPECT_EQ(list_2.at(1).id, "publisher_6");

/**
* Get all publishers that are not excluded
*/
ledger::PublisherInfoList list_3;
ledger::ActivityInfoFilter filter_3;
filter_3.excluded = ledger::EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED;
EXPECT_TRUE(publisher_info_database_->GetActivityList(0, 0, filter_3, &list_3));
EXPECT_EQ(static_cast<int>(list_3.size()), 5);

EXPECT_EQ(list_3.at(0).id, "publisher_1");
EXPECT_EQ(list_3.at(1).id, "publisher_2");
EXPECT_EQ(list_3.at(2).id, "publisher_3");
EXPECT_EQ(list_3.at(3).id, "publisher_5");
EXPECT_EQ(list_3.at(4).id, "publisher_6");

/**
* Get publisher with min_visits
*/
ledger::PublisherInfoList list_4;
ledger::ActivityInfoFilter filter_4;
filter_4.min_visits = 5;
filter_4.excluded = ledger::EXCLUDE_FILTER::FILTER_ALL;
EXPECT_TRUE(publisher_info_database_->GetActivityList(0, 0, filter_4, &list_4));
EXPECT_EQ(static_cast<int>(list_4.size()), 2);

EXPECT_EQ(list_4.at(0).id, "publisher_5");
EXPECT_EQ(list_4.at(1).id, "publisher_6");
}

} // namespace brave_rewards
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 @@ -73,6 +73,7 @@ class RewardsService : public KeyedService {
uint64_t min_visit_time,
uint64_t reconcile_stamp,
bool allow_non_verified,
uint32_t min_visits,
const GetContentSiteListCallback& callback) = 0;
virtual void FetchGrant(const std::string& lang, const std::string& paymentId) = 0;
virtual void GetGrantCaptcha() = 0;
Expand Down
2 changes: 2 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ void RewardsServiceImpl::GetContentSiteList(
uint64_t min_visit_time,
uint64_t reconcile_stamp,
bool allow_non_verified,
uint32_t min_visits,
const GetContentSiteListCallback& callback) {
ledger::ActivityInfoFilter filter;
filter.month = ledger::ACTIVITY_MONTH::ANY;
Expand All @@ -448,6 +449,7 @@ void RewardsServiceImpl::GetContentSiteList(
ledger::EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED;
filter.percent = 1;
filter.non_verified = allow_non_verified;
filter.min_visits = min_visits;

GetActivityInfoList(start, limit,
filter,
Expand Down
1 change: 1 addition & 0 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class RewardsServiceImpl : public RewardsService,
uint64_t min_visit_time,
uint64_t reconcile_stamp,
bool allow_non_verified,
uint32_t min_visits,
const GetContentSiteListCallback& callback) override;
void OnGetContentSiteList(
const GetContentSiteListCallback& callback,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ LEDGER_EXPORT struct ActivityInfoFilter {
uint64_t min_duration;
uint64_t reconcile_stamp;
bool non_verified;
uint32_t min_visits;
};

LEDGER_EXPORT struct ContributionInfo {
Expand Down
12 changes: 10 additions & 2 deletions vendor/bat-native-ledger/src/bat/ledger/ledger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ ActivityInfoFilter::ActivityInfoFilter() :
percent(0),
min_duration(0),
reconcile_stamp(0),
non_verified(true) {}
non_verified(true),
min_visits(0u) {}

ActivityInfoFilter::ActivityInfoFilter(const ActivityInfoFilter& filter) :
id(filter.id),
Expand All @@ -143,7 +144,8 @@ ActivityInfoFilter::ActivityInfoFilter(const ActivityInfoFilter& filter) :
order_by(filter.order_by),
min_duration(filter.min_duration),
reconcile_stamp(filter.reconcile_stamp),
non_verified(filter.non_verified) {}
non_verified(filter.non_verified),
min_visits(filter.min_visits) {}

ActivityInfoFilter::~ActivityInfoFilter() {}

Expand Down Expand Up @@ -185,6 +187,12 @@ bool ActivityInfoFilter::loadFromJson(const std::string& json) {
order_by.push_back(std::make_pair(i.name.GetString(),
i.value.GetBool()));
}

if (d.HasMember("min_visits") && d["min_visits"].IsUint()) {
min_visits = d["min_visits"].GetUint();
} else {
min_visits = 0u;
}
}

return !error;
Expand Down
3 changes: 2 additions & 1 deletion vendor/bat-native-ledger/src/bat_contribution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ void BatContribution::StartAutoContribute() {
ledger::EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED,
true,
current_reconcile_stamp,
ledger_->GetPublisherAllowNonVerified());
ledger_->GetPublisherAllowNonVerified(),
ledger_->GetPublisherMinVisits());
ledger_->GetActivityInfoList(
0,
0,
Expand Down
3 changes: 2 additions & 1 deletion vendor/bat-native-ledger/src/bat_get_media.cc
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,8 @@ void BatGetMedia::fetchPublisherDataFromDB(
ledger::EXCLUDE_FILTER::FILTER_ALL,
false,
ledger_->GetReconcileStamp(),
true);
true,
false);
ledger_->GetPanelPublisherInfo(filter,
std::bind(&BatGetMedia::onFetchPublisherFromDBResponse,
this, _1, _2, windowId, visit_data, providerType,
Expand Down
3 changes: 3 additions & 0 deletions vendor/bat-native-ledger/src/bat_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2659,6 +2659,9 @@ static bool ignore_ = false;
writer.String("non_verified");
writer.Bool(info.non_verified);

writer.String("min_visits");
writer.Uint(info.min_visits);

writer.EndObject();
}

Expand Down
Loading