aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/notice-controller.js
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2016-12-17 04:44:47 +0800
committerkumavis <aaron@kumavis.me>2016-12-17 04:44:47 +0800
commit73998feeb2b6ba4ebb9a16c6ed54cce195c09013 (patch)
tree35c3b7572bfb16b9a2e0f0492d99448cc1fb714f /app/scripts/notice-controller.js
parentff13f40d1fae3781426b9fb1d22b723e50952dd9 (diff)
downloadtangerine-wallet-browser-73998feeb2b6ba4ebb9a16c6ed54cce195c09013.tar
tangerine-wallet-browser-73998feeb2b6ba4ebb9a16c6ed54cce195c09013.tar.gz
tangerine-wallet-browser-73998feeb2b6ba4ebb9a16c6ed54cce195c09013.tar.bz2
tangerine-wallet-browser-73998feeb2b6ba4ebb9a16c6ed54cce195c09013.tar.lz
tangerine-wallet-browser-73998feeb2b6ba4ebb9a16c6ed54cce195c09013.tar.xz
tangerine-wallet-browser-73998feeb2b6ba4ebb9a16c6ed54cce195c09013.tar.zst
tangerine-wallet-browser-73998feeb2b6ba4ebb9a16c6ed54cce195c09013.zip
move notice code from metamask-controller + config-manager, in to notice-controller
Diffstat (limited to 'app/scripts/notice-controller.js')
-rw-r--r--app/scripts/notice-controller.js80
1 files changed, 79 insertions, 1 deletions
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)
+ }
+
+
}