Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #14134 from NejcZdovc/fix/#10767-torrent
Browse files Browse the repository at this point in the history
Fixes disabled torrent viewer torrent download
  • Loading branch information
bsclifton committed May 21, 2018
2 parents ea409ce + a4584cf commit 9f02082
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
21 changes: 20 additions & 1 deletion app/common/state/extensionState.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

const { makeImmutable } = require('./immutableUtil')
const Immutable = require('immutable')

// Constants
const config = require('../../../js/constants/config')
const settings = require('../../../js/constants/settings')

// Utils
const { makeImmutable } = require('./immutableUtil')
const platformUtil = require('../lib/platformUtil')
const getSetting = require('../../../js/settings').getSetting
const {chromeUrl} = require('../../../js/lib/appUrlUtil')

const browserActionDefaults = Immutable.fromJS({
Expand Down Expand Up @@ -205,6 +212,18 @@ const extensionState = {
})
})
return allProperties
},

isWebTorrentEnabled: (state) => {
if (state == null) {
return false
}

const settingsState = state.get('settings')
const extension = extensionState.getExtensionById(state, config.torrentExtensionId)
const extensionEnabled = extension != null ? extension.get('enabled') : false
const torrentEnabled = getSetting(settings.TORRENT_VIEWER_ENABLED, settingsState)
return extensionEnabled && torrentEnabled
}
}

Expand Down
5 changes: 1 addition & 4 deletions app/filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,10 +786,7 @@ module.exports.isResourceEnabled = (resourceName, url, isPrivate) => {
}

if (resourceName === 'webtorrent') {
const extension = extensionState.getExtensionById(appState, config.torrentExtensionId)
const torrentEnabled = getSetting(settings.TORRENT_VIEWER_ENABLED, settingsState)
const extensionEnabled = extension !== undefined ? extension.get('enabled') : false
return extensionEnabled && torrentEnabled
return extensionState.isWebTorrentEnabled(appState)
}

if (resourceName === 'ledger') {
Expand Down
4 changes: 3 additions & 1 deletion js/stores/appStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ const handleAppAction = (action) => {
// TODO(bridiver) - these should be refactored into reducers
appState = filtering.init(appState, action, appStore)
appState = basicAuth.init(appState, action, appStore)
appState = webtorrent.init(appState, action, appStore)
if (extensionState.isWebTorrentEnabled(appState)) {
appState = webtorrent.init(appState, action, appStore)
}
appState = profiles.init(appState, action, appStore)
appState = require('../../app/sync').init(appState, action, appStore)
calculateTopSites(true, true)
Expand Down
46 changes: 46 additions & 0 deletions test/unit/app/common/state/extensionStateTest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* global describe, it, before */
const extensionState = require('../../../../../app/common/state/extensionState')
const config = require('../../../../../js/constants/config')
const settings = require('../../../../../js/constants/settings')
const Immutable = require('immutable')
const assert = require('assert')

Expand Down Expand Up @@ -682,4 +684,48 @@ describe('extensionState', function () {
commonTests()
})
})

describe('isWebTorrentEnabled', function () {
const torrentId = config.torrentExtensionId

it('null case', function () {
const result = extensionState.isWebTorrentEnabled()
assert.equal(result, false)
})

it('empty state', function () {
const result = extensionState.isWebTorrentEnabled(defaultAppState)
assert.equal(result, false)
})

it('extension is disabled', function () {
const state = defaultAppState
.setIn(['extensions', torrentId], Immutable.fromJS({
enabled: false
}))
.setIn(['settings', settings.TORRENT_VIEWER_ENABLED], true)
const result = extensionState.isWebTorrentEnabled(state)
assert.equal(result, false)
})

it('torrent is disabled', function () {
const state = defaultAppState
.setIn(['extensions', torrentId], Immutable.fromJS({
enabled: true
}))
.setIn(['settings', settings.TORRENT_VIEWER_ENABLED], false)
const result = extensionState.isWebTorrentEnabled(state)
assert.equal(result, false)
})

it('everything is enabled', function () {
const state = defaultAppState
.setIn(['extensions', torrentId], Immutable.fromJS({
enabled: true
}))
.setIn(['settings', settings.TORRENT_VIEWER_ENABLED], true)
const result = extensionState.isWebTorrentEnabled(state)
assert.equal(result, true)
})
})
})

0 comments on commit 9f02082

Please sign in to comment.