Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes twitch panel #2618

Merged
merged 1 commit into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -260,7 +260,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 @@ -74,28 +74,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 @@ -109,7 +105,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 @@ -118,15 +114,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 @@ -135,10 +130,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 @@ -20,7 +20,8 @@ using PublisherInfo = mojom::PublisherInfo;
using PublisherInfoPtr = mojom::PublisherInfoPtr;
using PublisherInfoList = std::vector<PublisherInfoPtr>;

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 @@ -175,7 +175,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 @@ -228,7 +228,7 @@ void BatPublishers::saveVisitInternal(
}

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

Expand Down
Loading