Skip to content

Commit

Permalink
Fixes exclude being out of sync in ac table
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Mar 5, 2019
1 parent 4c246d2 commit 2a3bff2
Show file tree
Hide file tree
Showing 33 changed files with 253 additions and 268 deletions.
35 changes: 21 additions & 14 deletions browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnGetContributionAmount(double amount);
void OnGetAddresses(const std::string func_name,
const std::map<std::string, std::string>& addresses);
void OnGetNumExcludedSites(const std::string& publisher_id, uint32_t num);
void OnGetExcludedPublishersNumber(uint32_t num);
void OnGetAutoContributeProps(
int error_code,
std::unique_ptr<brave_rewards::WalletProperties> wallet_properties,
Expand All @@ -107,6 +107,8 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void GetAddressesForPaymentId(const base::ListValue* args);
void GetConfirmationsHistory(const base::ListValue* args);

void GetExcludedPublishersNumber(const base::ListValue* args);

void OnConfirmationsHistory(int total_viewed, double estimated_earnings);

// RewardsServiceObserver implementation
Expand Down Expand Up @@ -273,6 +275,9 @@ void RewardsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("brave_rewards.getConfirmationsHistory",
base::BindRepeating(&RewardsDOMHandler::GetConfirmationsHistory,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.getExcludedPublishersNumber",
base::BindRepeating(&RewardsDOMHandler::GetExcludedPublishersNumber,
base::Unretained(this)));
}

void RewardsDOMHandler::Init() {
Expand Down Expand Up @@ -608,27 +613,20 @@ void RewardsDOMHandler::OnContentSiteUpdated(
weak_factory_.GetWeakPtr()));
}

void RewardsDOMHandler::OnGetNumExcludedSites(const std::string& publisher_id,
uint32_t num) {
void RewardsDOMHandler::OnGetExcludedPublishersNumber(uint32_t num) {
if (web_ui()->CanCallJavascript()) {
base::DictionaryValue excludedSitesInfo;
excludedSitesInfo.SetString("num", std::to_string(num));
excludedSitesInfo.SetString("publisherKey", publisher_id);

web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.numExcludedSites",
excludedSitesInfo);
web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.excludedNumber",
base::Value(std::to_string(num)));
}
}

void RewardsDOMHandler::OnExcludedSitesChanged(
brave_rewards::RewardsService* rewards_service,
std::string publisher_id,
bool excluded) {
if (rewards_service_)
rewards_service_->GetNumExcludedSites(base::Bind(
&RewardsDOMHandler::OnGetNumExcludedSites,
weak_factory_.GetWeakPtr(),
publisher_id));
rewards_service_->GetExcludedPublishersNumber(
base::Bind(&RewardsDOMHandler::OnGetExcludedPublishersNumber,
weak_factory_.GetWeakPtr()));
}

void RewardsDOMHandler::OnNotificationAdded(
Expand Down Expand Up @@ -993,6 +991,15 @@ void RewardsDOMHandler::OnConfirmationsHistoryChanged(
}
}

void RewardsDOMHandler::GetExcludedPublishersNumber(
const base::ListValue* args) {
if (rewards_service_) {
rewards_service_->GetExcludedPublishersNumber(
base::Bind(&RewardsDOMHandler::OnGetExcludedPublishersNumber,
weak_factory_.GetWeakPtr()));
}
}

} // namespace

BraveRewardsUI::BraveRewardsUI(content::WebUI* web_ui, const std::string& name)
Expand Down
23 changes: 23 additions & 0 deletions components/brave_rewards/browser/publisher_info_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,29 @@ bool PublisherInfoDatabase::RestorePublishers() {
return restore_q.Run();
}

int PublisherInfoDatabase::GetExcludedPublishersCount() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

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

if (!initialized) {
return 0;
}

sql::Statement query(db_.GetUniqueStatement(
"SELECT COUNT(*) FROM publisher_info WHERE excluded=?"));

query.BindInt(0, static_cast<int>(
ledger::PUBLISHER_EXCLUDE::EXCLUDED));

if (query.Step()) {
return query.ColumnInt(0);
}

return 0;
}

/**
*
* ACTIVITY INFO
Expand Down
2 changes: 2 additions & 0 deletions components/brave_rewards/browser/publisher_info_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class PublisherInfoDatabase {

bool RestorePublishers();

int GetExcludedPublishersCount();

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

bool InsertOrUpdateActivityInfos(const ledger::PublisherInfoList& list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -825,4 +825,43 @@ TEST_F(PublisherInfoDatabaseTest, Migrationv4tov6) {
EXPECT_EQ(publisher_info_database_->GetTableVersionNumber(), 6);
}

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

// empty table
EXPECT_EQ(publisher_info_database_->GetExcludedPublishersCount(), 0);

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

EXPECT_TRUE(publisher_info_database_->InsertOrUpdatePublisherInfo(info));

info.id = "publisher_2";
EXPECT_TRUE(publisher_info_database_->InsertOrUpdatePublisherInfo(info));

info.id = "publisher_3";
info.excluded = ledger::PUBLISHER_EXCLUDE::INCLUDED;
EXPECT_TRUE(publisher_info_database_->InsertOrUpdatePublisherInfo(info));

info.id = "publisher_4";
info.excluded = ledger::PUBLISHER_EXCLUDE::EXCLUDED;
EXPECT_TRUE(publisher_info_database_->InsertOrUpdatePublisherInfo(info));

info.id = "publisher_5";
info.excluded = ledger::PUBLISHER_EXCLUDE::EXCLUDED;
EXPECT_TRUE(publisher_info_database_->InsertOrUpdatePublisherInfo(info));
EXPECT_EQ(CountTableRows("publisher_info"), 5);

EXPECT_EQ(publisher_info_database_->GetExcludedPublishersCount(), 2);
}

} // namespace brave_rewards
6 changes: 3 additions & 3 deletions components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ using GetWalletPassphraseCallback = base::Callback<void(const std::string&)>;
using GetContributionAmountCallback = base::Callback<void(double)>;
using GetAddressesCallback = base::Callback<void(
const std::map<std::string, std::string>&)>;
using GetNumExcludedSitesCallback = base::Callback<void(uint32_t)>;
using GetExcludedPublishersNumberCallback = base::Callback<void(uint32_t)>;
using GetAutoContributePropsCallback = base::Callback<void(
std::unique_ptr<brave_rewards::AutoContributeProps>)>;
using GetPublisherMinVisitTimeCallback = base::Callback<void(uint64_t)>;
Expand Down Expand Up @@ -91,8 +91,8 @@ class RewardsService : public KeyedService {
const std::string& promotionId) const = 0;
virtual void GetWalletPassphrase(
const GetWalletPassphraseCallback& callback) = 0;
virtual void GetNumExcludedSites(
const GetNumExcludedSitesCallback& callback) = 0;
virtual void GetExcludedPublishersNumber(
const GetExcludedPublishersNumberCallback& callback) = 0;
virtual void RecoverWallet(const std::string passPhrase) const = 0;
virtual void ExcludePublisher(const std::string publisherKey) const = 0;
virtual void RestorePublishers() = 0;
Expand Down
37 changes: 34 additions & 3 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1314,13 +1314,13 @@ void RewardsServiceImpl::GetWalletPassphrase(
bat_ledger_->GetWalletPassphrase(callback);
}

void RewardsServiceImpl::GetNumExcludedSites(
const GetNumExcludedSitesCallback& callback) {
void RewardsServiceImpl::GetExcludedPublishersNumber(
const GetExcludedPublishersNumberCallback& callback) {
if (!Connected()) {
return;
}

bat_ledger_->GetNumExcludedSites(callback);
bat_ledger_->GetExcludedPublishersNumber(callback);
}

void RewardsServiceImpl::RecoverWallet(const std::string passPhrase) const {
Expand Down Expand Up @@ -2667,4 +2667,35 @@ void RewardsServiceImpl::GetAddressesForPaymentId(
callback));
}

int GetExcludedPublishersNumberOnFileTaskRunner(PublisherInfoDatabase* backend) {
if (!backend) {
return 0;
}

return backend->GetExcludedPublishersCount();
}

void RewardsServiceImpl::OnGetExcludedPublishersNumberDB(
ledger::GetExcludedPublishersNumberDBCallback callback,
int number) {
if (!Connected()) {
callback(0);
return;
}

callback(number);
}

void RewardsServiceImpl::GetExcludedPublishersNumberDB(
ledger::GetExcludedPublishersNumberDBCallback callback) {
base::PostTaskAndReplyWithResult(
file_task_runner_.get(),
FROM_HERE,
base::BindOnce(&GetExcludedPublishersNumberOnFileTaskRunner,
publisher_info_backend_.get()),
base::BindOnce(&RewardsServiceImpl::OnGetExcludedPublishersNumberDB,
AsWeakPtr(),
callback));
}

} // namespace brave_rewards
10 changes: 8 additions & 2 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ class RewardsServiceImpl : public RewardsService,
const std::string& promotionId) const override;
void GetWalletPassphrase(
const GetWalletPassphraseCallback& callback) override;
void GetNumExcludedSites(
const GetNumExcludedSitesCallback& callback) override;
void GetExcludedPublishersNumber(
const GetExcludedPublishersNumberCallback& callback) override;
void RecoverWallet(const std::string passPhrase) const override;
void GetContentSiteList(
uint32_t start,
Expand Down Expand Up @@ -392,6 +392,12 @@ class RewardsServiceImpl : public RewardsService,
void SaveNormalizedPublisherList(
const ledger::PublisherInfoListStruct& list) override;

void GetExcludedPublishersNumberDB(
ledger::GetExcludedPublishersNumberDBCallback callback) override;

void OnGetExcludedPublishersNumberDB(ledger::GetExcludedPublishersNumberDBCallback callback,
int number);

// URLFetcherDelegate impl
void OnURLFetchComplete(const net::URLFetcher* source) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,6 @@ export const rewardsPanelReducer = (state: RewardsExtension.State | undefined, a
}

const publisherKey: string = payload.properties.publisher_key

if (!publisherKey) {
break
}

const excluded: boolean = payload.properties.excluded

let publishers: Record<string, RewardsExtension.Publisher> = state.publishers
Expand All @@ -286,6 +281,8 @@ export const rewardsPanelReducer = (state: RewardsExtension.State | undefined, a

if (publisher.publisher_key === publisherKey) {
publisher.excluded = !!excluded
} else if (publisherKey === '-1') {
publisher.excluded = false
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ export const onWalletExists = (exists: boolean) => action(types.ON_WALLET_EXISTS

export const restorePublishers = () => action(types.ON_RESTORE_PUBLISHERS)

export const onNumExcludedSites = (excludedSitesInfo: {num: string, publisherKey: string}) => action(types.ON_NUM_EXCLUDED_SITES, {
excludedSitesInfo
export const onExcludedNumber = (num: number) => action(types.ON_EXCLUDED_PUBLISHERS_NUMBER, {
num
})

export const onContributionAmount = (amount: number) => action(types.ON_CONTRIBUTION_AMOUNT, {
Expand Down Expand Up @@ -183,3 +183,5 @@ export const onConfirmationsHistory = (data: {adsTotalPages: number, adsEstimate
export const getConfirmationsHistory = () => action(types.GET_CONFIRMATIONS_HISTORY)

export const onConfirmationsHistoryChanged = () => action(types.ON_CONFIRMATIONS_HISTORY_CHANGED)

export const getExcludedPublishersNumber = () => action(types.GET_EXCLUDED_PUBLISHERS_NUMBER)
6 changes: 3 additions & 3 deletions components/brave_rewards/resources/ui/brave_rewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ window.cr.define('brave_rewards', function () {
getActions().onContributeList(list)
}

function numExcludedSites (excludedSitesInfo: {num: string, publisherKey: string}) {
getActions().onNumExcludedSites(excludedSitesInfo)
function excludedNumber (num: number) {
getActions().onExcludedNumber(num)
}

function balanceReports (reports: Record<string, Rewards.Report>) {
Expand Down Expand Up @@ -168,7 +168,7 @@ window.cr.define('brave_rewards', function () {
reconcileStamp,
addresses,
contributeList,
numExcludedSites,
excludedNumber,
balanceReports,
walletExists,
contributionAmount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class ContributeBox extends React.Component<Props, State> {
contributionMonthly,
enabledContribute,
reconcileStamp,
numExcludedSites,
excludedPublishersNumber,
autoContributeList
} = this.props.rewardsData
const monthlyList: MonthlyChoice[] = utils.generateContributionMonthly(walletInfo.choices, walletInfo.rates)
Expand Down Expand Up @@ -218,7 +218,7 @@ class ContributeBox extends React.Component<Props, State> {
? <ModalContribute
rows={contributeRows}
onRestore={this.onRestore}
numExcludedSites={numExcludedSites}
numExcludedSites={excludedPublishersNumber}
onClose={this.onModalContributeToggle}
/>
: null
Expand Down Expand Up @@ -258,7 +258,7 @@ class ContributeBox extends React.Component<Props, State> {
rows={topRows}
allSites={allSites}
numSites={numRows}
numExcludedSites={numExcludedSites}
numExcludedSites={excludedPublishersNumber}
onRestore={this.onRestore}
onShowAll={this.onModalContributeToggle}
headerColor={true}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class SettingsPage extends React.Component<Props, {}> {
this.actions.getPendingContributionsTotal()
this.actions.getReconcileStamp()
this.actions.getConfirmationsHistory()
this.actions.getExcludedPublishersNumber()
}

componentDidMount () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const enum types {
ON_RESTORE_PUBLISHERS = '@@rewards/ON_RESTORE_PUBLISHERS',
CHECK_WALLET_EXISTENCE = '@@rewards/CHECK_WALLET_EXISTENCE',
ON_WALLET_EXISTS = '@@rewards/ON_WALLET_EXISTS',
ON_NUM_EXCLUDED_SITES = '@@rewards/ON_NUM_EXCLUDED_SITES',
ON_EXCLUDED_PUBLISHERS_NUMBER = '@@rewards/ON_EXCLUDED_PUBLISHERS_NUMBER',
ON_CONTRIBUTION_AMOUNT = '@@rewards/ON_CONTRIBUTION_AMOUNT',
ON_RECURRING_DONATION_UPDATE = '@@rewards/ON_RECURRING_DONATION_UPDATE',
ON_REMOVE_RECURRING = '@@rewards/ON_REMOVE_RECURRING',
Expand All @@ -56,5 +56,6 @@ export const enum types {
ON_ADDRESSES_FOR_PAYMENT_ID = '@@rewards/ON_ADDRESSES_FOR_PAYMENT_ID',
GET_CONFIRMATIONS_HISTORY = '@@rewards/GET_CONFIRMATIONS_HISTORY',
ON_CONFIRMATIONS_HISTORY = '@@rewards/ON_CONFIRMATIONS_HISTORY',
ON_CONFIRMATIONS_HISTORY_CHANGED = '@@rewards/ON_CONFIRMATIONS_HISTORY_CHANGED'
ON_CONFIRMATIONS_HISTORY_CHANGED = '@@rewards/ON_CONFIRMATIONS_HISTORY_CHANGED',
GET_EXCLUDED_PUBLISHERS_NUMBER = '@@rewards/GET_EXCLUDED_PUBLISHERS_NUMBER'
}
Loading

0 comments on commit 2a3bff2

Please sign in to comment.