mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 09:57:02 +01:00
move notice code from metamask-controller + config-manager, in to notice-controller
This commit is contained in:
parent
ff13f40d1f
commit
73998feeb2
@ -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
|
||||
@ -162,69 +161,6 @@ 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
|
||||
//
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -13,100 +13,6 @@ describe('config-manager', function() {
|
||||
configManager = configManagerGen()
|
||||
})
|
||||
|
||||
describe('notices', function() {
|
||||
describe('#getNoticesList', function() {
|
||||
it('should return an empty array when new', function() {
|
||||
var testList = [{
|
||||
id:0,
|
||||
read:false,
|
||||
title:"Futuristic Notice"
|
||||
}]
|
||||
var result = configManager.getNoticesList()
|
||||
assert.equal(result.length, 0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#setNoticesList', function() {
|
||||
it('should set data appropriately', function () {
|
||||
var testList = [{
|
||||
id:0,
|
||||
read:false,
|
||||
title:"Futuristic Notice"
|
||||
}]
|
||||
configManager.setNoticesList(testList)
|
||||
var testListId = configManager.getNoticesList()[0].id
|
||||
assert.equal(testListId, 0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#updateNoticeslist', function() {
|
||||
it('should integrate the latest changes from the source', function() {
|
||||
var testList = [{
|
||||
id:55,
|
||||
read:false,
|
||||
title:"Futuristic Notice"
|
||||
}]
|
||||
configManager.setNoticesList(testList)
|
||||
configManager.updateNoticesList().then(() => {
|
||||
var newList = configManager.getNoticesList()
|
||||
assert.ok(newList[0].id === 55)
|
||||
assert.ok(newList[1])
|
||||
})
|
||||
})
|
||||
it('should not overwrite any existing fields', function () {
|
||||
var testList = [{
|
||||
id:0,
|
||||
read:false,
|
||||
title:"Futuristic Notice"
|
||||
}]
|
||||
configManager.setNoticesList(testList)
|
||||
configManager.updateNoticesList().then(() => {
|
||||
var newList = configManager.getNoticesList()
|
||||
assert.equal(newList[0].id, 0)
|
||||
assert.equal(newList[0].title, "Futuristic Notice")
|
||||
assert.equal(newList.length, 1)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('#markNoticeRead', function () {
|
||||
it('should mark a notice as read', function () {
|
||||
var testList = [{
|
||||
id:0,
|
||||
read:false,
|
||||
title:"Futuristic Notice"
|
||||
}]
|
||||
configManager.setNoticesList(testList)
|
||||
configManager.markNoticeRead(testList[0])
|
||||
var newList = configManager.getNoticesList()
|
||||
assert.ok(newList[0].read)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getLatestUnreadNotice', function () {
|
||||
it('should retrieve the latest unread notice', function () {
|
||||
var testList = [
|
||||
{id:0,read:true,title:"Past Notice"},
|
||||
{id:1,read:false,title:"Current Notice"},
|
||||
{id:2,read:false,title:"Future Notice"},
|
||||
]
|
||||
configManager.setNoticesList(testList)
|
||||
var latestUnread = configManager.getLatestUnreadNotice()
|
||||
assert.equal(latestUnread.id, 2)
|
||||
})
|
||||
it('should return undefined if no unread notices exist.', function () {
|
||||
var testList = [
|
||||
{id:0,read:true,title:"Past Notice"},
|
||||
{id:1,read:true,title:"Current Notice"},
|
||||
{id:2,read:true,title:"Future Notice"},
|
||||
]
|
||||
configManager.setNoticesList(testList)
|
||||
var latestUnread = configManager.getLatestUnreadNotice()
|
||||
assert.ok(!latestUnread)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('currency conversions', function() {
|
||||
|
||||
describe('#getCurrentFiat', function() {
|
||||
|
115
test/unit/notice-controller-test.js
Normal file
115
test/unit/notice-controller-test.js
Normal file
@ -0,0 +1,115 @@
|
||||
const assert = require('assert')
|
||||
const extend = require('xtend')
|
||||
const rp = require('request-promise')
|
||||
const nock = require('nock')
|
||||
const configManagerGen = require('../lib/mock-config-manager')
|
||||
const NoticeController = require('../../app/scripts/notice-controller')
|
||||
const STORAGE_KEY = 'metamask-persistance-key'
|
||||
// Hacking localStorage support into JSDom
|
||||
window.localStorage = {}
|
||||
|
||||
describe('notice-controller', function() {
|
||||
var noticeController
|
||||
|
||||
beforeEach(function() {
|
||||
let configManager = configManagerGen()
|
||||
noticeController = new NoticeController({
|
||||
configManager: configManager,
|
||||
})
|
||||
})
|
||||
|
||||
describe('notices', function() {
|
||||
describe('#getNoticesList', function() {
|
||||
it('should return an empty array when new', function() {
|
||||
var testList = [{
|
||||
id:0,
|
||||
read:false,
|
||||
title:"Futuristic Notice"
|
||||
}]
|
||||
var result = noticeController.getNoticesList()
|
||||
assert.equal(result.length, 0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#setNoticesList', function() {
|
||||
it('should set data appropriately', function () {
|
||||
var testList = [{
|
||||
id:0,
|
||||
read:false,
|
||||
title:"Futuristic Notice"
|
||||
}]
|
||||
noticeController.setNoticesList(testList)
|
||||
var testListId = noticeController.getNoticesList()[0].id
|
||||
assert.equal(testListId, 0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#updateNoticeslist', function() {
|
||||
it('should integrate the latest changes from the source', function() {
|
||||
var testList = [{
|
||||
id:55,
|
||||
read:false,
|
||||
title:"Futuristic Notice"
|
||||
}]
|
||||
noticeController.setNoticesList(testList)
|
||||
noticeController.updateNoticesList().then(() => {
|
||||
var newList = noticeController.getNoticesList()
|
||||
assert.ok(newList[0].id === 55)
|
||||
assert.ok(newList[1])
|
||||
})
|
||||
})
|
||||
it('should not overwrite any existing fields', function () {
|
||||
var testList = [{
|
||||
id:0,
|
||||
read:false,
|
||||
title:"Futuristic Notice"
|
||||
}]
|
||||
noticeController.setNoticesList(testList)
|
||||
noticeController.updateNoticesList().then(() => {
|
||||
var newList = noticeController.getNoticesList()
|
||||
assert.equal(newList[0].id, 0)
|
||||
assert.equal(newList[0].title, "Futuristic Notice")
|
||||
assert.equal(newList.length, 1)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('#markNoticeRead', function () {
|
||||
it('should mark a notice as read', function () {
|
||||
var testList = [{
|
||||
id:0,
|
||||
read:false,
|
||||
title:"Futuristic Notice"
|
||||
}]
|
||||
noticeController.setNoticesList(testList)
|
||||
noticeController.markNoticeRead(testList[0])
|
||||
var newList = noticeController.getNoticesList()
|
||||
assert.ok(newList[0].read)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getLatestUnreadNotice', function () {
|
||||
it('should retrieve the latest unread notice', function () {
|
||||
var testList = [
|
||||
{id:0,read:true,title:"Past Notice"},
|
||||
{id:1,read:false,title:"Current Notice"},
|
||||
{id:2,read:false,title:"Future Notice"},
|
||||
]
|
||||
noticeController.setNoticesList(testList)
|
||||
var latestUnread = noticeController.getLatestUnreadNotice()
|
||||
assert.equal(latestUnread.id, 2)
|
||||
})
|
||||
it('should return undefined if no unread notices exist.', function () {
|
||||
var testList = [
|
||||
{id:0,read:true,title:"Past Notice"},
|
||||
{id:1,read:true,title:"Current Notice"},
|
||||
{id:2,read:true,title:"Future Notice"},
|
||||
]
|
||||
noticeController.setNoticesList(testList)
|
||||
var latestUnread = noticeController.getLatestUnreadNotice()
|
||||
assert.ok(!latestUnread)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
})
|
Loading…
Reference in New Issue
Block a user