From fde913ea7ae220ab4c7d125c52adbf9053f79679 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Tue, 20 Nov 2018 20:03:49 +0100 Subject: [PATCH] Refactors restore flow --- include/bat/ledger/ledger_client.h | 2 ++ src/bat_publishers.cc | 33 ++++++++++++------------------ src/bat_publishers.h | 4 ++-- src/ledger_impl.cc | 6 +++++- src/ledger_impl.h | 1 + 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/include/bat/ledger/ledger_client.h b/include/bat/ledger/ledger_client.h index 72bbcfe..8981eb8 100644 --- a/include/bat/ledger/ledger_client.h +++ b/include/bat/ledger/ledger_client.h @@ -55,6 +55,7 @@ using GetNicewareListCallback = using RecurringDonationCallback = std::function; using RecurringRemoveCallback = std::function; using FetchIconCallback = std::function; +using OnRestoreCallback = std::function; class LEDGER_EXPORT LedgerClient { public: @@ -153,6 +154,7 @@ class LEDGER_EXPORT LedgerClient { const char* file, int line, const ledger::LogLevel log_level) const = 0; + virtual void OnRestorePublishers(ledger::OnRestoreCallback callback) = 0; }; } // namespace ledger diff --git a/src/bat_publishers.cc b/src/bat_publishers.cc index 123379c..19a7beb 100644 --- a/src/bat_publishers.cc +++ b/src/bat_publishers.cc @@ -372,28 +372,21 @@ void BatPublishers::onSetPanelExcludeInternal(ledger::PUBLISHER_EXCLUDE exclude, OnExcludedSitesChanged(publisherKey); } -// TODO refactor -void BatPublishers::restorePublishers() { - uint64_t currentReconcileStamp = ledger_->GetReconcileStamp(); - auto filter = CreateActivityFilter("", - ledger::ACTIVITY_MONTH::ANY, - -1, - ledger::EXCLUDE_FILTER::FILTER_EXCLUDED, - false, - currentReconcileStamp); - ledger_->GetActivityInfoList(0, 0, filter, std::bind(&BatPublishers::onRestorePublishersInternal, - this, _1, _2)); +void BatPublishers::RestorePublishers() { + ledger_->OnRestorePublishers( + std::bind(&BatPublishers::OnRestorePublishersInternal, + this, + _1)); } -void BatPublishers::onRestorePublishersInternal(const ledger::PublisherInfoList& publisherInfoList, uint32_t /* next_record */) { - if (publisherInfoList.size() == 0) { - return; - } - - for (size_t i = 0; i < publisherInfoList.size(); i++) { - // Set to PUBLISHER_EXCLUDE::DEFAULT (0) - setExclude(publisherInfoList[i].id, - ledger::PUBLISHER_EXCLUDE::DEFAULT); +void BatPublishers::OnRestorePublishersInternal(bool success) { + if (success) { + setNumExcludedSites(0); + OnExcludedSitesChanged(); + } else { + ledger_->Log(__func__, + ledger::LogLevel::LOG_ERROR, + {"Could not restore publishers."}); } } diff --git a/src/bat_publishers.h b/src/bat_publishers.h index df09641..1ff443c 100644 --- a/src/bat_publishers.h +++ b/src/bat_publishers.h @@ -56,7 +56,7 @@ class BatPublishers : public ledger::LedgerCallbackHandler { void setPanelExclude(const std::string& publisher_id, const ledger::PUBLISHER_EXCLUDE& exclude, uint64_t windowId); - void restorePublishers(); + void RestorePublishers(); void setPublisherAllowNonVerified(const bool& allow); void setPublisherAllowVideos(const bool& allow); @@ -168,7 +168,7 @@ class BatPublishers : public ledger::LedgerCallbackHandler { ledger::Result result, std::unique_ptr publisher_info); - void onRestorePublishersInternal(const ledger::PublisherInfoList& publisherInfoList, uint32_t /* next_record */); + void OnRestorePublishersInternal(bool success); double concaveScore(const uint64_t& duration); diff --git a/src/ledger_impl.cc b/src/ledger_impl.cc index 107fd71..1022a68 100644 --- a/src/ledger_impl.cc +++ b/src/ledger_impl.cc @@ -350,7 +350,11 @@ void LedgerImpl::SetPublisherPanelExclude(const std::string& publisher_id, } void LedgerImpl::RestorePublishers() { - bat_publishers_->restorePublishers(); + bat_publishers_->RestorePublishers(); +} + +void LedgerImpl::OnRestorePublishers(ledger::OnRestoreCallback callback) { + ledger_client_->OnRestorePublishers(callback); } void LedgerImpl::LoadNicewareList(ledger::GetNicewareListCallback callback) { diff --git a/src/ledger_impl.h b/src/ledger_impl.h index 5353424..cf8a29f 100644 --- a/src/ledger_impl.h +++ b/src/ledger_impl.h @@ -164,6 +164,7 @@ class LedgerImpl : public ledger::Ledger, const ledger::PUBLISHER_EXCLUDE& exclude, uint64_t windowId) override; void RestorePublishers() override; + void OnRestorePublishers(ledger::OnRestoreCallback callback); bool IsWalletCreated() const override; void GetPublisherActivityFromUrl(uint64_t windowId, const ledger::VisitData& visit_data) override; void GetMediaActivityFromUrl(uint64_t windowId,