Skip to content

Commit

Permalink
Fixes twitch panel
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Apr 1, 2019
1 parent 55b3a28 commit b3229c5
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 30 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 @@ -62,28 +62,24 @@ 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
// clear the interval if panel closes

const markupMatch = '<figure class=\"tw-avatar tw-avatar--size-36\">' +
'<div class=\"tw-border-radius-medium tw-overflow-hidden\">' +
'<img class=\"tw-avatar__img tw-image\" alt=\"'
const notYetRetrievedMatch = 'https://static-cdn.jtvnw.net/jtv_user_pictures/xarth/404_user_70x70.png'
const markupMatch = 'channel-header__channel-link'
let itr = 0
const limit = 10
const limit = 5
let interval = setInterval(poll, 1000)
function poll () {
chrome.tabs.executeScript(tabId, {
code: 'document.body.outerHTML'
}, function (result: string[]) {
if (result[0].includes(markupMatch) && !result[0].includes(notYetRetrievedMatch)) {
publisherBlob = result[0]
if (result[0].includes(markupMatch)) {
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 @@ -97,7 +93,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 @@ -106,15 +102,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 @@ -123,10 +118,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 @@ -926,6 +926,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 @@ -966,10 +970,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 @@ -168,7 +168,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 b3229c5

Please sign in to comment.