From 8819475a2ed2ee7c34e983ebb5ab12661be1a961 Mon Sep 17 00:00:00 2001 From: Kevin Serrano Date: Wed, 7 Dec 2016 14:34:15 -0800 Subject: Add ability to show notices to user & get confirmation. Implement generation of markdown for notice files. Create npm command. Enhance notice generation. Add test files to test multiple notices. Add basic markdown support to notices. Interval checks for updates. Add extensionizer and linker Add terms and conditions state file Add link support to disclaimer. Changelog addition. --- app/scripts/lib/config-manager.js | 64 ++++++++++++++++++++++++++++++++++++++ app/scripts/metamask-controller.js | 42 +++++++++++++++++++++++-- app/scripts/notice-controller.js | 18 +++++++++++ 3 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 app/scripts/notice-controller.js (limited to 'app/scripts') diff --git a/app/scripts/lib/config-manager.js b/app/scripts/lib/config-manager.js index b8ffb6991..b5e4995ad 100644 --- a/app/scripts/lib/config-manager.js +++ b/app/scripts/lib/config-manager.js @@ -2,6 +2,7 @@ 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 @@ -161,6 +162,69 @@ ConfigManager.prototype.setData = function (data) { this.migrator.saveData(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 631411bed..65619af82 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,9 @@ module.exports = class MetamaskController { this.idStore = new IdentityStore({ configManager: this.configManager, }) + this.noticeController = new NoticeController({ + configManager: this.configManager, + }) this.provider = this.initializeProvider(opts) this.ethStore = new EthStore(this.provider) this.idStore.setStore(this.ethStore) @@ -27,17 +31,19 @@ module.exports = class MetamaskController { this.configManager.setCurrentFiat(currentFiat) this.configManager.updateConversionRate() + this.checkNotices() this.checkTOSChange() this.scheduleConversionInterval() - + this.scheduleNoticeCheck() } getState () { return extend( this.ethStore.getState(), this.idStore.getState(), - this.configManager.getConfig() + this.configManager.getConfig(), + this.noticeController.getState() ) } @@ -55,6 +61,7 @@ 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 @@ -77,6 +84,8 @@ module.exports = class MetamaskController { buyEth: this.buyEth.bind(this), // shapeshift createShapeShiftTx: this.createShapeShiftTx.bind(this), + // notices + markNoticeRead: this.markNoticeRead.bind(this), } } @@ -289,6 +298,25 @@ module.exports = class MetamaskController { } + checkNotices () { + try { + this.configManager.updateNoticesList() + } catch (e) { + console.error('Error in checking notices.') + } + } + + // notice + + markNoticeRead (notice, cb) { + try { + this.configManager.markNoticeRead(notice) + cb(null, this.configManager.getLatestUnreadNotice()) + } catch (e) { + cb(e) + } + } + agreeToDisclaimer (cb) { try { this.configManager.setConfirmed(true) @@ -331,6 +359,7 @@ module.exports = class MetamaskController { }, 300000) } +<<<<<<< HEAD agreeToEthWarning (cb) { try { this.configManager.setShouldntShowWarning() @@ -338,6 +367,15 @@ module.exports = class MetamaskController { } catch (e) { cb(e) } +======= + scheduleNoticeCheck () { + if (this.noticeCheck) { + clearInterval(this.noticeCheck) + } + this.noticeCheck = setInterval(() => { + this.configManager.updateNoticesList() + }, 300000) +>>>>>>> 25acad7... Add ability to show notices to user & get confirmation. } // called from popup diff --git a/app/scripts/notice-controller.js b/app/scripts/notice-controller.js new file mode 100644 index 000000000..f1785d705 --- /dev/null +++ b/app/scripts/notice-controller.js @@ -0,0 +1,18 @@ +const EventEmitter = require('events').EventEmitter + +module.exports = class NoticeController extends EventEmitter { + + constructor (opts) { + super() + this.configManager = opts.configManager + } + + getState() { + var lastUnreadNotice = this.configManager.getLatestUnreadNotice() + + return { + lastUnreadNotice: lastUnreadNotice, + noActiveNotices: !lastUnreadNotice, + } + } +} -- cgit v1.2.3