Skip to content

Commit

Permalink
Fixes twitch, updated to the new data
Browse files Browse the repository at this point in the history
Resolves brave#13597

Auditors:

Test Plan:
  • Loading branch information
NejcZdovc committed Mar 26, 2018
1 parent 68ecb38 commit 30846e0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
5 changes: 3 additions & 2 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2584,12 +2584,13 @@ const deleteSynopsis = () => {
}

let currentMediaKey = null
const onMediaRequest = (state, xhr, type, tabId) => {
const onMediaRequest = (state, xhr, type, details) => {
if (!xhr || type == null) {
return state
}

const parsed = ledgerUtil.getMediaData(xhr, type)
const tabId = details.get('tabId')
const parsed = ledgerUtil.getMediaData(xhr, type, details)
const mediaId = ledgerUtil.getMediaId(parsed, type)

if (clientOptions.loggingP) {
Expand Down
2 changes: 1 addition & 1 deletion app/browser/reducers/ledgerReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ const ledgerReducer = (state, action, immutableAction) => {
}
case appConstants.APP_ON_LEDGER_MEDIA_DATA:
{
state = ledgerApi.onMediaRequest(state, action.get('url'), action.get('type'), action.get('tabId'))
state = ledgerApi.onMediaRequest(state, action.get('url'), action.get('type'), action.get('details'))
break
}
case appConstants.APP_ON_PRUNE_SYNOPSIS:
Expand Down
39 changes: 32 additions & 7 deletions app/common/lib/ledgerUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ const getMediaId = (data, type) => {
}
case ledgerMediaProviders.TWITCH:
{
console.log(data.event)
if (
Object.values(twitchEvents).includes(data.event) &&
data.properties
Expand All @@ -252,7 +253,7 @@ const getMediaKey = (id, type) => {
return `${type.toLowerCase()}_${id}`
}

const getMediaData = (xhr, type) => {
const getMediaData = (xhr, type, details) => {
let result = null

if (xhr == null || type == null) {
Expand All @@ -262,20 +263,32 @@ const getMediaData = (xhr, type) => {
const parsedUrl = urlParse(xhr)
const query = parsedUrl && parsedUrl.query

if (!parsedUrl || !query) {
if (!parsedUrl) {
return null
}

switch (type) {
case ledgerMediaProviders.YOUTUBE:
{
result = queryString.parse(parsedUrl.query)
if (!query) {
return null
}

result = queryString.parse(query)
break
}
case ledgerMediaProviders.TWITCH:
{
result = queryString.parse(parsedUrl.query)
if (!result.data) {
const data = details.getIn(['uploadData', 0, 'bytes'])
if (!data) {
result = null
break
}

let params = Buffer.from(data).toString('utf8')
result = queryString.parse(params)

if (!result || !result.data) {
result = null
break
}
Expand All @@ -286,12 +299,22 @@ const getMediaData = (xhr, type) => {
break
}

let parsed
try {
result = JSON.parse(obj)
parsed = JSON.parse(obj)
} catch (error) {
result = null
console.error(error.toString())
break
}

if (!Array.isArray(parsed)) {
result = null
break
}

result = parsed[0]

break
}
}
Expand Down Expand Up @@ -424,6 +447,8 @@ const getTwitchDuration = (state, data, mediaKey) => {
return 0
}

if (data.event === twitchEvents.START) debugger

const previousData = ledgerVideoCache.getDataByVideoId(state, mediaKey)
const oldEvent = previousData.get('event')
const twitchMinimumSeconds = 10
Expand Down Expand Up @@ -524,7 +549,7 @@ const getMediaProvider = (url, firstPartyUrl, referrer) => {
(firstPartyUrl && firstPartyUrl.startsWith('https://www.twitch.tv/')) ||
(referrer && referrer.startsWith('https://player.twitch.tv/'))
) &&
url.startsWith('https://api.mixpanel.com/')
url.includes('ttvnw.net/v1/segment')
) {
return ledgerMediaProviders.TWITCH
}
Expand Down
2 changes: 1 addition & 1 deletion app/filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function registerForBeforeRequest (session, partition) {
// Ledger media
const provider = ledgerUtil.getMediaProvider(url, firstPartyUrl, details.referrer)
if (provider) {
appActions.onLedgerMediaData(url, provider, details.tabId)
appActions.onLedgerMediaData(url, provider, details)
}
}

Expand Down
4 changes: 2 additions & 2 deletions js/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1897,12 +1897,12 @@ const appActions = {
})
},

onLedgerMediaData: function (url, type, tabId) {
onLedgerMediaData: function (url, type, details) {
dispatch({
actionType: appConstants.APP_ON_LEDGER_MEDIA_DATA,
url,
type,
tabId
details
})
},

Expand Down

0 comments on commit 30846e0

Please sign in to comment.