Skip to content

Commit

Permalink
Fixes panel updates on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Feb 26, 2019
1 parent 2e81293 commit cd6c044
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 1 deletion.
28 changes: 28 additions & 0 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,34 @@
"type": "function",
"description": "Fired when wallet creation failed",
"parameters": []
},
{
"name": "onPublisherListNormalized",
"type": "function",
"description": "Fired when publisher list was updated and normalized",
"parameters": [
{
"name": "publishers",
"type": "array",
"items": {
"type": "object",
"properties": {
"publisher_key": {
"type": "string",
"description": "publisher key, unique identifier"
},
"percentage": {
"type": "integer",
"description": "publisher attention score"
},
"verified": {
"type": "boolean",
"description": "is publisher verified"
}
}
}
}
]
}
],
"functions": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <utility>
#include <string>
#include <vector>

#include "base/base64.h"
#include "brave/common/extensions/api/brave_rewards.h"
Expand Down Expand Up @@ -266,4 +267,39 @@ void ExtensionRewardsServiceObserver::OnPendingContributionSaved(
event_router->BroadcastEvent(std::move(event));
}

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

std::vector<extensions::api::brave_rewards::OnPublisherListNormalized::
PublishersType> publishers;

for (size_t i = 0; i < list.size(); i ++) {
publishers.push_back(
extensions::api::brave_rewards::OnPublisherListNormalized::
PublishersType());

auto& publisher = publishers[publishers.size() -1];

publisher.publisher_key = list[i].id;
publisher.percentage = list[i].percentage;
publisher.verified = list[i].verified;
}

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

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

} // namespace brave_rewards
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <memory>

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

Expand All @@ -30,6 +31,9 @@ class ExtensionRewardsServiceObserver : public RewardsServiceObserver,
int error_code,
std::unique_ptr<brave_rewards::WalletProperties>
wallet_properties) override;
void OnPublisherListNormalized(
RewardsService* rewards_service,
brave_rewards::ContentSiteList list) override;

// RewardsServicePrivateObserver implementation
void OnGetCurrentBalanceReport(RewardsService* rewards_service,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,8 @@ export const onEnabledMain = (enabledMain: boolean) => action(types.ON_ENABLED_M
export const onEnabledAC = (enabled: boolean) => action(types.ON_ENABLED_AC, {
enabled
})

export const onPublisherListNormalized = (properties: RewardsExtension.PublisherNormalized[]) =>
action(types.ON_PUBLISHER_LIST_NORMALIZED, {
properties
})
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ chrome.braveRewards.onPendingContributionSaved.addListener((result: number) => {
chrome.braveRewards.onWalletFailed.addListener(() => {
rewardsPanelActions.onWalletCreateFailed()
})

chrome.braveRewards.onPublisherListNormalized.addListener((properties: RewardsExtension.PublisherNormalized[]) => {
rewardsPanelActions.onPublisherListNormalized(properties)
})
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,33 @@ export const rewardsPanelReducer = (state: RewardsExtension.State | undefined, a
state.enabledAC = payload.enabled
break
}
case types.ON_PUBLISHER_LIST_NORMALIZED: {
const list = payload.properties
let publishers: Record<string, RewardsExtension.Publisher> = state.publishers

if (!list || list.length === 0) {
break
}

for (const key in publishers) {
let publisher = publishers[key]
const updated = list.find((newPublisher: RewardsExtension.PublisherNormalized) =>
newPublisher.publisher_key === publisher.publisher_key)

if (updated) {
publisher.verified = updated.verified
publisher.percentage = updated.percentage
} else {
publisher.percentage = 0
}
}

state = {
...state,
publishers
}
break
}
}
return state
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export const enum types {
ON_GRANT_FINISH = '@@rewards_panel/ON_GRANT_FINISH',
ON_PENDING_CONTRIBUTIONS_TOTAL = '@@rewards_panel/ON_PENDING_CONTRIBUTIONS_TOTAL',
ON_ENABLED_MAIN = '@@rewards_panel/ON_ENABLED_MAIN',
ON_ENABLED_AC = '@@rewards_panel/ON_ENABLED_AC'
ON_ENABLED_AC = '@@rewards_panel/ON_ENABLED_AC',
ON_PUBLISHER_LIST_NORMALIZED = '@@rewards_panel/ON_PUBLISHER_LIST_NORMALIZED'
}

// Note: This declaration must match the RewardsNotificationType enum in
Expand Down
3 changes: 3 additions & 0 deletions components/definitions/chromel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ declare namespace chrome.braveRewards {
addListener: (callback: () => void) => void
}
const getACEnabled: (callback: (enabled: boolean) => void) => {}
const onPublisherListNormalized: {
addListener: (callback: (properties: RewardsExtension.PublisherNormalized[]) => void) => void
}
}

declare namespace chrome.rewardsNotifications {
Expand Down
6 changes: 6 additions & 0 deletions components/definitions/rewardsExtensions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,10 @@ declare namespace RewardsExtension {
timestamp: number
args: string[]
}

interface PublisherNormalized {
publisher_key: string
percentage: number
verified: boolean
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,97 @@ describe('rewards panel reducer', () => {
})
})
})

describe('ON_PUBLISHER_LIST_NORMALIZED', () => {
it('list is empty', () => {
let state = reducers({ rewardsPanelData: defaultState }, {
type: types.ON_PUBLISHER_LIST_NORMALIZED,
payload: {
properties: []
}
})

expect(state.rewardsPanelData).toEqual(defaultState)
})

it('list is undefined', () => {
let state = reducers({ rewardsPanelData: defaultState }, {
type: types.ON_PUBLISHER_LIST_NORMALIZED,
payload: {
properties: undefined
}
})

expect(state.rewardsPanelData).toEqual(defaultState)
})

it('publisher is update accordingly', () => {
const list = [
{
publisher_key: 'brave.com',
percentage: 50,
verified: true
},
{
publisher_key: 'brave1.com',
percentage: 30,
verified: true
},
{
publisher_key: 'brave2.com',
percentage: 10,
verified: true
},
{
publisher_key: 'brave3.com',
percentage: 10,
verified: true
}
]

let state = {
...defaultState,
publishers: {
id_1: {
tabUrl: 'https://brave.com',
publisher_key: 'brave.com',
percentage: 30,
verified: false
},
id_2: {
tabUrl: 'https://brave4.com',
publisher_key: 'brave4.com',
percentage: 40,
verified: true
}
}
}
const expectedState: Rewards.State = {
...defaultState,
publishers: {
id_1: {
tabUrl: 'https://brave.com',
publisher_key: 'brave.com',
percentage: 50,
verified: true
},
id_2: {
tabUrl: 'https://brave4.com',
publisher_key: 'brave4.com',
percentage: 0,
verified: true
}
}
}

state = reducers({ rewardsPanelData: state }, {
type: types.ON_PUBLISHER_LIST_NORMALIZED,
payload: {
properties: list
}
})

expect(state.rewardsPanelData).toEqual(expectedState)
})
})
})

0 comments on commit cd6c044

Please sign in to comment.