Skip to content

Commit

Permalink
Fixes twitch panel
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Mar 7, 2019
1 parent cb82f8b commit af2b1be
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo(
"UPDATE publisher_info SET favIcon = ? "
"WHERE publisher_id = ?"));

if (favicon == ledger::_clear_favicon) {
if (favicon == ledger::kClearFavicon) {
favicon.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdatePublisherInfo) {
/**
* If favicon is marked as clear, clear it
*/
info.favicon_url = ledger::_clear_favicon;
info.favicon_url = ledger::kClearFavicon;

success = publisher_info_database_->InsertOrUpdatePublisherInfo(info);
EXPECT_TRUE(success);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ export const onTabId = (tabId: number | undefined) => action(types.ON_TAB_ID, {
tabId
})

export const onTabRetrieved = (tab: chrome.tabs.Tab, publisherBlob: string) => action(types.ON_TAB_RETRIEVED, {
tab,
publisherBlob
})
export const onTabRetrieved = (tab: chrome.tabs.Tab, publisherBlob: string = 'ignore') =>
action(types.ON_TAB_RETRIEVED, {
tab,
publisherBlob
})

export const onPublisherData = (windowId: number, publisher: RewardsExtension.Publisher) => action(types.ON_PUBLISHER_DATA, {
windowId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
export const getTabData = (tabId: number) =>
chrome.tabs.get(tabId, (tab: chrome.tabs.Tab) => {
const rewardsPanelActions = require('../actions/rewardsPanelActions').default
rewardsPanelActions.onTabRetrieved(tab, '')
rewardsPanelActions.onTabRetrieved(tab)
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import rewardsPanelActions from '../actions/rewardsPanelActions'

chrome.tabs.onUpdated.addListener((tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) => {
rewardsPanelActions.onTabRetrieved(tab, '')
rewardsPanelActions.onTabRetrieved(tab)
})

chrome.tabs.onActivated.addListener((activeInfo: chrome.tabs.TabActiveInfo) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const rewardsPanelReducer = (state: RewardsExtension.State | undefined, a
const publishers: Record<string, RewardsExtension.Publisher> = state.publishers
const publisher = publishers[id]

if (!publisher || (publisher && publisher.tabUrl !== tab.url)) {
if (!publisher || (publisher && (publisher.tabUrl !== tab.url || !publisher.publisher_key))) {
chrome.braveRewards.getPublisherData(
tab.windowId,
tab.url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class RewardsPanel extends React.Component<Props, State> {
if (!tabs || !tabs.length) {
return
}
const pollTwitchPage = (tab: chrome.tabs.Tab, tabId: number, publisherBlob: string) => {
const pollTwitchPage = (tab: chrome.tabs.Tab, tabId: number) => {
// use an interval here to monitor when the DOM has finished
// generating. clear after the data is present.
// Check every second no more than 'limit' times
Expand All @@ -73,10 +73,9 @@ export class RewardsPanel extends React.Component<Props, State> {
code: 'document.body.outerHTML'
}, function (result: string[]) {
if (result[0].includes(markupMatch) && !result[0].includes(notYetRetrievedMatch)) {
publisherBlob = result[0]
clearInterval(interval)
const rewardsPanelActions = require('../background/actions/rewardsPanelActions').default
rewardsPanelActions.onTabRetrieved(tab, publisherBlob)
rewardsPanelActions.onTabRetrieved(tab, result[0])
} else {
chrome.storage.local.get(['rewards_panel_open'], function (result) {
if (result['rewards_panel_open'] === 'false') {
Expand All @@ -90,7 +89,7 @@ export class RewardsPanel extends React.Component<Props, State> {
clearInterval(interval)

const rewardsPanelActions = require('../background/actions/rewardsPanelActions').default
rewardsPanelActions.onTabRetrieved(tab, publisherBlob)
rewardsPanelActions.onTabRetrieved(tab, '')
}
}
})
Expand All @@ -99,15 +98,14 @@ export class RewardsPanel extends React.Component<Props, State> {
}

const pollData = (tab: chrome.tabs.Tab, tabId: number, url: URL) => {
let publisherBlob = ''
if (url && url.href.startsWith('https://www.twitch.tv/')) {
chrome.storage.local.get(['rewards_panel_open'], function (result) {
if (result['rewards_panel_open'] === 'true') {
pollTwitchPage(tab, tabId, publisherBlob)
pollTwitchPage(tab, tabId)
}
})
} else {
this.props.actions.onTabRetrieved(tab, publisherBlob)
this.props.actions.onTabRetrieved(tab, '')
}
}
let tab = tabs[0]
Expand All @@ -116,10 +114,10 @@ export class RewardsPanel extends React.Component<Props, State> {
if (url && url.host.endsWith('.twitch.tv')) {
pollData(tab, tab.id, url)
} else {
this.props.actions.onTabRetrieved(tab, '')
this.props.actions.onTabRetrieved(tab)
}
} else {
this.props.actions.onTabRetrieved(tab, '')
this.props.actions.onTabRetrieved(tab)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('rewards panel reducer', () => {
state.rewardsPanelData.publishers = {
id_1: {
tabUrl: 'https://clifton.io',
publisher_key: 'clifton.io',
name: 'Clifton'
}
}
Expand All @@ -65,6 +66,7 @@ describe('rewards panel reducer', () => {
publishers: {
id_1: {
tabUrl: 'https://clifton.io',
publisher_key: 'clifton.io',
name: 'Clifton'
}
}
Expand All @@ -78,6 +80,61 @@ describe('rewards panel reducer', () => {
expect(state.rewardsPanelData).toEqual(expectedState2)
})

it('url is the same, but publisher was not saved correctly', () => {
const initState: Rewards.State = { ...defaultState, walletCreated: true }
const payload = {
tab: {
url: 'https://clifton.io',
incognito: false,
active: true,
windowId: 1
}
}

// first visit
const expectedState1: Rewards.State = {
...defaultState,
walletCreated: true,
publishers: {
id_1: {
tabUrl: 'https://clifton.io'
}
}
}

let state = reducers({ rewardsPanelData: initState }, {
type: types.ON_TAB_RETRIEVED,
payload
})

expect(state.rewardsPanelData).toEqual(expectedState1)

// imitates ON_PUBLISHER_DATA
state.rewardsPanelData.publishers = {
id_1: {
tabUrl: 'https://clifton.io'
}
}

// second visit
const expectedState2: Rewards.State = {
...defaultState,
walletCreated: true,
publishers: {
id_1: {
tabUrl: 'https://clifton.io'
}
}
}

state = reducers(state, {
type: types.ON_TAB_RETRIEVED,
payload
})

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

it('url is not the same', () => {
const initState: Rewards.State = { ...defaultState, walletCreated: true }

Expand Down Expand Up @@ -110,6 +167,7 @@ describe('rewards panel reducer', () => {
state.rewardsPanelData.publishers = {
id_1: {
tabUrl: 'clifton.io',
publisher_key: 'clifton.io',
name: 'Clifton'
}
}
Expand Down
3 changes: 2 additions & 1 deletion vendor/bat-native-ledger/include/bat/ledger/publisher_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

namespace ledger {

const char _clear_favicon[] = "clear";
const char kClearFavicon[] = "clear";
const char kIgnorePublisherBlob[] = "ignore";

LEDGER_EXPORT enum REWARDS_CATEGORY {
AUTO_CONTRIBUTE = 1 << 1, // 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,10 @@ void BatGetMedia::processTwitchMediaPanel(
const ledger::VisitData& visit_data,
const std::string& providerType,
const std::string& publisher_blob) {
if (publisher_blob == ledger::kIgnorePublisherBlob) {
return;
}

if (!publisher_blob.empty()) {
std::string media_id = getTwitchMediaIdFromUrl(visit_data, publisher_blob);
std::transform(media_id.begin(),
Expand Down Expand Up @@ -914,10 +918,6 @@ std::string BatGetMedia::getTwitchMediaIdFromUrl(
extractData(visit_data.url, "twitch.tv/", "/");
if (visit_data.url.find("twitch.tv/videos/") != std::string::npos) {
mediaId = extractData(publisher_blob,
"<a class=\"tw-interactive channel-header__user tw-align-items-center "
"tw-flex tw-flex-nowrap tw-flex-shrink-0 tw-link "
"tw-link--hover-underline-none tw-pd-r-2 tw-pd-y-05\" "
"data-target=\"channel-header__channel-link\" "
"data-a-target=\"user-channel-header-item\" href=\"/", "\"");
}
return mediaId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void BatPublishers::saveVisitInternal(
publisher_info->favicon_url = fav_icon;
}
} else {
publisher_info->favicon_url = ledger::_clear_favicon;
publisher_info->favicon_url = ledger::kClearFavicon;
}

publisher_info->name = visit_data.name;
Expand Down

0 comments on commit af2b1be

Please sign in to comment.