diff --git a/browser/ui/webui/brave_rewards_ui.cc b/browser/ui/webui/brave_rewards_ui.cc index b4b213a8963d..891d618bcc25 100644 --- a/browser/ui/webui/brave_rewards_ui.cc +++ b/browser/ui/webui/brave_rewards_ui.cc @@ -115,6 +115,12 @@ class RewardsDOMHandler : public WebUIMessageHandler, void OnGetOneTimeTips( std::unique_ptr list); + void GetPendingContributions(const base::ListValue* args); + void OnGetPendingContributions( + std::unique_ptr list); + void RemovePendingContribution(const base::ListValue* args); + void RemoveAllPendingContributions(const base::ListValue* args); + // RewardsServiceObserver implementation void OnWalletInitialized(brave_rewards::RewardsService* rewards_service, uint32_t result) override; @@ -143,7 +149,6 @@ class RewardsDOMHandler : public WebUIMessageHandler, const std::string& viewing_id, const std::string& category, const std::string& probi) override; - void OnPendingContributionSaved( brave_rewards::RewardsService* rewards_service, int result) override; @@ -170,6 +175,10 @@ class RewardsDOMHandler : public WebUIMessageHandler, bool success, int category) override; + void OnPendingContributionRemoved( + brave_rewards::RewardsService* rewards_service, + int32_t result) override; + // RewardsNotificationsServiceObserver implementation void OnNotificationAdded( brave_rewards::RewardsNotificationService* rewards_notification_service, @@ -292,6 +301,16 @@ void RewardsDOMHandler::RegisterMessages() { web_ui()->RegisterMessageCallback("brave_rewards.getExcludedPublishersNumber", base::BindRepeating(&RewardsDOMHandler::GetExcludedPublishersNumber, base::Unretained(this))); + web_ui()->RegisterMessageCallback("brave_rewards.getPendingContributions", + base::BindRepeating(&RewardsDOMHandler::GetPendingContributions, + base::Unretained(this))); + web_ui()->RegisterMessageCallback("brave_rewards.removePendingContribution", + base::BindRepeating(&RewardsDOMHandler::RemovePendingContribution, + base::Unretained(this))); + web_ui()->RegisterMessageCallback( + "brave_rewards.removeAllPendingContribution", + base::BindRepeating(&RewardsDOMHandler::RemoveAllPendingContributions, + base::Unretained(this))); } void RewardsDOMHandler::Init() { @@ -941,6 +960,7 @@ void RewardsDOMHandler::SetBackupCompleted(const base::ListValue *args) { void RewardsDOMHandler::GetPendingContributionsTotal( const base::ListValue* args) { + // refactor this as total is not needed and we should just fetch all of them if (rewards_service_) { rewards_service_->GetPendingContributionsTotal(base::Bind( &RewardsDOMHandler::OnGetPendingContributionsTotal, @@ -958,10 +978,10 @@ void RewardsDOMHandler::OnGetPendingContributionsTotal(double amount) { void RewardsDOMHandler::OnPendingContributionSaved( brave_rewards::RewardsService* rewards_service, int result) { - if (web_ui()->CanCallJavascript()) { - web_ui()->CallJavascriptFunctionUnsafe( - "brave_rewards.onPendingContributionSaved", base::Value(result)); - } + if (web_ui()->CanCallJavascript()) { + web_ui()->CallJavascriptFunctionUnsafe( + "brave_rewards.onPendingContributionSaved", base::Value(result)); + } } void RewardsDOMHandler::OnRewardsMainEnabled( @@ -1079,6 +1099,72 @@ void RewardsDOMHandler::OnContributionSaved( "brave_rewards.onContributionSaved", result); } +void RewardsDOMHandler::GetPendingContributions( + const base::ListValue* args) { + if (rewards_service_) { + rewards_service_->GetPendingContributionsUI(base::Bind( + &RewardsDOMHandler::OnGetPendingContributions, + weak_factory_.GetWeakPtr())); + } +} + +void RewardsDOMHandler::OnGetPendingContributions( + std::unique_ptr list) { + if (web_ui()->CanCallJavascript()) { + auto contributions = std::make_unique(); + for (auto const& item : *list) { + auto contribution = std::make_unique(); + contribution->SetString("publisherKey", item.publisher_key); + contribution->SetBoolean("verified", item.verified); + contribution->SetString("name", item.name); + contribution->SetString("provider", item.provider); + contribution->SetString("url", item.url); + contribution->SetString("favIcon", item.favicon_url); + contribution->SetDouble("amount", item.amount); + contribution->SetInteger("addedDate", item.added_date); + contribution->SetInteger("category", item.category); + contribution->SetString("viewingId", item.viewing_id); + contribution->SetInteger("expirationDate", item.expiration_date); + contributions->Append(std::move(contribution)); + } + + web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.pendingContributions", + *contributions); + } +} + +void RewardsDOMHandler::RemovePendingContribution( + const base::ListValue* args) { + if (rewards_service_) { + std::string publisher_key; + std::string viewing_id; + int added_date; + args->GetString(0, &publisher_key); + args->GetString(1, &viewing_id); + args->GetInteger(2, &added_date); + rewards_service_->RemovePendingContributionUI( + publisher_key, + viewing_id, + static_cast(added_date)); + } +} + +void RewardsDOMHandler::RemoveAllPendingContributions( + const base::ListValue* args) { + if (rewards_service_) { + rewards_service_->RemoveAllPendingContributionsUI(); + } +} + +void RewardsDOMHandler::OnPendingContributionRemoved( + brave_rewards::RewardsService* rewards_service, + int32_t result) { + if (web_ui()->CanCallJavascript()) { + web_ui()->CallJavascriptFunctionUnsafe( + "brave_rewards.onRemovePendingContribution", base::Value(result)); + } +} + } // namespace BraveRewardsUI::BraveRewardsUI(content::WebUI* web_ui, const std::string& name) diff --git a/browser/ui/webui/brave_webui_source.cc b/browser/ui/webui/brave_webui_source.cc index 156c7bb40481..9325a510f727 100644 --- a/browser/ui/webui/brave_webui_source.cc +++ b/browser/ui/webui/brave_webui_source.cc @@ -368,6 +368,13 @@ void CustomizeWebUIHTMLSource(const std::string &name, { "paymentMonthly", IDS_BRAVE_UI_PAYMENT_MONTHLY }, { "paymentNotMade", IDS_BRAVE_UI_PAYMENT_NOT_MADE }, { "paymentWarning", IDS_BRAVE_UI_PAYMENT_WARNING }, + { "pendingContributions", IDS_BRAVE_UI_PENDING_CONTRIBUTIONS }, + { "pendingContributionEmpty", IDS_BRAVE_UI_PENDING_CONTRIBUTION_EMPTY }, + { "pendingContributionRemoveAll", IDS_BRAVE_UI_PENDING_CONTRIBUTION_REMOVE_ALL }, // NOLINT + { "pendingTyperecurring", IDS_BRAVE_UI_PENDING_TYPE_RECURRING }, + { "pendingTypetip", IDS_BRAVE_UI_PENDING_TYPE_TIP }, + { "pendingTypeac", IDS_BRAVE_UI_PENDING_TYPE_AC }, + { "pendingUntil", IDS_BRAVE_UI_PENDING_UNTIL }, { "pinnedSitesHeader", IDS_BRAVE_UI_PAYMENT_PINNED_SITES_HEADER }, { "pinnedSitesMsg", IDS_BRAVE_UI_PAYMENT_PINNED_SITES_MSG }, { "pinnedSitesOne", IDS_BRAVE_UI_PAYMENT_PINNED_SITES_ONE }, @@ -386,6 +393,7 @@ void CustomizeWebUIHTMLSource(const std::string &name, { "remove", IDS_BRAVE_UI_REMOVE }, { "reservedAmountText", IDS_BRAVE_UI_RESERVED_AMOUNT_TEXT }, { "reservedMoreLink", IDS_BRAVE_UI_RESERVED_MORE_LINK }, + { "reservedAllLink", IDS_BRAVE_UI_RESERVED_ALL_LINK }, { "restore", IDS_BRAVE_UI_RESTORE }, { "restoreAll", IDS_BRAVE_UI_RESTORE_ALL }, { "reviewSitesMsg", IDS_BRAVE_UI_REVIEW_SITE_MSG }, diff --git a/common/extensions/api/brave_rewards.json b/common/extensions/api/brave_rewards.json index 51e260dc07b8..af950f7a97bd 100644 --- a/common/extensions/api/brave_rewards.json +++ b/common/extensions/api/brave_rewards.json @@ -330,6 +330,17 @@ "type": "boolean" } ] + }, + { + "name": "onPendingContributionRemoved", + "type": "function", + "description": "", + "parameters": [ + { + "name": "result", + "type": "integer" + } + ] } ], "functions": [ diff --git a/components/brave_ads/browser/ads_service_impl_unittest.cc b/components/brave_ads/browser/ads_service_impl_unittest.cc index f86fb7553a03..978acd004c73 100644 --- a/components/brave_ads/browser/ads_service_impl_unittest.cc +++ b/components/brave_ads/browser/ads_service_impl_unittest.cc @@ -143,6 +143,12 @@ class MockRewardsService : public RewardsService { brave_rewards::RefreshPublisherCallback)); MOCK_METHOD0(GetAllNotifications, const brave_rewards::RewardsNotificationService::RewardsNotificationsMap&()); + MOCK_METHOD1(GetPendingContributionsUI, + void(brave_rewards::GetPendingContributionsCallback)); + MOCK_METHOD3(RemovePendingContributionUI, void(const std::string&, + const std::string&, + uint64_t)); + MOCK_METHOD0(RemoveAllPendingContributionsUI, void()); }; class AdsServiceTest : public testing::Test { diff --git a/components/brave_rewards/browser/BUILD.gn b/components/brave_rewards/browser/BUILD.gn index 07a19561a732..82d0ffbcfad3 100644 --- a/components/brave_rewards/browser/BUILD.gn +++ b/components/brave_rewards/browser/BUILD.gn @@ -27,6 +27,8 @@ source_set("browser") { "wallet_properties.h", "grant.cc", "grant.h", + "pending_contribution.cc", + "pending_contribution.h", "publisher_banner.cc", "publisher_banner.h", "contribution_info.cc", diff --git a/components/brave_rewards/browser/extension_rewards_service_observer.cc b/components/brave_rewards/browser/extension_rewards_service_observer.cc index bf0ff3ce7666..69c821d9ae60 100644 --- a/components/brave_rewards/browser/extension_rewards_service_observer.cc +++ b/components/brave_rewards/browser/extension_rewards_service_observer.cc @@ -359,4 +359,23 @@ void ExtensionRewardsServiceObserver::OnRecurringTipRemoved( event_router->BroadcastEvent(std::move(event)); } +void ExtensionRewardsServiceObserver::OnPendingContributionRemoved( + RewardsService* rewards_service, + int32_t result) { + extensions::EventRouter* event_router = + extensions::EventRouter::Get(profile_); + if (!event_router) { + return; + } + + std::unique_ptr args( + extensions::api::brave_rewards::OnPendingContributionRemoved::Create( + result).release()); + std::unique_ptr event(new extensions::Event( + extensions::events::BRAVE_START, + extensions::api::brave_rewards::OnPendingContributionRemoved::kEventName, + std::move(args))); + event_router->BroadcastEvent(std::move(event)); +} + } // namespace brave_rewards diff --git a/components/brave_rewards/browser/extension_rewards_service_observer.h b/components/brave_rewards/browser/extension_rewards_service_observer.h index f53da74bb0ec..f526aba634fc 100644 --- a/components/brave_rewards/browser/extension_rewards_service_observer.h +++ b/components/brave_rewards/browser/extension_rewards_service_observer.h @@ -46,6 +46,9 @@ class ExtensionRewardsServiceObserver : public RewardsServiceObserver, void OnRecurringTipRemoved(RewardsService* rewards_service, bool success) override; + void OnPendingContributionRemoved(RewardsService* rewards_service, + int32_t result) override; + // RewardsServicePrivateObserver implementation void OnGetCurrentBalanceReport(RewardsService* rewards_service, const BalanceReport& balance_report) override; diff --git a/components/brave_rewards/browser/pending_contribution.cc b/components/brave_rewards/browser/pending_contribution.cc index 8caefa9c387b..cfafef040fd3 100644 --- a/components/brave_rewards/browser/pending_contribution.cc +++ b/components/brave_rewards/browser/pending_contribution.cc @@ -1,16 +1,32 @@ -/* 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/. */ +/* 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/pending_contribution.h" namespace brave_rewards { -PendingContribution::PendingContribution() : +PendingContributionInfo::PendingContributionInfo() : amount(0), - added_date(0), - reconcile_date(0) { + added_date(0) { } -PendingContribution::~PendingContribution() { } +PendingContributionInfo::~PendingContributionInfo() { } + +PendingContributionInfo::PendingContributionInfo( + const PendingContributionInfo& data) { + publisher_key = data.publisher_key; + verified = data.verified; + name = data.name; + favicon_url = data.favicon_url; + url = data.url; + provider = data.provider; + amount = data.amount; + added_date = data.added_date; + viewing_id = data.viewing_id; + category = data.category; + expiration_date = data.expiration_date; +} } // namespace brave_rewards diff --git a/components/brave_rewards/browser/pending_contribution.h b/components/brave_rewards/browser/pending_contribution.h index 368760b38b04..c94e0a1cdd31 100644 --- a/components/brave_rewards/browser/pending_contribution.h +++ b/components/brave_rewards/browser/pending_contribution.h @@ -1,27 +1,36 @@ -/* 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/. */ +/* 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_BROWSER_PAYMENTS_PENDING_CONTRIBUTION_ -#define BRAVE_BROWSER_PAYMENTS_PENDING_CONTRIBUTION_ +#ifndef BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_PENDING_CONTRIBUTION_H_ +#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_PENDING_CONTRIBUTION_H_ #include +#include namespace brave_rewards { -struct PendingContribution { - PendingContribution(); - ~PendingContribution(); - PendingContribution(const PendingContribution& data) = default; +struct PendingContributionInfo { + PendingContributionInfo(); + ~PendingContributionInfo(); + PendingContributionInfo(const PendingContributionInfo& data); std::string publisher_key; + bool verified; + std::string name; + std::string favicon_url; + std::string url; + std::string provider; double amount = 0; uint32_t added_date = 0; - uint32_t reconcile_date = 0; + std::string viewing_id; + int category; + uint32_t expiration_date = 0; }; -using PendingContributionList = std::vector; +using PendingContributionInfoList = std::vector; } // namespace brave_rewards -#endif //BRAVE_BROWSER_PAYMENTS_PENDING_CONTRIBUTION_ +#endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_PENDING_CONTRIBUTION_H_ diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index ed53d8315705..4f828622f513 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -14,6 +14,7 @@ #include "base/command_line.h" #include "base/files/file_util.h" #include "bat/ledger/media_publisher_info.h" +#include "bat/ledger/pending_contribution.h" #include "build/build_config.h" #include "sql/meta_table.h" #include "sql/statement.h" @@ -941,7 +942,7 @@ bool PublisherInfoDatabase::InsertPendingContribution } double PublisherInfoDatabase::GetReservedAmount() { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); bool initialized = Init(); DCHECK(initialized); @@ -962,6 +963,84 @@ double PublisherInfoDatabase::GetReservedAmount() { return amount; } +void PublisherInfoDatabase::GetPendingContributions( + ledger::PendingContributionInfoList* list) { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + + bool initialized = Init(); + DCHECK(initialized); + + if (!initialized) { + return; + } + + sql::Statement info_sql(db_.GetUniqueStatement( + "SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, " + "pi.verified, pi.provider, pc.amount, pc.added_date, " + "pc.viewing_id, pc.category " + "FROM pending_contribution as pc " + "INNER JOIN publisher_info AS pi ON pc.publisher_id = pi.publisher_id")); + + while (info_sql.Step()) { + ledger::PendingContributionInfo info; + info.publisher_key = info_sql.ColumnString(0); + info.name = info_sql.ColumnString(1); + info.url = info_sql.ColumnString(2); + info.favicon_url = info_sql.ColumnString(3); + info.verified = info_sql.ColumnBool(4); + info.provider = info_sql.ColumnString(5); + info.amount = info_sql.ColumnDouble(6); + info.added_date = info_sql.ColumnInt64(7); + info.viewing_id = info_sql.ColumnString(8); + info.category = + static_cast(info_sql.ColumnInt(9)); + + list->push_back(info); + } +} + +bool PublisherInfoDatabase::RemovePendingContributions( + const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date) { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + + bool initialized = Init(); + DCHECK(initialized); + + if (!initialized) { + return false; + } + + sql::Statement statement(GetDB().GetCachedStatement( + SQL_FROM_HERE, + "DELETE FROM pending_contribution " + "WHERE publisher_id = ? AND viewing_id=? AND added_date=?")); + + statement.BindString(0, publisher_key); + statement.BindString(1, viewing_id); + statement.BindInt64(2, added_date); + + return statement.Run(); +} + +bool PublisherInfoDatabase::RemoveAllPendingContributions() { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + + bool initialized = Init(); + DCHECK(initialized); + + if (!initialized) { + return false; + } + + sql::Statement statement(GetDB().GetCachedStatement( + SQL_FROM_HERE, + "DELETE FROM pending_contribution")); + + return statement.Run(); +} + int PublisherInfoDatabase::GetCurrentVersion() { if (testing_current_version_ != -1) { return testing_current_version_; diff --git a/components/brave_rewards/browser/publisher_info_database.h b/components/brave_rewards/browser/publisher_info_database.h index 5e1bd88bf2fc..a189aa8bcc24 100644 --- a/components/brave_rewards/browser/publisher_info_database.h +++ b/components/brave_rewards/browser/publisher_info_database.h @@ -82,6 +82,15 @@ class PublisherInfoDatabase { double GetReservedAmount(); + void GetPendingContributions( + ledger::PendingContributionInfoList* list); + + bool RemovePendingContributions(const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date); + + bool RemoveAllPendingContributions(); + // Returns the current version of the publisher info database int GetCurrentVersion(); diff --git a/components/brave_rewards/browser/publisher_info_database_unittest.cc b/components/brave_rewards/browser/publisher_info_database_unittest.cc index 61f52b9d391a..9996bb92748b 100644 --- a/components/brave_rewards/browser/publisher_info_database_unittest.cc +++ b/components/brave_rewards/browser/publisher_info_database_unittest.cc @@ -36,6 +36,8 @@ class PublisherInfoDatabaseTest : public ::testing::Test { ~PublisherInfoDatabaseTest() override { } + void PreparePendingContributions(); + sql::Database& GetDB() { return publisher_info_database_->GetDB(); } @@ -1006,4 +1008,151 @@ TEST_F(PublisherInfoDatabaseTest, DeleteActivityInfo) { EXPECT_EQ(list.at(1)->id, "publisher_2"); } +void PublisherInfoDatabaseTest::PreparePendingContributions() { + // Insert publishers + ledger::PublisherInfo info; + info.id = "key1"; + info.verified = false; + info.excluded = ledger::PUBLISHER_EXCLUDE::DEFAULT; + info.name = "key1"; + info.url = "https://key1.com"; + info.provider = ""; + info.favicon_url = ""; + + bool success = publisher_info_database_->InsertOrUpdatePublisherInfo(info); + EXPECT_TRUE(success); + + info.id = "key2"; + info.name = "key2"; + info.url = "https://key2.com"; + success = publisher_info_database_->InsertOrUpdatePublisherInfo(info); + EXPECT_TRUE(success); + + info.id = "key3"; + info.name = "key3"; + info.url = "https://key3.com"; + success = publisher_info_database_->InsertOrUpdatePublisherInfo(info); + EXPECT_TRUE(success); + + info.id = "key4"; + info.name = "key4"; + info.url = "https://key4.com"; + success = publisher_info_database_->InsertOrUpdatePublisherInfo(info); + EXPECT_TRUE(success); + + EXPECT_EQ(CountTableRows("publisher_info"), 4); + + // Insert some pending contributions + ledger::PendingContribution contribution1; + contribution1.publisher_key = "key1"; + contribution1.amount = 10; + contribution1.viewing_id = "fsodfsdnf23r23rn"; + contribution1.category = ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE; + + ledger::PendingContribution contribution2; + contribution2.publisher_key = "key2"; + contribution2.amount = 20; + contribution2.viewing_id = "aafsoffdffdfsdnf23r23rn"; + contribution2.category = ledger::REWARDS_CATEGORY::ONE_TIME_TIP; + + ledger::PendingContribution contribution3; + contribution3.publisher_key = "key3"; + contribution3.amount = 30; + contribution3.viewing_id = "aafszxfzcofdfsdnf23r23rn"; + contribution3.category = ledger::REWARDS_CATEGORY::ONE_TIME_TIP; + + ledger::PendingContribution contribution4; + contribution4.publisher_key = "key4"; + contribution4.amount = 40; + contribution4.viewing_id = "aafsofdfs12333dnf23r23rn"; + contribution4.category = ledger::REWARDS_CATEGORY::ONE_TIME_TIP; + + ledger::PendingContributionList list; + list.list_.push_back(contribution1); + list.list_.push_back(contribution2); + list.list_.push_back(contribution3); + list.list_.push_back(contribution4); + + success = publisher_info_database_->InsertPendingContribution( + list); + EXPECT_TRUE(success); + EXPECT_EQ(CountTableRows("pending_contribution"), 4); +} + +TEST_F(PublisherInfoDatabaseTest, GetPendingContributions) { + base::ScopedTempDir temp_dir; + base::FilePath db_file; + CreateTempDatabase(&temp_dir, &db_file); + + PreparePendingContributions(); + + /** + * Good path + */ + ledger::PendingContributionInfoList select_list; + publisher_info_database_->GetPendingContributions(&select_list); + EXPECT_EQ(static_cast(select_list.size()), 4); + + EXPECT_EQ(select_list.at(0).publisher_key, "key1"); + EXPECT_EQ(select_list.at(1).publisher_key, "key2"); + EXPECT_EQ(select_list.at(2).publisher_key, "key3"); + EXPECT_EQ(select_list.at(3).publisher_key, "key4"); + + EXPECT_EQ(select_list.at(0).url, "https://key1.com"); +} + +TEST_F(PublisherInfoDatabaseTest, RemovePendingContributions) { + base::ScopedTempDir temp_dir; + base::FilePath db_file; + CreateTempDatabase(&temp_dir, &db_file); + + PreparePendingContributions(); + + /** + * Good path + */ + ledger::PendingContributionInfoList select_list; + publisher_info_database_->GetPendingContributions(&select_list); + EXPECT_EQ(select_list.at(0).publisher_key, "key1"); + bool success = publisher_info_database_->RemovePendingContributions( + "key1", + "fsodfsdnf23r23rn", + select_list.at(0).added_date); + EXPECT_TRUE(success); + + ledger::PendingContributionInfoList list; + publisher_info_database_->GetPendingContributions(&list); + EXPECT_EQ(static_cast(list.size()), 3); + + EXPECT_EQ(list.at(0).publisher_key, "key2"); + EXPECT_EQ(list.at(1).publisher_key, "key3"); + EXPECT_EQ(list.at(2).publisher_key, "key4"); + + /** + * Trying to delete not existing row + */ + success = publisher_info_database_->RemovePendingContributions( + "key0", + "viewing_id", + 10); + EXPECT_TRUE(success); + EXPECT_EQ(CountTableRows("pending_contribution"), 3); +} + + +TEST_F(PublisherInfoDatabaseTest, RemoveAllPendingContributions) { + base::ScopedTempDir temp_dir; + base::FilePath db_file; + CreateTempDatabase(&temp_dir, &db_file); + + PreparePendingContributions(); + + /** + * Good path + */ + bool success = publisher_info_database_->RemoveAllPendingContributions(); + EXPECT_TRUE(success); + EXPECT_EQ(CountTableRows("pending_contribution"), 0); +} + } // namespace brave_rewards diff --git a/components/brave_rewards/browser/rewards_service.h b/components/brave_rewards/browser/rewards_service.h index 0547ffd02318..df5ad9483e6b 100644 --- a/components/brave_rewards/browser/rewards_service.h +++ b/components/brave_rewards/browser/rewards_service.h @@ -16,6 +16,7 @@ #include "brave/components/brave_rewards/browser/balance_report.h" #include "brave/components/brave_rewards/browser/content_site.h" #include "brave/components/brave_rewards/browser/publisher_banner.h" +#include "brave/components/brave_rewards/browser/pending_contribution.h" #include "brave/components/brave_rewards/browser/rewards_internals_info.h" #include "brave/components/brave_rewards/browser/rewards_notification_service.h" #include "build/build_config.h" @@ -77,6 +78,8 @@ using GetPublisherBannerCallback = base::OnceCallback)>; using RefreshPublisherCallback = base::OnceCallback; +using GetPendingContributionsCallback = base::OnceCallback)>; class RewardsService : public KeyedService { public: @@ -195,6 +198,14 @@ class RewardsService : public KeyedService { const std::string& publisher_key, RefreshPublisherCallback callback) = 0; + virtual void GetPendingContributionsUI( + GetPendingContributionsCallback callback) = 0; + + virtual void RemovePendingContributionUI(const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date) = 0; + virtual void RemoveAllPendingContributionsUI() = 0; + void AddObserver(RewardsServiceObserver* observer); void RemoveObserver(RewardsServiceObserver* observer); diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 2355b8dd35ca..ac24a173808d 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -2932,4 +2932,182 @@ RewardsServiceImpl::GetAllNotifications() { return notification_service_->GetAllNotifications(); } +ledger::PendingContributionInfoList PendingContributionsOnFileTaskRunner( + PublisherInfoDatabase* backend) { + ledger::PendingContributionInfoList list; + if (!backend) { + return list; + } + + backend->GetPendingContributions(&list); + + return list; +} + +PendingContributionInfo PendingContributionLedgerToRewards( + const ledger::PendingContributionInfo& contribution) { + PendingContributionInfo info; + info.publisher_key = contribution.publisher_key; + info.category = contribution.category; + info.verified = contribution.verified; + info.name = contribution.name; + info.url = contribution.url; + info.provider = contribution.provider; + info.favicon_url = contribution.favicon_url; + info.amount = contribution.amount; + info.added_date = contribution.added_date; + info.viewing_id = contribution.viewing_id; + info.expiration_date = contribution.expiration_date; + return info; +} + +void RewardsServiceImpl::OnGetPendingContributionsUI( + GetPendingContributionsCallback callback, + const std::vector& json_list) { + std::unique_ptr new_list( + new brave_rewards::PendingContributionInfoList); + for (auto &json_contribution : json_list) { + ledger::PendingContributionInfo contribution; + contribution.loadFromJson(json_contribution); + brave_rewards::PendingContributionInfo new_contribution = + PendingContributionLedgerToRewards(contribution); + new_list->push_back(new_contribution); + } + + std::move(callback).Run(std::move(new_list)); +} + +void RewardsServiceImpl::GetPendingContributionsUI( + GetPendingContributionsCallback callback) { + bat_ledger_->GetPendingContributions( + base::BindOnce(&RewardsServiceImpl::OnGetPendingContributionsUI, + AsWeakPtr(), + std::move(callback))); +} + +void RewardsServiceImpl::OnGetPendingContributions( + const ledger::PendingContributionInfoListCallback& callback, + const ledger::PendingContributionInfoList& list) { + if (!Connected()) { + return; + } + + callback(list); +} + +void RewardsServiceImpl::GetPendingContributions( + const ledger::PendingContributionInfoListCallback& callback) { + base::PostTaskAndReplyWithResult( + file_task_runner_.get(), + FROM_HERE, + base::Bind(&PendingContributionsOnFileTaskRunner, + publisher_info_backend_.get()), + base::Bind(&RewardsServiceImpl::OnGetPendingContributions, + AsWeakPtr(), + callback)); +} + +void RewardsServiceImpl::OnPendingContributionRemovedUI(int32_t result) { + for (auto& observer : observers_) { + observer.OnPendingContributionRemoved(this, result); + } +} + +void RewardsServiceImpl::RemovePendingContributionUI( + const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date) { + bat_ledger_->RemovePendingContribution( + publisher_key, + viewing_id, + added_date, + base::BindOnce(&RewardsServiceImpl::OnPendingContributionRemovedUI, + AsWeakPtr())); +} + +bool RemovePendingContributionOnFileTaskRunner( + PublisherInfoDatabase* backend, + const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date) { + if (!backend) { + return false; + } + + return backend->RemovePendingContributions(publisher_key, + viewing_id, + added_date); +} + +void RewardsServiceImpl::RemovePendingContribution( + const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date, + const ledger::RemovePendingContributionCallback& callback) { + base::PostTaskAndReplyWithResult( + file_task_runner_.get(), + FROM_HERE, + base::Bind(&RemovePendingContributionOnFileTaskRunner, + publisher_info_backend_.get(), + publisher_key, + viewing_id, + added_date), + base::Bind(&RewardsServiceImpl::OnPendingContributionRemoved, + AsWeakPtr(), + callback)); +} + +void RewardsServiceImpl::OnPendingContributionRemoved( + ledger::RemovePendingContributionCallback callback, + bool result) { + ledger::Result result_new = result + ? ledger::Result::LEDGER_OK + : ledger::Result::LEDGER_ERROR; + + callback(result_new); +} + +bool RemoveAllPendingContributionOnFileTaskRunner( + PublisherInfoDatabase* backend) { + if (!backend) { + return false; + } + + return backend->RemoveAllPendingContributions(); +} + +void RewardsServiceImpl::OnRemoveAllPendingContributionsUI(int32_t result) { + for (auto& observer : observers_) { + observer.OnPendingContributionRemoved(this, result); + } +} + +void RewardsServiceImpl::RemoveAllPendingContributionsUI() { + bat_ledger_->RemoveAllPendingContributions( + base::BindOnce(&RewardsServiceImpl::OnRemoveAllPendingContributionsUI, + AsWeakPtr())); +} + +void RewardsServiceImpl::OnRemoveAllPendingContribution( + ledger::RemovePendingContributionCallback callback, + bool result) { + ledger::Result result_new = result + ? ledger::Result::LEDGER_OK + : ledger::Result::LEDGER_ERROR; + + callback(result_new); +} + +void RewardsServiceImpl::RemoveAllPendingContributions( + const ledger::RemovePendingContributionCallback& callback) { + base::PostTaskAndReplyWithResult( + file_task_runner_.get(), + FROM_HERE, + base::Bind(&RemoveAllPendingContributionOnFileTaskRunner, + publisher_info_backend_.get()), + base::Bind(&RewardsServiceImpl::OnRemoveAllPendingContribution, + AsWeakPtr(), + callback)); +} + } // namespace brave_rewards diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index 3c4990522bbb..bdcca29acca0 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -218,6 +218,15 @@ class RewardsServiceImpl : public RewardsService, void SetContributionAmount(const double amount) const override; + void GetPendingContributionsUI( + GetPendingContributionsCallback callback) override; + + void RemovePendingContributionUI(const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date) override; + + void RemoveAllPendingContributionsUI() override; + // Testing methods void SetLedgerEnvForTesting(); void StartAutoContributeForTest(); @@ -321,6 +330,45 @@ class RewardsServiceImpl : public RewardsService, uint32_t result, ledger::PublisherInfoPtr info); + void OnPendingContributionRemoved( + ledger::RemovePendingContributionCallback callback, + bool result); + + void OnRemoveAllPendingContribution( + ledger::RemovePendingContributionCallback callback, + bool result); + + void OnGetPendingContributionsUI( + GetPendingContributionsCallback callback, + const std::vector& json_list); + + void OnGetPendingContributions( + const ledger::PendingContributionInfoListCallback& callback, + const ledger::PendingContributionInfoList& list); + + void OnGetExcludedPublishersNumberDB( + ledger::GetExcludedPublishersNumberDBCallback callback, + int number); + + void OnURLLoaderComplete(network::SimpleURLLoader* loader, + ledger::LoadURLCallback callback, + std::unique_ptr response_body); + + void StartNotificationTimers(bool main_enabled); + void StopNotificationTimers(); + void OnNotificationTimerFired(); + + void MaybeShowNotificationAddFunds(); + bool ShouldShowNotificationAddFunds() const; + void ShowNotificationAddFunds(bool sufficient); + + void MaybeShowNotificationTipsPaid(); + void ShowNotificationTipsPaid(bool ac_enabled); + + void OnPendingContributionRemovedUI(int32_t result); + + void OnRemoveAllPendingContributionsUI(int32_t result); + // ledger::LedgerClient std::string GenerateGUID() const override; void OnWalletInitialized(ledger::Result result) override; @@ -441,24 +489,19 @@ class RewardsServiceImpl : public RewardsService, void GetExcludedPublishersNumberDB( ledger::GetExcludedPublishersNumberDBCallback callback) override; - void OnGetExcludedPublishersNumberDB( - ledger::GetExcludedPublishersNumberDBCallback callback, - int number); + void GetPendingContributions( + const ledger::PendingContributionInfoListCallback& callback) override; - void OnURLLoaderComplete(network::SimpleURLLoader* loader, - ledger::LoadURLCallback callback, - std::unique_ptr response_body); + void RemovePendingContribution( + const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date, + const ledger::RemovePendingContributionCallback& callback) override; - void StartNotificationTimers(bool main_enabled); - void StopNotificationTimers(); - void OnNotificationTimerFired(); + void RemoveAllPendingContributions( + const ledger::RemovePendingContributionCallback& callback) override; - void MaybeShowNotificationAddFunds(); - bool ShouldShowNotificationAddFunds() const; - void ShowNotificationAddFunds(bool sufficient); - - void MaybeShowNotificationTipsPaid(); - void ShowNotificationTipsPaid(bool ac_enabled); + // end ledger::LedgerClient // Mojo Proxy methods void OnGetTransactionHistoryForThisCycle( diff --git a/components/brave_rewards/browser/rewards_service_observer.h b/components/brave_rewards/browser/rewards_service_observer.h index 149294fe84ff..10a66dc01001 100644 --- a/components/brave_rewards/browser/rewards_service_observer.h +++ b/components/brave_rewards/browser/rewards_service_observer.h @@ -82,6 +82,9 @@ class RewardsServiceObserver : public base::CheckedObserver { brave_rewards::RewardsService* rewards_service, bool success, int category) {} + virtual void OnPendingContributionRemoved( + brave_rewards::RewardsService* rewards_service, + int32_t result) {} // DO NOT ADD ANY MORE METHODS HERE UNLESS IT IS A BROADCAST NOTIFICATION // RewardsServiceObserver should not be used to return responses to the // caller. Method calls on RewardsService should use callbacks to return diff --git a/components/brave_rewards/resources/extension/brave_rewards/background/events/rewardsEvents.ts b/components/brave_rewards/resources/extension/brave_rewards/background/events/rewardsEvents.ts index 57e127570974..5fc6de73505e 100644 --- a/components/brave_rewards/resources/extension/brave_rewards/background/events/rewardsEvents.ts +++ b/components/brave_rewards/resources/extension/brave_rewards/background/events/rewardsEvents.ts @@ -86,3 +86,11 @@ chrome.braveRewards.onRecurringTipRemoved.addListener((success: boolean) => { }) } }) + +chrome.braveRewards.onPendingContributionSaved.addListener((result: number) => { + if (result === 0) { + chrome.braveRewards.getPendingContributionsTotal(((amount: number) => { + rewardsPanelActions.OnPendingContributionsTotal(amount) + })) + } +}) diff --git a/components/brave_rewards/resources/ui/actions/rewards_actions.ts b/components/brave_rewards/resources/ui/actions/rewards_actions.ts index 1c4faed4b1d9..b4404dc1d603 100644 --- a/components/brave_rewards/resources/ui/actions/rewards_actions.ts +++ b/components/brave_rewards/resources/ui/actions/rewards_actions.ts @@ -158,11 +158,12 @@ export const getAddresses = () => action(types.GET_ADDRESSES) export const getReconcileStamp = () => action(types.GET_RECONCILE_STAMP) -export const getPendingContributionsTotal = () => action(types.GET_PENDING_CONTRIBUTION_TOTAL) +export const getPendingContributions = () => action(types.GET_PENDING_CONTRIBUTIONS) -export const onPendingContributionTotal = (amount: number) => action(types.ON_PENDING_CONTRIBUTION_TOTAL, { - amount -}) +export const onPendingContributions = (list: Rewards.PendingContribution[]) => + action(types.ON_PENDING_CONTRIBUTIONS, { + list + }) export const onRewardsEnabled = (enabled: boolean) => action(types.ON_REWARDS_ENABLED, { enabled @@ -200,3 +201,12 @@ export const onContributionSaved = (properties: Rewards.ContributionSaved) => action(types.ON_CONTRIBUTION_SAVED, { properties }) + +export const removePendingContribution = (publisherKey: string, viewingId: string, addedDate: number) => + action(types.REMOVE_PENDING_CONTRIBUTION, { + publisherKey, + viewingId, + addedDate + }) + +export const removeAllPendingContribution = () => action(types.REMOVE_ALL_PENDING_CONTRIBUTION) diff --git a/components/brave_rewards/resources/ui/brave_rewards.tsx b/components/brave_rewards/resources/ui/brave_rewards.tsx index b8df0b29f4e8..6886b97e13f5 100644 --- a/components/brave_rewards/resources/ui/brave_rewards.tsx +++ b/components/brave_rewards/resources/ui/brave_rewards.tsx @@ -129,13 +129,9 @@ window.cr.define('brave_rewards', function () { getActions().onAdsData(adsData) } - function pendingContributionTotal (amount: number) { - getActions().onPendingContributionTotal(amount) - } - function onPendingContributionSaved (result: number) { if (result === 0) { - getActions().getPendingContributionsTotal() + getActions().getPendingContributions() } } @@ -167,6 +163,16 @@ window.cr.define('brave_rewards', function () { getActions().onContributionSaved(properties) } + function pendingContributions (list: Rewards.PendingContribution[]) { + getActions().onPendingContributions(list) + } + + function onRemovePendingContribution (result: number) { + if (result === 0) { + getActions().getPendingContributions() + } + } + return { initialize, walletCreated, @@ -189,7 +195,7 @@ window.cr.define('brave_rewards', function () { initAutoContributeSettings, imported, adsData, - pendingContributionTotal, + pendingContributions, onPendingContributionSaved, rewardsEnabled, addressesForPaymentId, @@ -197,7 +203,8 @@ window.cr.define('brave_rewards', function () { transactionHistoryForThisCycleChanged, recurringTipSaved, recurringTipRemoved, - onContributionSaved + onContributionSaved, + onRemovePendingContribution } }) diff --git a/components/brave_rewards/resources/ui/components/pageWallet.tsx b/components/brave_rewards/resources/ui/components/pageWallet.tsx index 41c9be9f446a..483a2a7face0 100644 --- a/components/brave_rewards/resources/ui/components/pageWallet.tsx +++ b/components/brave_rewards/resources/ui/components/pageWallet.tsx @@ -5,18 +5,19 @@ import * as React from 'react' import { bindActionCreators, Dispatch } from 'redux' import { connect } from 'react-redux' - // Components import { ModalActivity, ModalBackupRestore, - WalletWrapper, + ModalPending, WalletEmpty, - WalletSummary + WalletSummary, + WalletWrapper } from 'brave-ui/features/rewards' import { WalletAddIcon } from 'brave-ui/components/icons' import { AlertWallet } from 'brave-ui/features/rewards/walletWrapper' - +import { Provider } from 'brave-ui/features/rewards/profile' +import { DetailRow as PendingDetailRow, PendingType } from 'brave-ui/features/rewards/tablePending' // Utils import { getLocale } from '../../../../common/locale' import * as rewardsActions from '../actions/rewards_actions' @@ -31,6 +32,7 @@ interface State { modalBackup: boolean modalActivity: boolean modalAddFunds: boolean + modalPendingContribution: boolean } interface Props extends Rewards.ComponentProps { @@ -43,7 +45,8 @@ class PageWallet extends React.Component { activeTabId: 0, modalBackup: false, modalActivity: false, - modalAddFunds: false + modalAddFunds: false, + modalPendingContribution: false } } @@ -185,6 +188,12 @@ class PageWallet extends React.Component { ) } + onModalPendingToggle = () => { + this.setState({ + modalPendingContribution: !this.state.modalPendingContribution + }) + } + isAddFundsUrl = () => { if (this.urlHashIs('#add-funds')) { this.setState({ @@ -262,7 +271,7 @@ class PageWallet extends React.Component { } getWalletSummary = () => { - const { walletInfo, reports } = this.props.rewardsData + const { walletInfo, reports, pendingContributionTotal } = this.props.rewardsData const { rates } = walletInfo let props = {} @@ -284,9 +293,57 @@ class PageWallet extends React.Component { } } - return { - report: props + let result: {report: Record, onSeeAllReserved?: () => {}} = { + report: props, + onSeeAllReserved: undefined } + + if (pendingContributionTotal > 0) { + result.onSeeAllReserved = this.onModalPendingToggle.bind(this) + } + + return result + } + + getPendingRows = (): PendingDetailRow[] => { + const { walletInfo, pendingContributions } = this.props.rewardsData + return pendingContributions.map((item: Rewards.PendingContribution) => { + let faviconUrl = `chrome://favicon/size/48@1x/${item.url}` + if (item.favIcon && item.verified) { + faviconUrl = `chrome://favicon/size/48@1x/${item.favIcon}` + } + + let type: PendingType = 'ac' + + if (item.category === 8) { // one time tip + type = 'tip' + } else if (item.category === 16) { // recurring tip + type = 'recurring' + } + + return { + profile: { + name: item.name, + verified: item.verified, + provider: (item.provider ? item.provider : undefined) as Provider, + src: faviconUrl + }, + url: item.url, + type, + amount: { + tokens: item.amount.toFixed(1), + converted: utils.convertBalance(item.amount.toString(), walletInfo.rates) + }, + date: new Date(item.expirationDate * 1000).toLocaleDateString(), + onRemove: () => { + this.actions.removePendingContribution(item.publisherKey, item.viewingId, item.addedDate) + } + } + }) + } + + removeAllPendingContribution = () => { + this.actions.removeAllPendingContribution() } render () { @@ -303,8 +360,7 @@ class PageWallet extends React.Component { const { walletRecoverySuccess, emptyWallet, modalBackup } = ui const addressArray = utils.getAddresses(addresses) - const pendingTotal = parseFloat( - (pendingContributionTotal || 0).toFixed(1)) + const pendingTotal = parseFloat((pendingContributionTotal || 0).toFixed(1)) return ( <> @@ -361,6 +417,15 @@ class PageWallet extends React.Component { /> : null } + { + this.state.modalPendingContribution + ? + : null + } { // TODO NZ add actual data for the whole section this.state.modalActivity diff --git a/components/brave_rewards/resources/ui/components/settingsPage.tsx b/components/brave_rewards/resources/ui/components/settingsPage.tsx index b8865010b8fb..cdb311de7b4a 100644 --- a/components/brave_rewards/resources/ui/components/settingsPage.tsx +++ b/components/brave_rewards/resources/ui/components/settingsPage.tsx @@ -36,7 +36,7 @@ class SettingsPage extends React.Component { this.actions.getCurrentReport() this.actions.getTipTable() this.actions.getContributeList() - this.actions.getPendingContributionsTotal() + this.actions.getPendingContributions() this.actions.getReconcileStamp() this.actions.getTransactionHistoryForThisCycle() this.actions.getExcludedPublishersNumber() diff --git a/components/brave_rewards/resources/ui/constants/rewards_types.ts b/components/brave_rewards/resources/ui/constants/rewards_types.ts index 845a0a2160ce..4ce523745394 100644 --- a/components/brave_rewards/resources/ui/constants/rewards_types.ts +++ b/components/brave_rewards/resources/ui/constants/rewards_types.ts @@ -49,8 +49,8 @@ export const enum types { ON_ADS_SETTING_SAVE = '@@rewards/ON_ADS_SETTING_SAVE', GET_CURRENT_REPORT = '@@rewards/GET_CURRENT_REPORT', GET_RECONCILE_STAMP = '@@rewards/GET_RECONCILE_STAMP', - GET_PENDING_CONTRIBUTION_TOTAL = '@@rewards/GET_PENDING_CONTRIBUTION_TOTAL', - ON_PENDING_CONTRIBUTION_TOTAL = '@@rewards/ON_PENDING_CONTRIBUTION_TOTAL', + GET_PENDING_CONTRIBUTIONS = '@@rewards/GET_PENDING_CONTRIBUTIONS', + ON_PENDING_CONTRIBUTIONS = '@@rewards/ON_PENDING_CONTRIBUTIONS', ON_REWARDS_ENABLED = '@@rewards/ON_REWARDS_ENABLED', GET_ADDRESSES_FOR_PAYMENT_ID = '@@rewards/GET_ADDRESSES_FOR_PAYMENT_ID', ON_ADDRESSES_FOR_PAYMENT_ID = '@@rewards/ON_ADDRESSES_FOR_PAYMENT_ID', @@ -61,5 +61,7 @@ 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_CONTRIBUTION_SAVED = '@@rewards/ON_CONTRIBUTION_SAVED', + REMOVE_PENDING_CONTRIBUTION = '@@rewards/REMOVE_PENDING_CONTRIBUTION', + REMOVE_ALL_PENDING_CONTRIBUTION = '@@rewards/REMOVE_ALL_PENDING_CONTRIBUTION' } diff --git a/components/brave_rewards/resources/ui/reducers/wallet_reducer.ts b/components/brave_rewards/resources/ui/reducers/wallet_reducer.ts index e1237d8f1838..f12a765fe32f 100644 --- a/components/brave_rewards/resources/ui/reducers/wallet_reducer.ts +++ b/components/brave_rewards/resources/ui/reducers/wallet_reducer.ts @@ -222,13 +222,26 @@ const walletReducer: Reducer = (state: Rewards.State, chrome.send('brave_rewards.getReconcileStamp') break } - case types.GET_PENDING_CONTRIBUTION_TOTAL: { - chrome.send('brave_rewards.getPendingContributionsTotal') + case types.GET_PENDING_CONTRIBUTIONS: { + chrome.send('brave_rewards.getPendingContributions') break } - case types.ON_PENDING_CONTRIBUTION_TOTAL: { + case types.ON_PENDING_CONTRIBUTIONS: { state = { ...state } - state.pendingContributionTotal = action.payload.amount + state.pendingContributions = action.payload.list + const total = state.pendingContributions + .reduce((accumulator: number, item: Rewards.PendingContribution) => { + return accumulator + item.amount + }, 0) + state.pendingContributionTotal = total + break + } + case types.REMOVE_PENDING_CONTRIBUTION: { + chrome.send('brave_rewards.removePendingContribution', [ + action.payload.publisherKey, + action.payload.viewingId, + action.payload.addedDate + ]) break } case types.GET_ADDRESSES_FOR_PAYMENT_ID: { @@ -249,6 +262,10 @@ const walletReducer: Reducer = (state: Rewards.State, } break } + case types.REMOVE_ALL_PENDING_CONTRIBUTION: { + chrome.send('brave_rewards.removeAllPendingContribution') + break + } } return state diff --git a/components/brave_rewards/resources/ui/storage.ts b/components/brave_rewards/resources/ui/storage.ts index b1675e9a9ec3..dbfc61ec4bf8 100644 --- a/components/brave_rewards/resources/ui/storage.ts +++ b/components/brave_rewards/resources/ui/storage.ts @@ -58,7 +58,8 @@ export const defaultState: Rewards.State = { }, pendingContributionTotal: 0, grants: [], - currentGrant: undefined + currentGrant: undefined, + pendingContributions: [] } const cleanData = (state: Rewards.State) => state diff --git a/components/definitions/rewards.d.ts b/components/definitions/rewards.d.ts index 3fd8dffd2748..e78a9ca62d3a 100644 --- a/components/definitions/rewards.d.ts +++ b/components/definitions/rewards.d.ts @@ -20,6 +20,12 @@ declare namespace Rewards { GRANT_NOT_FOUND = 13 } + export enum RewardsCategory { + AUTO_CONTRIBUTE = 2, + ONE_TIME_TIP = 8, + RECURRING_TIP = 16 + } + export type AddressesType = 'BTC' | 'ETH' | 'BAT' | 'LTC' export type Address = { address: string, qr: string | null } @@ -44,6 +50,7 @@ declare namespace Rewards { firstLoad: boolean | null grants?: Grant[] excludedPublishersNumber: number + pendingContributions: PendingContribution[] pendingContributionTotal: number reconcileStamp: number recoveryKey: string @@ -167,4 +174,19 @@ declare namespace Rewards { success: boolean category: Category } + + export interface PendingContribution { + publisherKey: string + percentage: number + verified: boolean + url: string + name: string + provider: string + favIcon: string + amount: number + addedDate: number + category: RewardsCategory + viewingId: string + expirationDate: number + } } diff --git a/components/resources/brave_components_strings.grd b/components/resources/brave_components_strings.grd index 104038ad9b07..30480806ddbf 100644 --- a/components/resources/brave_components_strings.grd +++ b/components/resources/brave_components_strings.grd @@ -499,6 +499,14 @@ This creator has not yet signed up to receive contributions from Brave users. Your browser will keep trying to contribute until they verify, or until 90 days have passed. Learn more. earned from ads + Pending contributions + Recurring tip + One-time tip + Auto-Contribute + Show all pending contributions + Pending until + No pending contributions… + Remove All 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 2d71117bc3ac..157e2a36817f 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc +++ b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc @@ -728,4 +728,69 @@ void BatLedgerClientMojoProxy::GetExcludedPublishersNumberDB( base::BindOnce(&OnExcludedNumberDB, std::move(callback))); } +void OnGetPendingContributions( + const ledger::PendingContributionInfoListCallback& callback, + const std::vector& info_list) { + ledger::PendingContributionInfoList list; + + for (const auto& info : info_list) { + ledger::PendingContributionInfo new_info; + new_info.loadFromJson(info); + list.push_back(new_info); + } + + callback(list); +} + +void BatLedgerClientMojoProxy::GetPendingContributions( + const ledger::PendingContributionInfoListCallback& callback) { + if (!Connected()) { + callback(std::vector()); + return; + } + + bat_ledger_client_->GetPendingContributions( + base::BindOnce(&OnGetPendingContributions, std::move(callback))); +} + +void OnRemovePendingContribution( + const ledger::RemovePendingContributionCallback& callback, + int32_t result) { + callback(ToLedgerResult(result)); +} + +void BatLedgerClientMojoProxy::RemovePendingContribution( + const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date, + const ledger::RemovePendingContributionCallback& callback) { + if (!Connected()) { + callback(ledger::Result::LEDGER_ERROR); + return; + } + + bat_ledger_client_->RemovePendingContribution( + publisher_key, + viewing_id, + added_date, + base::BindOnce(&OnRemovePendingContribution, std::move(callback))); +} + +void OnRemoveAllPendingContributions( + const ledger::RemovePendingContributionCallback& callback, + int32_t result) { + callback(ToLedgerResult(result)); +} + +void BatLedgerClientMojoProxy::RemoveAllPendingContributions( + const ledger::RemovePendingContributionCallback& callback) { + if (!Connected()) { + callback(ledger::Result::LEDGER_ERROR); + return; + } + + bat_ledger_client_->RemoveAllPendingContributions( + base::BindOnce(&OnRemoveAllPendingContributions, std::move(callback))); +} + } // namespace bat_ledger 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 9e47c7eebb50..891230d6ee54 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h +++ b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h @@ -140,6 +140,18 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient, void GetExcludedPublishersNumberDB( ledger::GetExcludedPublishersNumberDBCallback callback) override; + void GetPendingContributions( + const ledger::PendingContributionInfoListCallback& callback) override; + + void RemovePendingContribution( + const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date, + const ledger::RemovePendingContributionCallback& callback) override; + + void RemoveAllPendingContributions( + const ledger::RemovePendingContributionCallback& callback) override; + private: bool Connected() const; diff --git a/components/services/bat_ledger/bat_ledger_impl.cc b/components/services/bat_ledger/bat_ledger_impl.cc index eef82fd04a5e..7bbf12945a7b 100644 --- a/components/services/bat_ledger/bat_ledger_impl.cc +++ b/components/services/bat_ledger/bat_ledger_impl.cc @@ -534,4 +534,77 @@ void BatLedgerImpl::StartAutoContribute() { ledger_->StartAutoContribute(); } +// static +void BatLedgerImpl::OnGetPendingContributions( + CallbackHolder* holder, + const ledger::PendingContributionInfoList& list) { + + std::vector json_list; + for (auto const& item : list) { + json_list.push_back(item.ToJson()); + } + + if (holder->is_valid()) { + std::move(holder->get()).Run(json_list); + } + delete holder; +} + +void BatLedgerImpl::GetPendingContributions( + GetPendingContributionsCallback callback) { + auto* holder = new CallbackHolder( + AsWeakPtr(), std::move(callback)); + + ledger_->GetPendingContributions(std::bind( + BatLedgerImpl::OnGetPendingContributions, holder, _1)); +} + +// static +void BatLedgerImpl::OnRemovePendingContribution( + CallbackHolder* holder, + ledger::Result result) { + if (holder->is_valid()) { + std::move(holder->get()).Run(result); + } + delete holder; +} + +void BatLedgerImpl::RemovePendingContribution( + const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date, + RemovePendingContributionCallback callback) { + auto* holder = new CallbackHolder( + AsWeakPtr(), std::move(callback)); + + ledger_->RemovePendingContribution( + publisher_key, + viewing_id, + added_date, + std::bind(BatLedgerImpl::OnRemovePendingContribution, + holder, + _1)); +} + +// static +void BatLedgerImpl::OnRemoveAllPendingContributions( + CallbackHolder* holder, + ledger::Result result) { + if (holder->is_valid()) { + std::move(holder->get()).Run(result); + } + delete holder; +} + +void BatLedgerImpl::RemoveAllPendingContributions( + RemovePendingContributionCallback callback) { + auto* holder = new CallbackHolder( + AsWeakPtr(), std::move(callback)); + + ledger_->RemoveAllPendingContributions( + std::bind(BatLedgerImpl::OnRemoveAllPendingContributions, + holder, + _1)); +} + } // namespace bat_ledger diff --git a/components/services/bat_ledger/bat_ledger_impl.h b/components/services/bat_ledger/bat_ledger_impl.h index f29f3a1b091d..184a09d46941 100644 --- a/components/services/bat_ledger/bat_ledger_impl.h +++ b/components/services/bat_ledger/bat_ledger_impl.h @@ -153,6 +153,18 @@ class BatLedgerImpl : public mojom::BatLedger, const std::string& publisher_key, LoadPublisherInfoCallback callback) override; + void GetPendingContributions( + GetPendingContributionsCallback callback) override; + + void RemovePendingContribution( + const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date, + RemovePendingContributionCallback callback) override; + + void RemoveAllPendingContributions( + RemovePendingContributionCallback callback) override; + private: void SetCatalogIssuers(const std::string& info) override; void ConfirmAd(const std::string& info) override; @@ -218,6 +230,18 @@ class BatLedgerImpl : public mojom::BatLedger, ledger::Result result, ledger::PublisherInfoPtr info); + static void OnGetPendingContributions( + CallbackHolder* holder, + const ledger::PendingContributionInfoList& list); + + static void OnRemovePendingContribution( + CallbackHolder* holder, + ledger::Result result); + + static void OnRemoveAllPendingContributions( + CallbackHolder* holder, + ledger::Result result); + std::unique_ptr bat_ledger_client_mojo_proxy_; std::unique_ptr ledger_; 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 76796c264fd6..527abec9805d 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 @@ -688,4 +688,75 @@ void LedgerClientMojoProxy::GetOneTimeTips( _2)); } +// static +void LedgerClientMojoProxy::OnGetPendingContributions( + CallbackHolder* holder, + const ledger::PendingContributionInfoList& info_list) { + std::vector list; + for (const auto& info : info_list) { + list.push_back(info.ToJson()); + } + + if (holder->is_valid()) + std::move(holder->get()).Run(list); + delete holder; +} + +void LedgerClientMojoProxy::GetPendingContributions( + GetPendingContributionsCallback callback) { + // deleted in OnGetPendingContributions + auto* holder = new CallbackHolder( + AsWeakPtr(), std::move(callback)); + ledger_client_->GetPendingContributions( + std::bind(LedgerClientMojoProxy::OnGetPendingContributions, + holder, + _1)); +} + +// static +void LedgerClientMojoProxy::OnRemovePendingContribution( + CallbackHolder* holder, + ledger::Result result) { + if (holder->is_valid()) + std::move(holder->get()).Run(ToMojomResult(result)); + delete holder; +} + +void LedgerClientMojoProxy::RemovePendingContribution( + const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date, + RemovePendingContributionCallback callback) { + // deleted in OnRemovePendingContribution + auto* holder = new CallbackHolder( + AsWeakPtr(), std::move(callback)); + ledger_client_->RemovePendingContribution( + publisher_key, + viewing_id, + added_date, + std::bind(LedgerClientMojoProxy::OnRemovePendingContribution, + holder, + _1)); +} + +// static +void LedgerClientMojoProxy::OnRemoveAllPendingContributions( + CallbackHolder* holder, + ledger::Result result) { + if (holder->is_valid()) + std::move(holder->get()).Run(ToMojomResult(result)); + delete holder; +} + +void LedgerClientMojoProxy::RemoveAllPendingContributions( + RemovePendingContributionCallback callback) { + // deleted in OnRemoveAllPendingContributions + auto* holder = new CallbackHolder( + AsWeakPtr(), std::move(callback)); + ledger_client_->RemoveAllPendingContributions( + std::bind(LedgerClientMojoProxy::OnRemoveAllPendingContributions, + holder, + _1)); +} + } // namespace bat_ledger 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 fe3775742aba..c5ba56d64c76 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 @@ -127,6 +127,18 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, void GetExcludedPublishersNumberDB( GetExcludedPublishersNumberDBCallback callback) override; + void GetPendingContributions( + GetPendingContributionsCallback callback) override; + + void RemovePendingContribution( + const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date, + RemovePendingContributionCallback callback) override; + + void RemoveAllPendingContributions( + RemovePendingContributionCallback callback) override; + private: // workaround to pass base::OnceCallback into std::bind // also serves as a wrapper for passing ledger::LedgerCallbackHandler* @@ -240,6 +252,18 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, ledger::PublisherInfoList publisher_info_list, uint32_t next_record); + static void OnGetPendingContributions( + CallbackHolder* holder, + const ledger::PendingContributionInfoList& info_list); + + static void OnRemovePendingContribution( + CallbackHolder* holder, + ledger::Result result); + + static void OnRemoveAllPendingContributions( + CallbackHolder* holder, + ledger::Result result); + ledger::LedgerClient* ledger_client_; DISALLOW_COPY_AND_ASSIGN(LedgerClientMojoProxy); diff --git a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom index 000195a383b9..a1e36a3b1ec2 100644 --- a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom +++ b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom @@ -118,6 +118,13 @@ interface BatLedger { RefreshPublisher(string publisher_key) => (bool verified); StartAutoContribute(); + + GetPendingContributions() => (array list); + + RemovePendingContribution(string publisher_key, string viewing_id, + uint64 added_date) => (int32 result); + + RemoveAllPendingContributions() => (int32 result); }; interface BatLedgerClient { @@ -202,4 +209,11 @@ interface BatLedgerClient { ConfirmationsTransactionHistoryDidChange(); GetExcludedPublishersNumberDB() => (uint32 number); + + GetPendingContributions() => (array info_list); + + RemovePendingContribution(string publisher_key, string viewing_id, + uint64 added_date) => (int32 result); + + RemoveAllPendingContributions() => (int32 result); }; diff --git a/package-lock.json b/package-lock.json index 494f9172f053..1dbeef4f622e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1597,8 +1597,8 @@ } }, "brave-ui": { - "version": "github:brave/brave-ui#079dbadba9c270b975eac1962355914a43935b34", - "from": "github:brave/brave-ui#079dbadba9c270b975eac1962355914a43935b34", + "version": "github:brave/brave-ui#bd1a71cb81129519adaed1fbf5d2359774793074", + "from": "github:brave/brave-ui#bd1a71cb81129519adaed1fbf5d2359774793074", "dev": true, "requires": { "@ctrl/tinycolor": "^2.2.1", diff --git a/package.json b/package.json index 51788600b6a4..80c9cc581989 100644 --- a/package.json +++ b/package.json @@ -277,7 +277,7 @@ "@types/react-redux": "6.0.4", "@types/redux-logger": "^3.0.7", "awesome-typescript-loader": "^5.2.1", - "brave-ui": "github:brave/brave-ui#079dbadba9c270b975eac1962355914a43935b34", + "brave-ui": "github:brave/brave-ui#bd1a71cb81129519adaed1fbf5d2359774793074", "css-loader": "^2.1.1", "csstype": "^2.5.5", "deep-freeze-node": "^1.1.3", 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 d7c8c06e596c..bb96dd54dbb1 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 @@ -247,6 +247,18 @@ class MockConfirmationsClient : public ConfirmationsClient { MOCK_METHOD1(GetExcludedPublishersNumberDB, void( ledger::GetExcludedPublishersNumberDBCallback callback)); + + MOCK_METHOD1(GetPendingContributions, void( + const ledger::PendingContributionInfoListCallback& callback)); + + MOCK_METHOD4(RemovePendingContribution, void( + const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date, + const ledger::RemovePendingContributionCallback& callback)); + + MOCK_METHOD1(RemoveAllPendingContributions, void( + const ledger::RemovePendingContributionCallback& callback)); }; } // namespace confirmations diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger.h b/vendor/bat-native-ledger/include/bat/ledger/ledger.h index 799f15afac62..e4e6a51a3ea6 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger.h @@ -18,6 +18,7 @@ #include "bat/ledger/media_publisher_info.h" #include "bat/ledger/transactions_info.h" #include "bat/ledger/rewards_internals_info.h" +#include "bat/ledger/pending_contribution.h" namespace ledger { @@ -287,6 +288,18 @@ class LEDGER_EXPORT Ledger { ledger::OnRefreshPublisherCallback callback) = 0; virtual void StartAutoContribute() = 0; + + virtual void GetPendingContributions( + ledger::PendingContributionInfoListCallback callback) = 0; + + virtual void RemovePendingContribution( + const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date, + const ledger::RemovePendingContributionCallback& callback) = 0; + + virtual void RemoveAllPendingContributions( + const ledger::RemovePendingContributionCallback& callback) = 0; }; } // namespace ledger 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 356c409855e0..410da7d028ba 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h @@ -68,6 +68,9 @@ using OnLoadCallback = std::function; using OnResetCallback = std::function; using GetExcludedPublishersNumberDBCallback = std::function; +using PendingContributionInfoListCallback = + std::function; +using RemovePendingContributionCallback = std::function; class LEDGER_EXPORT LedgerClient { public: @@ -222,6 +225,18 @@ class LEDGER_EXPORT LedgerClient { virtual void GetExcludedPublishersNumberDB( ledger::GetExcludedPublishersNumberDBCallback callback) = 0; + + virtual void GetPendingContributions( + const ledger::PendingContributionInfoListCallback& callback) = 0; + + virtual void RemovePendingContribution( + const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date, + const ledger::RemovePendingContributionCallback& callback) = 0; + + virtual void RemoveAllPendingContributions( + const ledger::RemovePendingContributionCallback& callback) = 0; }; } // namespace ledger diff --git a/vendor/bat-native-ledger/include/bat/ledger/pending_contribution.h b/vendor/bat-native-ledger/include/bat/ledger/pending_contribution.h index 8d857a45e76e..749b2049042f 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/pending_contribution.h +++ b/vendor/bat-native-ledger/include/bat/ledger/pending_contribution.h @@ -10,6 +10,7 @@ #include #include "bat/ledger/export.h" +#include "bat/ledger/publisher_info.h" namespace ledger { @@ -39,6 +40,29 @@ LEDGER_EXPORT struct PendingContributionList { std::vector list_; }; +LEDGER_EXPORT struct PendingContributionInfo { + PendingContributionInfo(); + PendingContributionInfo(const PendingContributionInfo& info); + ~PendingContributionInfo(); + + const std::string ToJson() const; + bool loadFromJson(const std::string& json); + + std::string publisher_key; + REWARDS_CATEGORY category; + bool verified; + std::string name; + std::string url; + std::string provider; + std::string favicon_url; + double amount = 0; + uint64_t added_date = 0; + std::string viewing_id; + uint64_t expiration_date = 0; +}; + +using PendingContributionInfoList = std::vector; + } // namespace ledger #endif // BAT_LEDGER_PENDING_CONTRIBUTION_HANDLER_ diff --git a/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h b/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h index 5dda34682fd6..964d0b9b7b21 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h +++ b/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h @@ -25,7 +25,7 @@ const char _clear_favicon[] = "clear"; LEDGER_EXPORT enum REWARDS_CATEGORY { AUTO_CONTRIBUTE = 1 << 1, // 2 ONE_TIME_TIP = 1 << 3, // 8 - RECURRING_TIP = 1 << 4, // 21 + RECURRING_TIP = 1 << 4, // 16 ALL_CATEGORIES = (1 << 5) - 1, }; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper.cc index 67243122211a..40f555e79115 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper.cc @@ -2957,4 +2957,44 @@ void saveToJson(JsonWriter* writer, const ledger::RewardsInternalsInfo& info) { writer->EndObject(); } +void saveToJson(JsonWriter* writer, + const ledger::PendingContributionInfo& info) { + writer->StartObject(); + + writer->String("publisher_key"); + writer->String(info.publisher_key.c_str()); + + writer->String("category"); + writer->Int(info.category); + + writer->String("verified"); + writer->Bool(info.verified); + + writer->String("name"); + writer->String(info.name.c_str()); + + writer->String("url"); + writer->String(info.url.c_str()); + + writer->String("provider"); + writer->String(info.provider.c_str()); + + writer->String("favicon_url"); + writer->String(info.favicon_url.c_str()); + + writer->String("amount"); + writer->Double(info.amount); + + writer->String("added_date"); + writer->Uint64(info.added_date); + + writer->String("viewing_id"); + writer->String(info.viewing_id.c_str()); + + writer->String("expiration_date"); + writer->Uint64(info.expiration_date); + + writer->EndObject(); +} + } // namespace braveledger_bat_helper 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 82e9d4743780..96edc530b131 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 @@ -1504,4 +1504,45 @@ scoped_refptr LedgerImpl::GetTaskRunner() { return task_runner_; } +void LedgerImpl::OnGetPendingContributions( + const ledger::PendingContributionInfoList& list, + ledger::PendingContributionInfoListCallback callback) { + std::vector new_list; + for (const auto& item : list) { + ledger::PendingContributionInfo new_item(item); + new_item.expiration_date = + new_item.added_date + + braveledger_ledger::_pending_contribution_expiration; + + new_list.push_back(new_item); + } + + callback(new_list); +} + +void LedgerImpl::GetPendingContributions( + ledger::PendingContributionInfoListCallback callback) { + ledger_client_->GetPendingContributions( + std::bind(&LedgerImpl::OnGetPendingContributions, + this, + _1, + callback)); +} + +void LedgerImpl::RemovePendingContribution( + const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date, + const ledger::RemovePendingContributionCallback& callback) { + ledger_client_->RemovePendingContribution(publisher_key, + viewing_id, + added_date, + callback); +} + +void LedgerImpl::RemoveAllPendingContributions( + const ledger::RemovePendingContributionCallback& callback) { + ledger_client_->RemoveAllPendingContributions(callback); +} + } // namespace bat_ledger 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 351ce3a79274..86c87ed6be2a 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 @@ -440,6 +440,18 @@ class LedgerImpl : public ledger::Ledger, const std::string& publisher_key, ledger::OnRefreshPublisherCallback callback) override; + void GetPendingContributions( + ledger::PendingContributionInfoListCallback callback) override; + + void RemovePendingContribution( + const std::string& publisher_key, + const std::string& viewing_id, + uint64_t added_date, + const ledger::RemovePendingContributionCallback& callback) override; + + void RemoveAllPendingContributions( + const ledger::RemovePendingContributionCallback& callback) override; + private: void AddRecurringPayment(const std::string& publisher_id, const double& value) override; @@ -493,6 +505,10 @@ class LedgerImpl : public ledger::Ledger, uint32_t record, ledger::PublisherInfoListCallback callback); + void OnGetPendingContributions( + const ledger::PendingContributionInfoList& list, + ledger::PendingContributionInfoListCallback callback); + // ledger::LedgerCallbacHandler implementation void OnPublisherStateLoaded(ledger::Result result, const std::string& data) override; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/rapidjson_bat_helper.h b/vendor/bat-native-ledger/src/bat/ledger/internal/rapidjson_bat_helper.h index a70163f0d552..301bb98c9e96 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/rapidjson_bat_helper.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/rapidjson_bat_helper.h @@ -26,6 +26,7 @@ struct VisitData; struct WalletInfo; struct PendingContribution; struct PendingContributionList; +struct PendingContributionInfo; } // namespace ledger @@ -75,6 +76,7 @@ void saveToJson(JsonWriter* writer, const ledger::PendingContribution&); void saveToJson(JsonWriter* writer, const ledger::PendingContributionList&); void saveToJson(JsonWriter* writer, const WALLET_PROPERTIES_ST&); void saveToJson(JsonWriter* writer, const GRANT&); +void saveToJson(JsonWriter* writer, const ledger::PendingContributionInfo&); template void saveToJsonString(const T& t, std::string* json) { diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/static_values.h b/vendor/bat-native-ledger/src/bat/ledger/internal/static_values.h index 9935fd7ca9f8..f5f6628ea7ff 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/static_values.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/static_values.h @@ -114,6 +114,9 @@ static const uint64_t _reconcile_default_interval = 30 * 24 * 60 * 60; // 1 day in seconds static const uint64_t _grant_load_interval = 24 * 60 * 60; +// pending contribution expiration in seconds (90 days) +static const uint64_t _pending_contribution_expiration = 90 * 24 * 60 * 60; + } // namespace braveledger_ledger #endif // BRAVELEDGER_STATIC_VALUES_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc index 79d78c59a5f4..ea04459b6ec3 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc @@ -660,4 +660,59 @@ bool PendingContributionList::loadFromJson(const std::string& json) { return !error; } +PendingContributionInfo::PendingContributionInfo() {} +PendingContributionInfo::~PendingContributionInfo() {} +PendingContributionInfo::PendingContributionInfo( + const ledger::PendingContributionInfo &properties) { + publisher_key = properties.publisher_key; + category = properties.category; + verified = properties.verified; + name = properties.name; + url = properties.url; + provider = properties.provider; + favicon_url = properties.favicon_url; + amount = properties.amount; + added_date = properties.added_date; + viewing_id = properties.viewing_id; + expiration_date = properties.expiration_date; +} + +const std::string PendingContributionInfo::ToJson() const { + std::string json; + braveledger_bat_helper::saveToJsonString(*this, &json); + return json; +} + +bool PendingContributionInfo::loadFromJson(const std::string& json) { + rapidjson::Document d; + d.Parse(json.c_str()); + + // has parser errors or wrong types + bool error = d.HasParseError(); + + if (!error) { + error = !(d.HasMember("publisher_key") && d["publisher_key"].IsString() && + d.HasMember("amount") && d["amount"].IsDouble() && + d.HasMember("added_date") && d["added_date"].IsUint64() && + d.HasMember("viewing_id") && d["viewing_id"].IsString() && + d.HasMember("category") && d["category"].IsInt()); + } + + if (!error) { + publisher_key = d["publisher_key"].GetString(); + category = static_cast(d["category"].GetInt()); + verified = d["verified"].GetBool(); + name = d["name"].GetString(); + url = d["url"].GetString(); + provider = d["provider"].GetString(); + favicon_url = d["favicon_url"].GetString(); + amount = d["amount"].GetDouble(); + added_date = d["added_date"].GetUint64(); + viewing_id = d["viewing_id"].GetString(); + expiration_date = d["expiration_date"].GetUint64(); + } + + return !error; +} + } // namespace ledger