diff options
move notice code from metamask-controller + config-manager, in to notice-controller
Diffstat (limited to 'app')
-rw-r--r-- | app/scripts/lib/config-manager.js | 64 | ||||
-rw-r--r-- | app/scripts/metamask-controller.js | 65 | ||||
-rw-r--r-- | app/scripts/notice-controller.js | 80 |
3 files changed, 98 insertions, 111 deletions
diff --git a/app/scripts/lib/config-manager.js b/app/scripts/lib/config-manager.js index b5e4995ad..b8ffb6991 100644 --- a/app/scripts/lib/config-manager.js +++ b/app/scripts/lib/config-manager.js @@ -2,7 +2,6 @@ const Migrator = require('pojo-migrator') const MetamaskConfig = require('../config.js') const migrations = require('./migrations') const rp = require('request-promise') -const notices = require('../../../development/notices.json') const TESTNET_RPC = MetamaskConfig.network.testnet const MAINNET_RPC = MetamaskConfig.network.mainnet @@ -163,69 +162,6 @@ ConfigManager.prototype.setData = function (data) { } // -// Notices -// - -ConfigManager.prototype.getNoticesList = function () { - var data = this.getData() - if ('noticesList' in data) { - return data.noticesList - } else { - return [] - } -} - -ConfigManager.prototype.setNoticesList = function (list) { - var data = this.getData() - data.noticesList = list - this.setData(data) - return Promise.resolve(true) -} - -ConfigManager.prototype.markNoticeRead = function (notice) { - var notices = this.getNoticesList() - var id = notice.id - notices[id].read = true - this.setNoticesList(notices) -} - -ConfigManager.prototype.updateNoticesList = function () { - return this._retrieveNoticeData().then((newNotices) => { - var oldNotices = this.getNoticesList() - var combinedNotices = this._mergeNotices(oldNotices, newNotices) - return Promise.resolve(this.setNoticesList(combinedNotices)) - }) -} - -ConfigManager.prototype.getLatestUnreadNotice = function () { - var notices = this.getNoticesList() - var filteredNotices = notices.filter((notice) => { - return notice.read === false - }) - return filteredNotices[filteredNotices.length - 1] -} - -ConfigManager.prototype._mergeNotices = function (oldNotices, newNotices) { - var noticeMap = this._mapNoticeIds(oldNotices) - newNotices.forEach((notice) => { - if (noticeMap.indexOf(notice.id) === -1) { - oldNotices.push(notice) - } - }) - return oldNotices -} - -ConfigManager.prototype._mapNoticeIds = function (notices) { - return notices.map((notice) => notice.id) -} - -ConfigManager.prototype._retrieveNoticeData = function () { - // Placeholder for the API. - return Promise.resolve(notices) -} - - -// // Tx // diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index b7e37b3a5..1477ce9e7 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -18,9 +18,13 @@ module.exports = class MetamaskController { this.idStore = new IdentityStore({ configManager: this.configManager, }) + // notices this.noticeController = new NoticeController({ configManager: this.configManager, }) + this.noticeController.updateNoticesList() + // to be uncommented when retrieving notices from a remote server. + // this.noticeController.startPolling() this.provider = this.initializeProvider(opts) this.ethStore = new EthStore(this.provider) this.idStore.setStore(this.ethStore) @@ -31,13 +35,9 @@ module.exports = class MetamaskController { this.configManager.setCurrentFiat(currentFiat) this.configManager.updateConversionRate() - this.checkNotices() this.checkTOSChange() this.scheduleConversionInterval() - - // to be uncommented when retrieving notices from a remote server. - // this.scheduleNoticeCheck() } getState () { @@ -51,6 +51,7 @@ module.exports = class MetamaskController { getApi () { const idStore = this.idStore + const noticeController = this.noticeController return { getState: (cb) => { cb(null, this.getState()) }, @@ -63,7 +64,6 @@ module.exports = class MetamaskController { agreeToEthWarning: this.agreeToEthWarning.bind(this), setTOSHash: this.setTOSHash.bind(this), checkTOSChange: this.checkTOSChange.bind(this), - checkNotices: this.checkNotices.bind(this), setGasMultiplier: this.setGasMultiplier.bind(this), // forward directly to idStore @@ -87,7 +87,8 @@ module.exports = class MetamaskController { // shapeshift createShapeShiftTx: this.createShapeShiftTx.bind(this), // notices - markNoticeRead: this.markNoticeRead.bind(this), + checkNotices: noticeController.updateNoticesList.bind(noticeController), + markNoticeRead: noticeController.markNoticeRead.bind(noticeController), } } @@ -282,7 +283,7 @@ module.exports = class MetamaskController { setTOSHash (hash) { try { this.configManager.setTOSHash(hash) - } catch (e) { + } catch (err) { console.error('Error in setting terms of service hash.') } } @@ -294,56 +295,28 @@ module.exports = class MetamaskController { this.resetDisclaimer() this.setTOSHash(global.TOS_HASH) } - } catch (e) { + } catch (err) { console.error('Error in checking TOS change.') } } - // notice - - markNoticeRead (notice, cb) { - try { - this.configManager.markNoticeRead(notice) - cb(null, this.configManager.getLatestUnreadNotice()) - } catch (e) { - cb(e) - } - } - - checkNotices () { - try { - this.configManager.updateNoticesList() - } catch (e) { - console.error('Error in checking notices.') - } - } - - scheduleNoticeCheck () { - if (this.noticeCheck) { - clearInterval(this.noticeCheck) - } - this.noticeCheck = setInterval(() => { - this.configManager.updateNoticesList() - }, 300000) - } - // disclaimer agreeToDisclaimer (cb) { try { this.configManager.setConfirmed(true) cb() - } catch (e) { - cb(e) + } catch (err) { + cb(err) } } resetDisclaimer () { try { this.configManager.setConfirmed(false) - } catch (e) { - console.error(e) + } catch (err) { + console.error(err) } } @@ -358,8 +331,8 @@ module.exports = class MetamaskController { conversionDate: this.configManager.getConversionDate(), } cb(data) - } catch (e) { - cb(null, e) + } catch (err) { + cb(null, err) } } @@ -376,8 +349,8 @@ module.exports = class MetamaskController { try { this.configManager.setShouldntShowWarning() cb() - } catch (e) { - cb(e) + } catch (err) { + cb(err) } } @@ -422,8 +395,8 @@ module.exports = class MetamaskController { try { this.configManager.setGasMultiplier(gasMultiplier) cb() - } catch (e) { - cb(e) + } catch (err) { + cb(err) } } } diff --git a/app/scripts/notice-controller.js b/app/scripts/notice-controller.js index f1785d705..438f5c27e 100644 --- a/app/scripts/notice-controller.js +++ b/app/scripts/notice-controller.js @@ -1,18 +1,96 @@ const EventEmitter = require('events').EventEmitter +const hardCodedNotices = require('../../development/notices.json') module.exports = class NoticeController extends EventEmitter { constructor (opts) { super() this.configManager = opts.configManager + this.noticePoller = null } getState() { - var lastUnreadNotice = this.configManager.getLatestUnreadNotice() + var lastUnreadNotice = this.getLatestUnreadNotice() return { lastUnreadNotice: lastUnreadNotice, noActiveNotices: !lastUnreadNotice, } } + + getNoticesList() { + var data = this.configManager.getData() + if ('noticesList' in data) { + return data.noticesList + } else { + return [] + } + } + + setNoticesList(list) { + var data = this.configManager.getData() + data.noticesList = list + this.configManager.setData(data) + return Promise.resolve(true) + } + + markNoticeRead(notice, cb) { + cb = cb || function(err){ if (err) throw err } + try { + var notices = this.getNoticesList() + var id = notice.id + notices[id].read = true + this.setNoticesList(notices) + let latestNotice = this.getLatestUnreadNotice() + cb(null, latestNotice) + } catch (err) { + cb(err) + } + } + + updateNoticesList() { + return this._retrieveNoticeData().then((newNotices) => { + var oldNotices = this.getNoticesList() + var combinedNotices = this._mergeNotices(oldNotices, newNotices) + return Promise.resolve(this.setNoticesList(combinedNotices)) + }) + } + + getLatestUnreadNotice() { + var notices = this.getNoticesList() + var filteredNotices = notices.filter((notice) => { + return notice.read === false + }) + return filteredNotices[filteredNotices.length - 1] + } + + startPolling () { + if (this.noticePoller) { + clearInterval(this.noticePoller) + } + this.noticePoller = setInterval(() => { + this.noticeController.updateNoticesList() + }, 300000) + } + + _mergeNotices(oldNotices, newNotices) { + var noticeMap = this._mapNoticeIds(oldNotices) + newNotices.forEach((notice) => { + if (noticeMap.indexOf(notice.id) === -1) { + oldNotices.push(notice) + } + }) + return oldNotices + } + + _mapNoticeIds(notices) { + return notices.map((notice) => notice.id) + } + + _retrieveNoticeData() { + // Placeholder for the API. + return Promise.resolve(hardCodedNotices) + } + + } |