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

Fixes disabled torrent viewer torrent download #14134

Merged
merged 1 commit into from
May 21, 2018
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
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)
})
})
})