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

Add configurable default tip amounts (uplift to 1.3.x) #4248

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,19 @@ ExtensionFunction::ResponseAction BraveRewardsGetPublisherDataFunction::Run() {
return RespondNow(NoArguments());
}

BraveRewardsGetWalletPropertiesFunction::
~BraveRewardsGetWalletPropertiesFunction() = default;

ExtensionFunction::ResponseAction
BraveRewardsGetWalletPropertiesFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
auto* rewards_service = RewardsServiceFactory::GetForProfile(profile);
if (rewards_service) {
rewards_service->FetchWalletProperties();
}
return RespondNow(NoArguments());
}

BraveRewardsGetCurrentReportFunction::~BraveRewardsGetCurrentReportFunction() {
}

Expand Down
10 changes: 10 additions & 0 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ class BraveRewardsGetPublisherDataFunction : public ExtensionFunction {
ResponseAction Run() override;
};

class BraveRewardsGetWalletPropertiesFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getWalletProperties", UNKNOWN)

protected:
~BraveRewardsGetWalletPropertiesFunction() override;

ResponseAction Run() override;
};

class BraveRewardsGetCurrentReportFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getCurrentReport", UNKNOWN)
Expand Down
21 changes: 16 additions & 5 deletions browser/ui/webui/brave_tip_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/memory/weak_ptr.h"
#include "base/strings/utf_string_conversions.h"
Expand Down Expand Up @@ -177,6 +178,15 @@ void RewardsTipDOMHandler::GetWalletProperties(const base::ListValue* args) {
rewards_service_->FetchWalletProperties();
}

static std::unique_ptr<base::ListValue> CreateListOfDoubles(
const std::vector<double>& items) {
auto result = std::make_unique<base::ListValue>();
for (double const& item : items) {
result->AppendDouble(item);
}
return result;
}

void RewardsTipDOMHandler::OnWalletProperties(
brave_rewards::RewardsService* rewards_service,
int error_code,
Expand All @@ -191,11 +201,12 @@ void RewardsTipDOMHandler::OnWalletProperties(
auto walletInfo = std::make_unique<base::DictionaryValue>();

if (error_code == 0 && wallet_properties) {
auto choices = std::make_unique<base::ListValue>();
for (double const& choice : wallet_properties->parameters_choices) {
choices->AppendDouble(choice);
}
walletInfo->SetList("choices", std::move(choices));
walletInfo->SetList("choices",
CreateListOfDoubles(wallet_properties->parameters_choices));
walletInfo->SetList("defaultTipChoices",
CreateListOfDoubles(wallet_properties->default_tip_choices));
walletInfo->SetList("defaultMonthlyTipChoices",
CreateListOfDoubles(wallet_properties->default_monthly_tip_choices));
}

result.SetDictionary("wallet", std::move(walletInfo));
Expand Down
46 changes: 19 additions & 27 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,34 +156,20 @@
"name": "properties",
"type": "object",
"properties": {
"probi": {
"type": "string",
"description": "balance represented in probis"
},
"balance": {
"type": "double",
"description": "balance"
"defaultTipChoices": {
"type": "array",
"description": "default tip choices for one-time tips",
"items": {
"type": "number",
"minimum": 0
}
},
"rates": {
"type": "object",
"description": "rates for different currencies",
"properties": {
"BTC": {
"type": "double",
"description": "BTC rate"
},
"ETH": {
"type": "double",
"description": "ETH rate"
},
"USD": {
"type": "double",
"description": "USD rate"
},
"EUR": {
"type": "double",
"description": "EUR rate"
}
"defaultMonthlyTipChoices": {
"type": "array",
"description": "default tip choices for recurring tips",
"items": {
"type": "number",
"minimum": 0
}
}
}
Expand Down Expand Up @@ -566,6 +552,12 @@
}
]
},
{
"name": "getWalletProperties",
"type": "function",
"description": "Get default values that we get from the server",
"parameters": []
},
{
"name": "getCurrentReport",
"type": "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ void ExtensionRewardsServiceObserver::OnWalletProperties(
std::unique_ptr<brave_rewards::WalletProperties> wallet_properties) {
auto* event_router =
extensions::EventRouter::Get(profile_);
if (!event_router) {
return;
}

if (error_code == 17) { // ledger::Result::CORRUPT_WALLET
std::unique_ptr<base::ListValue> args(
extensions::api::brave_rewards::OnWalletInitialized::Create(
Expand All @@ -64,6 +68,26 @@ void ExtensionRewardsServiceObserver::OnWalletProperties(
event_router->BroadcastEvent(std::move(event));
return;
}

if (!wallet_properties) {
return;
}

extensions::api::brave_rewards::OnWalletProperties::Properties properties;

properties.default_tip_choices = wallet_properties->default_tip_choices;
properties.default_monthly_tip_choices =
wallet_properties->default_monthly_tip_choices;

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

std::unique_ptr<extensions::Event> event(new extensions::Event(
extensions::events::BRAVE_ON_WALLET_PROPERTIES,
extensions::api::brave_rewards::OnWalletProperties::kEventName,
std::move(args)));
event_router->BroadcastEvent(std::move(event));
}

void ExtensionRewardsServiceObserver::OnGetCurrentBalanceReport(
Expand Down
Loading