diff --git a/browser/ui/webui/brave_rewards_page_ui.cc b/browser/ui/webui/brave_rewards_page_ui.cc index a452ff2e7e50..841f33b30c33 100644 --- a/browser/ui/webui/brave_rewards_page_ui.cc +++ b/browser/ui/webui/brave_rewards_page_ui.cc @@ -196,11 +196,12 @@ class RewardsDOMHandler : public WebUIMessageHandler, void OnExcludedSitesChanged(brave_rewards::RewardsService* rewards_service, std::string publisher_id, bool excluded) override; - void OnReconcileComplete(brave_rewards::RewardsService* rewards_service, - unsigned int result, - const std::string& viewing_id, - const std::string& probi, - const int32_t type) override; + void OnReconcileComplete( + brave_rewards::RewardsService* rewards_service, + unsigned int result, + const std::string& viewing_id, + const double amount, + const int32_t type) override; void OnPendingContributionSaved( brave_rewards::RewardsService* rewards_service, int result) override; @@ -222,11 +223,6 @@ class RewardsDOMHandler : public WebUIMessageHandler, void OnRecurringTipRemoved(brave_rewards::RewardsService* rewards_service, bool success) override; - void OnContributionSaved( - brave_rewards::RewardsService* rewards_service, - bool success, - int type) override; - void OnPendingContributionRemoved( brave_rewards::RewardsService* rewards_service, int32_t result) override; @@ -992,7 +988,7 @@ void RewardsDOMHandler::OnReconcileComplete( brave_rewards::RewardsService* rewards_service, unsigned int result, const std::string& viewing_id, - const std::string& probi, + const double amount, const int32_t type) { if (web_ui()->CanCallJavascript()) { base::DictionaryValue complete; @@ -1447,22 +1443,6 @@ void RewardsDOMHandler::OnRecurringTipRemoved( "brave_rewards.recurringTipRemoved", base::Value(success)); } -void RewardsDOMHandler::OnContributionSaved( - brave_rewards::RewardsService* rewards_service, - bool success, - int type) { - if (!web_ui()->CanCallJavascript()) { - return; - } - - base::DictionaryValue result; - result.SetBoolean("success", success); - result.SetInteger("type", type); - - web_ui()->CallJavascriptFunctionUnsafe( - "brave_rewards.onContributionSaved", result); -} - void RewardsDOMHandler::SetInlineTipSetting(const base::ListValue* args) { std::string key; args->GetString(0, &key); diff --git a/browser/ui/webui/brave_tip_ui.cc b/browser/ui/webui/brave_tip_ui.cc index 6a116d222f20..fb56aa79c410 100644 --- a/browser/ui/webui/brave_tip_ui.cc +++ b/browser/ui/webui/brave_tip_ui.cc @@ -95,7 +95,7 @@ class RewardsTipDOMHandler : public WebUIMessageHandler, brave_rewards::RewardsService* rewards_service, unsigned int result, const std::string& viewing_id, - const std::string& probi, + const double amount, const int32_t type) override; brave_rewards::RewardsService* rewards_service_; // NOT OWNED @@ -489,7 +489,7 @@ void RewardsTipDOMHandler::OnReconcileComplete( brave_rewards::RewardsService* rewards_service, unsigned int result, const std::string& viewing_id, - const std::string& probi, + const double amount, const int32_t type) { if (!web_ui()->CanCallJavascript()) { return; diff --git a/components/brave_rewards/browser/BUILD.gn b/components/brave_rewards/browser/BUILD.gn index 3f6a4f1a4e94..cd50049f7478 100644 --- a/components/brave_rewards/browser/BUILD.gn +++ b/components/brave_rewards/browser/BUILD.gn @@ -35,12 +35,8 @@ source_set("browser") { "pending_contribution.h", "publisher_banner.cc", "publisher_banner.h", - "contribution_info.cc", - "contribution_info.h", "reconcile_info.cc", "reconcile_info.h", - "recurring_donation.cc", - "recurring_donation.h", "rewards_internals_info.cc", "rewards_internals_info.h", "balance.cc", @@ -78,6 +74,8 @@ source_set("browser") { sources += [ "database/database_contribution_info.cc", "database/database_contribution_info.h", + "database/database_contribution_info_publishers.cc", + "database/database_contribution_info_publishers.h", "database/database_contribution_queue.cc", "database/database_contribution_queue.h", "database/database_contribution_queue_publishers.cc", diff --git a/components/brave_rewards/browser/contribution_info.cc b/components/brave_rewards/browser/contribution_info.cc deleted file mode 100644 index 6173e0888cef..000000000000 --- a/components/brave_rewards/browser/contribution_info.cc +++ /dev/null @@ -1,25 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * 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/contribution_info.h" - -namespace brave_rewards { - -ContributionInfo::ContributionInfo() : - date(0) { -} - -ContributionInfo::~ContributionInfo() = default; - -ContributionInfo::ContributionInfo(const ContributionInfo& data) { - probi = data.probi; - month = data.month; - year = data.year; - type = data.type; - date = data.date; - publisher_key = data.publisher_key; -} - -} // namespace brave_rewards diff --git a/components/brave_rewards/browser/contribution_info.h b/components/brave_rewards/browser/contribution_info.h deleted file mode 100644 index 4efa881181a1..000000000000 --- a/components/brave_rewards/browser/contribution_info.h +++ /dev/null @@ -1,28 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * 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_COMPONENTS_BRAVE_REWARDS_BROWSER_CONTRIBUTION_INFO_H_ -#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_CONTRIBUTION_INFO_H_ - -#include - -namespace brave_rewards { - -struct ContributionInfo { - ContributionInfo(); - ~ContributionInfo(); - ContributionInfo(const ContributionInfo& properties); - - std::string probi; - int month; - int year; - int type; - uint32_t date = 0; - std::string publisher_key; -}; - -} // namespace brave_rewards - -#endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_CONTRIBUTION_INFO_H_ diff --git a/components/brave_rewards/browser/database/database_contribution_info.cc b/components/brave_rewards/browser/database/database_contribution_info.cc index f04bdd4f4059..d5dc1d461c4e 100644 --- a/components/brave_rewards/browser/database/database_contribution_info.cc +++ b/components/brave_rewards/browser/database/database_contribution_info.cc @@ -13,14 +13,29 @@ #include "sql/statement.h" #include "sql/transaction.h" +namespace { + double ProbiToDouble(const std::string& probi) { + const int32_t probi_size = 18; + const size_t size = probi.size(); + std::string amount = "0"; + if (size > probi_size) { + amount = probi; + amount.insert(size - probi_size, "."); + } + + return std::stod(amount); + } +} // namespace + namespace brave_rewards { DatabaseContributionInfo::DatabaseContributionInfo(int current_db_version) : - DatabaseTable(current_db_version) { + DatabaseTable(current_db_version), + publishers_(std::make_unique + (current_db_version)) { } -DatabaseContributionInfo::~DatabaseContributionInfo() { -} +DatabaseContributionInfo::~DatabaseContributionInfo() = default; bool DatabaseContributionInfo::Init(sql::Database* db) { if (GetCurrentDBVersion() < minimum_version_) { @@ -37,7 +52,7 @@ bool DatabaseContributionInfo::Init(sql::Database* db) { return false; } - success = CreateIndex(db); + success = publishers_->Init(db); if (!success) { return false; } @@ -46,18 +61,18 @@ bool DatabaseContributionInfo::Init(sql::Database* db) { } bool DatabaseContributionInfo::CreateTable(sql::Database* db) { - if (db->DoesTableExist(table_name_)) { - return true; - } - - return CreateTableV8(db); + return CreateTableV11(db); } bool DatabaseContributionInfo::CreateIndex(sql::Database* db) { - return CreateIndexV8(db); + return true; } bool DatabaseContributionInfo::CreateTableV8(sql::Database* db) { + if (db->DoesTableExist(table_name_)) { + return true; + } + const std::string query = base::StringPrintf( "CREATE TABLE %s (" "publisher_id LONGVARCHAR," @@ -76,15 +91,38 @@ bool DatabaseContributionInfo::CreateTableV8(sql::Database* db) { return db->Execute(query.c_str()); } +bool DatabaseContributionInfo::CreateTableV11(sql::Database* db) { + if (db->DoesTableExist(table_name_)) { + return true; + } + + const std::string query = base::StringPrintf( + "CREATE TABLE %s (" + "contribution_id TEXT NOT NULL," + "amount DOUBLE NOT NULL," + "type INTEGER NOT NULL," + "step INTEGER NOT NULL DEFAULT -1," + "retry_count INTEGER NOT NULL DEFAULT -1," + "created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP," + "PRIMARY KEY (contribution_id)" + ")", + table_name_); + + return db->Execute(query.c_str()); +} + bool DatabaseContributionInfo::CreateIndexV8(sql::Database* db) { return this->InsertIndex(db, table_name_, "publisher_id"); } -bool DatabaseContributionInfo::Migrate(sql::Database* db, const int to) { - switch (to) { +bool DatabaseContributionInfo::Migrate(sql::Database* db, const int target) { + switch (target) { case 8: { return MigrateToV8(db); } + case 11: { + return MigrateToV11(db); + } default: { NOTREACHED(); return false; @@ -131,27 +169,140 @@ bool DatabaseContributionInfo::MigrateToV8(sql::Database* db) { return true; } +bool DatabaseContributionInfo::MigrateToV11(sql::Database* db) { + const std::string sql = + "DROP INDEX IF EXISTS contribution_info_publisher_id_index;"; + if (!db->Execute(sql.c_str())) { + return false; + } + + const std::string temp_table_name = base::StringPrintf( + "%s_temp", + table_name_); + + if (!RenameDBTable(db, table_name_, temp_table_name)) { + return false; + } + + if (!CreateTableV11(db)) { + return false; + } + + if (!publishers_->Migrate(db, 11)) { + return false; + } + + const std::string query = base::StringPrintf( + "SELECT publisher_id, probi, date, type FROM %s", + temp_table_name.c_str()); + + sql::Statement statement(db->GetUniqueStatement(query.c_str())); + + uint32_t count = 0; + while (statement.Step()) { + const std::string publisher_key = statement.ColumnString(0); + const double amount = ProbiToDouble(statement.ColumnString(1)); + const auto date = statement.ColumnInt64(2); + const int type = statement.ColumnInt(3); + const std::string contribution_id = base::StringPrintf( + "id_%s_%s", + std::to_string(date).c_str(), + std::to_string(count).c_str()); + + std::string query = base::StringPrintf( + "INSERT INTO %s " + "(contribution_id, amount, type, step, " + "retry_count, created_at) " + "VALUES (?, ?, ?, ?, ?, ?)", + table_name_); + + sql::Statement contribution( + db->GetCachedStatement(SQL_FROM_HERE, query.c_str())); + + contribution.BindString(0, contribution_id); + contribution.BindDouble(1, amount); + contribution.BindInt(2, static_cast(type)); + contribution.BindInt(3, -1); + contribution.BindInt(4, -1); + contribution.BindInt64(5, date); + + if (!contribution.Run()) { + return false; + } + count++; + + if (publisher_key.empty()) { + continue; + } + + query = + "INSERT INTO contribution_info_publishers " + "(contribution_id, publisher_key, total_amount, contributed_amount) " + "VALUES (?, ?, ?, ?)"; + + sql::Statement publisher( + db->GetCachedStatement(SQL_FROM_HERE, query.c_str())); + + publisher.BindString(0, contribution_id); + publisher.BindString(1, publisher_key); + publisher.BindDouble(2, amount); + publisher.BindDouble(3, amount); + + if (!publisher.Run()) { + return false; + } + } + + if (!DropTable(db, temp_table_name)) { + return false; + } + + return true; +} + bool DatabaseContributionInfo::InsertOrUpdate( sql::Database* db, - const brave_rewards::ContributionInfo& info) { + ledger::ContributionInfoPtr info) { + if (!info) { + return false; + } + + sql::Transaction transaction(db); + if (!transaction.Begin()) { + return false; + } + const std::string query = base::StringPrintf( "INSERT OR REPLACE INTO %s " - "(publisher_id, probi, date, type, month, year) " + "(contribution_id, amount, type, step, " + "retry_count, created_at) " "VALUES (?, ?, ?, ?, ?, ?)", table_name_); sql::Statement statement( db->GetCachedStatement(SQL_FROM_HERE, query.c_str())); - statement.BindString(0, info.publisher_key); - statement.BindString(1, info.probi); - statement.BindInt64(2, info.date); - statement.BindInt(3, info.type); - statement.BindInt(4, info.month); - statement.BindInt(5, info.year); + statement.BindString(0, info->contribution_id); + statement.BindDouble(1, info->amount); + statement.BindInt(2, static_cast(info->type)); + statement.BindInt(3, info->step); + statement.BindInt(4, info->retry_count); + if (info->created_at == 0) { + statement.BindNull(5); + } else { + statement.BindInt64(5, info->created_at); + } + + if (!statement.Run()) { + return false; + } - return statement.Run(); + if (!publishers_->InsertOrUpdate(db, info->Clone())) { + return false; + } + + return transaction.Commit(); } bool DatabaseContributionInfo::GetOneTimeTips( @@ -163,20 +314,24 @@ bool DatabaseContributionInfo::GetOneTimeTips( return false; } - const std::string query = base::StringPrintf( + const std::string query = "SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, " - "ci.probi, ci.date, spi.status, pi.provider " - "FROM %s as ci " - "INNER JOIN publisher_info AS pi ON ci.publisher_id = pi.publisher_id " + "ci.amount, ci.created_at, spi.status, pi.provider " + "FROM contribution_info as ci " + "INNER JOIN contribution_info_publishers AS cp " + "ON cp.contribution_id = ci.contribution_id " + "INNER JOIN publisher_info AS pi ON cp.publisher_key = pi.publisher_id " "LEFT JOIN server_publisher_info AS spi " "ON spi.publisher_key = pi.publisher_id " - "WHERE ci.month = ? AND ci.year = ? AND ci.type = ?", - table_name_); + "WHERE strftime('%m', datetime(ci.created_at, 'unixepoch')) = ? AND " + "strftime('%Y', datetime(ci.created_at, 'unixepoch')) = ? AND ci.type = ?"; sql::Statement statement(db->GetUniqueStatement(query.c_str())); - statement.BindInt(0, static_cast(month)); - statement.BindInt(1, year); + const std::string formatted_month = base::StringPrintf("%02d", month); + + statement.BindString(0, formatted_month); + statement.BindString(1, std::to_string(year)); statement.BindInt(2, static_cast(ledger::RewardsType::ONE_TIME_TIP)); while (statement.Step()) { diff --git a/components/brave_rewards/browser/database/database_contribution_info.h b/components/brave_rewards/browser/database/database_contribution_info.h index 05fc684d5e74..923aa3ea387e 100644 --- a/components/brave_rewards/browser/database/database_contribution_info.h +++ b/components/brave_rewards/browser/database/database_contribution_info.h @@ -12,7 +12,7 @@ #include #include "bat/ledger/mojom_structs.h" -#include "brave/components/brave_rewards/browser/contribution_info.h" +#include "brave/components/brave_rewards/browser/database/database_contribution_info_publishers.h" #include "brave/components/brave_rewards/browser/database/database_table.h" namespace brave_rewards { @@ -28,11 +28,9 @@ class DatabaseContributionInfo: public DatabaseTable { bool CreateIndex(sql::Database* db) override; - bool Migrate(sql::Database* db, const int to); + bool Migrate(sql::Database* db, const int target); - bool InsertOrUpdate( - sql::Database* db, - const brave_rewards::ContributionInfo& info); + bool InsertOrUpdate(sql::Database* db, ledger::ContributionInfoPtr info); bool GetOneTimeTips( sql::Database* db, @@ -43,12 +41,17 @@ class DatabaseContributionInfo: public DatabaseTable { private: const char* table_name_ = "contribution_info"; const int minimum_version_ = 2; + std::unique_ptr publishers_; bool CreateTableV8(sql::Database* db); + bool CreateTableV11(sql::Database* db); + bool CreateIndexV8(sql::Database* db); bool MigrateToV8(sql::Database* db); + + bool MigrateToV11(sql::Database* db); }; } // namespace brave_rewards diff --git a/components/brave_rewards/browser/database/database_contribution_info_publishers.cc b/components/brave_rewards/browser/database/database_contribution_info_publishers.cc new file mode 100644 index 000000000000..7ee351ee290c --- /dev/null +++ b/components/brave_rewards/browser/database/database_contribution_info_publishers.cc @@ -0,0 +1,178 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * 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 +#include + +#include "base/bind.h" +#include "base/strings/stringprintf.h" +#include "brave/components/brave_rewards/browser/database/database_contribution_info_publishers.h" +#include "brave/components/brave_rewards/browser/database/database_util.h" +#include "sql/statement.h" +#include "sql/transaction.h" + +namespace brave_rewards { + +DatabaseContributionInfoPublishers::DatabaseContributionInfoPublishers + (int current_db_version) : DatabaseTable(current_db_version) { +} + +DatabaseContributionInfoPublishers:: +~DatabaseContributionInfoPublishers()= default; + +bool DatabaseContributionInfoPublishers::Init(sql::Database* db) { + if (GetCurrentDBVersion() < minimum_version_) { + return true; + } + + sql::Transaction transaction(db); + if (!transaction.Begin()) { + return false; + } + + bool success = CreateTable(db); + if (!success) { + return false; + } + + success = CreateIndex(db); + if (!success) { + return false; + } + + return transaction.Commit(); +} +bool DatabaseContributionInfoPublishers::CreateTable(sql::Database* db) { + return CreateTable11(db); +} + +bool DatabaseContributionInfoPublishers::CreateTable11(sql::Database* db) { + if (db->DoesTableExist(table_name_)) { + return true; + } + + const std::string query = base::StringPrintf( + "CREATE TABLE %s (" + "contribution_id TEXT NOT NULL," + "publisher_key TEXT NOT NULL," + "total_amount DOUBLE NOT NULL," + "contributed_amount DOUBLE," + "CONSTRAINT fk_contribution_info_publishers_contribution_id " + " FOREIGN KEY (contribution_id) " + " REFERENCES contribution_info (contribution_id) " + " ON DELETE CASCADE," + "CONSTRAINT fk_contribution_info_publishers_publisher_id " + " FOREIGN KEY (publisher_key) " + " REFERENCES publisher_info (publisher_id)" + ")", + table_name_); + + return db->Execute(query.c_str()); +} + +bool DatabaseContributionInfoPublishers::CreateIndex(sql::Database* db) { + return CreateIndex11(db); +} + +bool DatabaseContributionInfoPublishers::CreateIndex11(sql::Database* db) { + bool success = this->InsertIndex(db, table_name_, "contribution_id"); + + if (!success) { + return false; + } + + return this->InsertIndex(db, table_name_, "publisher_key"); +} + +bool DatabaseContributionInfoPublishers::Migrate( + sql::Database* db, + const int target) { + switch (target) { + case 11: { + return MigrateToV11(db); + } + default: { + NOTREACHED(); + return false; + } + } +} + +bool DatabaseContributionInfoPublishers::MigrateToV11(sql::Database* db) { + if (!CreateTable11(db)) { + return false; + } + + if (!CreateIndex11(db)) { + return false; + } + + return true; +} + +bool DatabaseContributionInfoPublishers::InsertOrUpdate( + sql::Database* db, + ledger::ContributionInfoPtr info) { + if (!info) { + return false; + } + + const std::string query = base::StringPrintf( + "INSERT OR REPLACE INTO %s " + "(contribution_id, publisher_key, total_amount, contributed_amount) " + "VALUES (?, ?, ?, ?)", + table_name_); + + sql::Transaction transaction(db); + if (!transaction.Begin()) { + return false; + } + + for (const auto& publisher : info->publishers) { + sql::Statement statement( + db->GetCachedStatement(SQL_FROM_HERE, query.c_str())); + + statement.BindString(0, publisher->contribution_id); + statement.BindString(1, publisher->publisher_key); + statement.BindDouble(2, publisher->total_amount); + statement.BindDouble(3, publisher->contributed_amount); + statement.Run(); + } + + return transaction.Commit(); +} + +bool DatabaseContributionInfoPublishers::GetRecords( + sql::Database* db, + const std::string& contribution_id, + ledger::ContributionPublisherList* list) { + if (contribution_id.empty() || !list) { + return false; + } + + const std::string query = base::StringPrintf( + "SELECT contribution_id, publisher_key, total_amount, contributed_amount " + "FROM %s WHERE contribution_id = ?", + table_name_); + + sql::Statement statement(db->GetUniqueStatement(query.c_str())); + + statement.BindString(0, contribution_id); + + while (statement.Step()) { + auto publisher = ledger::ContributionPublisher::New(); + + publisher->contribution_id = statement.ColumnString(0); + publisher->publisher_key = statement.ColumnString(1); + publisher->total_amount = statement.ColumnDouble(2); + publisher->contributed_amount = statement.ColumnDouble(3); + + list->push_back(std::move(publisher)); + } + + return true; +} + +} // namespace brave_rewards diff --git a/components/brave_rewards/browser/database/database_contribution_info_publishers.h b/components/brave_rewards/browser/database/database_contribution_info_publishers.h new file mode 100644 index 000000000000..2b89942c579d --- /dev/null +++ b/components/brave_rewards/browser/database/database_contribution_info_publishers.h @@ -0,0 +1,54 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * 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_COMPONENTS_BRAVE_REWARDS_BROWSER_DATABASE_DATABASE_CONTRIBUTION_INFO_PUBLISHERS_H_ +#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_DATABASE_DATABASE_CONTRIBUTION_INFO_PUBLISHERS_H_ + +#include + +#include +#include + +#include "bat/ledger/mojom_structs.h" +#include "brave/components/brave_rewards/browser/database/database_table.h" + +namespace brave_rewards { + +class DatabaseContributionInfoPublishers: public DatabaseTable { + public: + explicit DatabaseContributionInfoPublishers(int current_db_version); + ~DatabaseContributionInfoPublishers() override; + + bool Init(sql::Database* db) override; + + bool CreateTable(sql::Database* db) override; + + bool CreateIndex(sql::Database* db) override; + + bool Migrate(sql::Database* db, const int target); + + bool InsertOrUpdate( + sql::Database* db, + ledger::ContributionInfoPtr info); + + bool GetRecords( + sql::Database* db, + const std::string& contribution_id, + ledger::ContributionPublisherList* list); + + private: + const char* table_name_ = "contribution_info_publishers"; + const int minimum_version_ = 11; + + bool MigrateToV11(sql::Database* db); + + bool CreateTable11(sql::Database* db); + + bool CreateIndex11(sql::Database* db); +}; + +} // namespace brave_rewards + +#endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_DATABASE_DATABASE_CONTRIBUTION_INFO_PUBLISHERS_H_ diff --git a/components/brave_rewards/browser/database/database_util.cc b/components/brave_rewards/browser/database/database_util.cc index b2e53a575f98..5ca8ff76c7a4 100644 --- a/components/brave_rewards/browser/database/database_util.cc +++ b/components/brave_rewards/browser/database/database_util.cc @@ -9,6 +9,21 @@ namespace brave_rewards { +bool DropTable( + sql::Database* db, + const std::string& table_name) { + DCHECK(!table_name.empty()); + if (!db) { + return false; + } + + std::string sql = "PRAGMA foreign_keys = off;"; + sql.append(base::StringPrintf("DROP TABLE %s;", table_name.c_str())); + sql.append("PRAGMA foreign_keys = on;"); + + return db->Execute(sql.c_str()); +} + std::string GenerateDBInsertQuery( sql::Database* db, const std::string& from, diff --git a/components/brave_rewards/browser/database/database_util.h b/components/brave_rewards/browser/database/database_util.h index f8b842969ee0..4104caeaeb57 100644 --- a/components/brave_rewards/browser/database/database_util.h +++ b/components/brave_rewards/browser/database/database_util.h @@ -39,6 +39,10 @@ bool RenameDBTable( const std::string& from, const std::string& to); +bool DropTable( + sql::Database* db, + const std::string& table_name); + } // namespace brave_rewards #endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_DATABASE_DATABASE_UTIL_H_ diff --git a/components/brave_rewards/browser/database/publisher_info_database.cc b/components/brave_rewards/browser/database/publisher_info_database.cc index d626d1780849..91e550609631 100644 --- a/components/brave_rewards/browser/database/publisher_info_database.cc +++ b/components/brave_rewards/browser/database/publisher_info_database.cc @@ -16,7 +16,6 @@ #include "sql/statement.h" #include "sql/transaction.h" #include "brave/components/brave_rewards/browser/content_site.h" -#include "brave/components/brave_rewards/browser/recurring_donation.h" #include "brave/components/brave_rewards/browser/rewards_p3a.h" #include "brave/components/brave_rewards/browser/database/publisher_info_database.h" #include "brave/components/brave_rewards/browser/database/database_util.h" @@ -25,7 +24,7 @@ namespace brave_rewards { namespace { -const int kCurrentVersionNumber = 10; +const int kCurrentVersionNumber = 11; const int kCompatibleVersionNumber = 1; } // namespace @@ -134,7 +133,7 @@ bool PublisherInfoDatabase::Init() { * */ bool PublisherInfoDatabase::InsertOrUpdateContributionInfo( - const brave_rewards::ContributionInfo& info) { + ledger::ContributionInfoPtr info) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); bool initialized = Init(); @@ -144,7 +143,7 @@ bool PublisherInfoDatabase::InsertOrUpdateContributionInfo( return false; } - return contribution_info_->InsertOrUpdate(&GetDB(), info); + return contribution_info_->InsertOrUpdate(&GetDB(), std::move(info)); } void PublisherInfoDatabase::GetOneTimeTips( @@ -792,13 +791,13 @@ bool PublisherInfoDatabase::CreateRecurringTipsIndex() { } bool PublisherInfoDatabase::InsertOrUpdateRecurringTip( - const brave_rewards::RecurringDonation& info) { + ledger::RecurringTipPtr info) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); bool initialized = Init(); DCHECK(initialized); - if (!initialized || info.publisher_key.empty()) { + if (!initialized || !info || info->publisher_key.empty()) { return false; } @@ -808,9 +807,9 @@ bool PublisherInfoDatabase::InsertOrUpdateRecurringTip( "(publisher_id, amount, added_date) " "VALUES (?, ?, ?)")); - statement.BindString(0, info.publisher_key); - statement.BindDouble(1, info.amount); - statement.BindInt64(2, info.added_date); + statement.BindString(0, info->publisher_key); + statement.BindDouble(1, info->amount); + statement.BindInt64(2, info->created_at); return statement.Run(); } @@ -1751,6 +1750,19 @@ bool PublisherInfoDatabase::MigrateV9toV10() { return true; } +bool PublisherInfoDatabase::MigrateV10toV11() { + sql::Transaction transaction(&GetDB()); + if (!transaction.Begin()) { + return false; + } + + if (!contribution_info_->Migrate(&GetDB(), 11)) { + return false; + } + + return transaction.Commit(); +} + bool PublisherInfoDatabase::Migrate(int version) { switch (version) { case 2: { @@ -1780,6 +1792,9 @@ bool PublisherInfoDatabase::Migrate(int version) { case 10: { return MigrateV9toV10(); } + case 11: { + return MigrateV10toV11(); + } default: return false; } diff --git a/components/brave_rewards/browser/database/publisher_info_database.h b/components/brave_rewards/browser/database/publisher_info_database.h index 4a6ba4b0a336..6d6262af7184 100644 --- a/components/brave_rewards/browser/database/publisher_info_database.h +++ b/components/brave_rewards/browser/database/publisher_info_database.h @@ -19,14 +19,12 @@ #include "base/memory/memory_pressure_listener.h" #include "base/sequence_checker.h" #include "bat/ledger/mojom_structs.h" -#include "brave/components/brave_rewards/browser/contribution_info.h" #include "brave/components/brave_rewards/browser/database/database_contribution_info.h" #include "brave/components/brave_rewards/browser/database/database_contribution_queue.h" #include "brave/components/brave_rewards/browser/database/database_promotion.h" #include "brave/components/brave_rewards/browser/database/database_server_publisher_info.h" #include "brave/components/brave_rewards/browser/database/database_unblinded_token.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" #include "sql/init_status.h" @@ -47,8 +45,7 @@ class PublisherInfoDatabase { db_.set_error_callback(error_callback); } - bool InsertOrUpdateContributionInfo( - const brave_rewards::ContributionInfo& info); + bool InsertOrUpdateContributionInfo(ledger::ContributionInfoPtr info); void GetOneTimeTips( ledger::PublisherInfoList* list, @@ -82,8 +79,7 @@ class PublisherInfoDatabase { ledger::PublisherInfoPtr GetMediaPublisherInfo( const std::string& media_key); - bool InsertOrUpdateRecurringTip( - const brave_rewards::RecurringDonation& info); + bool InsertOrUpdateRecurringTip(ledger::RecurringTipPtr info); void GetRecurringTips(ledger::PublisherInfoList* list); @@ -195,6 +191,8 @@ class PublisherInfoDatabase { bool MigrateV9toV10(); + bool MigrateV10toV11(); + bool Migrate(int version); sql::InitStatus EnsureCurrentVersion(); diff --git a/components/brave_rewards/browser/database/publisher_info_database_unittest.cc b/components/brave_rewards/browser/database/publisher_info_database_unittest.cc index ad9652fb2c28..2a595939b39d 100644 --- a/components/brave_rewards/browser/database/publisher_info_database_unittest.cc +++ b/components/brave_rewards/browser/database/publisher_info_database_unittest.cc @@ -16,6 +16,7 @@ #include "base/path_service.h" #include "base/strings/string_util.h" #include "base/strings/string_split.h" +#include "base/strings/stringprintf.h" #include "base/strings/utf_string_conversions.h" #include "brave/common/brave_paths.h" #include "bat/ledger/global_constants.h" @@ -146,30 +147,31 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdateContributionInfo) { base::FilePath db_file; CreateTempDatabase(&temp_dir, &db_file); - ContributionInfo info; - info.probi = "12345678901234567890123456789012345678901234"; - info.month = static_cast(ledger::ActivityMonth::JANUARY); - info.year = 1970; - info.type = static_cast(ledger::RewardsType::AUTO_CONTRIBUTE); - info.date = base::Time::Now().ToJsTime(); - info.publisher_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + auto info = ledger::ContributionInfo::New(); + info->contribution_id = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + info->amount = 5.0; + info->type = ledger::RewardsType::AUTO_CONTRIBUTE; + info->step = -1; + info->retry_count = -1; + info->created_at = base::Time::Now().ToJsTime(); - bool success = publisher_info_database_->InsertOrUpdateContributionInfo(info); + bool success = + publisher_info_database_->InsertOrUpdateContributionInfo(info->Clone()); EXPECT_TRUE(success); - std::string query = "SELECT * FROM contribution_info WHERE publisher_id=?"; + std::string query = "SELECT * FROM contribution_info WHERE contribution_id=?"; sql::Statement info_sql(GetDB().GetUniqueStatement(query.c_str())); - info_sql.BindString(0, info.publisher_key); + info_sql.BindString(0, info->contribution_id); EXPECT_TRUE(info_sql.Step()); EXPECT_EQ(CountTableRows("contribution_info"), 1); - EXPECT_EQ(info_sql.ColumnString(0), info.publisher_key); - EXPECT_EQ(info_sql.ColumnString(1), info.probi); - EXPECT_EQ(info_sql.ColumnInt64(2), info.date); - EXPECT_EQ(info_sql.ColumnInt(3), info.type); - EXPECT_EQ(info_sql.ColumnInt(4), info.month); - EXPECT_EQ(info_sql.ColumnInt(5), info.year); + EXPECT_EQ(info_sql.ColumnString(0), info->contribution_id); + EXPECT_EQ(info_sql.ColumnDouble(1), info->amount); + EXPECT_EQ(info_sql.ColumnInt(2), static_cast(info->type)); + EXPECT_EQ(info_sql.ColumnInt(3), info->step); + EXPECT_EQ(info_sql.ColumnInt(4), info->retry_count); + EXPECT_EQ(info_sql.ColumnInt64(5), static_cast(info->created_at)); } TEST_F(PublisherInfoDatabaseTest, InsertOrUpdatePublisherInfo) { @@ -572,56 +574,56 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdateRecurringTip) { base::FilePath db_file; CreateTempDatabase(&temp_dir, &db_file); - brave_rewards::RecurringDonation info; - info.publisher_key = "key"; - info.amount = 20; - info.added_date = base::Time::Now().ToJsTime(); + auto info = ledger::RecurringTip::New(); + info->publisher_key = "key"; + info->amount = 20; + info->created_at = base::Time::Now().ToJsTime(); bool success = publisher_info_database_->InsertOrUpdateRecurringTip( - info); + info->Clone()); EXPECT_TRUE(success); std::string query = "SELECT * FROM recurring_donation WHERE publisher_id=?"; sql::Statement info_sql(GetDB().GetUniqueStatement(query.c_str())); - info_sql.BindString(0, info.publisher_key); + info_sql.BindString(0, info->publisher_key); EXPECT_TRUE(info_sql.Step()); EXPECT_EQ(CountTableRows("recurring_donation"), 1); - EXPECT_EQ(info_sql.ColumnString(0), info.publisher_key); - EXPECT_EQ(info_sql.ColumnDouble(1), info.amount); - EXPECT_EQ(info_sql.ColumnInt64(2), info.added_date); + EXPECT_EQ(info_sql.ColumnString(0), info->publisher_key); + EXPECT_EQ(info_sql.ColumnDouble(1), info->amount); + EXPECT_EQ(info_sql.ColumnInt64(2), static_cast(info->created_at)); /** * Make sure that second insert is update and not insert */ - info.amount = 30; + info->amount = 30; - success = publisher_info_database_->InsertOrUpdateRecurringTip(info); + success = publisher_info_database_->InsertOrUpdateRecurringTip(info->Clone()); EXPECT_TRUE(success); query ="SELECT * FROM recurring_donation WHERE publisher_id=?"; sql::Statement info_sql_1(GetDB().GetUniqueStatement(query.c_str())); - info_sql_1.BindString(0, info.publisher_key); + info_sql_1.BindString(0, info->publisher_key); EXPECT_TRUE(info_sql_1.Step()); EXPECT_EQ(CountTableRows("recurring_donation"), 1); - EXPECT_EQ(info_sql_1.ColumnString(0), info.publisher_key); - EXPECT_EQ(info_sql_1.ColumnDouble(1), info.amount); - EXPECT_EQ(info_sql_1.ColumnInt64(2), info.added_date); + EXPECT_EQ(info_sql_1.ColumnString(0), info->publisher_key); + EXPECT_EQ(info_sql_1.ColumnDouble(1), info->amount); + EXPECT_EQ(info_sql_1.ColumnInt64(2), static_cast(info->created_at)); /** * Publisher key is missing */ - info.publisher_key = ""; - success = publisher_info_database_->InsertOrUpdateRecurringTip(info); + info->publisher_key = ""; + success = publisher_info_database_->InsertOrUpdateRecurringTip(info->Clone()); EXPECT_FALSE(success); query = "SELECT * FROM recurring_donation WHERE publisher_id=?"; sql::Statement info_sql_2(GetDB().GetUniqueStatement(query.c_str())); - info_sql_2.BindString(0, info.publisher_key); + info_sql_2.BindString(0, info->publisher_key); EXPECT_FALSE(info_sql_2.Step()); } @@ -1106,27 +1108,23 @@ TEST_F(PublisherInfoDatabaseTest, Migrationv7tov8_ContributionInfo) { CreateMigrationDatabase(&temp_dir, &db_file, 7, 8); EXPECT_TRUE(publisher_info_database_->Init()); - ContributionInfo contribution; - contribution.probi = "1000000000000000000"; - contribution.month = static_cast(ledger::ActivityMonth::OCTOBER); - contribution.year = 2019; - contribution.type = static_cast(ledger::RewardsType::ONE_TIME_TIP); - contribution.date = 1570614352; - contribution.publisher_key = "3zsistemi.si"; + const std::string publisher_key = "3zsistemi.si"; std::string query = "SELECT * FROM contribution_info WHERE publisher_id=?"; sql::Statement info_sql(GetDB().GetUniqueStatement(query.c_str())); - info_sql.BindString(0, contribution.publisher_key); + info_sql.BindString(0, publisher_key); EXPECT_TRUE(info_sql.Step()); EXPECT_EQ(CountTableRows("contribution_info"), 1); - EXPECT_EQ(info_sql.ColumnString(0), contribution.publisher_key); - EXPECT_EQ(info_sql.ColumnString(1), contribution.probi); - EXPECT_EQ(info_sql.ColumnInt64(2), contribution.date); - EXPECT_EQ(info_sql.ColumnInt(3), contribution.type); - EXPECT_EQ(info_sql.ColumnInt(4), contribution.month); - EXPECT_EQ(info_sql.ColumnInt(5), contribution.year); + EXPECT_EQ(info_sql.ColumnString(0), publisher_key); + EXPECT_EQ(info_sql.ColumnString(1), "1000000000000000000"); + EXPECT_EQ(info_sql.ColumnInt64(2), 1570614352); + EXPECT_EQ(info_sql.ColumnInt(3), + static_cast(ledger::RewardsType::ONE_TIME_TIP)); + EXPECT_EQ(info_sql.ColumnInt(4), + static_cast(ledger::ActivityMonth::OCTOBER)); + EXPECT_EQ(info_sql.ColumnInt(5), 2019); } TEST_F(PublisherInfoDatabaseTest, Migrationv7tov8_PendingContribution) { @@ -1182,6 +1180,65 @@ TEST_F(PublisherInfoDatabaseTest, Migrationv9tov10) { EXPECT_EQ(schema, GetSchemaString(10)); } +TEST_F(PublisherInfoDatabaseTest, Migrationv10tov11) { + base::ScopedTempDir temp_dir; + base::FilePath db_file; + CreateMigrationDatabase(&temp_dir, &db_file, 10, 11); + EXPECT_TRUE(publisher_info_database_->Init()); + + EXPECT_EQ(publisher_info_database_->GetTableVersionNumber(), 11); + + const std::string schema = publisher_info_database_->GetSchema(); + EXPECT_EQ(schema, GetSchemaString(11)); +} + +TEST_F(PublisherInfoDatabaseTest, Migrationv10tov11_ContributionInfo) { + base::ScopedTempDir temp_dir; + base::FilePath db_file; + CreateMigrationDatabase(&temp_dir, &db_file, 10, 11); + EXPECT_TRUE(publisher_info_database_->Init()); + EXPECT_EQ(CountTableRows("contribution_info"), 5); + EXPECT_EQ(CountTableRows("contribution_info_publishers"), 4); + + const std::string query = + "SELECT ci.contribution_id, ci.amount, ci.type, ci.created_at, " + "cip.publisher_key, cip.total_amount, cip.contributed_amount " + "FROM contribution_info as ci " + "LEFT JOIN contribution_info_publishers AS cip " + "ON ci.contribution_id = cip.contribution_id " + "WHERE ci.contribution_id = ?"; + + // one time tip + const std::string tip_id = "id_1570614352_0"; + sql::Statement tip_sql(GetDB().GetUniqueStatement(query.c_str())); + tip_sql.BindString(0, tip_id); + + ASSERT_TRUE(tip_sql.Step()); + EXPECT_EQ(tip_sql.ColumnString(0), tip_id); + EXPECT_EQ(tip_sql.ColumnDouble(1), 1.0); + EXPECT_EQ(tip_sql.ColumnInt(2), + static_cast(ledger::RewardsType::ONE_TIME_TIP));; + EXPECT_EQ(tip_sql.ColumnInt64(3), 1570614352); + EXPECT_EQ(tip_sql.ColumnString(4), "3zsistemi.si"); + EXPECT_EQ(tip_sql.ColumnDouble(5), 1.0); + EXPECT_EQ(tip_sql.ColumnDouble(6), 1.0); + + // Auto contribute + const std::string ac_id = "id_1574671381_4"; + sql::Statement ac_sql(GetDB().GetUniqueStatement(query.c_str())); + ac_sql.BindString(0, ac_id); + + ASSERT_TRUE(ac_sql.Step()); + EXPECT_EQ(ac_sql.ColumnString(0), ac_id); + EXPECT_EQ(ac_sql.ColumnDouble(1), 10.0); + EXPECT_EQ(ac_sql.ColumnInt(2), + static_cast(ledger::RewardsType::AUTO_CONTRIBUTE));; + EXPECT_EQ(ac_sql.ColumnInt64(3), 1574671381); + EXPECT_EQ(ac_sql.ColumnString(4), ""); + EXPECT_EQ(ac_sql.ColumnDouble(5), 0.0); + EXPECT_EQ(ac_sql.ColumnDouble(6), 0.0); +} + TEST_F(PublisherInfoDatabaseTest, DeleteActivityInfo) { base::ScopedTempDir temp_dir; base::FilePath db_file; diff --git a/components/brave_rewards/browser/extension_rewards_service_observer.cc b/components/brave_rewards/browser/extension_rewards_service_observer.cc index 0d36a923f080..1b9468f3b89f 100644 --- a/components/brave_rewards/browser/extension_rewards_service_observer.cc +++ b/components/brave_rewards/browser/extension_rewards_service_observer.cc @@ -358,7 +358,7 @@ void ExtensionRewardsServiceObserver::OnReconcileComplete( RewardsService* rewards_service, unsigned int result, const std::string& viewing_id, - const std::string& probi, + const double amount, const int32_t type) { auto* event_router = extensions::EventRouter::Get(profile_); if (!event_router) { diff --git a/components/brave_rewards/browser/extension_rewards_service_observer.h b/components/brave_rewards/browser/extension_rewards_service_observer.h index c00b2d9ccd41..278d9c60bad1 100644 --- a/components/brave_rewards/browser/extension_rewards_service_observer.h +++ b/components/brave_rewards/browser/extension_rewards_service_observer.h @@ -50,11 +50,12 @@ class ExtensionRewardsServiceObserver : public RewardsServiceObserver, void OnPendingContributionRemoved(RewardsService* rewards_service, int32_t result) override; - void OnReconcileComplete(RewardsService* rewards_service, - unsigned int result, - const std::string& viewing_id, - const std::string& probi, - const int32_t type) override; + void OnReconcileComplete( + RewardsService* rewards_service, + unsigned int result, + const std::string& viewing_id, + const double amount, + const int32_t type) override; void OnDisconnectWallet( brave_rewards::RewardsService* rewards_service, diff --git a/components/brave_rewards/browser/recurring_donation.cc b/components/brave_rewards/browser/recurring_donation.cc deleted file mode 100644 index c40d6edc6b4b..000000000000 --- a/components/brave_rewards/browser/recurring_donation.cc +++ /dev/null @@ -1,21 +0,0 @@ -/* 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/recurring_donation.h" - -namespace brave_rewards { - - RecurringDonation::RecurringDonation() : - amount(0), - added_date(0) { - } - - RecurringDonation::~RecurringDonation() { } - - RecurringDonation::RecurringDonation(const RecurringDonation &data) { - publisher_key = data.publisher_key; - amount = data.amount; - added_date = data.added_date; - } - -} // namespace brave_rewards diff --git a/components/brave_rewards/browser/recurring_donation.h b/components/brave_rewards/browser/recurring_donation.h deleted file mode 100644 index 176fdf59a172..000000000000 --- a/components/brave_rewards/browser/recurring_donation.h +++ /dev/null @@ -1,27 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * 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_COMPONENTS_BRAVE_REWARDS_BROWSER_RECURRING_DONATION_H_ -#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_RECURRING_DONATION_H_ - -#include - -// TODO(nejczdovc): Rename to RecurringTip - -namespace brave_rewards { - -struct RecurringDonation { - RecurringDonation(); - ~RecurringDonation(); - RecurringDonation(const RecurringDonation& data); - - std::string publisher_key; - double amount = 0; - uint32_t added_date = 0; -}; - -} // namespace brave_rewards - -#endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_RECURRING_DONATION_H_ diff --git a/components/brave_rewards/browser/rewards_notification_service_impl.cc b/components/brave_rewards/browser/rewards_notification_service_impl.cc index bed8e510aa78..01ad2764599b 100644 --- a/components/brave_rewards/browser/rewards_notification_service_impl.cc +++ b/components/brave_rewards/browser/rewards_notification_service_impl.cc @@ -370,7 +370,7 @@ void RewardsNotificationServiceImpl::OnReconcileComplete( RewardsService* rewards_service, unsigned int result, const std::string& viewing_id, - const std::string& probi, + const double amount, const int32_t type) { auto converted_result = static_cast(result); auto converted_type = static_cast(type); @@ -383,7 +383,7 @@ void RewardsNotificationServiceImpl::OnReconcileComplete( args.push_back(viewing_id); args.push_back(std::to_string(result)); args.push_back(std::to_string(type)); - args.push_back(probi); + args.push_back(std::to_string(amount)); AddNotification( RewardsNotificationService::REWARDS_NOTIFICATION_AUTO_CONTRIBUTE, diff --git a/components/brave_rewards/browser/rewards_notification_service_impl.h b/components/brave_rewards/browser/rewards_notification_service_impl.h index 51c290224b1d..11294d740cb3 100644 --- a/components/brave_rewards/browser/rewards_notification_service_impl.h +++ b/components/brave_rewards/browser/rewards_notification_service_impl.h @@ -58,11 +58,12 @@ class RewardsNotificationServiceImpl RewardsService* rewards_service, const uint32_t result, Promotion promotion) override; - void OnReconcileComplete(RewardsService* rewards_service, - unsigned int result, - const std::string& viewing_id, - const std::string& probi, - const int32_t type) override; + void OnReconcileComplete( + RewardsService* rewards_service, + unsigned int result, + const std::string& viewing_id, + const double amount, + const int32_t type) override; void TriggerOnNotificationAdded( const RewardsNotification& rewards_notification); diff --git a/components/brave_rewards/browser/rewards_service_browsertest.cc b/components/brave_rewards/browser/rewards_service_browsertest.cc index 756529abaa21..d8408a578ebe 100644 --- a/components/brave_rewards/browser/rewards_service_browsertest.cc +++ b/components/brave_rewards/browser/rewards_service_browsertest.cc @@ -1432,20 +1432,13 @@ class BraveRewardsBrowserTest wait_for_publisher_list_normalized_loop_->Quit(); } - void OnReconcileComplete(brave_rewards::RewardsService* rewards_service, - unsigned int result, - const std::string& viewing_id, - const std::string& probi, - const int32_t type) { - const size_t size = probi.size(); - std::string amount = "0"; - if (size > 18) { - amount = probi; - amount.insert(size - 18, "."); - } - - double amount_double = std::stod(amount); - UpdateContributionBalance(amount_double, true); + void OnReconcileComplete( + brave_rewards::RewardsService* rewards_service, + unsigned int result, + const std::string& viewing_id, + const double amount, + const int32_t type) { + UpdateContributionBalance(amount, true); const auto converted_result = static_cast(result); const auto converted_type = @@ -1461,7 +1454,7 @@ class BraveRewardsBrowserTest if (converted_type == ledger::RewardsType::ONE_TIME_TIP || converted_type == ledger::RewardsType::RECURRING_TIP) { - reconciled_tip_total_ += amount_double; + reconciled_tip_total_ += amount; // Single tip tracking tip_reconcile_completed_ = true; diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index af60fd850a32..a9a045896443 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -1025,25 +1025,13 @@ void RewardsServiceImpl::OnRecoverWallet( } void RewardsServiceImpl::OnReconcileComplete( - ledger::Result result, + const ledger::Result result, const std::string& viewing_id, - const std::string& probi, + const double amount, const ledger::RewardsType type) { - if (result == ledger::Result::LEDGER_OK) { - auto now = base::Time::Now(); - if (!Connected()) - return; - - if (type == ledger::RewardsType::RECURRING_TIP) { - MaybeShowNotificationTipsPaid(); - } - - bat_ledger_->OnReconcileCompleteSuccess(viewing_id, - type, - probi, - GetPublisherMonth(now), - GetPublisherYear(now), - GetCurrentTimestamp()); + if (result == ledger::Result::LEDGER_OK && + type == ledger::RewardsType::RECURRING_TIP) { + MaybeShowNotificationTipsPaid(); } GetCurrentBalanceReport(); @@ -1051,7 +1039,7 @@ void RewardsServiceImpl::OnReconcileComplete( observer.OnReconcileComplete(this, static_cast(result), viewing_id, - probi, + amount, static_cast(type)); } @@ -2436,53 +2424,31 @@ void RewardsServiceImpl::OnTip(const std::string& publisher_key, bat_ledger_->DoDirectTip(publisher_key, amount, "BAT", base::DoNothing()); } -bool SaveContributionInfoOnFileTaskRunner( - const brave_rewards::ContributionInfo info, - PublisherInfoDatabase* backend) { - if (backend && backend->InsertOrUpdateContributionInfo(info)) - return true; +ledger::Result SaveContributionInfoOnFileTaskRunner( + ledger::ContributionInfoPtr info, + PublisherInfoDatabase* backend) { + const bool result = + backend && backend->InsertOrUpdateContributionInfo(std::move(info)); - return false; + return result ? ledger::Result::LEDGER_OK : ledger::Result::LEDGER_ERROR; } void RewardsServiceImpl::OnContributionInfoSaved( - const ledger::RewardsType type, - bool success) { - for (auto& observer : observers_) { - observer.OnContributionSaved(this, success, static_cast(type)); - } + ledger::ResultCallback callback, + const ledger::Result result) { + callback(result); } -void RewardsServiceImpl::SaveContributionInfo(const std::string& probi, - const ledger::ActivityMonth month, - const int year, - const uint32_t date, - const std::string& publisher_key, - const ledger::RewardsType type) { - brave_rewards::ContributionInfo info; - info.probi = probi; - info.month = static_cast(month); - info.year = year; - info.date = date; - info.publisher_key = publisher_key; - info.type = static_cast(type); - +void RewardsServiceImpl::SaveContributionInfo( + ledger::ContributionInfoPtr info, + ledger::ResultCallback callback) { base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE, - base::Bind(&SaveContributionInfoOnFileTaskRunner, - info, + base::BindOnce(&SaveContributionInfoOnFileTaskRunner, + std::move(info), publisher_info_backend_.get()), - base::Bind(&RewardsServiceImpl::OnContributionInfoSaved, + base::BindOnce(&RewardsServiceImpl::OnContributionInfoSaved, AsWeakPtr(), - type)); -} - -bool SaveRecurringTipOnFileTaskRunner( - const brave_rewards::RecurringDonation info, - PublisherInfoDatabase* backend) { - if (backend && backend->InsertOrUpdateRecurringTip(info)) - return true; - - return false; + callback)); } void RewardsServiceImpl::OnSaveRecurringTipUI( @@ -2501,10 +2467,10 @@ void RewardsServiceImpl::SaveRecurringTipUI( const std::string& publisher_key, const double amount, SaveRecurringTipCallback callback) { - ledger::ContributionInfoPtr info = ledger::ContributionInfo::New(); - info->publisher = publisher_key; - info->value = amount; - info->date = GetCurrentTimestamp(); + ledger::RecurringTipPtr info = ledger::RecurringTip::New(); + info->publisher_key = publisher_key; + info->amount = amount; + info->created_at = GetCurrentTimestamp(); bat_ledger_->SaveRecurringTip( std::move(info), @@ -2524,24 +2490,28 @@ void RewardsServiceImpl::OnRecurringTipSaved( : ledger::Result::LEDGER_ERROR); } +bool SaveRecurringTipOnFileTaskRunner( + ledger::RecurringTipPtr info, + PublisherInfoDatabase* backend) { + if (backend && backend->InsertOrUpdateRecurringTip(std::move(info))) + return true; + + return false; +} + void RewardsServiceImpl::SaveRecurringTip( - ledger::ContributionInfoPtr info, + ledger::RecurringTipPtr info, ledger::SaveRecurringTipCallback callback) { if (!info) { callback(ledger::Result::NOT_FOUND); return; } - brave_rewards::RecurringDonation new_info; - new_info.publisher_key = info->publisher; - new_info.amount = info->value; - new_info.added_date = info->date; - base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE, - base::Bind(&SaveRecurringTipOnFileTaskRunner, - new_info, + base::BindOnce(&SaveRecurringTipOnFileTaskRunner, + std::move(info), publisher_info_backend_.get()), - base::Bind(&RewardsServiceImpl::OnRecurringTipSaved, + base::BindOnce(&RewardsServiceImpl::OnRecurringTipSaved, AsWeakPtr(), callback)); } diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index 3f2aaac89870..117f0a422923 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -28,7 +28,6 @@ #include "mojo/public/cpp/bindings/associated_binding.h" #include "brave/components/brave_rewards/browser/balance_report.h" #include "brave/components/brave_rewards/browser/content_site.h" -#include "brave/components/brave_rewards/browser/contribution_info.h" #include "ui/gfx/image/image.h" #include "brave/components/brave_rewards/browser/publisher_banner.h" #include "brave/components/brave_rewards/browser/rewards_service_private_observer.h" @@ -365,8 +364,10 @@ class RewardsServiceImpl : public RewardsService, ledger::PublisherInfoPtr publisher_info); void OnResetTheWholeState(base::Callback callback, bool success); - void OnContributionInfoSaved(const ledger::RewardsType type, - bool success); + void OnContributionInfoSaved( + ledger::ResultCallback callback, + const ledger::Result result); + void OnRecurringTipSaved( ledger::SaveRecurringTipCallback callback, const bool success); @@ -518,10 +519,11 @@ class RewardsServiceImpl : public RewardsService, void OnRecoverWallet( ledger::Result result, double balance); - void OnReconcileComplete(ledger::Result result, - const std::string& viewing_id, - const std::string& probi, - const ledger::RewardsType type) override; + void OnReconcileComplete( + const ledger::Result result, + const std::string& viewing_id, + const double amount, + const ledger::RewardsType type) override; void OnAttestPromotion( AttestPromotionCallback callback, const ledger::Result result, @@ -583,14 +585,12 @@ class RewardsServiceImpl : public RewardsService, void OnSetOnDemandFaviconComplete(const std::string& favicon_url, ledger::FetchIconCallback callback, bool success); - void SaveContributionInfo(const std::string& probi, - const ledger::ActivityMonth month, - const int year, - const uint32_t date, - const std::string& publisher_key, - const ledger::RewardsType type) override; - void SaveRecurringTip( + void SaveContributionInfo( ledger::ContributionInfoPtr info, + ledger::ResultCallback callback) override; + + void SaveRecurringTip( + ledger::RecurringTipPtr info, ledger::SaveRecurringTipCallback callback) override; void GetRecurringTips( diff --git a/components/brave_rewards/browser/rewards_service_impl_unittest.cc b/components/brave_rewards/browser/rewards_service_impl_unittest.cc index a08c5d95a485..17a4660ac858 100644 --- a/components/brave_rewards/browser/rewards_service_impl_unittest.cc +++ b/components/brave_rewards/browser/rewards_service_impl_unittest.cc @@ -42,7 +42,7 @@ class MockRewardsServiceObserver : public RewardsServiceObserver { MOCK_METHOD5(OnReconcileComplete, void(RewardsService*, unsigned int, const std::string&, - const std::string&, + const double, const int32_t)); MOCK_METHOD2(OnGetRecurringTips, void(RewardsService*, const brave_rewards::ContentSiteList&)); diff --git a/components/brave_rewards/browser/rewards_service_observer.h b/components/brave_rewards/browser/rewards_service_observer.h index dbb2a8b68e16..79da205907fb 100644 --- a/components/brave_rewards/browser/rewards_service_observer.h +++ b/components/brave_rewards/browser/rewards_service_observer.h @@ -54,7 +54,7 @@ class RewardsServiceObserver : public base::CheckedObserver { RewardsService* rewards_service, unsigned int result, const std::string& viewing_id, - const std::string& probi, + const double amount, const int32_t type) {} virtual void OnAdsEnabled( brave_rewards::RewardsService* rewards_service, @@ -76,10 +76,6 @@ class RewardsServiceObserver : public base::CheckedObserver { virtual void OnRecurringTipRemoved( brave_rewards::RewardsService* rewards_service, bool success) {} - virtual void OnContributionSaved( - brave_rewards::RewardsService* rewards_service, - bool success, - int type) {} virtual void OnPendingContributionRemoved( brave_rewards::RewardsService* rewards_service, int32_t result) {} diff --git a/components/brave_rewards/resources/page/actions/rewards_actions.ts b/components/brave_rewards/resources/page/actions/rewards_actions.ts index dd89aa07401b..fc1a9a724930 100644 --- a/components/brave_rewards/resources/page/actions/rewards_actions.ts +++ b/components/brave_rewards/resources/page/actions/rewards_actions.ts @@ -248,11 +248,6 @@ export const onRecurringTipRemoved = (success: boolean) => action(types.ON_RECUR success }) -export const onContributionSaved = (properties: Rewards.ContributionSaved) => - action(types.ON_CONTRIBUTION_SAVED, { - properties - }) - export const onInlineTipSettingChange = (key: string, value: boolean) => action(types.ON_INLINE_TIP_SETTINGS_CHANGE, { key, value diff --git a/components/brave_rewards/resources/page/brave_rewards_page.tsx b/components/brave_rewards/resources/page/brave_rewards_page.tsx index 4d1cd9ee233a..e52aee27eb26 100644 --- a/components/brave_rewards/resources/page/brave_rewards_page.tsx +++ b/components/brave_rewards/resources/page/brave_rewards_page.tsx @@ -179,10 +179,6 @@ window.cr.define('brave_rewards', function () { getActions().onRecurringTipRemoved(success) } - function onContributionSaved (properties: Rewards.ContributionSaved) { - getActions().onContributionSaved(properties) - } - function pendingContributions (list: Rewards.PendingContribution[]) { getActions().onPendingContributions(list) } @@ -208,6 +204,10 @@ window.cr.define('brave_rewards', function () { getActions().getBalance() getActions().getWalletProperties() + if (properties.type === 8) { // Rewards.RewardsType.ONE_TIME_TIP + chrome.send('brave_rewards.getOneTimeTips') + } + // EXPIRED TOKEN if (properties.result === 24) { getActions().getExternalWallet('uphold') @@ -272,7 +272,6 @@ window.cr.define('brave_rewards', function () { transactionHistoryChanged, recurringTipSaved, recurringTipRemoved, - onContributionSaved, onRemovePendingContribution, excludedSiteChanged, balance, diff --git a/components/brave_rewards/resources/page/components/tipsBox.tsx b/components/brave_rewards/resources/page/components/tipsBox.tsx index 4416da0b9f46..50a028f42022 100644 --- a/components/brave_rewards/resources/page/components/tipsBox.tsx +++ b/components/brave_rewards/resources/page/components/tipsBox.tsx @@ -72,7 +72,7 @@ class TipBox extends React.Component { faviconUrl = `chrome://favicon/size/48@1x/${item.favIcon}` } - const token = utils.convertProbiToFixed(item.percentage.toString()) + const token = item.percentage.toFixed(1) return { profile: { @@ -183,7 +183,7 @@ class TipBox extends React.Component { const topRows = tipRows.slice(0, 5) const numRows = tipRows && tipRows.length const allSites = !(numRows > 5) - const total = utils.tipsListTotal(tipsList, true) + const total = utils.tipsListTotal(tipsList) const converted = utils.convertBalance(total, balance.rates) const { onlyAnonWallet } = ui diff --git a/components/brave_rewards/resources/page/constants/rewards_types.ts b/components/brave_rewards/resources/page/constants/rewards_types.ts index d9b5444cf14f..104bdf150761 100644 --- a/components/brave_rewards/resources/page/constants/rewards_types.ts +++ b/components/brave_rewards/resources/page/constants/rewards_types.ts @@ -70,7 +70,6 @@ export const enum types { GET_REWARDS_MAIN_ENABLED = '@@rewards/GET_REWARDS_MAIN_ENABLED', ON_RECURRING_TIP_SAVED = '@@rewards/ON_RECURRING_TIP_SAVED', ON_RECURRING_TIP_REMOVED = '@@rewards/ON_RECURRING_TIP_REMOVED', - ON_CONTRIBUTION_SAVED = '@@rewards/ON_CONTRIBUTION_SAVED', ON_INLINE_TIP_SETTINGS_CHANGE = '@@rewards/ON_INLINE_TIP_SETTINGS_CHANGE', REMOVE_PENDING_CONTRIBUTION = '@@rewards/REMOVE_PENDING_CONTRIBUTION', REMOVE_ALL_PENDING_CONTRIBUTION = '@@rewards/REMOVE_ALL_PENDING_CONTRIBUTION', diff --git a/components/brave_rewards/resources/page/reducers/rewards_reducer.ts b/components/brave_rewards/resources/page/reducers/rewards_reducer.ts index cfe9281bb7da..3d006e963017 100644 --- a/components/brave_rewards/resources/page/reducers/rewards_reducer.ts +++ b/components/brave_rewards/resources/page/reducers/rewards_reducer.ts @@ -246,14 +246,6 @@ const rewardsReducer: Reducer = (state: Rewards.State chrome.send('brave_rewards.getRewardsMainEnabled', []) break } - case types.ON_CONTRIBUTION_SAVED: { - const properties = action.payload.properties - console.log(properties) - if (properties && properties.success && properties.type === 8) { - chrome.send('brave_rewards.getOneTimeTips') - } - break - } case types.ON_INLINE_TIP_SETTINGS_CHANGE: { if (!state.inlineTip) { state.inlineTip = { diff --git a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc index 65feb87421a3..a7d2db592b7d 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc +++ b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc @@ -136,7 +136,7 @@ void BatLedgerClientMojoProxy::OnWalletProperties( void BatLedgerClientMojoProxy::OnReconcileComplete( ledger::Result result, const std::string& viewing_id, - const std::string& probi, + const double amount, const ledger::RewardsType type) { if (!Connected()) return; @@ -144,7 +144,7 @@ void BatLedgerClientMojoProxy::OnReconcileComplete( bat_ledger_client_->OnReconcileComplete( result, viewing_id, - probi, + amount, type); } @@ -365,7 +365,7 @@ void OnSaveRecurringTip(const ledger::SaveRecurringTipCallback& callback, } void BatLedgerClientMojoProxy::SaveRecurringTip( - ledger::ContributionInfoPtr info, + ledger::RecurringTipPtr info, ledger::SaveRecurringTipCallback callback) { if (!Connected()) { callback(ledger::Result::LEDGER_ERROR); @@ -447,23 +447,21 @@ void BatLedgerClientMojoProxy::RemoveRecurringTip( base::BindOnce(&OnRemoveRecurringTip, std::move(callback))); } +void OnSaveContributionInfo( + const ledger::ResultCallback& callback, + const ledger::Result result) { + callback(result); +} + void BatLedgerClientMojoProxy::SaveContributionInfo( - const std::string& probi, - const ledger::ActivityMonth month, - const int year, - const uint32_t date, - const std::string& publisher_key, - const ledger::RewardsType type) { + ledger::ContributionInfoPtr info, + ledger::ResultCallback callback) { if (!Connected()) return; bat_ledger_client_->SaveContributionInfo( - probi, - month, - year, - date, - publisher_key, - type); + std::move(info), + base::BindOnce(&OnSaveContributionInfo, std::move(callback))); } void BatLedgerClientMojoProxy::SaveMediaPublisherInfo( diff --git a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h index fb8de04005b1..ed4840496bb0 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h +++ b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h @@ -33,7 +33,7 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient, ledger::WalletPropertiesPtr properties) override; void OnReconcileComplete(ledger::Result result, const std::string& viewing_id, - const std::string& probi, + const double amount, const ledger::RewardsType type) override; void LoadLedgerState(ledger::OnLoadCallback callback) override; void LoadPublisherState(ledger::OnLoadCallback callback) override; @@ -64,14 +64,13 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient, void FetchFavIcon(const std::string& url, const std::string& favicon_key, ledger::FetchIconCallback callback) override; - void SaveContributionInfo(const std::string& probi, - const ledger::ActivityMonth month, - const int year, - const uint32_t date, - const std::string& publisher_key, - const ledger::RewardsType type) override; - void SaveRecurringTip( + + void SaveContributionInfo( ledger::ContributionInfoPtr info, + ledger::ResultCallback callback) override; + + void SaveRecurringTip( + ledger::RecurringTipPtr info, ledger::SaveRecurringTipCallback callback) override; void GetRecurringTips(ledger::PublisherInfoListCallback callback) override; void GetOneTimeTips(ledger::PublisherInfoListCallback callback) override; diff --git a/components/services/bat_ledger/bat_ledger_impl.cc b/components/services/bat_ledger/bat_ledger_impl.cc index 2dfe8f460994..9beea90c10e2 100644 --- a/components/services/bat_ledger/bat_ledger_impl.cc +++ b/components/services/bat_ledger/bat_ledger_impl.cc @@ -214,22 +214,6 @@ void BatLedgerImpl::SetBalanceReportItem( ledger_->SetBalanceReportItem(month, year, type, probi); } -void BatLedgerImpl::OnReconcileCompleteSuccess( - const std::string& viewing_id, - const ledger::RewardsType type, - const std::string& probi, - ledger::ActivityMonth month, - int32_t year, - uint32_t data) { - ledger_->OnReconcileCompleteSuccess( - viewing_id, - type, - probi, - month, - year, - data); -} - void BatLedgerImpl::OnFetchPromotions( CallbackHolder* holder, const ledger::Result result, @@ -557,7 +541,7 @@ void BatLedgerImpl::OnSaveRecurringTip( } void BatLedgerImpl::SaveRecurringTip( - ledger::ContributionInfoPtr info, + ledger::RecurringTipPtr info, SaveRecurringTipCallback callback) { // deleted in OnSaveRecurringTip auto* holder = new CallbackHolder( diff --git a/components/services/bat_ledger/bat_ledger_impl.h b/components/services/bat_ledger/bat_ledger_impl.h index d561aaa68348..e8c8447ff3a3 100644 --- a/components/services/bat_ledger/bat_ledger_impl.h +++ b/components/services/bat_ledger/bat_ledger_impl.h @@ -73,13 +73,6 @@ class BatLedgerImpl : public mojom::BatLedger, int32_t year, ledger::ReportType type, const std::string& probi) override; - void OnReconcileCompleteSuccess( - const std::string& viewing_id, - const ledger::RewardsType type, - const std::string& probi, - ledger::ActivityMonth month, - int32_t year, - uint32_t data) override; void FetchPromotions(FetchPromotionsCallback callback) override; void ClaimPromotion( @@ -146,7 +139,7 @@ class BatLedgerImpl : public mojom::BatLedger, void StartMonthlyContribution() override; void SaveRecurringTip( - ledger::ContributionInfoPtr info, + ledger::RecurringTipPtr info, SaveRecurringTipCallback callback) override; void GetRecurringTips(GetRecurringTipsCallback callback) override; diff --git a/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.cc b/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.cc index 3579906717e1..7c3b98c16623 100644 --- a/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.cc +++ b/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.cc @@ -135,12 +135,12 @@ void LedgerClientMojoProxy::CallbackHolder< void LedgerClientMojoProxy::OnReconcileComplete( const ledger::Result result, const std::string& viewing_id, - const std::string& probi, + const double amount, const ledger::RewardsType type) { ledger_client_->OnReconcileComplete( result, viewing_id, - probi, + amount, type); } @@ -281,7 +281,7 @@ void LedgerClientMojoProxy::OnSaveRecurringTip( } void LedgerClientMojoProxy::SaveRecurringTip( - ledger::ContributionInfoPtr info, + ledger::RecurringTipPtr info, SaveRecurringTipCallback callback) { // deleted in OnSaveRecurringTip auto* holder = new CallbackHolder( @@ -356,20 +356,25 @@ void LedgerClientMojoProxy::RemoveRecurringTip(const std::string& publisher_key, std::bind(LedgerClientMojoProxy::OnRemoveRecurringTip, holder, _1)); } +// static +void LedgerClientMojoProxy::OnSaveContributionInfo( + CallbackHolder* holder, + const ledger::Result result) { + DCHECK(holder); + if (holder->is_valid()) + std::move(holder->get()).Run(result); + delete holder; +} + void LedgerClientMojoProxy::SaveContributionInfo( - const std::string& probi, - ledger::ActivityMonth month, - int32_t year, - uint32_t date, - const std::string& publisher_key, - const ledger::RewardsType type) { + ledger::ContributionInfoPtr info, + SaveContributionInfoCallback callback) { + // deleted in OnSaveContributionInfo + auto* holder = new CallbackHolder( + AsWeakPtr(), std::move(callback)); ledger_client_->SaveContributionInfo( - probi, - month, - year, - date, - publisher_key, - type); + std::move(info), + std::bind(LedgerClientMojoProxy::OnSaveContributionInfo, holder, _1)); } void LedgerClientMojoProxy::SaveMediaPublisherInfo( diff --git a/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h b/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h index f89267420744..894aa714e1d7 100644 --- a/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h +++ b/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h @@ -33,7 +33,7 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, void OnReconcileComplete( const ledger::Result result, const std::string& viewing_id, - const std::string& probi, + const double amount, const ledger::RewardsType type) override; void LoadPublisherState(LoadPublisherStateCallback callback) override; @@ -54,7 +54,7 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, void FetchFavIcon(const std::string& url, const std::string& favicon_key, FetchFavIconCallback callback) override; void SaveRecurringTip( - ledger::ContributionInfoPtr info, + ledger::RecurringTipPtr info, SaveRecurringTipCallback callback) override; void GetRecurringTips(GetRecurringTipsCallback callback) override; @@ -71,13 +71,11 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, const ledger::Result result, ledger::PublisherInfoPtr info, uint64_t window_id) override; + void SaveContributionInfo( - const std::string& probi, - ledger::ActivityMonth month, - int32_t year, - uint32_t date, - const std::string& publisher_key, - const ledger::RewardsType type) override; + ledger::ContributionInfoPtr info, + SaveContributionInfoCallback callback) override; + void SaveMediaPublisherInfo(const std::string& media_key, const std::string& publisher_id) override; @@ -336,6 +334,10 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, CallbackHolder* holder, const ledger::Result result); + static void OnSaveContributionInfo( + CallbackHolder* holder, + const ledger::Result result); + static void OnLoadURL( CallbackHolder* holder, int32_t response_code, const std::string& response, diff --git a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom index d8b5e69dbb6c..b28b6d9ad4c0 100644 --- a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom +++ b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom @@ -59,8 +59,6 @@ interface BatLedger { RestorePublishers() => (ledger.mojom.Result result); SetBalanceReportItem(ledger.mojom.ActivityMonth month, int32 year, ledger.mojom.ReportType type, string probi); - OnReconcileCompleteSuccess(string viewing_id, ledger.mojom.RewardsType type, string probi, - ledger.mojom.ActivityMonth month, int32 year, uint32 data); FetchPromotions() => (ledger.mojom.Result result, array promotion); ClaimPromotion(string payload) => (ledger.mojom.Result result, string response); @@ -110,7 +108,7 @@ interface BatLedger { GetTransactionHistory() => (string transactions); GetRewardsInternalsInfo() => (ledger.mojom.RewardsInternalsInfo info); - SaveRecurringTip(ledger.mojom.ContributionInfo info) => (ledger.mojom.Result result); + SaveRecurringTip(ledger.mojom.RecurringTip info) => (ledger.mojom.Result result); GetRecurringTips() => (array list); GetOneTimeTips() => (array list); @@ -163,7 +161,7 @@ interface BatLedgerClient { OnWalletProperties(ledger.mojom.Result result, ledger.mojom.WalletProperties? properties); OnReconcileComplete(ledger.mojom.Result result, string viewing_id, - string probi, ledger.mojom.RewardsType type); + double amount, ledger.mojom.RewardsType type); SavePublisherInfo(ledger.mojom.PublisherInfo publisher_info) => (ledger.mojom.Result result, ledger.mojom.PublisherInfo? publisher_info); @@ -179,7 +177,7 @@ interface BatLedgerClient { uint64 window_id); FetchFavIcon(string url, string favicon_key) => (bool success, string favicon_url); - SaveRecurringTip(ledger.mojom.ContributionInfo info) => (ledger.mojom.Result result); + SaveRecurringTip(ledger.mojom.RecurringTip info) => (ledger.mojom.Result result); GetRecurringTips() => ( array publisher_info_list, uint32 next_record); @@ -197,8 +195,7 @@ interface BatLedgerClient { [Sync] SetTimer(uint64 time_offset) => (uint32 timer_id); KillTimer(uint32 timer_id); - SaveContributionInfo(string probi, ledger.mojom.ActivityMonth month, int32 year, uint32 date, - string publisher_key, ledger.mojom.RewardsType type); + SaveContributionInfo(ledger.mojom.ContributionInfo info) => (ledger.mojom.Result result); SaveMediaPublisherInfo(string media_key, string publisher_id); [Sync] diff --git a/test/data/rewards-data/migration/publisher_info_db_v10 b/test/data/rewards-data/migration/publisher_info_db_v10 new file mode 100644 index 000000000000..e5ec62f65937 Binary files /dev/null and b/test/data/rewards-data/migration/publisher_info_db_v10 differ diff --git a/test/data/rewards-data/migration/publisher_info_db_v7 b/test/data/rewards-data/migration/publisher_info_db_v7 index 26353db00cc0..119717e1b094 100644 Binary files a/test/data/rewards-data/migration/publisher_info_db_v7 and b/test/data/rewards-data/migration/publisher_info_db_v7 differ diff --git a/test/data/rewards-data/migration/publisher_info_schema_v11.txt b/test/data/rewards-data/migration/publisher_info_schema_v11.txt new file mode 100644 index 000000000000..a09e565fdb8e --- /dev/null +++ b/test/data/rewards-data/migration/publisher_info_schema_v11.txt @@ -0,0 +1,42 @@ +index|activity_info_publisher_id_index|activity_info|CREATE INDEX activity_info_publisher_id_index ON activity_info (publisher_id) +index|contribution_info_publishers_contribution_id_index|contribution_info_publishers|CREATE INDEX contribution_info_publishers_contribution_id_index ON contribution_info_publishers (contribution_id) +index|contribution_info_publishers_publisher_key_index|contribution_info_publishers|CREATE INDEX contribution_info_publishers_publisher_key_index ON contribution_info_publishers (publisher_key) +index|pending_contribution_publisher_id_index|pending_contribution|CREATE INDEX pending_contribution_publisher_id_index ON pending_contribution (publisher_id) +index|promotion_creds_promotion_id_index|promotion_creds|CREATE INDEX promotion_creds_promotion_id_index ON promotion_creds (promotion_id) +index|promotion_promotion_id_index|promotion|CREATE INDEX promotion_promotion_id_index ON promotion (promotion_id) +index|recurring_donation_publisher_id_index|recurring_donation|CREATE INDEX recurring_donation_publisher_id_index ON recurring_donation (publisher_id) +index|server_publisher_amounts_publisher_key_index|server_publisher_amounts|CREATE INDEX server_publisher_amounts_publisher_key_index ON server_publisher_amounts (publisher_key) +index|server_publisher_banner_publisher_key_index|server_publisher_banner|CREATE INDEX server_publisher_banner_publisher_key_index ON server_publisher_banner (publisher_key) +index|server_publisher_info_publisher_key_index|server_publisher_info|CREATE INDEX server_publisher_info_publisher_key_index ON server_publisher_info (publisher_key) +index|server_publisher_links_publisher_key_index|server_publisher_links|CREATE INDEX server_publisher_links_publisher_key_index ON server_publisher_links (publisher_key) +index|sqlite_autoindex_activity_info_1|activity_info| +index|sqlite_autoindex_contribution_info_1|contribution_info| +index|sqlite_autoindex_media_publisher_info_1|media_publisher_info| +index|sqlite_autoindex_meta_1|meta| +index|sqlite_autoindex_promotion_1|promotion| +index|sqlite_autoindex_promotion_creds_1|promotion_creds| +index|sqlite_autoindex_publisher_info_1|publisher_info| +index|sqlite_autoindex_recurring_donation_1|recurring_donation| +index|sqlite_autoindex_server_publisher_amounts_1|server_publisher_amounts| +index|sqlite_autoindex_server_publisher_banner_1|server_publisher_banner| +index|sqlite_autoindex_server_publisher_info_1|server_publisher_info| +index|sqlite_autoindex_server_publisher_links_1|server_publisher_links| +index|unblinded_tokens_token_id_index|unblinded_tokens|CREATE INDEX unblinded_tokens_token_id_index ON unblinded_tokens (token_id) +table|activity_info|activity_info|CREATE TABLE activity_info(publisher_id LONGVARCHAR NOT NULL,duration INTEGER DEFAULT 0 NOT NULL,visits INTEGER DEFAULT 0 NOT NULL,score DOUBLE DEFAULT 0 NOT NULL,percent INTEGER DEFAULT 0 NOT NULL,weight DOUBLE DEFAULT 0 NOT NULL,reconcile_stamp INTEGER DEFAULT 0 NOT NULL,CONSTRAINT activity_unique UNIQUE (publisher_id, reconcile_stamp) CONSTRAINT fk_activity_info_publisher_id FOREIGN KEY (publisher_id) REFERENCES "publisher_info_old" (publisher_id) ON DELETE CASCADE) +table|contribution_info|contribution_info|CREATE TABLE contribution_info (contribution_id TEXT NOT NULL,amount DOUBLE NOT NULL,type INTEGER NOT NULL,step INTEGER NOT NULL DEFAULT -1,retry_count INTEGER NOT NULL DEFAULT -1,created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,PRIMARY KEY (contribution_id)) +table|contribution_info_publishers|contribution_info_publishers|CREATE TABLE contribution_info_publishers (contribution_id TEXT NOT NULL,publisher_key TEXT NOT NULL,total_amount DOUBLE NOT NULL,contributed_amount DOUBLE,CONSTRAINT fk_contribution_info_publishers_contribution_id FOREIGN KEY (contribution_id) REFERENCES "contribution_info_temp" (contribution_id) ON DELETE CASCADE,CONSTRAINT fk_contribution_info_publishers_publisher_id FOREIGN KEY (publisher_key) REFERENCES publisher_info (publisher_id)) +table|contribution_queue|contribution_queue|CREATE TABLE contribution_queue (contribution_queue_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,type INTEGER NOT NULL,amount DOUBLE NOT NULL,partial INTEGER NOT NULL DEFAULT 0,created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL) +table|contribution_queue_publishers|contribution_queue_publishers|CREATE TABLE contribution_queue_publishers (contribution_queue_id INTEGER NOT NULL,publisher_key TEXT NOT NULL,amount_percent DOUBLE NOT NULL,CONSTRAINT fk_contribution_queue_publishers_publisher_key FOREIGN KEY (publisher_key) REFERENCES publisher_info (publisher_id),CONSTRAINT fk_contribution_queue_publishers_id FOREIGN KEY (contribution_queue_id) REFERENCES contribution_queue (contribution_queue_id) ON DELETE CASCADE) +table|media_publisher_info|media_publisher_info|CREATE TABLE media_publisher_info(media_key TEXT NOT NULL PRIMARY KEY UNIQUE,publisher_id LONGVARCHAR NOT NULL,CONSTRAINT fk_media_publisher_info_publisher_id FOREIGN KEY (publisher_id) REFERENCES "publisher_info_old" (publisher_id) ON DELETE CASCADE) +table|meta|meta|CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR) +table|pending_contribution|pending_contribution|CREATE TABLE pending_contribution(publisher_id LONGVARCHAR NOT NULL,amount DOUBLE DEFAULT 0 NOT NULL,added_date INTEGER DEFAULT 0 NOT NULL,viewing_id LONGVARCHAR NOT NULL,type INTEGER NOT NULL,CONSTRAINT fk_pending_contribution_publisher_id FOREIGN KEY (publisher_id) REFERENCES publisher_info (publisher_id) ON DELETE CASCADE) +table|promotion|promotion|CREATE TABLE promotion (promotion_id TEXT NOT NULL,version INTEGER NOT NULL,type INTEGER NOT NULL,public_keys TEXT NOT NULL,suggestions INTEGER NOT NULL DEFAULT 0,approximate_value DOUBLE NOT NULL DEFAULT 0,status INTEGER NOT NULL DEFAULT 0,expires_at TIMESTAMP NOT NULL,created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,PRIMARY KEY (promotion_id)) +table|promotion_creds|promotion_creds|CREATE TABLE promotion_creds (promotion_id TEXT UNIQUE NOT NULL,tokens TEXT NOT NULL,blinded_creds TEXT NOT NULL,signed_creds TEXT,public_key TEXT,batch_proof TEXT,claim_id TEXT,CONSTRAINT fk_promotion_creds_promotion_id FOREIGN KEY (promotion_id) REFERENCES promotion (promotion_id) ON DELETE CASCADE) +table|publisher_info|publisher_info|CREATE TABLE publisher_info(publisher_id LONGVARCHAR PRIMARY KEY NOT NULL UNIQUE,excluded INTEGER DEFAULT 0 NOT NULL,name TEXT NOT NULL,favIcon TEXT NOT NULL,url TEXT NOT NULL,provider TEXT NOT NULL) +table|recurring_donation|recurring_donation|CREATE TABLE recurring_donation(publisher_id LONGVARCHAR NOT NULL PRIMARY KEY UNIQUE,amount DOUBLE DEFAULT 0 NOT NULL,added_date INTEGER DEFAULT 0 NOT NULL,CONSTRAINT fk_recurring_donation_publisher_id FOREIGN KEY (publisher_id) REFERENCES "publisher_info_old" (publisher_id) ON DELETE CASCADE) +table|server_publisher_amounts|server_publisher_amounts|CREATE TABLE server_publisher_amounts (publisher_key LONGVARCHAR NOT NULL,amount DOUBLE DEFAULT 0 NOT NULL,CONSTRAINT server_publisher_amounts_unique UNIQUE (publisher_key, amount) CONSTRAINT fk_server_publisher_amounts_publisher_key FOREIGN KEY (publisher_key) REFERENCES server_publisher_info (publisher_key) ON DELETE CASCADE) +table|server_publisher_banner|server_publisher_banner|CREATE TABLE server_publisher_banner (publisher_key LONGVARCHAR PRIMARY KEY NOT NULL UNIQUE,title TEXT,description TEXT,background TEXT,logo TEXT,CONSTRAINT fk_server_publisher_banner_publisher_key FOREIGN KEY (publisher_key) REFERENCES server_publisher_info (publisher_key) ON DELETE CASCADE) +table|server_publisher_info|server_publisher_info|CREATE TABLE server_publisher_info (publisher_key LONGVARCHAR PRIMARY KEY NOT NULL UNIQUE,status INTEGER DEFAULT 0 NOT NULL,excluded INTEGER DEFAULT 0 NOT NULL,address TEXT NOT NULL) +table|server_publisher_links|server_publisher_links|CREATE TABLE server_publisher_links (publisher_key LONGVARCHAR NOT NULL,provider TEXT,link TEXT,CONSTRAINT server_publisher_links_unique UNIQUE (publisher_key, provider) CONSTRAINT fk_server_publisher_links_publisher_key FOREIGN KEY (publisher_key) REFERENCES server_publisher_info (publisher_key) ON DELETE CASCADE) +table|sqlite_sequence|sqlite_sequence|CREATE TABLE sqlite_sequence(name,seq) +table|unblinded_tokens|unblinded_tokens|CREATE TABLE unblinded_tokens (token_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,token_value TEXT,public_key TEXT,value DOUBLE NOT NULL DEFAULT 0,promotion_id TEXT,created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,CONSTRAINT fk_unblinded_tokens_promotion_id FOREIGN KEY (promotion_id) REFERENCES promotion (promotion_id) ON DELETE CASCADE) diff --git a/test/data/rewards-data/migration/publisher_info_schema_v8.txt b/test/data/rewards-data/migration/publisher_info_schema_v8.txt index aadce94ad42e..9872e6e31b72 100644 --- a/test/data/rewards-data/migration/publisher_info_schema_v8.txt +++ b/test/data/rewards-data/migration/publisher_info_schema_v8.txt @@ -16,7 +16,7 @@ index|sqlite_autoindex_server_publisher_banner_1|server_publisher_banner| index|sqlite_autoindex_server_publisher_info_1|server_publisher_info| index|sqlite_autoindex_server_publisher_links_1|server_publisher_links| table|activity_info|activity_info|CREATE TABLE activity_info(publisher_id LONGVARCHAR NOT NULL,duration INTEGER DEFAULT 0 NOT NULL,visits INTEGER DEFAULT 0 NOT NULL,score DOUBLE DEFAULT 0 NOT NULL,percent INTEGER DEFAULT 0 NOT NULL,weight DOUBLE DEFAULT 0 NOT NULL,reconcile_stamp INTEGER DEFAULT 0 NOT NULL,CONSTRAINT activity_unique UNIQUE (publisher_id, reconcile_stamp) CONSTRAINT fk_activity_info_publisher_id FOREIGN KEY (publisher_id) REFERENCES "publisher_info_old" (publisher_id) ON DELETE CASCADE) -table|contribution_info|contribution_info|CREATE TABLE contribution_info(publisher_id LONGVARCHAR,probi TEXT "0" NOT NULL,date INTEGER NOT NULL,type INTEGER NOT NULL,month INTEGER NOT NULL,year INTEGER NOT NULL,CONSTRAINT fk_contribution_info_publisher_id FOREIGN KEY (publisher_id) REFERENCES publisher_info (publisher_id) ON DELETE CASCADE) +table|contribution_info|contribution_info|CREATE TABLE contribution_info (publisher_id LONGVARCHAR,probi TEXT "0" NOT NULL,date INTEGER NOT NULL,type INTEGER NOT NULL,month INTEGER NOT NULL,year INTEGER NOT NULL,CONSTRAINT fk_contribution_info_publisher_id FOREIGN KEY (publisher_id) REFERENCES publisher_info (publisher_id) ON DELETE CASCADE) table|media_publisher_info|media_publisher_info|CREATE TABLE media_publisher_info(media_key TEXT NOT NULL PRIMARY KEY UNIQUE,publisher_id LONGVARCHAR NOT NULL,CONSTRAINT fk_media_publisher_info_publisher_id FOREIGN KEY (publisher_id) REFERENCES "publisher_info_old" (publisher_id) ON DELETE CASCADE) table|meta|meta|CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR) table|pending_contribution|pending_contribution|CREATE TABLE pending_contribution(publisher_id LONGVARCHAR NOT NULL,amount DOUBLE DEFAULT 0 NOT NULL,added_date INTEGER DEFAULT 0 NOT NULL,viewing_id LONGVARCHAR NOT NULL,type INTEGER NOT NULL,CONSTRAINT fk_pending_contribution_publisher_id FOREIGN KEY (publisher_id) REFERENCES publisher_info (publisher_id) ON DELETE CASCADE) diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_client_mock.h b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_client_mock.h index c9b62f4496b8..ceefaa64bb9e 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_client_mock.h +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_client_mock.h @@ -67,7 +67,7 @@ class MockConfirmationsClient : public ConfirmationsClient { MOCK_METHOD4(OnReconcileComplete, void( Result result, const std::string& viewing_id, - const std::string& probi, + const double amount, const ledger::RewardsType type)); MOCK_METHOD1(LoadLedgerState, void( @@ -139,16 +139,12 @@ class MockConfirmationsClient : public ConfirmationsClient { const std::string& favicon_key, ledger::FetchIconCallback callback)); - MOCK_METHOD6(SaveContributionInfo, void( - const std::string& probi, - const ledger::ActivityMonth month, - const int year, - const uint32_t date, - const std::string& publisher_key, - const ledger::RewardsType type)); + MOCK_METHOD2(SaveContributionInfo, void( + ledger::ContributionInfoPtr info, + ledger::ResultCallback callback)); MOCK_METHOD2(SaveRecurringTip, void( - ledger::ContributionInfoPtr info, + ledger::RecurringTipPtr info, ledger::SaveRecurringTipCallback callback)); MOCK_METHOD1(GetRecurringTips, void( diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger.h b/vendor/bat-native-ledger/include/bat/ledger/ledger.h index 44a67840083c..4eb1325c6ffe 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger.h @@ -246,14 +246,6 @@ class LEDGER_EXPORT Ledger { const std::string& publisher_id, ledger::PublisherBannerCallback callback) = 0; - virtual void OnReconcileCompleteSuccess( - const std::string& viewing_id, - const ledger::RewardsType type, - const std::string& probi, - const ledger::ActivityMonth month, - const int year, - const uint32_t date) = 0; - virtual void RemoveRecurringTip( const std::string& publisher_key, RemoveRecurringTipCallback callback) = 0; @@ -280,7 +272,7 @@ class LEDGER_EXPORT Ledger { ledger::RewardsInternalsInfoCallback callback) = 0; virtual void SaveRecurringTip( - ledger::ContributionInfoPtr info, + ledger::RecurringTipPtr info, ledger::SaveRecurringTipCallback callback) = 0; virtual void GetRecurringTips(ledger::PublisherInfoListCallback callback) = 0; diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h b/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h index 262d55b9665a..6dd5df1bd69c 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h @@ -87,10 +87,11 @@ class LEDGER_EXPORT LedgerClient { Result result, ledger::WalletPropertiesPtr properties) = 0; - virtual void OnReconcileComplete(Result result, - const std::string& viewing_id, - const std::string& probi, - const ledger::RewardsType type) = 0; + virtual void OnReconcileComplete( + const Result result, + const std::string& viewing_id, + const double amount, + const ledger::RewardsType type) = 0; virtual void LoadLedgerState(OnLoadCallback callback) = 0; @@ -138,15 +139,11 @@ class LEDGER_EXPORT LedgerClient { FetchIconCallback callback) = 0; virtual void SaveContributionInfo( - const std::string& probi, - const ledger::ActivityMonth month, - const int year, - const uint32_t date, - const std::string& publisher_key, - const ledger::RewardsType type) = 0; + ledger::ContributionInfoPtr info, + ledger::ResultCallback callback) = 0; virtual void SaveRecurringTip( - ledger::ContributionInfoPtr info, + ledger::RecurringTipPtr info, ledger::SaveRecurringTipCallback callback) = 0; virtual void GetRecurringTips( diff --git a/vendor/bat-native-ledger/include/bat/ledger/mojom_structs.h b/vendor/bat-native-ledger/include/bat/ledger/mojom_structs.h index 8f0bd36d58f9..37b6ceca79ec 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/mojom_structs.h +++ b/vendor/bat-native-ledger/include/bat/ledger/mojom_structs.h @@ -37,6 +37,11 @@ using ClientInfoPtr = mojom::ClientInfoPtr; using ContributionInfo = mojom::ContributionInfo; using ContributionInfoPtr = mojom::ContributionInfoPtr; +using ContributionPublisher = mojom::ContributionPublisher; +using ContributionPublisherPtr = mojom::ContributionPublisherPtr; +using ContributionPublisherList = + std::vector; + using ContributionQueue = ledger::mojom::ContributionQueue; using ContributionQueuePtr = ledger::mojom::ContributionQueuePtr; using ContributionQueueList = std::vector; @@ -96,6 +101,9 @@ using PublisherExclude = mojom::PublisherExclude; using ReconcileInfo = mojom::ReconcileInfo; using ReconcileInfoPtr = mojom::ReconcileInfoPtr; +using RecurringTip = mojom::RecurringTip; +using RecurringTipPtr = mojom::RecurringTipPtr; + using ReportType = mojom::ReportType; using Result = mojom::Result; diff --git a/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom b/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom index a9108b2eddf9..a93235b21808 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom +++ b/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom @@ -4,9 +4,21 @@ module ledger.mojom; struct ContributionInfo { - string publisher; // Filled only for recurrent tips - double value; - uint64 date; + string contribution_id; + double amount; + RewardsType type; + int32 step; + int32 retry_count; + uint64 created_at; + + array publishers; +}; + +struct ContributionPublisher { + string contribution_id; + string publisher_key; + double total_amount; + double contributed_amount; }; struct PublisherInfo { @@ -24,8 +36,6 @@ struct PublisherInfo { string url; string provider; string favicon_url; - - array contributions; }; struct PublisherBanner { @@ -370,3 +380,9 @@ struct ClientInfo { Platform platform; OperatingSystem os; }; + +struct RecurringTip { + string publisher_key; + double amount; + uint64 created_at; +}; \ No newline at end of file diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_util.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_util.cc index 7c8cc9db560f..eb850cd48523 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_util.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_util.cc @@ -29,4 +29,15 @@ std::string ConvertToProbi(const std::string& amount) { return before_dot + after_dot + rest_probi; } +double ProbiToDouble(const std::string& probi) { + const size_t size = probi.size(); + std::string amount = "0"; + if (size > 18) { + amount = probi; + amount.insert(size - 18, "."); + } + + return std::stod(amount); +} + } // namespace braveledger_bat_util diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_util.h b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_util.h index 23c239af7dc3..33b764e4f2c7 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_util.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_util.h @@ -12,6 +12,8 @@ namespace braveledger_bat_util { std::string ConvertToProbi(const std::string& amount); +double ProbiToDouble(const std::string& probi); + } // namespace braveledger_bat_util #endif // BRAVELEDGER_BAT_UTIL_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_util_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_util_unittest.cc index 46ea4582ad6a..2f0f5990a3d9 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_util_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_util_unittest.cc @@ -32,3 +32,23 @@ TEST(BatUtilTest, ConvertToProbi) { result = braveledger_bat_util::ConvertToProbi("5.45"); ASSERT_EQ(result, "5450000000000000000"); } + +TEST(BatUtilTest, ProbiToDouble) { + // empty string + double result = braveledger_bat_util::ProbiToDouble(""); + ASSERT_EQ(result, 0); + + // wrong probi + result = braveledger_bat_util::ProbiToDouble("10"); + ASSERT_EQ(result, 0); + + // full number probi + result = + braveledger_bat_util::ProbiToDouble("5000000000000000000"); + ASSERT_EQ(result, 5.0); + + // full number probi + result = + braveledger_bat_util::ProbiToDouble("1125600000000000000000"); + ASSERT_EQ(result, 1125.6); +} diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.cc index b8deda370220..883101c57976 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.cc @@ -15,6 +15,7 @@ #include "bat/ledger/global_constants.h" #include "bat/ledger/internal/bat_util.h" #include "bat/ledger/internal/common/bind_util.h" +#include "bat/ledger/internal/common/time_util.h" #include "bat/ledger/internal/contribution/contribution.h" #include "bat/ledger/internal/contribution/contribution_unblinded.h" #include "bat/ledger/internal/contribution/contribution_util.h" @@ -392,42 +393,44 @@ void Contribution::SetTimer(uint32_t* timer_id, uint64_t start_timer_in) { ledger_->SetTimer(start_timer_in, timer_id); } -void Contribution::OnReconcileCompleteSuccess( +void Contribution::ReconcileSuccess( const std::string& viewing_id, - const ledger::RewardsType type, - const std::string& probi, - ledger::ActivityMonth month, - int year, - uint32_t date) { - if (type == ledger::RewardsType::AUTO_CONTRIBUTE) { - ledger_->SetBalanceReportItem( - month, - year, - GetReportTypeFromRewardsType(type), + const double amount, + const bool delete_reconcile) { + const auto reconcile = ledger_->GetReconcileById(viewing_id); + const std::string probi = + braveledger_bat_util::ConvertToProbi(std::to_string(amount)); + + ledger_->SetBalanceReportItem( + braveledger_time_util::GetCurrentMonth(), + braveledger_time_util::GetCurrentYear(), + GetReportTypeFromRewardsType(reconcile.type_), probi); - ledger_->SaveContributionInfo(probi, month, year, date, "", type); - return; + + ledger::ContributionPublisherList publisher_list; + for (auto& item : reconcile.directions_) { + auto publisher = ledger::ContributionPublisher::New(); + publisher->contribution_id = viewing_id; + publisher->publisher_key = item.publisher_key_; + publisher->total_amount = (item.amount_percent_ * amount) / 100; + publisher->contributed_amount = publisher->total_amount; + publisher_list.push_back(std::move(publisher)); } - if (type == ledger::RewardsType::ONE_TIME_TIP || - type == ledger::RewardsType::RECURRING_TIP) { - ledger_->SetBalanceReportItem( - month, - year, - GetReportTypeFromRewardsType(type), - probi); - const auto reconcile = ledger_->GetReconcileById(viewing_id); - const auto donations = reconcile.directions_; - if (donations.size() > 0) { - std::string publisher_key = donations[0].publisher_key_; - ledger_->SaveContributionInfo(probi, - month, - year, - date, - publisher_key, - type); - } - return; + const uint64_t now = static_cast(base::Time::Now().ToDoubleT()); + auto info = ledger::ContributionInfo::New(); + info->contribution_id = viewing_id; + info->amount = amount; + info->type = reconcile.type_; + info->step = -1; + info->retry_count = -1; + info->created_at = now; + info->publishers = std::move(publisher_list); + + ledger_->SaveContributionInfo(std::move(info), [](ledger::Result _){}); + + if (!viewing_id.empty() && delete_reconcile) { + ledger_->RemoveReconcileById(viewing_id); } } @@ -997,17 +1000,11 @@ void Contribution::OnExternalWalletServerPublisherInfo( const ledger::ExternalWallet& wallet) { const auto reconcile = ledger_->GetReconcileById(viewing_id); if (!info) { - const auto probi = - braveledger_bat_util::ConvertToProbi(std::to_string(amount)); - ledger_->OnReconcileComplete( + ledger_->ReconcileComplete( ledger::Result::LEDGER_ERROR, + amount, viewing_id, - probi, reconcile.type_); - - if (!viewing_id.empty()) { - ledger_->RemoveReconcileById(viewing_id); - } return; } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.h b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.h index 492579f35947..f0eceda255bf 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.h @@ -137,13 +137,11 @@ class Contribution { // Does final stage in contribution // Sets reports and contribution info - void OnReconcileCompleteSuccess( + void ReconcileSuccess( const std::string& viewing_id, - const ledger::RewardsType type, - const std::string& probi, - ledger::ActivityMonth month, - int year, - uint32_t date); + const double amount, + const bool delete_reconcile); + void HasSufficientBalance( ledger::HasSufficientBalanceToReconcileCallback callback); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_unblinded.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_unblinded.cc index 5ae9fb0fd22d..d81bb6d47116 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_unblinded.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_unblinded.cc @@ -222,20 +222,27 @@ void Unblinded::PrepareAutoContribution( const std::string& viewing_id, ledger::UnblindedTokenList list) { if (list.size() == 0) { - ContributionCompleted(ledger::Result::LEDGER_ERROR, viewing_id); + ContributionCompleted(ledger::Result::AC_TABLE_EMPTY, viewing_id); return; } - const auto reconcile = ledger_->GetReconcileById(viewing_id); + auto reconcile = ledger_->GetReconcileById(viewing_id); + const double total_votes = static_cast(list.size()); Winners winners; - GetStatisticalVotingWinners(list.size(), reconcile.directions_, &winners); + GetStatisticalVotingWinners(total_votes, reconcile.directions_, &winners); + braveledger_bat_helper::Directions new_directions; uint32_t current_position = 0; for (auto & winner : winners) { if (winner.second == 0) { continue; } + braveledger_bat_helper::RECONCILE_DIRECTION direction; + direction.publisher_key_ = winner.first; + direction.amount_percent_ = (winner.second / total_votes) * 100; + new_directions.push_back(direction); + const uint32_t new_position = current_position + winner.second; ledger::UnblindedTokenList new_list; for (size_t i = current_position; i < new_position; i++) { @@ -250,6 +257,9 @@ void Unblinded::PrepareAutoContribution( [](const ledger::Result _){}); } + reconcile.directions_ = new_directions; + ledger_->UpdateReconcile(reconcile); + ContributionCompleted(ledger::Result::LEDGER_OK, viewing_id); } @@ -312,20 +322,11 @@ void Unblinded::ContributionCompleted( const ledger::Result result, const std::string& viewing_id) { const auto reconcile = ledger_->GetReconcileById(viewing_id); - const auto amount = - braveledger_bat_util::ConvertToProbi(std::to_string(reconcile.fee_)); - - ledger_->OnReconcileComplete(result, - viewing_id, - amount, - reconcile.type_); - - if (result != ledger::Result::LEDGER_OK) { - if (!viewing_id.empty()) { - ledger_->RemoveReconcileById(viewing_id); - } - return; - } + ledger_->ReconcileComplete( + result, + reconcile.fee_, + viewing_id, + reconcile.type_); } } // namespace braveledger_contribution diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/phase_one.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/phase_one.cc index e4c2a7f9183c..89dceb539201 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/phase_one.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/phase_one.cc @@ -76,9 +76,7 @@ void PhaseOne::ReconcileCallback( success = ledger_->UpdateReconcile(reconcile); if (!success) { - Complete(ledger::Result::LEDGER_ERROR, - viewing_id, - reconcile.type_); + Complete(ledger::Result::LEDGER_ERROR, viewing_id, reconcile.type_); return; } @@ -170,9 +168,7 @@ void PhaseOne::CurrentReconcileCallback( success = ledger_->UpdateReconcile(reconcile); if (!success) { - Complete(ledger::Result::LEDGER_ERROR, - viewing_id, - reconcile.type_); + Complete(ledger::Result::LEDGER_ERROR, viewing_id, reconcile.type_); return; } @@ -260,9 +256,10 @@ void PhaseOne::ReconcilePayloadCallback( if (response_status_code != net::HTTP_OK) { if (response_status_code == net::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE) { - Complete(ledger::Result::CONTRIBUTION_AMOUNT_TOO_LOW, - viewing_id, - reconcile.type_); + Complete( + ledger::Result::CONTRIBUTION_AMOUNT_TOO_LOW, + viewing_id, + reconcile.type_); } else { contribution_->AddRetry(ledger::ContributionRetry::STEP_PAYLOAD, viewing_id); @@ -352,9 +349,7 @@ void PhaseOne::RegisterViewingCallback( success = ledger_->UpdateReconcile(reconcile); if (!success) { - Complete(ledger::Result::LEDGER_ERROR, - viewing_id, - reconcile.type_); + Complete(ledger::Result::LEDGER_ERROR, viewing_id, reconcile.type_); return; } @@ -460,9 +455,7 @@ void PhaseOne::ViewingCredentialsCallback( success = ledger_->UpdateReconcile(reconcile); if (!success) { - Complete(ledger::Result::LEDGER_ERROR, - viewing_id, - reconcile.type_); + Complete(ledger::Result::LEDGER_ERROR, viewing_id, reconcile.type_); return; } @@ -500,21 +493,22 @@ void PhaseOne::ViewingCredentialsCallback( probi); } -void PhaseOne::Complete(ledger::Result result, - const std::string& viewing_id, - const ledger::RewardsType type, - const std::string& probi) { - ledger_->OnReconcileComplete(result, viewing_id, probi, type); +void PhaseOne::Complete( + ledger::Result result, + const std::string& viewing_id, + const ledger::RewardsType type, + const std::string& probi) { + const bool error = result != ledger::Result::LEDGER_OK; + const double amount = braveledger_bat_util::ProbiToDouble(probi); + ledger_->ReconcileComplete(result, amount, viewing_id, type, error); - if (result != ledger::Result::LEDGER_OK) { - if (!viewing_id.empty()) { - ledger_->RemoveReconcileById(viewing_id); - } + if (error) { return; } - ledger_->AddReconcileStep(viewing_id, - ledger::ContributionRetry::STEP_WINNERS); + ledger_->AddReconcileStep( + viewing_id, + ledger::ContributionRetry::STEP_WINNERS); contribution_->StartPhaseTwo(viewing_id); } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc index 904fa76d336b..9797eb5777ba 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc @@ -607,14 +607,25 @@ uint64_t LedgerImpl::GetReconcileStamp() const { return bat_state_->GetReconcileStamp(); } -void LedgerImpl::OnReconcileComplete(ledger::Result result, - const std::string& viewing_id, - const std::string& probi, - const ledger::RewardsType type) { +void LedgerImpl::ReconcileComplete( + const ledger::Result result, + const double amount, + const std::string& viewing_id, + const ledger::RewardsType type, + const bool delete_reconcile) { + const auto reconcile = GetReconcileById(viewing_id); + + if (result == ledger::Result::LEDGER_OK) { + bat_contribution_->ReconcileSuccess( + viewing_id, + amount, + delete_reconcile); + } + ledger_client_->OnReconcileComplete( result, viewing_id, - probi, + amount, type); } @@ -714,10 +725,11 @@ void LedgerImpl::OnTimer(uint32_t timer_id) { } void LedgerImpl::SaveRecurringTip( - ledger::ContributionInfoPtr info, + ledger::RecurringTipPtr info, ledger::SaveRecurringTipCallback callback) { - ledger_client_->SaveRecurringTip(std::move(info), - callback); + ledger_client_->SaveRecurringTip( + std::move(info), + callback); } void LedgerImpl::GetRecurringTips( @@ -780,21 +792,6 @@ void LedgerImpl::GetPublisherBanner(const std::string& publisher_id, bat_publisher_->GetPublisherBanner(publisher_id, callback); } -void LedgerImpl::OnReconcileCompleteSuccess( - const std::string& viewing_id, - const ledger::RewardsType type, - const std::string& probi, - const ledger::ActivityMonth month, - const int year, - const uint32_t date) { - bat_contribution_->OnReconcileCompleteSuccess(viewing_id, - type, - probi, - month, - year, - date); -} - void LedgerImpl::RemoveRecurringTip( const std::string& publisher_key, ledger::RemoveRecurringTipCallback callback) { @@ -1083,18 +1080,9 @@ bool LedgerImpl::ReconcileExists(const std::string& viewingId) { } void LedgerImpl::SaveContributionInfo( - const std::string& probi, - const ledger::ActivityMonth month, - const int year, - const uint32_t date, - const std::string& publisher_key, - const ledger::RewardsType type) { - ledger_client_->SaveContributionInfo(probi, - month, - year, - date, - publisher_key, - type); + ledger::ContributionInfoPtr info, + ledger::ResultCallback callback) { + ledger_client_->SaveContributionInfo(std::move(info), callback); } void LedgerImpl::NormalizeContributeWinners( diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h index 3ec8c02b9740..ba1993b60a93 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h @@ -212,10 +212,12 @@ class LedgerImpl : public ledger::Ledger, const ledger::UrlMethod method, ledger::LoadURLCallback callback); - void OnReconcileComplete(ledger::Result result, - const std::string& viewing_id, - const std::string& probi, - const ledger::RewardsType type); + void ReconcileComplete( + const ledger::Result result, + const double amount, + const std::string& viewing_id, + const ledger::RewardsType type, + const bool delete_reconcile = true); std::string URIEncode(const std::string& value) override; @@ -277,15 +279,8 @@ class LedgerImpl : public ledger::Ledger, void GetPublisherBanner(const std::string& publisher_id, ledger::PublisherBannerCallback callback) override; - void OnReconcileCompleteSuccess(const std::string& viewing_id, - const ledger::RewardsType type, - const std::string& probi, - const ledger::ActivityMonth month, - const int year, - const uint32_t date) override; - void SaveRecurringTip( - ledger::ContributionInfoPtr info, + ledger::RecurringTipPtr info, ledger::SaveRecurringTipCallback callback) override; void GetRecurringTips(ledger::PublisherInfoListCallback callback) override; @@ -383,12 +378,9 @@ class LedgerImpl : public ledger::Ledger, bool ReconcileExists(const std::string& viewingId); - void SaveContributionInfo(const std::string& probi, - const ledger::ActivityMonth month, - const int year, - const uint32_t date, - const std::string& publisher_key, - const ledger::RewardsType type); + void SaveContributionInfo( + ledger::ContributionInfoPtr info, + ledger::ResultCallback callback); void NormalizeContributeWinners( ledger::PublisherInfoList* newList, diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/test/mock_ledger_client.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/test/mock_ledger_client.cc index 6906e7a3dd4d..6c8c485d095c 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/test/mock_ledger_client.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/test/mock_ledger_client.cc @@ -33,10 +33,6 @@ void MockLedgerClient::Shutdown() { ledger_.reset(); } -void MockLedgerClient::OnReconcileComplete(ledger::Result result, - const std::string& viewing_id) { -} - void MockLedgerClient::LoadLedgerState(ledger::LedgerCallbackHandler* handler) { handler->OnLedgerStateLoaded(ledger::Result::OK, ledger_state_); } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/test/mock_ledger_client.h b/vendor/bat-native-ledger/src/bat/ledger/internal/test/mock_ledger_client.h index d0c78e36f681..a905b8350eb1 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/test/mock_ledger_client.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/test/mock_ledger_client.h @@ -32,8 +32,6 @@ class MockLedgerClient : public ledger::LedgerClient { protected: // ledger::LedgerClient std::string GenerateGUID() const override; - void OnReconcileComplete(ledger::Result result, - const std::string& viewing_id) override; void LoadLedgerState(ledger::LedgerCallbackHandler* handler) override; void LoadPublisherState(ledger::LedgerCallbackHandler* handler) override; void SaveLedgerState(const std::string& ledger_state, diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.cc index b3f9d9205ac7..32655f28ce27 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.cc @@ -84,8 +84,6 @@ void Uphold::ContributionCompleted( const double fee, const ledger::ExternalWallet& wallet) { const auto reconcile = ledger_->GetReconcileById(viewing_id); - const auto amount = - braveledger_bat_util::ConvertToProbi(std::to_string(reconcile.fee_)); if (result == ledger::Result::LEDGER_OK) { const auto current_time_seconds = base::Time::Now().ToDoubleT(); @@ -97,17 +95,11 @@ void Uphold::ContributionCompleted( SaveTransferFee(std::move(transfer_fee)); } - ledger_->OnReconcileComplete(result, - viewing_id, - amount, - reconcile.type_); - - if (result != ledger::Result::LEDGER_OK) { - if (!viewing_id.empty()) { - ledger_->RemoveReconcileById(viewing_id); - } - return; - } + ledger_->ReconcileComplete( + result, + reconcile.fee_, + viewing_id, + reconcile.type_); } void Uphold::FetchBalance( diff --git a/vendor/brave-ios/Ledger/BATBraveLedger.mm b/vendor/brave-ios/Ledger/BATBraveLedger.mm index e5b0d639a56f..760d9f4ee92f 100644 --- a/vendor/brave-ios/Ledger/BATBraveLedger.mm +++ b/vendor/brave-ios/Ledger/BATBraveLedger.mm @@ -516,10 +516,10 @@ - (void)listRecurringTips:(void (NS_NOESCAPE ^)(NSArray *))c - (void)addRecurringTipToPublisherWithId:(NSString *)publisherId amount:(double)amount completion:(void (^)(BOOL success))completion { - ledger::ContributionInfoPtr info = ledger::ContributionInfo::New(); - info->publisher = publisherId.UTF8String; - info->value = amount; - info->date = [[NSDate date] timeIntervalSince1970]; + ledger::RecurringTipPtr info = ledger::RecurringTip::New(); + info->publisher_key = publisherId.UTF8String; + info->amount = amount; + info->created_at = [[NSDate date] timeIntervalSince1970]; ledger->SaveRecurringTip(std::move(info), ^(ledger::Result result){ const auto success = (result == ledger::Result::LEDGER_OK); if (success) { @@ -715,23 +715,14 @@ - (BATAutoContributeProps *)autoContributeProps #pragma mark - Reconcile -- (void)onReconcileComplete:(ledger::Result)result viewingId:(const std::string &)viewing_id type:(const ledger::RewardsType)type probi:(const std::string &)probi +- (void)onReconcileComplete:(ledger::Result)result viewingId:(const std::string &)viewing_id type:(const ledger::RewardsType)type amount:(const double)amount { + // TODO we changed from probi to amount, so from string to double if (result == ledger::Result::LEDGER_OK) { - const auto now = [NSDate date]; - const auto nowTimestamp = [now timeIntervalSince1970]; - if (type == ledger::RewardsType::RECURRING_TIP) { [self showTipsProcessedNotificationIfNeccessary]; } [self fetchBalance:nil]; - - ledger->OnReconcileCompleteSuccess(viewing_id, - type, - probi, - BATGetPublisherMonth(now), - BATGetPublisherYear(now), - nowTimestamp); } if ((result == ledger::Result::LEDGER_OK && type == ledger::RewardsType::AUTO_CONTRIBUTE) || @@ -742,7 +733,7 @@ - (void)onReconcileComplete:(ledger::Result)result viewingId:(const std::string const auto info = @{ @"viewingId": viewingId, @"result": @((BATResult)result), @"type": @((BATRewardsType)type), - @"amount": [NSString stringWithUTF8String:probi.c_str()] }; + @"amount": [@(amount) stringValue] }; [self addNotificationOfKind:BATRewardsNotificationKindAutoContribute userInfo:info @@ -757,7 +748,7 @@ - (void)onReconcileComplete:(ledger::Result)result viewingId:(const std::string observer.reconcileCompleted(static_cast(result), [NSString stringWithUTF8String:viewing_id.c_str()], static_cast(type), - [NSString stringWithUTF8String:probi.c_str()]); + [@(amount) stringValue]); } } } @@ -1557,11 +1548,11 @@ - (void)getOneTimeTips:(ledger::PublisherInfoListCallback)callback [self handlePublisherListing:publishers start:0 limit:0 callback:callback]; } -- (void)saveRecurringTip:(ledger::ContributionInfoPtr)info callback:(ledger::SaveRecurringTipCallback)callback +- (void)saveRecurringTip:(ledger::RecurringTipPtr)info callback:(ledger::SaveRecurringTipCallback)callback { - [BATLedgerDatabase insertOrUpdateRecurringTipWithPublisherID:[NSString stringWithUTF8String:info->publisher.c_str()] - amount:info->value - dateAdded:info->date + [BATLedgerDatabase insertOrUpdateRecurringTipWithPublisherID:[NSString stringWithUTF8String:info->publisher_key.c_str()] + amount:info->amount + dateAdded:info->created_at completion:^(BOOL success) { if (!success) { callback(ledger::Result::LEDGER_ERROR); @@ -1675,21 +1666,10 @@ - (void)saveActivityInfo:(ledger::PublisherInfoPtr)publisher_info callback:(ledg } } -- (void)saveContributionInfo:(const std::string &)probi month:(const ledger::ActivityMonth)month year:(const int)year date:(const uint32_t)date publisherKey:(const std::string &)publisher_key type:(const ledger::RewardsType)type -{ - [BATLedgerDatabase insertContributionInfo:[NSString stringWithUTF8String:probi.c_str()] - month:(BATActivityMonth)month - year:year - date:date - publisherKey:[NSString stringWithUTF8String:publisher_key.c_str()] - type:(BATRewardsType)type - completion:^(BOOL success) { - for (BATBraveLedgerObserver *observer in [self.observers copy]) { - if (observer.contributionAdded) { - observer.contributionAdded(success, static_cast(type)); - } - } - }]; +- (void)saveContributionInfo:(ledger::ContributionInfoPtr)info callback:(ledger::ResultCallback)callback +{ + BLOG(ledger::LogLevel::LOG_ERROR) << "Cannot save contribution info; Neccessary DB update not available" << std::endl; + callback(ledger::Result::LEDGER_ERROR); } - (void)saveMediaPublisherInfo:(const std::string &)media_key publisherId:(const std::string &)publisher_id diff --git a/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h b/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h index 73ae63b74f47..72773248016e 100644 --- a/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h +++ b/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h @@ -22,7 +22,7 @@ class NativeLedgerClient : public ledger::LedgerClient { void GetOneTimeTips(ledger::PublisherInfoListCallback callback) override; void GetPendingContributions(ledger::PendingContributionInfoListCallback callback) override; void GetPendingContributionsTotal(ledger::PendingContributionsTotalCallback callback) override; - void SaveRecurringTip(ledger::ContributionInfoPtr info, ledger::SaveRecurringTipCallback callback) override; + void SaveRecurringTip(ledger::RecurringTipPtr info, ledger::SaveRecurringTipCallback callback) override; void GetRecurringTips(ledger::PublisherInfoListCallback callback) override; void KillTimer(const uint32_t timer_id) override; void LoadActivityInfo(ledger::ActivityInfoFilterPtr filter, ledger::PublisherInfoCallback callback) override; @@ -36,7 +36,7 @@ class NativeLedgerClient : public ledger::LedgerClient { void LoadURL(const std::string & url, const std::vector & headers, const std::string & content, const std::string & contentType, const ledger::UrlMethod method, ledger::LoadURLCallback callback) override; std::unique_ptr Log(const char * file, int line, const ledger::LogLevel log_level) const override; void OnPanelPublisherInfo(ledger::Result result, ledger::PublisherInfoPtr publisher_info, uint64_t windowId) override; - void OnReconcileComplete(ledger::Result result, const std::string & viewing_id, const std::string & probi, const ledger::RewardsType type) override; + void OnReconcileComplete(ledger::Result result, const std::string & viewing_id, const double amount, const ledger::RewardsType type) override; void RemoveRecurringTip(const std::string & publisher_key, ledger::RemoveRecurringTipCallback callback) override; void RestorePublishers(ledger::RestorePublishersCallback callback) override; void OnWalletProperties(ledger::Result result, ledger::WalletPropertiesPtr arg1) override; @@ -44,7 +44,7 @@ class NativeLedgerClient : public ledger::LedgerClient { void RemovePendingContribution(const std::string & publisher_key, const std::string & viewing_id, uint64_t added_date, ledger::RemovePendingContributionCallback callback) override; void ResetState(const std::string & name, ledger::OnResetCallback callback) override; void SaveActivityInfo(ledger::PublisherInfoPtr publisher_info, ledger::PublisherInfoCallback callback) override; - void SaveContributionInfo(const std::string & probi, const ledger::ActivityMonth month, const int year, const uint32_t date, const std::string & publisher_key, const ledger::RewardsType type) override; + void SaveContributionInfo(ledger::ContributionInfoPtr info, ledger::ResultCallback callback) override; void SaveLedgerState(const std::string & ledger_state, ledger::LedgerCallbackHandler * handler) override; void SaveMediaPublisherInfo(const std::string & media_key, const std::string & publisher_id) override; void SaveNormalizedPublisherList(ledger::PublisherInfoList normalized_list) override; diff --git a/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.mm b/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.mm index 559a8e8d5846..d24af3480853 100644 --- a/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.mm +++ b/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.mm @@ -32,7 +32,7 @@ void NativeLedgerClient::GetPendingContributionsTotal(ledger::PendingContributionsTotalCallback callback) { [bridge_ getPendingContributionsTotal:callback]; } -void NativeLedgerClient::SaveRecurringTip(ledger::ContributionInfoPtr info, ledger::SaveRecurringTipCallback callback) { +void NativeLedgerClient::SaveRecurringTip(ledger::RecurringTipPtr info, ledger::SaveRecurringTipCallback callback) { [bridge_ saveRecurringTip:std::move(info) callback:callback]; } void NativeLedgerClient::GetRecurringTips(ledger::PublisherInfoListCallback callback) { @@ -74,8 +74,8 @@ void NativeLedgerClient::OnPanelPublisherInfo(ledger::Result result, ledger::PublisherInfoPtr publisher_info, uint64_t windowId) { [bridge_ onPanelPublisherInfo:result publisherInfo:std::move(publisher_info) windowId:windowId]; } -void NativeLedgerClient::OnReconcileComplete(ledger::Result result, const std::string & viewing_id, const std::string & probi, const ledger::RewardsType type) { - [bridge_ onReconcileComplete:result viewingId:viewing_id type:type probi:probi]; +void NativeLedgerClient::OnReconcileComplete(ledger::Result result, const std::string & viewing_id, const double amount, const ledger::RewardsType type) { + [bridge_ onReconcileComplete:result viewingId:viewing_id type:type amount:amount]; } void NativeLedgerClient::RemoveRecurringTip(const std::string & publisher_key, ledger::RemoveRecurringTipCallback callback) { [bridge_ removeRecurringTip:publisher_key callback:callback]; @@ -99,8 +99,8 @@ void NativeLedgerClient::SaveActivityInfo(ledger::PublisherInfoPtr publisher_info, ledger::PublisherInfoCallback callback) { [bridge_ saveActivityInfo:std::move(publisher_info) callback:callback]; } -void NativeLedgerClient::SaveContributionInfo(const std::string & probi, const ledger::ActivityMonth month, const int year, const uint32_t date, const std::string & publisher_key, const ledger::RewardsType type) { - [bridge_ saveContributionInfo:probi month:month year:year date:date publisherKey:publisher_key type:type]; +void NativeLedgerClient::SaveContributionInfo(ledger::ContributionInfoPtr info, ledger::ResultCallback callback) { + [bridge_ saveContributionInfo:std::move(info) callback:callback]; } void NativeLedgerClient::SaveLedgerState(const std::string & ledger_state, ledger::LedgerCallbackHandler * handler) { [bridge_ saveLedgerState:ledger_state handler:handler]; diff --git a/vendor/brave-ios/Ledger/Generated/NativeLedgerClientBridge.h b/vendor/brave-ios/Ledger/Generated/NativeLedgerClientBridge.h index 8c8fbf8967c9..f484770f3532 100644 --- a/vendor/brave-ios/Ledger/Generated/NativeLedgerClientBridge.h +++ b/vendor/brave-ios/Ledger/Generated/NativeLedgerClientBridge.h @@ -15,7 +15,7 @@ - (void)getOneTimeTips:(ledger::PublisherInfoListCallback)callback; - (void)getPendingContributions:(ledger::PendingContributionInfoListCallback)callback; - (void)getPendingContributionsTotal:(ledger::PendingContributionsTotalCallback)callback; -- (void)saveRecurringTip:(ledger::ContributionInfoPtr)info callback:(ledger::SaveRecurringTipCallback)callback; +- (void)saveRecurringTip:(ledger::RecurringTipPtr)info callback:(ledger::SaveRecurringTipCallback)callback; - (void)getRecurringTips:(ledger::PublisherInfoListCallback)callback; - (void)killTimer:(const uint32_t)timer_id; - (void)loadActivityInfo:(ledger::ActivityInfoFilterPtr)filter callback:(ledger::PublisherInfoCallback)callback; @@ -29,7 +29,7 @@ - (void)loadURL:(const std::string &)url headers:(const std::vector &)headers content:(const std::string &)content contentType:(const std::string &)contentType method:(const ledger::UrlMethod)method callback:(ledger::LoadURLCallback)callback; - (std::unique_ptr)log:(const char *)file line:(int)line logLevel:(const ledger::LogLevel)log_level; - (void)onPanelPublisherInfo:(ledger::Result)result publisherInfo:(ledger::PublisherInfoPtr)publisher_info windowId:(uint64_t)windowId; -- (void)onReconcileComplete:(ledger::Result)result viewingId:(const std::string &)viewing_id type:(const ledger::RewardsType)type probi:(const std::string &)probi; +- (void)onReconcileComplete:(ledger::Result)result viewingId:(const std::string &)viewing_id type:(const ledger::RewardsType)type amount:(const double)amount; - (void)removeRecurringTip:(const std::string &)publisher_key callback:(ledger::RemoveRecurringTipCallback)callback; - (void)restorePublishers:(ledger::RestorePublishersCallback)callback; - (void)onWalletProperties:(ledger::Result)result arg1:(ledger::WalletPropertiesPtr)arg1; @@ -37,7 +37,7 @@ - (void)removePendingContribution:(const std::string &)publisher_key viewingId:(const std::string &)viewing_id addedDate:(uint64_t)added_date callback:(ledger::RemovePendingContributionCallback )callback; - (void)resetState:(const std::string &)name callback:(ledger::OnResetCallback)callback; - (void)saveActivityInfo:(ledger::PublisherInfoPtr)publisher_info callback:(ledger::PublisherInfoCallback)callback; -- (void)saveContributionInfo:(const std::string &)probi month:(const ledger::ActivityMonth)month year:(const int)year date:(const uint32_t)date publisherKey:(const std::string &)publisher_key type:(const ledger::RewardsType)type; +- (void)saveContributionInfo:(ledger::ContributionInfoPtr)info callback:(ledger::ResultCallback)callback; - (void)saveLedgerState:(const std::string &)ledger_state handler:(ledger::LedgerCallbackHandler *)handler; - (void)saveMediaPublisherInfo:(const std::string &)media_key publisherId:(const std::string &)publisher_id; - (void)saveNormalizedPublisherList:(ledger::PublisherInfoList)normalized_list;