diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/scripts/metamask-controller.js | 42 | ||||
-rw-r--r-- | app/scripts/notice-controller.js | 96 |
2 files changed, 124 insertions, 14 deletions
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 631411bed..1477ce9e7 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -2,6 +2,7 @@ const extend = require('xtend') const EthStore = require('eth-store') const MetaMaskProvider = require('web3-provider-engine/zero.js') const IdentityStore = require('./lib/idStore') +const NoticeController = require('./notice-controller') const messageManager = require('./lib/message-manager') const HostStore = require('./lib/remote-store.js').HostStore const Web3 = require('web3') @@ -17,6 +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) @@ -30,19 +38,20 @@ module.exports = class MetamaskController { this.checkTOSChange() this.scheduleConversionInterval() - } getState () { return extend( this.ethStore.getState(), this.idStore.getState(), - this.configManager.getConfig() + this.configManager.getConfig(), + this.noticeController.getState() ) } getApi () { const idStore = this.idStore + const noticeController = this.noticeController return { getState: (cb) => { cb(null, this.getState()) }, @@ -77,6 +86,9 @@ module.exports = class MetamaskController { buyEth: this.buyEth.bind(this), // shapeshift createShapeShiftTx: this.createShapeShiftTx.bind(this), + // notices + checkNotices: noticeController.updateNoticesList.bind(noticeController), + markNoticeRead: noticeController.markNoticeRead.bind(noticeController), } } @@ -271,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.') } } @@ -283,26 +295,28 @@ module.exports = class MetamaskController { this.resetDisclaimer() this.setTOSHash(global.TOS_HASH) } - } catch (e) { + } catch (err) { console.error('Error in checking TOS change.') } } + // 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) } } @@ -317,8 +331,8 @@ module.exports = class MetamaskController { conversionDate: this.configManager.getConversionDate(), } cb(data) - } catch (e) { - cb(null, e) + } catch (err) { + cb(null, err) } } @@ -335,8 +349,8 @@ module.exports = class MetamaskController { try { this.configManager.setShouldntShowWarning() cb() - } catch (e) { - cb(e) + } catch (err) { + cb(err) } } @@ -381,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 new file mode 100644 index 000000000..438f5c27e --- /dev/null +++ b/app/scripts/notice-controller.js @@ -0,0 +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.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) + } + + +} |