audiobookshelf/server/objects/Notification.js
2022-09-22 18:12:48 -05:00

96 lines
2.4 KiB
JavaScript

const { getId } = require('../utils/index')
class Notification {
constructor(notification = null) {
this.id = null
this.libraryId = null
this.eventName = ''
this.urls = []
this.titleTemplate = ''
this.bodyTemplate = ''
this.type = 'info'
this.enabled = false
this.createdAt = null
if (notification) {
this.construct(notification)
}
}
construct(notification) {
this.id = notification.id
this.libraryId = notification.libraryId || null
this.eventName = notification.eventName
this.urls = notification.urls || []
this.titleTemplate = notification.titleTemplate || ''
this.bodyTemplate = notification.bodyTemplate || ''
this.type = notification.type || 'info'
this.enabled = !!notification.enabled
this.createdAt = notification.createdAt
}
toJSON() {
return {
id: this.id,
libraryId: this.libraryId,
eventName: this.eventName,
urls: this.urls,
titleTemplate: this.titleTemplate,
bodyTemplate: this.bodyTemplate,
enabled: this.enabled,
type: this.type,
createdAt: this.createdAt
}
}
setData(payload) {
this.id = getId('noti')
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) {
const keysToUpdate = ['libraryId', 'eventName', 'urls', 'titleTemplate', 'bodyTemplate', 'enabled', 'type']
var hasUpdated = false
for (const key of keysToUpdate) {
if (payload[key]) {
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
}
parseTitleTemplate(data) {
// TODO: Implement template parsing
return 'Test Title'
}
parseBodyTemplate(data) {
// TODO: Implement template parsing
return 'Test Body'
}
getApprisePayload(data) {
return {
urls: this.urls,
title: this.parseTitleTemplate(data),
body: this.parseBodyTemplate(data)
}
}
}
module.exports = Notification