2022-09-23 01:12:48 +02:00
|
|
|
const { getId } = require('../utils/index')
|
|
|
|
|
2022-09-21 01:08:41 +02:00
|
|
|
class Notification {
|
|
|
|
constructor(notification = null) {
|
|
|
|
this.id = null
|
2022-09-22 01:01:10 +02:00
|
|
|
this.libraryId = null
|
2022-09-21 01:08:41 +02:00
|
|
|
this.eventName = ''
|
|
|
|
this.urls = []
|
|
|
|
this.titleTemplate = ''
|
|
|
|
this.bodyTemplate = ''
|
2022-09-22 01:01:10 +02:00
|
|
|
this.type = 'info'
|
2022-09-21 01:08:41 +02:00
|
|
|
this.enabled = false
|
|
|
|
|
|
|
|
this.createdAt = null
|
|
|
|
|
|
|
|
if (notification) {
|
|
|
|
this.construct(notification)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(notification) {
|
|
|
|
this.id = notification.id
|
2022-09-22 01:01:10 +02:00
|
|
|
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 || ''
|
2022-09-22 01:01:10 +02:00
|
|
|
this.type = notification.type || 'info'
|
2022-09-21 01:08:41 +02:00
|
|
|
this.enabled = !!notification.enabled
|
|
|
|
this.createdAt = notification.createdAt
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
2022-09-22 01:01:10 +02:00
|
|
|
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,
|
2022-09-22 01:01:10 +02:00
|
|
|
type: this.type,
|
2022-09-21 01:08:41 +02:00
|
|
|
createdAt: this.createdAt
|
|
|
|
}
|
|
|
|
}
|
2022-09-22 01:01:10 +02:00
|
|
|
|
2022-09-23 01:12:48 +02:00
|
|
|
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) {
|
2022-09-24 21:03:14 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-22 01:01:10 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2022-09-21 01:08:41 +02:00
|
|
|
}
|
|
|
|
module.exports = Notification
|