2016-12-07 23:34:15 +01:00
|
|
|
const EventEmitter = require('events').EventEmitter
|
2017-02-03 00:59:39 +01:00
|
|
|
const hardCodedNotices = require('../../notices/notices.json')
|
2016-12-07 23:34:15 +01:00
|
|
|
|
|
|
|
module.exports = class NoticeController extends EventEmitter {
|
|
|
|
|
|
|
|
constructor (opts) {
|
|
|
|
super()
|
|
|
|
this.configManager = opts.configManager
|
2016-12-16 21:44:47 +01:00
|
|
|
this.noticePoller = null
|
2016-12-07 23:34:15 +01:00
|
|
|
}
|
|
|
|
|
2016-12-21 01:45:22 +01:00
|
|
|
getState () {
|
2016-12-16 21:44:47 +01:00
|
|
|
var lastUnreadNotice = this.getLatestUnreadNotice()
|
2016-12-07 23:34:15 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
lastUnreadNotice: lastUnreadNotice,
|
|
|
|
noActiveNotices: !lastUnreadNotice,
|
|
|
|
}
|
|
|
|
}
|
2016-12-16 21:44:47 +01:00
|
|
|
|
2016-12-21 01:45:22 +01:00
|
|
|
getNoticesList () {
|
2016-12-16 21:44:47 +01:00
|
|
|
var data = this.configManager.getData()
|
|
|
|
if ('noticesList' in data) {
|
|
|
|
return data.noticesList
|
|
|
|
} else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 01:45:22 +01:00
|
|
|
setNoticesList (list) {
|
2016-12-16 21:44:47 +01:00
|
|
|
var data = this.configManager.getData()
|
|
|
|
data.noticesList = list
|
|
|
|
this.configManager.setData(data)
|
|
|
|
return Promise.resolve(true)
|
|
|
|
}
|
|
|
|
|
2016-12-21 01:45:22 +01:00
|
|
|
markNoticeRead (notice, cb) {
|
|
|
|
cb = cb || function (err) { if (err) throw err }
|
2016-12-16 21:44:47 +01:00
|
|
|
try {
|
|
|
|
var notices = this.getNoticesList()
|
|
|
|
var id = notice.id
|
|
|
|
notices[id].read = true
|
|
|
|
this.setNoticesList(notices)
|
2016-12-21 01:45:22 +01:00
|
|
|
const latestNotice = this.getLatestUnreadNotice()
|
2016-12-16 21:44:47 +01:00
|
|
|
cb(null, latestNotice)
|
|
|
|
} catch (err) {
|
|
|
|
cb(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 01:45:22 +01:00
|
|
|
updateNoticesList () {
|
2016-12-16 21:44:47 +01:00
|
|
|
return this._retrieveNoticeData().then((newNotices) => {
|
|
|
|
var oldNotices = this.getNoticesList()
|
|
|
|
var combinedNotices = this._mergeNotices(oldNotices, newNotices)
|
|
|
|
return Promise.resolve(this.setNoticesList(combinedNotices))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-21 01:45:22 +01:00
|
|
|
getLatestUnreadNotice () {
|
2016-12-16 21:44:47 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-12-21 01:45:22 +01:00
|
|
|
_mergeNotices (oldNotices, newNotices) {
|
2016-12-16 21:44:47 +01:00
|
|
|
var noticeMap = this._mapNoticeIds(oldNotices)
|
|
|
|
newNotices.forEach((notice) => {
|
|
|
|
if (noticeMap.indexOf(notice.id) === -1) {
|
|
|
|
oldNotices.push(notice)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return oldNotices
|
|
|
|
}
|
|
|
|
|
2016-12-21 01:45:22 +01:00
|
|
|
_mapNoticeIds (notices) {
|
2016-12-16 21:44:47 +01:00
|
|
|
return notices.map((notice) => notice.id)
|
|
|
|
}
|
|
|
|
|
2016-12-21 01:45:22 +01:00
|
|
|
_retrieveNoticeData () {
|
2016-12-16 21:44:47 +01:00
|
|
|
// Placeholder for the API.
|
|
|
|
return Promise.resolve(hardCodedNotices)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-07 23:34:15 +01:00
|
|
|
}
|