Skip to content

Commit

Permalink
Merge pull request #1671 from brave/verified-image-60
Browse files Browse the repository at this point in the history
Fixes favicon image for verified youtube publishers
  • Loading branch information
NejcZdovc authored Feb 14, 2019
2 parents a82d319 + 4d4ac9b commit 9160009
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
34 changes: 31 additions & 3 deletions components/brave_rewards/browser/publisher_info_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,22 +241,50 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo(
return false;
}

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

sql::Statement publisher_info_statement(
GetDB().GetCachedStatement(SQL_FROM_HERE,
"INSERT OR REPLACE INTO publisher_info "
"(publisher_id, verified, excluded, "
"name, url, provider, favIcon) "
"VALUES (?, ?, ?, ?, ?, ?, ?)"));
"VALUES (?, ?, ?, ?, ?, ?, "
"(SELECT IFNULL( "
"(SELECT favicon FROM publisher_info "
"WHERE publisher_id = ?), \"\"))"
")"));

publisher_info_statement.BindString(0, info.id);
publisher_info_statement.BindBool(1, info.verified);
publisher_info_statement.BindInt(2, static_cast<int>(info.excluded));
publisher_info_statement.BindString(3, info.name);
publisher_info_statement.BindString(4, info.url);
publisher_info_statement.BindString(5, info.provider);
publisher_info_statement.BindString(6, info.favicon_url);
publisher_info_statement.BindString(6, info.id);

publisher_info_statement.Run();

std::string favicon = info.favicon_url;
if (!favicon.empty()) {
sql::Statement favicon_statement(
GetDB().GetCachedStatement(SQL_FROM_HERE,
"UPDATE publisher_info SET favIcon = ? "
"WHERE publisher_id = ?"));

if (favicon == ledger::clear_favicon) {
favicon.clear();
}

favicon_statement.BindString(0, favicon);
favicon_statement.BindString(1, info.id);

return publisher_info_statement.Run();
favicon_statement.Run();
}

return transaction.Commit();
}

std::unique_ptr<ledger::PublisherInfo>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,17 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdatePublisherInfo) {
base::FilePath db_file;
CreateTempDatabase(&temp_dir, &db_file);


const std::string fav_icon = "1";

ledger::PublisherInfo info;
info.id = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
info.verified = false;
info.excluded = ledger::PUBLISHER_EXCLUDE::DEFAULT;
info.name = "name";
info.url = "https://brave.com";
info.provider = "";
info.favicon_url = "";
info.favicon_url = "0";

bool success = publisher_info_database_->InsertOrUpdatePublisherInfo(info);
EXPECT_TRUE(success);
Expand Down Expand Up @@ -137,7 +140,7 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdatePublisherInfo) {
info.name = "updated";
info.url = "https://clifton.com";
info.provider = "";
info.favicon_url = "1";
info.favicon_url = fav_icon;

success = publisher_info_database_->InsertOrUpdatePublisherInfo(info);
EXPECT_TRUE(success);
Expand All @@ -158,6 +161,36 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdatePublisherInfo) {
EXPECT_EQ(info_sql_1.ColumnString(5), info.url);
EXPECT_EQ(info_sql_1.ColumnString(6), info.provider);

/**
* If favicon is empty, don't update record
*/
info.name = "updated2";
info.favicon_url = "";

success = publisher_info_database_->InsertOrUpdatePublisherInfo(info);
EXPECT_TRUE(success);

query = "SELECT favicon, name FROM publisher_info WHERE publisher_id=?";
sql::Statement info_sql_2(GetDB().GetUniqueStatement(query.c_str()));
info_sql_2.BindString(0, info.id);
EXPECT_TRUE(info_sql_2.Step());
EXPECT_EQ(info_sql_2.ColumnString(0), fav_icon);
EXPECT_EQ(info_sql_2.ColumnString(1), info.name);

/**
* If favicon is marked as clear, clear it
*/
info.favicon_url = ledger::clear_favicon;

success = publisher_info_database_->InsertOrUpdatePublisherInfo(info);
EXPECT_TRUE(success);

query = "SELECT favicon FROM publisher_info WHERE publisher_id=?";
sql::Statement info_sql_3(GetDB().GetUniqueStatement(query.c_str()));
info_sql_3.BindString(0, info.id);
EXPECT_TRUE(info_sql_3.Step());
EXPECT_EQ(info_sql_3.ColumnString(0), "");

/**
* Publisher key is missing
*/
Expand All @@ -167,11 +200,11 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdatePublisherInfo) {
EXPECT_FALSE(success);

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

info_sql_2.BindString(0, info.id);
info_sql_4.BindString(0, info.id);

EXPECT_FALSE(info_sql_2.Step());
EXPECT_FALSE(info_sql_4.Step());
}

TEST_F(PublisherInfoDatabaseTest, InsertOrUpdateActivityInfo) {
Expand Down
2 changes: 2 additions & 0 deletions vendor/bat-native-ledger/include/bat/ledger/publisher_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace ledger {

const std::string clear_favicon = "clear";

LEDGER_EXPORT enum REWARDS_CATEGORY {
AUTO_CONTRIBUTE = 1 << 1, // 2
TIPPING = 1 << 2,
Expand Down
2 changes: 1 addition & 1 deletion vendor/bat-native-ledger/src/bat_publishers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void BatPublishers::saveVisitInternal(
publisher_info->favicon_url = fav_icon;
}
} else {
publisher_info->favicon_url = std::string();
publisher_info->favicon_url = ledger::clear_favicon;
}

publisher_info->name = visit_data.name;
Expand Down

0 comments on commit 9160009

Please sign in to comment.