Skip to content

Commit

Permalink
Merge pull request #888 from jasonrsadler/twitch-panel-tip-core
Browse files Browse the repository at this point in the history
Panel, tipping, and favicon should now be working correctly for twitch publishers
  • Loading branch information
Jason Sadler authored Jan 22, 2019
2 parents cdfba01 + 95d5f9e commit cbb3d57
Show file tree
Hide file tree
Showing 22 changed files with 469 additions and 121 deletions.
3 changes: 2 additions & 1 deletion browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ ExtensionFunction::ResponseAction BraveRewardsGetPublisherDataFunction::Run() {
if (rewards_service_) {
rewards_service_->GetPublisherActivityFromUrl(params->window_id,
params->url,
params->favicon_url);
params->favicon_url,
params->publisher_blob);
}
return RespondNow(NoArguments());
}
Expand Down
4 changes: 4 additions & 0 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@
{
"name": "faviconUrl",
"type": "string"
},
{
"name": "publisherBlob",
"type": "string"
}
]
},
Expand Down
6 changes: 5 additions & 1 deletion components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ class RewardsService : public KeyedService {
const GetAllBalanceReportsCallback& callback) = 0;
virtual void GetCurrentBalanceReport() = 0;
virtual void IsWalletCreated(const IsWalletCreatedCallback& callback) = 0;
virtual void GetPublisherActivityFromUrl(uint64_t windowId, const std::string& url, const std::string& favicon_url) = 0;
virtual void GetPublisherActivityFromUrl(
uint64_t windowId,
const std::string& url,
const std::string& favicon_url,
const std::string& publisher_blob) = 0;
virtual void GetContributionAmount(
const GetContributionAmountCallback& callback) = 0;
virtual void GetPublisherBanner(const std::string& publisher_id) = 0;
Expand Down
11 changes: 7 additions & 4 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1628,9 +1628,11 @@ void RewardsServiceImpl::IsWalletCreated(
bat_ledger_->IsWalletCreated(callback);
}

void RewardsServiceImpl::GetPublisherActivityFromUrl(uint64_t windowId,
const std::string& url,
const std::string& favicon_url) {
void RewardsServiceImpl::GetPublisherActivityFromUrl(
uint64_t windowId,
const std::string& url,
const std::string& favicon_url,
const std::string& publisher_blob) {
GURL parsedUrl(url);

if (!parsedUrl.is_valid()) {
Expand Down Expand Up @@ -1660,7 +1662,8 @@ void RewardsServiceImpl::GetPublisherActivityFromUrl(uint64_t windowId,
visitData.url = origin.spec();
visitData.favicon_url = favicon_url;

bat_ledger_->GetPublisherActivityFromUrl(windowId, visitData.ToJson());
bat_ledger_->GetPublisherActivityFromUrl(
windowId, visitData.ToJson(), publisher_blob);
}

void RewardsServiceImpl::OnExcludedSitesChanged(const std::string& publisher_id) {
Expand Down
6 changes: 5 additions & 1 deletion components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ class RewardsServiceImpl : public RewardsService,
const GetAllBalanceReportsCallback& callback) override;
void GetCurrentBalanceReport() override;
void IsWalletCreated(const IsWalletCreatedCallback& callback) override;
void GetPublisherActivityFromUrl(uint64_t windowId, const std::string& url, const std::string& favicon_url) override;
void GetPublisherActivityFromUrl(
uint64_t windowId,
const std::string& url,
const std::string& favicon_url,
const std::string& publisher_blob) override;
void GetContributionAmount(const GetContributionAmountCallback& callback) override;
void GetPublisherBanner(const std::string& publisher_id) override;
void OnPublisherBanner(std::unique_ptr<ledger::PublisherBanner> banner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ export const onTabId = (tabId: number | undefined) => action(types.ON_TAB_ID, {
tabId
})

export const onTabRetrieved = (tab: chrome.tabs.Tab) => action(types.ON_TAB_RETRIEVED, {
tab
export const onTabRetrieved = (tab: chrome.tabs.Tab, publisherBlob: string | undefined) => action(types.ON_TAB_RETRIEVED, {
tab,
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 @@ -46,12 +46,23 @@ chrome.runtime.onInstalled.addListener(function (details) {
})

chrome.runtime.onStartup.addListener(function () {
chrome.storage.local.get(['is_dismissed'], function (result) {
if (result && result['is_dismissed'] === 'false') {
chrome.browserAction.setBadgeText({
text: '1'
chrome.runtime.onConnect.addListener(function (externalPort) {
chrome.storage.local.set({
'rewards_panel_open': 'true'
})

chrome.storage.local.get(['is_dismissed'], function (result) {
if (result && result['is_dismissed'] === 'false') {
chrome.browserAction.setBadgeText({
text: '1'
})
}
})
externalPort.onDisconnect.addListener(function () {
chrome.storage.local.set({
'rewards_panel_open': 'false'
})
}
})
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import rewardsPanelActions from '../actions/rewardsPanelActions'

chrome.tabs.onCreated.addListener((tab: chrome.tabs.Tab) => {
rewardsPanelActions.onTabRetrieved(tab)
rewardsPanelActions.onTabRetrieved(tab, '')
})

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 id = getWindowId(tab.windowId)
const publishers: Record<string, RewardsExtension.Publisher> = state.publishers
const publisher = publishers[id]
chrome.braveRewards.getPublisherData(tab.windowId, tab.url, tab.favIconUrl || '')
chrome.braveRewards.getPublisherData(tab.windowId, tab.url, tab.favIconUrl || '', payload.publisherBlob || '')
if (!publisher || (publisher && publisher.tabUrl !== tab.url)) {
if (publisher) {
delete publishers[id]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,72 @@ export class RewardsPanel extends React.Component<Props, State> {
if (!tabs || !tabs.length) {
return
}
this.props.actions.onTabRetrieved(tabs[0])
const pollTwitchPage = (tab: chrome.tabs.Tab, tabId: number, publisherBlob: string) => {
// 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'
let itr = 0
const limit = 10
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]
clearInterval(interval)
const rewardsPanelActions = require('../background/actions/rewardsPanelActions').default
rewardsPanelActions.onTabRetrieved(tab, publisherBlob)
} else {
chrome.storage.local.get(['rewards_panel_open'], function (result) {
if (result['rewards_panel_open'] === 'false') {
// panel was closed. give up
clearInterval(interval)
}
})
itr++
if (itr === limit) {
// give up
clearInterval(interval)

const rewardsPanelActions = require('../background/actions/rewardsPanelActions').default
rewardsPanelActions.onTabRetrieved(tab, publisherBlob)
}
}
})
}
poll()
}

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)
}
})
} else {
this.props.actions.onTabRetrieved(tab, publisherBlob)
}
}
let tab = tabs[0]
if (tab.url && tab.id) {
let url = new URL(tab.url)
if (url && url.host.endsWith('.twitch.tv')) {
pollData(tab, tab.id, url)
} else {
this.props.actions.onTabRetrieved(tab)
}
} else {
this.props.actions.onTabRetrieved(tabs[0])
}
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"permissions": [
"storage",
"tabs",
"chrome://favicon/*"
"chrome://favicon/*",
"https://www.twitch.tv/*"
],
"browser_action": {
"default_popup": "brave_rewards_panel.html",
Expand Down
2 changes: 1 addition & 1 deletion components/definitions/chromel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ declare namespace chrome.dns {
declare namespace chrome.braveRewards {
const createWallet: () => {}
const donateToSite: (tabId: number, publisherKey: string) => {}
const getPublisherData: (windowId: number, url: string, faviconUrl: string) => {}
const getPublisherData: (windowId: number, url: string, faviconUrl: string, publisherBlob: string | undefined) => {}
const getWalletProperties: () => {}
const getCurrentReport: () => {}
const onWalletCreated: {
Expand Down
8 changes: 5 additions & 3 deletions components/services/bat_ledger/bat_ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,13 @@ void BatLedgerImpl::IsWalletCreated(IsWalletCreatedCallback callback) {
std::move(callback).Run(ledger_->IsWalletCreated());
}

void BatLedgerImpl::GetPublisherActivityFromUrl(uint64_t window_id,
const std::string& visit_data) {
void BatLedgerImpl::GetPublisherActivityFromUrl(
uint64_t window_id,
const std::string& visit_data,
const std::string& publisher_blob) {
ledger::VisitData visitData;
if (visitData.loadFromJson(visit_data))
ledger_->GetPublisherActivityFromUrl(window_id, visitData);
ledger_->GetPublisherActivityFromUrl(window_id, visitData, publisher_blob);
}

// static
Expand Down
6 changes: 4 additions & 2 deletions components/services/bat_ledger/bat_ledger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ class BatLedgerImpl : public mojom::BatLedger,

void IsWalletCreated(IsWalletCreatedCallback callback) override;

void GetPublisherActivityFromUrl(uint64_t window_id,
const std::string& visit_data) override;
void GetPublisherActivityFromUrl(
uint64_t window_id,
const std::string& visit_data,
const std::string& publisher_blob) override;

void GetContributionAmount(
GetContributionAmountCallback callback) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ interface BatLedger {

IsWalletCreated() => (bool wallet_created);

GetPublisherActivityFromUrl(uint64 window_id, string visit_data);
GetPublisherActivityFromUrl(uint64 window_id, string visit_data,
string publisher_blob);
GetContributionAmount() => (double contribution_amount);
GetPublisherBanner(string publisher_id) => (string banner);

Expand Down
5 changes: 4 additions & 1 deletion vendor/bat-native-ledger/include/bat/ledger/ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ class LEDGER_EXPORT Ledger {
const ledger::PUBLISHER_EXCLUDE& exclude, uint64_t windowId) = 0;
virtual void RestorePublishers() = 0;
virtual bool IsWalletCreated() const = 0;
virtual void GetPublisherActivityFromUrl(uint64_t windowId, const ledger::VisitData& visit_data) = 0;
virtual void GetPublisherActivityFromUrl(
uint64_t windowId,
const ledger::VisitData& visit_data,
const std::string& publisher_blob) = 0;
virtual void SetBalanceReportItem(ACTIVITY_MONTH month,
int year,
ledger::ReportType type,
Expand Down
Loading

0 comments on commit cbb3d57

Please sign in to comment.