Skip to content

Commit

Permalink
Adds promotions info in internals page
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Apr 29, 2020
1 parent 2b7dcf9 commit 02514af
Show file tree
Hide file tree
Showing 22 changed files with 316 additions and 5 deletions.
44 changes: 44 additions & 0 deletions browser/ui/webui/brave_rewards_internals_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "brave/browser/brave_rewards/rewards_service_factory.h"
Expand Down Expand Up @@ -49,6 +50,8 @@ class RewardsInternalsDOMHandler : public content::WebUIMessageHandler {
void OnGetBalance(
int32_t result,
std::unique_ptr<brave_rewards::Balance> balance);
void GetPromotions(const base::ListValue* args);
void OnGetPromotions(const std::vector<brave_rewards::Promotion>& list);

brave_rewards::RewardsService* rewards_service_; // NOT OWNED
Profile* profile_;
Expand Down Expand Up @@ -78,6 +81,11 @@ void RewardsInternalsDOMHandler::RegisterMessages() {
base::BindRepeating(
&RewardsInternalsDOMHandler::GetBalance,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"brave_rewards_internals.getPromotions",
base::BindRepeating(
&RewardsInternalsDOMHandler::GetPromotions,
base::Unretained(this)));
}

void RewardsInternalsDOMHandler::Init() {
Expand Down Expand Up @@ -182,6 +190,42 @@ void RewardsInternalsDOMHandler::OnGetBalance(
std::move(balance_value));
}

void RewardsInternalsDOMHandler::GetPromotions(const base::ListValue *args) {
if (!rewards_service_) {
return;
}

rewards_service_->GetAllPromotions(base::BindOnce(
&RewardsInternalsDOMHandler::OnGetPromotions,
weak_ptr_factory_.GetWeakPtr()));
}

void RewardsInternalsDOMHandler::OnGetPromotions(
const std::vector<brave_rewards::Promotion>& list) {
if (!web_ui()->CanCallJavascript()) {
return;
}

base::ListValue promotions;
for (const auto & item : list) {
auto dict = std::make_unique<base::DictionaryValue>();
dict->SetDouble("amount", item.amount);
dict->SetString("promotionId", item.promotion_id);
dict->SetInteger("expiresAt", item.expires_at);
dict->SetInteger("type", item.type);
dict->SetInteger("status", item.status);
dict->SetInteger("claimedAt", item.claimed_at);
dict->SetBoolean("legacyClaimed", item.legacy_claimed);
dict->SetString("claimId", item.claim_id);
dict->SetInteger("version", item.version);
promotions.Append(std::move(dict));
}

web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards_internals.promotions",
std::move(promotions));
}

} // namespace

BraveRewardsInternalsUI::BraveRewardsInternalsUI(content::WebUI* web_ui,
Expand Down
18 changes: 18 additions & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,24 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "processorBraveTokens", IDS_BRAVE_UI_PROCESSOR_BRAVE_TOKENS },
{ "processorUphold", IDS_BRAVE_UI_PROCESSOR_UPHOLD },
{ "processorBraveUserFunds", IDS_BRAVE_UI_PROCESSOR_BRAVE_USER_FUNDS },
{ "promotionAds", IDS_BRAVE_REWARDS_INTERNALS_PROMOTION_ADS },
{ "promotionAmount", IDS_BRAVE_REWARDS_INTERNALS_PROMOTION_AMOUNT },
{ "promotionClaimedAt", IDS_BRAVE_REWARDS_INTERNALS_PROMOTION_CLAIMED_AT }, // NOLINT
{ "promotionClaimId", IDS_BRAVE_REWARDS_INTERNALS_PROMOTION_CLAIM_ID },
{ "promotionExpiresAt", IDS_BRAVE_REWARDS_INTERNALS_PROMOTION_EXPIRES_AT }, // NOLINT
{ "promotionId", IDS_BRAVE_REWARDS_INTERNALS_PROMOTION_ID },
{ "promotionLegacyClaimed", IDS_BRAVE_REWARDS_INTERNALS_PROMOTION_LEGACY_CLAIMED }, // NOLINT
{ "promotionLegacyNo", IDS_BRAVE_REWARDS_INTERNALS_PROMOTION_LEGACY_NO }, // NOLINT
{ "promotionLegacyYes", IDS_BRAVE_REWARDS_INTERNALS_PROMOTION_LEGACY_YES }, // NOLINT
{ "promotions", IDS_BRAVE_REWARDS_INTERNALS_PROMOTIONS },
{ "promotionStatus", IDS_BRAVE_REWARDS_INTERNALS_PROMOTION_STATUS },
{ "promotionStatusActive", IDS_BRAVE_REWARDS_INTERNALS_PROMOTION_STATUS_ACTIVE }, // NOLINT
{ "promotionStatusAttested", IDS_BRAVE_REWARDS_INTERNALS_PROMOTION_STATUS_ATTESTED }, // NOLINT
{ "promotionStatusFinished", IDS_BRAVE_REWARDS_INTERNALS_PROMOTION_STATUS_FINISHED }, // NOLINT
{ "promotionStatusOver", IDS_BRAVE_REWARDS_INTERNALS_PROMOTION_STATUS_OVER }, // NOLINT
{ "promotionType", IDS_BRAVE_REWARDS_INTERNALS_PROMOTION_TYPE },
{ "promotionUGP", IDS_BRAVE_REWARDS_INTERNALS_PROMOTION_UGP },
{ "promotionVersion", IDS_BRAVE_REWARDS_INTERNALS_PROMOTION_VERSION },
{ "refreshButton", IDS_BRAVE_REWARDS_INTERNALS_REFRESH_BUTTON },
{ "retryLevel", IDS_BRAVE_REWARDS_INTERNALS_RETRY_LEVEL },
{ "retryStep", IDS_BRAVE_REWARDS_INTERNALS_RETRY_STEP },
Expand Down
3 changes: 3 additions & 0 deletions components/brave_ads/browser/ads_service_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ class MockRewardsService : public RewardsService {

MOCK_METHOD1(GetAllMonthlyReportIds, void(
brave_rewards::GetAllMonthlyReportIdsCallback callback));

MOCK_METHOD1(GetAllPromotions, void(
brave_rewards::GetAllPromotionsCallback callback));
};

class AdsServiceTest : public testing::Test {
Expand Down
4 changes: 4 additions & 0 deletions components/brave_rewards/browser/promotion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Promotion::Promotion(const Promotion &properties) {
expires_at = properties.expires_at;
type = properties.type;
status = properties.status;
claimed_at = properties.claimed_at;
legacy_claimed = properties.legacy_claimed;
claim_id = properties.claim_id;
version = properties.version;
}

} // namespace brave_rewards
4 changes: 4 additions & 0 deletions components/brave_rewards/browser/promotion.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ struct Promotion {
uint64_t expires_at;
uint32_t type;
uint32_t status;
uint64_t claimed_at;
bool legacy_claimed;
std::string claim_id;
uint32_t version;
};

} // namespace brave_rewards
Expand Down
6 changes: 6 additions & 0 deletions components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ using GetMonthlyReportCallback = base::OnceCallback<void(
using GetAllMonthlyReportIdsCallback =
base::OnceCallback<void(const std::vector<std::string>&)>;

using GetAllPromotionsCallback =
base::OnceCallback<void(const std::vector<brave_rewards::Promotion>&)>;

class RewardsService : public KeyedService {
public:
RewardsService();
Expand Down Expand Up @@ -313,6 +316,9 @@ class RewardsService : public KeyedService {
virtual void GetAllMonthlyReportIds(
GetAllMonthlyReportIdsCallback callback) = 0;

virtual void GetAllPromotions(
GetAllPromotionsCallback callback) = 0;

protected:
base::ObserverList<RewardsServiceObserver> observers_;

Expand Down
31 changes: 31 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3463,4 +3463,35 @@ void RewardsServiceImpl::OnGetAllMonthlyReportIds(
std::move(callback).Run(ids);
}

void RewardsServiceImpl::GetAllPromotions(GetAllPromotionsCallback callback) {
bat_ledger_->GetAllPromotions(
base::BindOnce(&RewardsServiceImpl::OnGetAllPromotions,
AsWeakPtr(),
std::move(callback)));
}

void RewardsServiceImpl::OnGetAllPromotions(
GetAllPromotionsCallback callback,
base::flat_map<std::string, ledger::PromotionPtr> promotions) {
std::vector<brave_rewards::Promotion> converted_rewards;
for (const auto& promotion : promotions) {
if (!promotion.second) {
continue;
}

brave_rewards::Promotion properties;
properties.promotion_id = promotion.second->id;
properties.amount = promotion.second->approximate_value;
properties.expires_at = promotion.second->expires_at;
properties.type = static_cast<uint32_t>(promotion.second->type);
properties.status = static_cast<uint32_t>(promotion.second->status);
properties.claimed_at = promotion.second->claimed_at;
properties.legacy_claimed = promotion.second->legacy_claimed;
properties.claim_id = promotion.second->claim_id;
properties.version = promotion.second->version;
converted_rewards.push_back(properties);
}
std::move(callback).Run(std::move(converted_rewards));
}

} // namespace brave_rewards
6 changes: 6 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ class RewardsServiceImpl : public RewardsService,

void GetAllMonthlyReportIds(GetAllMonthlyReportIdsCallback callback) override;

void GetAllPromotions(GetAllPromotionsCallback callback) override;

// Testing methods
void SetLedgerEnvForTesting();
void StartMonthlyContributionForTest();
Expand Down Expand Up @@ -656,6 +658,10 @@ class RewardsServiceImpl : public RewardsService,
GetAllMonthlyReportIdsCallback callback,
const std::vector<std::string>& ids);

void OnGetAllPromotions(
GetAllPromotionsCallback callback,
base::flat_map<std::string, ledger::PromotionPtr> promotions);

#if defined(OS_ANDROID)
ledger::Environment GetServerEnvironmentForAndroid();
void CreateWalletAttestationResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ export const onBalance = (balance: RewardsInternals.Balance) =>
action(types.ON_BALANCE, {
balance
})

export const getPromotions = () => action(types.GET_PROMOTIONS)

export const onPromotions = (promotions: RewardsInternals.Promotion[]) =>
action(types.ON_PROMOTIONS, {
promotions
})
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ window.cr.define('brave_rewards_internals', function () {
getActions().onBalance(balance)
}

function promotions (promotions: RewardsInternals.Promotion[]) {
getActions().onPromotions(promotions)
}

function initialize () {
window.i18nTemplate.process(window.document, window.loadTimeData)

Expand All @@ -55,7 +59,8 @@ window.cr.define('brave_rewards_internals', function () {
initialize,
onGetRewardsEnabled,
onGetRewardsInternalsInfo,
balance
balance,
promotions
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { connect } from 'react-redux'
import { Contributions } from './contributions'
import { WalletInfo } from './walletInfo'
import { Balance } from './balance'
import { Promotions } from './promotions'

// Utils
import { getLocale } from '../../../../common/locale'
Expand All @@ -33,19 +34,21 @@ export class RewardsInternalsPage extends React.Component<Props, {}> {
this.actions.getRewardsEnabled()
this.actions.getRewardsInternalsInfo()
this.actions.getBalance()
this.actions.getPromotions()
}

onRefresh = () => {
this.getData()
}

render () {
const { isRewardsEnabled, balance, info } = this.props.rewardsInternalsData
const { isRewardsEnabled, balance, info, promotions } = this.props.rewardsInternalsData
if (isRewardsEnabled) {
return (
<div id='rewardsInternalsPage'>
<WalletInfo state={this.props.rewardsInternalsData} />
<Balance info={balance} />
<Promotions items={promotions} />
<br/>
<br/>
<button type='button' onClick={this.onRefresh}>{getLocale('refreshButton')}</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const getWalletName = (walletKey: string) => {

const getWalletBalance = (wallets: Record<string, number>) => {
let items = []
for (let key in wallets) {
for (const key in wallets) {
items.push(<div key={'wallet-' + key}> {getWalletName(key)}: {wallets[key]} {getLocale('bat')} </div>)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* 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/. */

import * as React from 'react'

interface Props {
promotion: RewardsInternals.Promotion
}

// Utils
import { getLocale } from '../../../../common/locale'

const getType = (type: number) => {
switch (type) {
case 0:
return getLocale('promotionUGP')
case 1:
return getLocale('promotionAds')
}

return 'Missing type'
}
const getStatus = (status: number) => {
switch (status) {
case 0:
return getLocale('promotionStatusActive')
case 1:
return getLocale('promotionStatusAttested')
case 4:
return getLocale('promotionStatusFinished')
case 5:
return getLocale('promotionStatusOver')
}

return 'Missing status'
}

const getLegacyClaimed = (legacy: boolean) => {
return legacy
? getLocale('promotionLegacyYes')
: getLocale('promotionLegacyNo')
}

const getClaimData = (claimedAt: number, claimId: string) => {
if (claimedAt === 0) {
return null
}

return (
<>
{getLocale('promotionClaimedAt')}: {new Date(claimedAt * 1000).toLocaleDateString()}
<br/>
{getLocale('promotionClaimId')}: {claimId}
</>
)
}

export const Promotion = (props: Props) => (
<div>
{getLocale('promotionId')}: {props.promotion.promotionId}
<br/>
{getLocale('promotionStatus')}: {getStatus(props.promotion.status)}
<br/>
{getLocale('promotionAmount')}: {props.promotion.amount} {getLocale('bat')}
<br/>
{getLocale('promotionType')}: {getType(props.promotion.type)}
<br/>
{getLocale('promotionExpiresAt')}: {new Date(props.promotion.expiresAt * 1000).toLocaleDateString()}
<br/>
{getLocale('promotionLegacyClaimed')}: {getLegacyClaimed(props.promotion.legacyClaimed)}
<br/>
{getLocale('promotionVersion')}: {props.promotion.version}
<br/>
{getClaimData(props.promotion.claimedAt, props.promotion.claimId)}
</div>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* 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/. */

import * as React from 'react'

import { Promotion } from './promotion'

interface Props {
items: RewardsInternals.Promotion[]
}

// Utils
import { getLocale } from '../../../../common/locale'

export const Promotions = (props: Props) => {
if (!props.items || props.items.length === 0) {
return null
}

return (
<>
<h3>{getLocale('promotions')}</h3>
{props.items.map((item, index) => (
<div key={item.promotionId}>
<Promotion promotion={item} />
{(index !== props.items.length - 1) ? <hr/> : null}
</div>
))}
</>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ export const enum types {
GET_REWARDS_INTERNALS_INFO = '@@rewards_internals/GET_REWARDS_INTERNALS_INFO',
ON_GET_REWARDS_INTERNALS_INFO = '@@rewards_internals/ON_GET_REWARDS_INTERNALS_INFO',
GET_BALANCE = '@@rewards_internals/GET_BALANCE',
ON_BALANCE = '@@rewards_internals/ON_BALANCE'
ON_BALANCE = '@@rewards_internals/ON_BALANCE',
GET_PROMOTIONS = '@@rewards_internals/GET_PROMOTIONS',
ON_PROMOTIONS = '@@rewards_internals/ON_PROMOTIONS'
}
Loading

0 comments on commit 02514af

Please sign in to comment.