Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds functionality to monthly contribution dropdown (Panel) #957

Merged
merged 1 commit into from Dec 3, 2018
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Fixes brave/brave-browser#2245, monthly donation functionality

  • Loading branch information
ryanml committed Nov 27, 2018
commit a4959e8c74227d8dee352e2507550d37a0e7e5da
@@ -138,5 +138,44 @@ ExtensionFunction::ResponseAction BraveRewardsGetGrantFunction::Run() {
return RespondNow(NoArguments());
}

BraveRewardsSaveRecurringDonationFunction::~BraveRewardsSaveRecurringDonationFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsSaveRecurringDonationFunction::Run() {

std::unique_ptr<brave_rewards::SaveRecurringDonation::Params> params(
brave_rewards::SaveRecurringDonation::Params::Create(*args_));

Profile* profile = Profile::FromBrowserContext(browser_context());
RewardsService* rewards_service_ = RewardsServiceFactory::GetForProfile(profile);

if (rewards_service_) {
rewards_service_->AddRecurringPayment(params->publisher_key, params->new_amount);
}

return RespondNow(NoArguments());
}

BraveRewardsRemoveRecurringDonationFunction::~BraveRewardsRemoveRecurringDonationFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsRemoveRecurringDonationFunction::Run() {

std::unique_ptr<brave_rewards::RemoveRecurringDonation::Params> params(
brave_rewards::RemoveRecurringDonation::Params::Create(*args_));

Profile* profile = Profile::FromBrowserContext(browser_context());
RewardsService* rewards_service_ = RewardsServiceFactory::GetForProfile(profile);

if (rewards_service_) {
rewards_service_->RemoveRecurring(params->publisher_key);
}

return RespondNow(NoArguments());
}


} // namespace api
} // namespace extensions
@@ -81,6 +81,26 @@ class BraveRewardsGetGrantFunction : public UIThreadExtensionFunction {
ResponseAction Run() override;
};

class BraveRewardsSaveRecurringDonationFunction : public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.saveRecurringDonation", UNKNOWN)

protected:
~BraveRewardsSaveRecurringDonationFunction() override;

ResponseAction Run() override;
};

class BraveRewardsRemoveRecurringDonationFunction : public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.removeRecurringDonation", UNKNOWN)

protected:
~BraveRewardsRemoveRecurringDonationFunction() override;

ResponseAction Run() override;
};

} // namespace api
} // namespace extensions

@@ -177,6 +177,17 @@
}
}
]
},
{
"name": "onRecurringDonations",
"type": "function",
"description": "",
"parameters": [
{
"name": "result",
"type": "any"
}
]
}
],
"functions": [
@@ -256,6 +267,32 @@
"type": "function",
"description": "Retrieves grant when panel is mounted",
"parameters": []
},
{
"name": "saveRecurringDonation",
"type": "function",
"description": "Updates recurring donation amount for rewards panel",
"parameters": [
{
"name": "publisher_key",
"type": "string"
},
{
"name": "new_amount",
"type": "integer"
}
]
},
{
"name": "removeRecurringDonation",
"type": "function",
"description": "Removes recurring donation for rewards panel",
"parameters": [
{
"name": "publisher_key",
"type": "string"
}
]
}
]
}
@@ -12,6 +12,7 @@ namespace brave_rewards {
percentage(0),
verified(false),
excluded(0),
weight(0),
reconcile_stamp(0) {
}

@@ -26,6 +27,7 @@ namespace brave_rewards {
url = properties.url;
provider = properties.provider;
id = properties.id;
weight = properties.weight;
reconcile_stamp = properties.reconcile_stamp;
}

@@ -26,6 +26,7 @@ struct ContentSite {
std::string favicon_url;
std::string url;
std::string provider;
double weight;
uint64_t reconcile_stamp;
};

@@ -3,7 +3,6 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/components/brave_rewards/browser/extension_rewards_service_observer.h"

#include "brave/common/extensions/api/brave_rewards.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "chrome/browser/profiles/profile.h"
@@ -140,4 +139,37 @@ void ExtensionRewardsServiceObserver::OnGetPublisherActivityFromUrl(
event_router->BroadcastEvent(std::move(event));
}

void ExtensionRewardsServiceObserver::OnRecurringDonations(
RewardsService* rewards_service,
brave_rewards::ContentSiteList list) {
extensions::EventRouter* event_router = extensions::EventRouter::Get(profile_);
if (!event_router) {
return;
}

base::DictionaryValue result;
auto recurringDonations = std::make_unique<base::ListValue>();

if (!list.empty()) {
for (auto const& item: list) {
auto recurringDonation = std::make_unique<base::DictionaryValue>();
recurringDonation->SetString("publisherKey", item.id);
recurringDonation->SetInteger("amount", item.weight);
recurringDonations->Append(std::move(recurringDonation));
}
}

result.SetList("recurringDonations", std::move(recurringDonations));

std::unique_ptr<base::ListValue> args(
extensions::api::brave_rewards::OnRecurringDonations::Create(result)
.release());

std::unique_ptr<extensions::Event> event(new extensions::Event(
extensions::events::BRAVE_START,

This comment has been minimized.

Copy link
@ryanml

ryanml Nov 28, 2018

Author Member

Using BRAVE_START constant per @bridiver

extensions::api::brave_rewards::OnRecurringDonations::kEventName,
std::move(args)));
event_router->BroadcastEvent(std::move(event));
}

} // namespace brave_rewards
@@ -9,6 +9,7 @@

#include "base/macros.h"
#include "brave/components/brave_rewards/browser/rewards_service_observer.h"
#include "brave/components/brave_rewards/browser/content_site.h"
#include "brave/components/brave_rewards/browser/rewards_service_private_observer.h"

class Profile;
@@ -40,6 +41,8 @@ class ExtensionRewardsServiceObserver : public RewardsServiceObserver,
std::unique_ptr<ledger::PublisherInfo> info,
uint64_t windowId) override;

void OnRecurringDonations(RewardsService* rewards_service,
brave_rewards::ContentSiteList) override;
private:
Profile* profile_;

@@ -0,0 +1,60 @@
/* 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/rewards_service.h"
#include "brave/components/brave_rewards/browser/rewards_service_factory.h"
#include "brave/components/brave_rewards/browser/extension_rewards_service_observer.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test_utils.h"

using namespace brave_rewards;

class ExtensionRewardsServiceObserverBrowserTest
: public InProcessBrowserTest,
public RewardsServiceObserver {
public:

void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
rewards_service_ = RewardsServiceFactory::GetForProfile(browser()->profile());
}

void TearDown() override {
InProcessBrowserTest::TearDown();
}

void OnRecurringDonations(
RewardsService* rewards_service,
const ledger::PublisherInfoList& list) override {

ledger::PublisherInfo firstPublisher = list.front();
EXPECT_STREQ(firstPublisher.id.c_str(), "brave.com");
EXPECT_STREQ(std::to_string(firstPublisher.weight).c_str(), "10");

on_recurring_notifications_callback_was_called_ = true;
}

void WaitForOnRecurringDonationsCallback() {
if (on_recurring_notifications_callback_was_called_) {
return;
}

base::RunLoop run_loop;
run_loop.Run();
}

RewardsService* rewards_service_;
bool on_recurring_notifications_callback_was_called_ = false;
};

IN_PROC_BROWSER_TEST_F(ExtensionRewardsServiceObserverBrowserTest, SaveARecurringDonation) {
rewards_service_->AddObserver(this);

rewards_service_->AddRecurringPayment("brave.com", 10);
WaitForOnRecurringDonationsCallback();

rewards_service_->RemoveObserver(this);
}
@@ -97,6 +97,7 @@ class RewardsService : public KeyedService {
virtual void GetPublisherBanner(const std::string& publisher_id) = 0;
virtual void OnDonate(const std::string& publisher_key, int amount, bool recurring) = 0;
virtual void RemoveRecurring(const std::string& publisher_key) = 0;
virtual void AddRecurringPayment(const std::string& publisher_key, double new_amount) = 0;
virtual void UpdateRecurringDonationsList() = 0;
virtual void UpdateTipsList() = 0;
virtual void SetContributionAutoInclude(
@@ -145,6 +145,7 @@ ContentSite PublisherInfoToContentSite(
content_site.provider = publisher_info.provider;
content_site.favicon_url = publisher_info.favicon_url;
content_site.id = publisher_info.id;
content_site.weight = publisher_info.weight;
content_site.reconcile_stamp = publisher_info.reconcile_stamp;
return content_site;
}
@@ -1504,6 +1505,19 @@ ledger::PublisherInfoList GetRecurringDonationsOnFileTaskRunner(PublisherInfoDat
void RewardsServiceImpl::OnRecurringDonationsData(const ledger::PublisherInfoListCallback callback,
const ledger::PublisherInfoList list) {
callback(list, 0);

// Incoming ledger::PublisherInfoList needs to be converted to brave_rewards::ContentSiteList
brave_rewards::ContentSiteList site_list;
for (auto& info : list) {
site_list.push_back(PublisherInfoToContentSite(info));
}

TriggerOnRecurringDonations(site_list);
}

void RewardsServiceImpl::TriggerOnRecurringDonations(brave_rewards::ContentSiteList list) {
for (auto& observer : observers_)
observer.OnRecurringDonations(this, list);
}

void RewardsServiceImpl::GetRecurringDonations(ledger::PublisherInfoListCallback callback) {
@@ -1564,6 +1578,10 @@ void RewardsServiceImpl::OnTipsUpdatedData(const ledger::PublisherInfoList list)
}
}

void RewardsServiceImpl::AddRecurringPayment(const std::string& publisher_key, double new_amount) {

This comment has been minimized.

Copy link
@NejcZdovc

NejcZdovc Nov 27, 2018

Member

Can we just use SaveRecurringDonation directly? So that we don't need to add new function

This comment has been minimized.

Copy link
@jasonrsadler

jasonrsadler Nov 30, 2018

Contributor

@NejcZdovc SaveRecurringDonation is marked private and wouldn't be accessible to brave_rewards_api and tests

SaveRecurringDonation(publisher_key, new_amount);
}

void RewardsServiceImpl::RemoveRecurring(const std::string& publisher_key) {
ledger_->RemoveRecurring(publisher_key);
}
@@ -122,6 +122,7 @@ class RewardsServiceImpl : public RewardsService,
void RemoveRecurring(const std::string& publisher_key) override;
void UpdateRecurringDonationsList() override;
void UpdateTipsList() override;
void AddRecurringPayment(const std::string& publisher_key, double new_amount) override;
void SetContributionAutoInclude(
std::string publisher_key, bool excluded, uint64_t windowId) override;
RewardsNotificationService* GetNotificationService() const override;
@@ -189,6 +190,7 @@ class RewardsServiceImpl : public RewardsService,
ledger::Result result,
std::unique_ptr<ledger::PublisherInfo> info,
uint64_t windowId);
void TriggerOnRecurringDonations(brave_rewards::ContentSiteList);

// ledger::LedgerClient
std::string GenerateGUID() const override;
@@ -37,6 +37,7 @@ class MockRewardsServiceObserver : public RewardsServiceObserver {
MOCK_METHOD2(OnCurrentTips, void(RewardsService*, brave_rewards::ContentSiteList));
MOCK_METHOD2(OnPublisherBanner, void(RewardsService*, const brave_rewards::PublisherBanner));
MOCK_METHOD4(OnGetPublisherActivityFromUrl, void(RewardsService*, int, ledger::PublisherInfo*, uint64_t));
MOCK_METHOD2(OnRecurringDonations, void(brave_rewards::RewardsService*, const ledger::PublisherInfoList*));
};

class RewardsServiceTest : public testing::Test {
@@ -50,6 +50,9 @@ class RewardsServiceObserver : public base::CheckedObserver {
brave_rewards::ContentSiteList) {};
virtual void OnPublisherBanner(brave_rewards::RewardsService* rewards_service,
const brave_rewards::PublisherBanner banner) {};
virtual void OnRecurringDonations(
This conversation was marked as resolved by ryanml

This comment has been minimized.

Copy link
@bridiver

bridiver Nov 28, 2018

Collaborator

ledger::* types cannot be exposed here

This comment has been minimized.

Copy link
@bridiver

bridiver Nov 28, 2018

Collaborator

you should use ContentSiteList instead

This comment has been minimized.

Copy link
@ryanml

ryanml Nov 28, 2018

Author Member

Got it 👍

This comment has been minimized.

Copy link
@ryanml

ryanml Nov 28, 2018

Author Member

@bridiver in use

brave_rewards::RewardsService* rewards_service,
brave_rewards::ContentSiteList) {};
};

} // namespace brave_rewards
@@ -62,3 +62,18 @@ export const includeInAutoContribution = (publisherKey: string, excluded: boolea
})

export const getGrant = () => action(types.GET_GRANT, {})

export const saveRecurringDonation = (publisherKey: string, newAmount: number) => action(types.SAVE_RECURRING_DONATION, {
publisherKey,
newAmount
})

export const removeRecurringContribution = (publisherKey: string) => action(types.REMOVE_RECURRING_DONATION, {
publisherKey
})

export const getRecurringDonations = () => action(types.GET_RECURRING_DONATIONS)

export const onRecurringDonations = (result: RewardsExtension.RecurringDonation) => action(types.ON_RECURRING_DONATIONS, {
result
})
@@ -27,3 +27,7 @@ chrome.rewardsNotifications.onNotificationAdded.addListener((id: string, type: n
chrome.rewardsNotifications.onNotificationDeleted.addListener((id: string, type: number, timestamp: number) => {
rewardsPanelActions.onNotificationDeleted(id, type, timestamp)
})

chrome.braveRewards.onRecurringDonations.addListener((result: RewardsExtension.RecurringDonation) => {
rewardsPanelActions.onRecurringDonations(result)
})
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.