diff options
author | Thomas Huang <tmashuang@users.noreply.github.com> | 2017-11-30 07:25:10 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-30 07:25:10 +0800 |
commit | c30b543a8069c3925bb254716699752e38eaf97a (patch) | |
tree | d9185abffcb5adfecdd99324b0e2e1e4f1e2b586 /app/scripts | |
parent | 23a5ea9321e4046338323e9df9e64d230d057df1 (diff) | |
parent | 91817573c95f2a1b234ab9fb7e39ac15ad44741d (diff) | |
download | tangerine-wallet-browser-c30b543a8069c3925bb254716699752e38eaf97a.tar tangerine-wallet-browser-c30b543a8069c3925bb254716699752e38eaf97a.tar.gz tangerine-wallet-browser-c30b543a8069c3925bb254716699752e38eaf97a.tar.bz2 tangerine-wallet-browser-c30b543a8069c3925bb254716699752e38eaf97a.tar.lz tangerine-wallet-browser-c30b543a8069c3925bb254716699752e38eaf97a.tar.xz tangerine-wallet-browser-c30b543a8069c3925bb254716699752e38eaf97a.tar.zst tangerine-wallet-browser-c30b543a8069c3925bb254716699752e38eaf97a.zip |
Merge pull request #2630 from MetaMask/i2624-VersionedNoticesv3.12.1
Versioned notices
Diffstat (limited to 'app/scripts')
-rw-r--r-- | app/scripts/metamask-controller.js | 14 | ||||
-rw-r--r-- | app/scripts/migrations/020.js | 41 | ||||
-rw-r--r-- | app/scripts/migrations/index.js | 1 | ||||
-rw-r--r-- | app/scripts/notice-controller.js | 44 |
4 files changed, 84 insertions, 16 deletions
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index f9b7b4182..130ad1471 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -45,6 +45,7 @@ module.exports = class MetamaskController extends EventEmitter { this.opts = opts const initState = opts.initState || {} + this.recordFirstTimeInfo(initState) // platform-specific api this.platform = opts.platform @@ -150,6 +151,8 @@ module.exports = class MetamaskController extends EventEmitter { // notices this.noticeController = new NoticeController({ initState: initState.NoticeController, + version, + firstVersion: initState.firstTimeInfo.version, }) this.noticeController.updateNoticesList() // to be uncommented when retrieving notices from a remote server. @@ -484,7 +487,7 @@ module.exports = class MetamaskController extends EventEmitter { vault = await this.keyringController.fullUpdate() } else { - let vault = await this.keyringController.createNewVaultAndKeychain(password) + vault = await this.keyringController.createNewVaultAndKeychain(password) this.selectFirstIdentity(vault) } release() @@ -798,4 +801,13 @@ module.exports = class MetamaskController extends EventEmitter { return rpcTarget } + recordFirstTimeInfo (initState) { + if (!('firstTimeInfo' in initState)) { + initState.firstTimeInfo = { + version, + date: Date.now(), + } + } + } + } diff --git a/app/scripts/migrations/020.js b/app/scripts/migrations/020.js new file mode 100644 index 000000000..8159b3e70 --- /dev/null +++ b/app/scripts/migrations/020.js @@ -0,0 +1,41 @@ +const version = 20 + +/* + +This migration ensures previous installations +get a `firstTimeInfo` key on the metamask state, +so that we can version notices in the future. + +*/ + +const clone = require('clone') + +module.exports = { + version, + + migrate: function (originalVersionedData) { + const versionedData = clone(originalVersionedData) + versionedData.meta.version = version + try { + const state = versionedData.data + const newState = transformState(state) + versionedData.data = newState + } catch (err) { + console.warn(`MetaMask Migration #${version}` + err.stack) + } + return Promise.resolve(versionedData) + }, +} + +function transformState (state) { + const newState = state + if ('metamask' in newState && + !('firstTimeInfo' in newState.metamask)) { + newState.metamask.firstTimeInfo = { + version: '3.12.0', + date: Date.now(), + } + } + return newState +} + diff --git a/app/scripts/migrations/index.js b/app/scripts/migrations/index.js index e9cbd7b98..9d0631042 100644 --- a/app/scripts/migrations/index.js +++ b/app/scripts/migrations/index.js @@ -30,4 +30,5 @@ module.exports = [ require('./017'), require('./018'), require('./019'), + require('./020'), ] diff --git a/app/scripts/notice-controller.js b/app/scripts/notice-controller.js index 57aad40c5..db2b8c4f4 100644 --- a/app/scripts/notice-controller.js +++ b/app/scripts/notice-controller.js @@ -1,13 +1,17 @@ const EventEmitter = require('events').EventEmitter +const semver = require('semver') const extend = require('xtend') const ObservableStore = require('obs-store') const hardCodedNotices = require('../../notices/notices.json') +const uniqBy = require('lodash.uniqby') module.exports = class NoticeController extends EventEmitter { constructor (opts) { super() this.noticePoller = null + this.firstVersion = opts.firstVersion + this.version = opts.version const initState = extend({ noticesList: [], }, opts.initState) @@ -30,9 +34,9 @@ module.exports = class NoticeController extends EventEmitter { return unreadNotices[unreadNotices.length - 1] } - setNoticesList (noticesList) { + async setNoticesList (noticesList) { this.store.updateState({ noticesList }) - return Promise.resolve(true) + return true } markNoticeRead (noticeToMark, cb) { @@ -50,12 +54,14 @@ module.exports = class NoticeController extends EventEmitter { } } - updateNoticesList () { - return this._retrieveNoticeData().then((newNotices) => { - var oldNotices = this.getNoticesList() - var combinedNotices = this._mergeNotices(oldNotices, newNotices) - return Promise.resolve(this.setNoticesList(combinedNotices)) - }) + async updateNoticesList () { + const newNotices = await this._retrieveNoticeData() + const oldNotices = this.getNoticesList() + const combinedNotices = this._mergeNotices(oldNotices, newNotices) + const filteredNotices = this._filterNotices(combinedNotices) + const result = this.setNoticesList(filteredNotices) + this._updateMemstore() + return result } startPolling () { @@ -68,22 +74,30 @@ module.exports = class NoticeController extends EventEmitter { } _mergeNotices (oldNotices, newNotices) { - var noticeMap = this._mapNoticeIds(oldNotices) - newNotices.forEach((notice) => { - if (noticeMap.indexOf(notice.id) === -1) { - oldNotices.push(notice) + return uniqBy(oldNotices.concat(newNotices), 'id') + } + + _filterNotices(notices) { + return notices.filter((newNotice) => { + if ('version' in newNotice) { + const satisfied = semver.satisfies(this.version, newNotice.version) + return satisfied + } + if ('firstVersion' in newNotice) { + const satisfied = semver.satisfies(this.firstVersion, newNotice.firstVersion) + return satisfied } + return true }) - return oldNotices } _mapNoticeIds (notices) { return notices.map((notice) => notice.id) } - _retrieveNoticeData () { + async _retrieveNoticeData () { // Placeholder for the API. - return Promise.resolve(hardCodedNotices) + return hardCodedNotices } _updateMemstore () { |