Skip to content

Commit

Permalink
Fixes twitch panel
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Jun 12, 2019
1 parent de3f6d3 commit b611413
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,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 @@ -257,7 +257,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,10 @@ export const onTabId = (tabId: number | undefined) => action(types.ON_TAB_ID, {
tabId
})

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

export const onPublisherData = (windowId: number, publisher: RewardsExtension.Publisher) => action(types.ON_PUBLISHER_DATA, {
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 @@ -6,7 +6,7 @@ import rewardsPanelActions from '../actions/rewardsPanelActions'

chrome.tabs.onUpdated.addListener((tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) => {
const activeTabIsLoadingTriggered = Boolean(tab.active && changeInfo.status === 'loading')
rewardsPanelActions.onTabRetrieved(tab, '', activeTabIsLoadingTriggered)
rewardsPanelActions.onTabRetrieved(tab, activeTabIsLoadingTriggered)
})

chrome.tabs.onActivated.addListener((activeInfo: chrome.tabs.TabActiveInfo) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,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, false, result[0])
} else {
chrome.storage.local.get(['rewards_panel_open'], function (result) {
if (result['rewards_panel_open'] === 'false') {
Expand All @@ -106,7 +102,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 @@ -115,15 +111,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 @@ -132,10 +127,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 @@ -59,8 +59,8 @@ describe('rewards panel reducer', () => {
state.rewardsPanelData.publishers = {
id_1: {
tabUrl: 'https://clifton.io',
name: 'Clifton',
publisher_key: 'clifton.io'
publisher_key: 'clifton.io',
name: 'Clifton'
}
}

Expand All @@ -72,8 +72,63 @@ describe('rewards panel reducer', () => {
publishers: {
id_1: {
tabUrl: 'https://clifton.io',
name: 'Clifton',
publisher_key: 'clifton.io'
publisher_key: 'clifton.io',
name: 'Clifton'
}
}
}

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

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'
}
}
}
Expand Down Expand Up @@ -122,9 +177,9 @@ describe('rewards panel reducer', () => {
// imitates ON_PUBLISHER_DATA
state.rewardsPanelData.publishers = {
id_1: {
tabUrl: 'https://clifton.io',
name: 'Clifton',
publisher_key: 'clifton.io'
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 @@ -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 Expand Up @@ -219,7 +219,7 @@ void BatPublishers::saveVisitInternal(
}

if (panel_info && window_id > 0) {
if (panel_info->favicon_url == ledger::_clear_favicon) {
if (panel_info->favicon_url == ledger::kClearFavicon) {
panel_info->favicon_url = std::string();
}

Expand Down
Loading

0 comments on commit b611413

Please sign in to comment.