2022-09-24 21:03:14 +02:00
|
|
|
const Logger = require('../../Logger')
|
2022-09-23 01:12:48 +02:00
|
|
|
const Notification = require('../Notification')
|
2022-09-25 17:42:26 +02:00
|
|
|
const { isNullOrNaN } = require('../../utils')
|
2022-09-23 01:12:48 +02:00
|
|
|
|
2022-09-22 01:01:10 +02:00
|
|
|
class NotificationSettings {
|
|
|
|
constructor(settings = null) {
|
|
|
|
this.id = 'notification-settings'
|
|
|
|
this.appriseType = 'api'
|
|
|
|
this.appriseApiUrl = null
|
|
|
|
this.notifications = []
|
2022-09-25 17:19:44 +02:00
|
|
|
this.maxFailedAttempts = 5
|
|
|
|
this.maxNotificationQueue = 20 // once reached events will be ignored
|
|
|
|
this.notificationDelay = 1000 // ms delay between firing notifications
|
2022-09-22 01:01:10 +02:00
|
|
|
|
|
|
|
if (settings) {
|
|
|
|
this.construct(settings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(settings) {
|
|
|
|
this.appriseType = settings.appriseType
|
|
|
|
this.appriseApiUrl = settings.appriseApiUrl || null
|
2022-09-24 01:10:03 +02:00
|
|
|
this.notifications = (settings.notifications || []).map(n => new Notification(n))
|
2022-09-25 17:19:44 +02:00
|
|
|
this.maxFailedAttempts = settings.maxFailedAttempts || 5
|
|
|
|
this.maxNotificationQueue = settings.maxNotificationQueue || 20
|
|
|
|
this.notificationDelay = settings.notificationDelay || 1000
|
2022-09-22 01:01:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
appriseType: this.appriseType,
|
|
|
|
appriseApiUrl: this.appriseApiUrl,
|
2022-09-25 17:19:44 +02:00
|
|
|
notifications: this.notifications.map(n => n.toJSON()),
|
|
|
|
maxFailedAttempts: this.maxFailedAttempts,
|
|
|
|
maxNotificationQueue: this.maxNotificationQueue,
|
|
|
|
notificationDelay: this.notificationDelay
|
2022-09-22 01:01:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get isUseable() {
|
|
|
|
return !!this.appriseApiUrl
|
|
|
|
}
|
|
|
|
|
2022-09-25 16:46:45 +02:00
|
|
|
getActiveNotificationsForEvent(eventName) {
|
|
|
|
return this.notifications.filter(n => n.eventName === eventName && n.enabled)
|
2022-09-22 01:01:10 +02:00
|
|
|
}
|
|
|
|
|
2022-09-24 01:10:03 +02:00
|
|
|
getNotification(id) {
|
|
|
|
return this.notifications.find(n => n.id === id)
|
|
|
|
}
|
|
|
|
|
|
|
|
removeNotification(id) {
|
|
|
|
if (this.notifications.some(n => n.id === id)) {
|
|
|
|
this.notifications = this.notifications.filter(n => n.id !== id)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-09-22 01:01:10 +02:00
|
|
|
update(payload) {
|
|
|
|
if (!payload) return false
|
2022-09-25 17:42:26 +02:00
|
|
|
|
|
|
|
var hasUpdates = false
|
2022-09-22 01:01:10 +02:00
|
|
|
if (payload.appriseApiUrl !== this.appriseApiUrl) {
|
|
|
|
this.appriseApiUrl = payload.appriseApiUrl || null
|
2022-09-25 17:42:26 +02:00
|
|
|
hasUpdates = true
|
2022-09-22 01:01:10 +02:00
|
|
|
}
|
2022-09-25 17:42:26 +02:00
|
|
|
|
|
|
|
const _maxFailedAttempts = isNullOrNaN(payload.maxFailedAttempts) ? 5 : Number(payload.maxFailedAttempts)
|
|
|
|
if (_maxFailedAttempts !== this.maxFailedAttempts) {
|
|
|
|
this.maxFailedAttempts = _maxFailedAttempts
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
|
|
|
|
const _maxNotificationQueue = isNullOrNaN(payload.maxNotificationQueue) ? 20 : Number(payload.maxNotificationQueue)
|
|
|
|
if (_maxNotificationQueue !== this.maxNotificationQueue) {
|
|
|
|
this.maxNotificationQueue = _maxNotificationQueue
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasUpdates
|
2022-09-22 01:01:10 +02:00
|
|
|
}
|
2022-09-23 01:12:48 +02:00
|
|
|
|
2022-09-24 01:10:03 +02:00
|
|
|
createNotification(payload) {
|
2022-09-23 01:12:48 +02:00
|
|
|
if (!payload) return false
|
2022-09-24 21:03:14 +02:00
|
|
|
if (!payload.eventName || !payload.urls.length) return false
|
2022-09-23 01:12:48 +02:00
|
|
|
|
|
|
|
const notification = new Notification()
|
|
|
|
notification.setData(payload)
|
|
|
|
this.notifications.push(notification)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-09-24 01:10:03 +02:00
|
|
|
updateNotification(payload) {
|
2022-09-23 01:12:48 +02:00
|
|
|
if (!payload) return false
|
|
|
|
const notification = this.notifications.find(n => n.id === payload.id)
|
2022-09-24 21:03:14 +02:00
|
|
|
if (!notification) {
|
|
|
|
Logger.error(`[NotificationSettings] updateNotification: Notification not found ${payload.id}`)
|
|
|
|
return false
|
|
|
|
}
|
2022-09-23 01:12:48 +02:00
|
|
|
|
|
|
|
return notification.update(payload)
|
|
|
|
}
|
2022-09-22 01:01:10 +02:00
|
|
|
}
|
|
|
|
module.exports = NotificationSettings
|