From 9c465b65628bb4342bf6cfdf456d5f88d99bf27f Mon Sep 17 00:00:00 2001 From: Jason Sadler Date: Sun, 7 Oct 2018 15:23:59 -0400 Subject: [PATCH] Address brave/brave-browser#1283. Fixes brave/brave-browser#1440. wip - debugging wip - wip - wip - include_switch linting clean up debugging Brackets for reducer switch case clean up debug lines linting for CI --- browser/extensions/api/brave_rewards_api.cc | 19 +++++++++++++++++++ browser/extensions/api/brave_rewards_api.h | 11 +++++++++++ common/extensions/api/brave_rewards.json | 2 +- .../brave_rewards/browser/rewards_service.h | 2 ++ .../browser/rewards_service_impl.cc | 6 ++++++ .../browser/rewards_service_impl.h | 2 ++ .../actions/rewards_panel_actions.ts | 5 +++++ .../reducers/rewards_panel_reducer.ts | 7 +++++++ .../brave_rewards/components/panel.tsx | 11 ++++++++++- .../constants/rewards_panel_types.ts | 3 ++- components/definitions/chromel.d.ts | 1 + 11 files changed, 66 insertions(+), 3 deletions(-) diff --git a/browser/extensions/api/brave_rewards_api.cc b/browser/extensions/api/brave_rewards_api.cc index ec029b3ce226..afc665b1d089 100644 --- a/browser/extensions/api/brave_rewards_api.cc +++ b/browser/extensions/api/brave_rewards_api.cc @@ -69,6 +69,25 @@ ExtensionFunction::ResponseAction BraveRewardsDonateToSiteFunction::Run() { BraveRewardsGetPublisherDataFunction::~BraveRewardsGetPublisherDataFunction() { } +BraveRewardsIncludeInAutoContributionFunction:: + ~BraveRewardsIncludeInAutoContributionFunction() { +} + +ExtensionFunction::ResponseAction + BraveRewardsIncludeInAutoContributionFunction::Run() { + + std::unique_ptr params( + brave_rewards::IncludeInAutoContribution::Params::Create(*args_)); + Profile* profile = Profile::FromBrowserContext(browser_context()); + RewardsService* rewards_service_ = + RewardsServiceFactory::GetForProfile(profile); + if (rewards_service_) { + rewards_service_->SetContributionAutoInclude( + params->publisher_key, params->excluded); + } + return RespondNow(NoArguments()); +} + ExtensionFunction::ResponseAction BraveRewardsGetPublisherDataFunction::Run() { std::unique_ptr params( brave_rewards::GetPublisherData::Params::Create(*args_)); diff --git a/browser/extensions/api/brave_rewards_api.h b/browser/extensions/api/brave_rewards_api.h index bb8cca3760e6..ed8402e4a981 100644 --- a/browser/extensions/api/brave_rewards_api.h +++ b/browser/extensions/api/brave_rewards_api.h @@ -60,6 +60,17 @@ class BraveRewardsGetCurrentReportFunction : public UIThreadExtensionFunction { ResponseAction Run() override; }; +class BraveRewardsIncludeInAutoContributionFunction : + public UIThreadExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("braveRewards.includeInAutoContribution", UNKNOWN) + + protected: + ~BraveRewardsIncludeInAutoContributionFunction() override; + + ResponseAction Run() override; +}; + } // namespace api } // namespace extensions diff --git a/common/extensions/api/brave_rewards.json b/common/extensions/api/brave_rewards.json index bc2f002a7b03..0a41ec345512 100644 --- a/common/extensions/api/brave_rewards.json +++ b/common/extensions/api/brave_rewards.json @@ -234,4 +234,4 @@ } ] } -] \ No newline at end of file +] diff --git a/components/brave_rewards/browser/rewards_service.h b/components/brave_rewards/browser/rewards_service.h index 696b6d05180e..27d98f929425 100644 --- a/components/brave_rewards/browser/rewards_service.h +++ b/components/brave_rewards/browser/rewards_service.h @@ -92,6 +92,8 @@ class RewardsService : public KeyedService { virtual void RemoveRecurring(const std::string& publisher_key) = 0; virtual void UpdateRecurringDonationsList() = 0; virtual void UpdateTipsList() = 0; + virtual void SetContributionAutoInclude( + std::string publisher_key, bool excluded) = 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 47ccc9502251..de14e14194f3 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -1573,4 +1573,10 @@ void RewardsServiceImpl::OnRemoveRecurring(const std::string& publisher_key, AsWeakPtr(), callback)); } +void RewardsServiceImpl::SetContributionAutoInclude(std::string publisher_key, + bool excluded) { + ledger_->SetPublisherExclude(publisher_key, excluded ? + ledger::PUBLISHER_EXCLUDE::EXCLUDED : ledger::PUBLISHER_EXCLUDE::INCLUDED); +} + } // namespace brave_rewards diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index a2ae254b0302..5a98cd6fbdb4 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -110,6 +110,8 @@ class RewardsServiceImpl : public RewardsService, void RemoveRecurring(const std::string& publisher_key) override; void UpdateRecurringDonationsList() override; void UpdateTipsList() override; + void SetContributionAutoInclude( + std::string publisher_key, bool excluded) override; private: friend void RunIOTaskCallback( diff --git a/components/brave_rewards/extension/brave_rewards/actions/rewards_panel_actions.ts b/components/brave_rewards/extension/brave_rewards/actions/rewards_panel_actions.ts index f92af3c07ff2..6db9a492efd3 100644 --- a/components/brave_rewards/extension/brave_rewards/actions/rewards_panel_actions.ts +++ b/components/brave_rewards/extension/brave_rewards/actions/rewards_panel_actions.ts @@ -54,3 +54,8 @@ export const onNotificationDeleted = (id: number, type: number, timestamp: numbe export const deleteNotification = (id: number) => action(types.DELETE_NOTIFICATION, { id }) + +export const includeInAutoContribution = (publisherKey: string, excluded: boolean) => action(types.INCLUDE_IN_AUTO_CONTRIBUTION, { + publisherKey, + excluded +}) diff --git a/components/brave_rewards/extension/brave_rewards/background/reducers/rewards_panel_reducer.ts b/components/brave_rewards/extension/brave_rewards/background/reducers/rewards_panel_reducer.ts index a494af927123..20f973f3dc2f 100644 --- a/components/brave_rewards/extension/brave_rewards/background/reducers/rewards_panel_reducer.ts +++ b/components/brave_rewards/extension/brave_rewards/background/reducers/rewards_panel_reducer.ts @@ -170,6 +170,13 @@ export const rewardsPanelReducer = (state: RewardsExtension.State | undefined, a setBadgeText(state) break } + case types.INCLUDE_IN_AUTO_CONTRIBUTION: + { + let publisherKey = payload.publisherKey + let excluded = payload.excluded + chrome.braveRewards.includeInAutoContribution(publisherKey, excluded) + break + } } if (state !== startingState) { diff --git a/components/brave_rewards/extension/brave_rewards/components/panel.tsx b/components/brave_rewards/extension/brave_rewards/components/panel.tsx index aced40dd59de..71f6ec6eca2e 100644 --- a/components/brave_rewards/extension/brave_rewards/components/panel.tsx +++ b/components/brave_rewards/extension/brave_rewards/components/panel.tsx @@ -78,6 +78,15 @@ export class Panel extends React.Component { console.log('doNothing click') } + switchAutoContribute = () => { + const publisher: RewardsExtension.Publisher | undefined = this.getPublisher() + const publisherKey = publisher && publisher.publisher_key + const excluded = publisher && publisher.excluded + if (publisherKey && publisherKey.length > 0 && excluded !== undefined) { + this.props.actions.includeInAutoContribution(publisherKey, !excluded) + } + } + getPublisher = () => { let windowId = this.props.windowId.toString() @@ -251,7 +260,7 @@ export class Panel extends React.Component { onToggleTips={this.doNothing} donationAction={this.showDonateToSiteDetail} onAmountChange={this.doNothing} - onIncludeInAuto={this.doNothing} + onIncludeInAuto={this.switchAutoContribute} /> : null } diff --git a/components/brave_rewards/extension/brave_rewards/constants/rewards_panel_types.ts b/components/brave_rewards/extension/brave_rewards/constants/rewards_panel_types.ts index c98ea4919ce6..35a30e6cdcee 100644 --- a/components/brave_rewards/extension/brave_rewards/constants/rewards_panel_types.ts +++ b/components/brave_rewards/extension/brave_rewards/constants/rewards_panel_types.ts @@ -15,5 +15,6 @@ export const enum types { ON_CURRENT_REPORT = '@@rewards_panel/ON_CURRENT_REPORT', ON_NOTIFICATION_ADDED = '@@rewards_panel/ON_NOTIFICATION_ADDED', ON_NOTIFICATION_DELETED = '@@rewards_panel/ON_NOTIFICATION_DELETED', - DELETE_NOTIFICATION = '@@rewards_panel/DELETE_NOTIFICATION' + DELETE_NOTIFICATION = '@@rewards_panel/DELETE_NOTIFICATION', + INCLUDE_IN_AUTO_CONTRIBUTION = '@@rewards_panel/INCLUDE_IN_AUTO_CONTRIBUTION' } diff --git a/components/definitions/chromel.d.ts b/components/definitions/chromel.d.ts index dedfd88a5ca5..6eebcabd664c 100644 --- a/components/definitions/chromel.d.ts +++ b/components/definitions/chromel.d.ts @@ -30,6 +30,7 @@ declare namespace chrome.braveRewards { const onCurrentReport: { addListener: (callback: (properties: RewardsExtension.Report) => void) => void } + const includeInAutoContribution: (publisherKey: string, excluded: boolean) => {} } declare namespace chrome.rewardsNotifications {