2019-02-12 15:03:37 +01:00
|
|
|
const {EventEmitter} = require('events')
|
2017-11-28 20:14:57 +01:00
|
|
|
const semver = require('semver')
|
2017-02-03 07:23:45 +01:00
|
|
|
const extend = require('xtend')
|
|
|
|
const ObservableStore = require('obs-store')
|
2018-06-14 07:56:46 +02:00
|
|
|
const hardCodedNotices = require('../../notices/notices.js')
|
2017-11-29 00:16:04 +01:00
|
|
|
const uniqBy = require('lodash.uniqby')
|
2016-12-07 23:34:15 +01:00
|
|
|
|
|
|
|
module.exports = class NoticeController extends EventEmitter {
|
|
|
|
|
2018-09-11 21:12:35 +02:00
|
|
|
constructor (opts = {}) {
|
2016-12-07 23:34:15 +01:00
|
|
|
super()
|
2016-12-16 21:44:47 +01:00
|
|
|
this.noticePoller = null
|
2017-11-28 20:22:09 +01:00
|
|
|
this.firstVersion = opts.firstVersion
|
2017-11-29 00:16:04 +01:00
|
|
|
this.version = opts.version
|
2017-02-03 07:23:45 +01:00
|
|
|
const initState = extend({
|
2018-06-18 21:10:35 +02:00
|
|
|
noticesList: [],
|
2017-02-03 07:23:45 +01:00
|
|
|
}, opts.initState)
|
|
|
|
this.store = new ObservableStore(initState)
|
2018-06-18 21:10:35 +02:00
|
|
|
// setup memStore
|
2017-02-03 07:23:45 +01:00
|
|
|
this.memStore = new ObservableStore({})
|
|
|
|
this.store.subscribe(() => this._updateMemstore())
|
2018-06-14 09:23:01 +02:00
|
|
|
this._updateMemstore()
|
2018-06-18 21:10:35 +02:00
|
|
|
// pull in latest notices
|
|
|
|
this.updateNoticesList()
|
2016-12-07 23:34:15 +01:00
|
|
|
}
|
|
|
|
|
2017-02-03 07:23:45 +01:00
|
|
|
getNoticesList () {
|
|
|
|
return this.store.getState().noticesList
|
|
|
|
}
|
2016-12-07 23:34:15 +01:00
|
|
|
|
2017-02-03 07:23:45 +01:00
|
|
|
getUnreadNotices () {
|
|
|
|
const notices = this.getNoticesList()
|
|
|
|
return notices.filter((notice) => notice.read === false)
|
2016-12-07 23:34:15 +01:00
|
|
|
}
|
2016-12-16 21:44:47 +01:00
|
|
|
|
2018-06-14 08:30:31 +02:00
|
|
|
getNextUnreadNotice () {
|
2017-02-03 07:23:45 +01:00
|
|
|
const unreadNotices = this.getUnreadNotices()
|
2018-06-14 08:30:31 +02:00
|
|
|
return unreadNotices[0]
|
2016-12-16 21:44:47 +01:00
|
|
|
}
|
|
|
|
|
2017-11-29 00:16:04 +01:00
|
|
|
async setNoticesList (noticesList) {
|
2017-02-03 07:23:45 +01:00
|
|
|
this.store.updateState({ noticesList })
|
2017-11-29 00:16:04 +01:00
|
|
|
return true
|
2016-12-16 21:44:47 +01:00
|
|
|
}
|
|
|
|
|
2017-02-10 02:47:51 +01:00
|
|
|
markNoticeRead (noticeToMark, cb) {
|
2016-12-21 01:45:22 +01:00
|
|
|
cb = cb || function (err) { if (err) throw err }
|
2016-12-16 21:44:47 +01:00
|
|
|
try {
|
2019-02-12 15:03:37 +01:00
|
|
|
const notices = this.getNoticesList()
|
|
|
|
const index = notices.findIndex((currentNotice) => currentNotice.id === noticeToMark.id)
|
2017-02-10 02:47:51 +01:00
|
|
|
notices[index].read = true
|
2017-03-23 14:58:57 +01:00
|
|
|
notices[index].body = ''
|
2016-12-16 21:44:47 +01:00
|
|
|
this.setNoticesList(notices)
|
2018-06-14 08:30:31 +02:00
|
|
|
const latestNotice = this.getNextUnreadNotice()
|
2016-12-16 21:44:47 +01:00
|
|
|
cb(null, latestNotice)
|
|
|
|
} catch (err) {
|
|
|
|
cb(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 00:16:04 +01:00
|
|
|
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
|
2016-12-16 21:44:47 +01:00
|
|
|
}
|
|
|
|
|
2016-12-21 01:45:22 +01:00
|
|
|
_mergeNotices (oldNotices, newNotices) {
|
2017-11-29 00:16:04 +01:00
|
|
|
return uniqBy(oldNotices.concat(newNotices), 'id')
|
|
|
|
}
|
|
|
|
|
2017-12-28 02:27:48 +01:00
|
|
|
_filterNotices (notices) {
|
2017-11-29 00:16:04 +01:00
|
|
|
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
|
2016-12-16 21:44:47 +01:00
|
|
|
}
|
2017-11-29 00:16:04 +01:00
|
|
|
return true
|
2016-12-16 21:44:47 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-29 00:16:04 +01:00
|
|
|
async _retrieveNoticeData () {
|
2018-06-18 21:10:35 +02:00
|
|
|
// Placeholder for remote notice API.
|
|
|
|
return hardCodedNotices
|
2016-12-16 21:44:47 +01:00
|
|
|
}
|
|
|
|
|
2017-02-03 07:23:45 +01:00
|
|
|
_updateMemstore () {
|
2018-06-14 08:30:31 +02:00
|
|
|
const nextUnreadNotice = this.getNextUnreadNotice()
|
|
|
|
const noActiveNotices = !nextUnreadNotice
|
|
|
|
this.memStore.updateState({ nextUnreadNotice, noActiveNotices })
|
2017-02-03 07:23:45 +01:00
|
|
|
}
|
2016-12-16 21:44:47 +01:00
|
|
|
|
2016-12-07 23:34:15 +01:00
|
|
|
}
|