audiobookshelf/server/objects/Notification.js

133 lines
3.7 KiB
JavaScript
Raw Normal View History

2023-07-05 01:14:44 +02:00
const uuidv4 = require("uuid").v4
2022-09-23 01:12:48 +02:00
2022-09-21 01:08:41 +02:00
class Notification {
constructor(notification = null) {
this.id = null
this.libraryId = null
2022-09-21 01:08:41 +02:00
this.eventName = ''
this.urls = []
this.titleTemplate = ''
this.bodyTemplate = ''
this.type = 'info'
2022-09-21 01:08:41 +02:00
this.enabled = false
this.lastFiredAt = null
this.lastAttemptFailed = false
this.numConsecutiveFailedAttempts = 0
this.numTimesFired = 0
2022-09-21 01:08:41 +02:00
this.createdAt = null
if (notification) {
this.construct(notification)
}
}
construct(notification) {
this.id = notification.id
this.libraryId = notification.libraryId || null
2022-09-21 01:08:41 +02:00
this.eventName = notification.eventName
this.urls = notification.urls || []
this.titleTemplate = notification.titleTemplate || ''
this.bodyTemplate = notification.bodyTemplate || ''
this.type = notification.type || 'info'
2022-09-21 01:08:41 +02:00
this.enabled = !!notification.enabled
this.lastFiredAt = notification.lastFiredAt || null
this.lastAttemptFailed = !!notification.lastAttemptFailed
this.numConsecutiveFailedAttempts = notification.numConsecutiveFailedAttempts || 0
this.numTimesFired = notification.numTimesFired || 0
2022-09-21 01:08:41 +02:00
this.createdAt = notification.createdAt
}
toJSON() {
return {
id: this.id,
libraryId: this.libraryId,
2022-09-21 01:08:41 +02:00
eventName: this.eventName,
urls: this.urls,
titleTemplate: this.titleTemplate,
bodyTemplate: this.bodyTemplate,
enabled: this.enabled,
type: this.type,
lastFiredAt: this.lastFiredAt,
lastAttemptFailed: this.lastAttemptFailed,
numConsecutiveFailedAttempts: this.numConsecutiveFailedAttempts,
numTimesFired: this.numTimesFired,
2022-09-21 01:08:41 +02:00
createdAt: this.createdAt
}
}
2022-09-23 01:12:48 +02:00
setData(payload) {
2023-07-05 01:14:44 +02:00
this.id = uuidv4()
2022-09-23 01:12:48 +02:00
this.libraryId = payload.libraryId || null
this.eventName = payload.eventName
this.urls = payload.urls
this.titleTemplate = payload.titleTemplate
this.bodyTemplate = payload.bodyTemplate
this.enabled = !!payload.enabled
this.type = payload.type || null
this.createdAt = Date.now()
}
update(payload) {
if (!this.enabled && payload.enabled) {
// Reset
this.lastFiredAt = null
this.lastAttemptFailed = false
this.numConsecutiveFailedAttempts = 0
}
2022-09-23 01:12:48 +02:00
const keysToUpdate = ['libraryId', 'eventName', 'urls', 'titleTemplate', 'bodyTemplate', 'enabled', 'type']
var hasUpdated = false
for (const key of keysToUpdate) {
if (payload[key] !== undefined) {
2022-09-23 01:12:48 +02:00
if (key === 'urls') {
if (payload[key].join(',') !== this.urls.join(',')) {
this.urls = [...payload[key]]
hasUpdated = true
}
} else if (payload[key] !== this[key]) {
this[key] = payload[key]
hasUpdated = true
}
}
}
return hasUpdated
}
updateNotificationFired(success) {
this.lastFiredAt = Date.now()
this.lastAttemptFailed = !success
this.numConsecutiveFailedAttempts = success ? 0 : this.numConsecutiveFailedAttempts + 1
this.numTimesFired++
}
replaceVariablesInTemplate(templateText, data) {
const ptrn = /{{ ?([a-zA-Z]+) ?}}/mg
var match
var updatedTemplate = templateText
while ((match = ptrn.exec(templateText)) != null) {
if (data[match[1]]) {
updatedTemplate = updatedTemplate.replace(match[0], data[match[1]])
}
}
return updatedTemplate
}
parseTitleTemplate(data) {
return this.replaceVariablesInTemplate(this.titleTemplate, data)
}
parseBodyTemplate(data) {
return this.replaceVariablesInTemplate(this.bodyTemplate, data)
}
getApprisePayload(data) {
return {
urls: this.urls,
title: this.parseTitleTemplate(data),
body: this.parseBodyTemplate(data)
}
}
2022-09-21 01:08:41 +02:00
}
module.exports = Notification