Skip to content

Commit

Permalink
Merge pull request #1457 from brave/publisher-updates
Browse files Browse the repository at this point in the history
Publisher updates flow
  • Loading branch information
NejcZdovc committed Feb 1, 2019
1 parent b227b15 commit e066ec1
Show file tree
Hide file tree
Showing 34 changed files with 464 additions and 204 deletions.
32 changes: 24 additions & 8 deletions browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnIsWalletCreated(bool created);
void GetPendingContributionsTotal(const base::ListValue* args);
void OnGetPendingContributionsTotal(double amount);
void OnContentSiteUpdated(brave_rewards::RewardsService* rewards_service) override;

// RewardsServiceObserver implementation
void OnWalletInitialized(brave_rewards::RewardsService* rewards_service,
Expand All @@ -116,7 +117,6 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnGrantFinish(brave_rewards::RewardsService* rewards_service,
unsigned int result,
brave_rewards::Grant grant) override;
void OnContentSiteUpdated(brave_rewards::RewardsService* rewards_service) override;
void OnExcludedSitesChanged(brave_rewards::RewardsService* rewards_service,
std::string publisher_id) override;
void OnReconcileComplete(brave_rewards::RewardsService* rewards_service,
Expand All @@ -137,6 +137,10 @@ class RewardsDOMHandler : public WebUIMessageHandler,
brave_rewards::RewardsService* rewards_service,
bool rewards_main_enabled) override;

void OnPublisherListNormalized(
brave_rewards::RewardsService* rewards_service,
brave_rewards::ContentSiteList list) override;

// RewardsNotificationsServiceObserver implementation
void OnNotificationAdded(
brave_rewards::RewardsNotificationService* rewards_notification_service,
Expand Down Expand Up @@ -550,11 +554,14 @@ void RewardsDOMHandler::GetAddresses(const base::ListValue* args) {

void RewardsDOMHandler::OnAutoContributePropsReady(
std::unique_ptr<brave_rewards::AutoContributeProps> props) {
rewards_service_->GetContentSiteList(0, 0,
props->contribution_min_time, props->reconcile_stamp,
rewards_service_->GetContentSiteList(
0,
0,
props->contribution_min_time,
props->reconcile_stamp,
props->contribution_non_verified,
base::Bind(&RewardsDOMHandler::OnContentSiteList,
weak_factory_.GetWeakPtr()));
weak_factory_.GetWeakPtr()));
}

void RewardsDOMHandler::OnContentSiteUpdated(
Expand Down Expand Up @@ -628,22 +635,18 @@ void RewardsDOMHandler::SaveSetting(const base::ListValue* args) {

if (key == "contributionMinTime") {
rewards_service_->SetPublisherMinVisitTime(std::stoull(value));
OnContentSiteUpdated(rewards_service_);
}

if (key == "contributionMinVisits") {
rewards_service_->SetPublisherMinVisits(std::stoul(value));
OnContentSiteUpdated(rewards_service_);
}

if (key == "contributionNonVerified") {
rewards_service_->SetPublisherAllowNonVerified(value == "true");
OnContentSiteUpdated(rewards_service_);
}

if (key == "contributionVideos") {
rewards_service_->SetPublisherAllowVideos(value == "true");
OnContentSiteUpdated(rewards_service_);
}

if (key == "enabledContribute") {
Expand Down Expand Up @@ -893,6 +896,19 @@ void RewardsDOMHandler::OnRewardsMainEnabled(
}
}


void RewardsDOMHandler::OnPublisherListNormalized(
brave_rewards::RewardsService* rewards_service,
brave_rewards::ContentSiteList list) {
std::unique_ptr<brave_rewards::ContentSiteList> site_list(
new brave_rewards::ContentSiteList);
for (auto& publisher : list) {
site_list->push_back(publisher);
}

OnContentSiteList(std::move(site_list), 0);
}

} // namespace

BraveRewardsUI::BraveRewardsUI(content::WebUI* web_ui, const std::string& name)
Expand Down
5 changes: 5 additions & 0 deletions components/brave_rewards/browser/content_site.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ struct ContentSite {
ContentSite(const ContentSite& properties);
~ContentSite();

// DESC sort
bool operator<(const ContentSite& other) const {
return percentage > other.percentage;
}

std::string id;
double percentage;
bool verified;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void ExtensionRewardsServiceObserver::OnGetCurrentBalanceReport(
}
}

void ExtensionRewardsServiceObserver::OnGetPublisherActivityFromUrl(
void ExtensionRewardsServiceObserver::OnPanelPublisherInfo(
RewardsService* rewards_service,
int error_code,
std::unique_ptr<ledger::PublisherInfo> info,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ExtensionRewardsServiceObserver : public RewardsServiceObserver,
// RewardsServicePrivateObserver implementation
void OnGetCurrentBalanceReport(RewardsService* rewards_service,
const BalanceReport& balance_report) override;
void OnGetPublisherActivityFromUrl(
void OnPanelPublisherInfo(
RewardsService* rewards_service,
int error_code,
std::unique_ptr<ledger::PublisherInfo> info,
Expand Down
39 changes: 31 additions & 8 deletions components/brave_rewards/browser/publisher_info_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -302,22 +302,19 @@ PublisherInfoDatabase::GetPanelPublisher(
bool initialized = Init();
DCHECK(initialized);

if (!initialized) {
if (!initialized || filter.id.empty()) {
return nullptr;
}

sql::Statement info_sql(db_.GetUniqueStatement(
"SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, pi.provider, "
"pi.verified, pi.excluded, ai.percent FROM publisher_info AS pi "
"LEFT JOIN activity_info AS ai ON pi.publisher_id = ai.publisher_id "
"WHERE pi.publisher_id=? AND ((ai.month = ? "
"AND ai.year = ? AND ai.reconcile_stamp = ?) OR "
"ai.percent IS NULL) LIMIT 1"));
"WHERE pi.publisher_id=? AND "
"(ai.reconcile_stamp = ? OR ai.percent IS NULL) LIMIT 1"));

info_sql.BindString(0, filter.id);
info_sql.BindInt(1, filter.month);
info_sql.BindInt(2, filter.year);
info_sql.BindInt64(3, filter.reconcile_stamp);
info_sql.BindInt64(1, filter.reconcile_stamp);

if (info_sql.Step()) {
std::unique_ptr<ledger::PublisherInfo> info;
Expand Down Expand Up @@ -411,7 +408,7 @@ bool PublisherInfoDatabase::InsertOrUpdateActivityInfo(
bool initialized = Init();
DCHECK(initialized);

if (!initialized) {
if (!initialized || info.id.empty()) {
return false;
}

Expand Down Expand Up @@ -439,6 +436,32 @@ bool PublisherInfoDatabase::InsertOrUpdateActivityInfo(
return activity_info_insert.Run();
}

bool PublisherInfoDatabase::InsertOrUpdateActivityInfos(
const ledger::PublisherInfoList& list) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

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

if (!initialized || list.size() == 0) {
return false;
}

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

for (const auto& info : list) {
if (!InsertOrUpdateActivityInfo(info)) {
transaction.Rollback();
return false;
}
}

return transaction.Commit();
}

bool PublisherInfoDatabase::GetActivityList(
int start,
int limit,
Expand Down
2 changes: 2 additions & 0 deletions components/brave_rewards/browser/publisher_info_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class PublisherInfoDatabase {

bool InsertOrUpdateActivityInfo(const ledger::PublisherInfo& info);

bool InsertOrUpdateActivityInfos(const ledger::PublisherInfoList& list);

bool GetActivityList(int start,
int limit,
const ledger::ActivityInfoFilter& filter,
Expand Down
100 changes: 100 additions & 0 deletions components/brave_rewards/browser/publisher_info_database_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,106 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdateRecurringDonation) {
EXPECT_FALSE(info_sql_2.Step());
}

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

/**
* Publisher ID is missing
*/
ledger::ActivityInfoFilter filter_1;
EXPECT_EQ(publisher_info_database_->GetPanelPublisher(filter_1), nullptr);

/**
* Empty table
*/
ledger::ActivityInfoFilter filter_2;
filter_2.id = "test";
EXPECT_EQ(publisher_info_database_->GetPanelPublisher(filter_2), nullptr);

/**
* Ignore month and year filter
*/
ledger::PublisherInfo info_1;
info_1.id = "brave.com";
info_1.url = "https://brave.com";
info_1.percent = 11;
info_1.month = ledger::ACTIVITY_MONTH::JANUARY;
info_1.year = 2019;
info_1.reconcile_stamp = 10;

bool success = publisher_info_database_->InsertOrUpdateActivityInfo(info_1);
EXPECT_TRUE(success);

ledger::ActivityInfoFilter filter_3;
filter_3.id = "brave.com";
filter_3.month = ledger::ACTIVITY_MONTH::ANY;
filter_3.year = -1;
filter_3.reconcile_stamp = 10;
std::unique_ptr<ledger::PublisherInfo> result =
publisher_info_database_->GetPanelPublisher(filter_3);
EXPECT_TRUE(result);
EXPECT_EQ(result->id, "brave.com");
}

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

/**
* Good path
*/
ledger::PublisherInfo info_1;
info_1.id = "brave.com";
info_1.url = "https://brave.com";
info_1.percent = 11;
info_1.month = ledger::ACTIVITY_MONTH::JANUARY;
info_1.year = 2019;
info_1.reconcile_stamp = 10;

ledger::PublisherInfo info_2;
info_2.id = "clifton.io";
info_2.url = "https://clifton.io";
info_2.percent = 11;
info_2.month = ledger::ACTIVITY_MONTH::JANUARY;
info_2.year = 2019;
info_2.reconcile_stamp = 10;

ledger::PublisherInfoList list;
list.push_back(info_1);
list.push_back(info_2);

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

/**
* Empty list
*/
ledger::PublisherInfoList list_empty;

success = publisher_info_database_->InsertOrUpdateActivityInfos(list_empty);
EXPECT_FALSE(success);

/**
* One publisher has empty ID
*/

ledger::PublisherInfo info_3;
info_3.id = "";
info_3.url = "https://page.io";
info_3.percent = 11;
info_3.month = ledger::ACTIVITY_MONTH::JANUARY;
info_3.year = 2019;
info_3.reconcile_stamp = 10;

list.push_back(info_3);

success = publisher_info_database_->InsertOrUpdateActivityInfos(list);
EXPECT_FALSE(success);
}

TEST_F(PublisherInfoDatabaseTest, InsertPendingContribution) {

}
Expand Down
Loading

0 comments on commit e066ec1

Please sign in to comment.