Notification create/update events UI

This commit is contained in:
advplyr 2022-09-22 18:12:48 -05:00
parent ff04eb8d5e
commit b08ad8785e
9 changed files with 389 additions and 9 deletions

View file

@ -80,7 +80,7 @@ class Server {
this.cronManager = new CronManager(this.db, this.scanner, this.podcastManager)
// Routers
this.apiRouter = new ApiRouter(this.db, this.auth, this.scanner, this.playbackSessionManager, this.abMergeManager, this.coverManager, this.backupManager, this.watcher, this.cacheManager, this.podcastManager, this.audioMetadataManager, this.rssFeedManager, this.cronManager, this.emitter.bind(this), this.clientEmitter.bind(this))
this.apiRouter = new ApiRouter(this.db, this.auth, this.scanner, this.playbackSessionManager, this.abMergeManager, this.coverManager, this.backupManager, this.watcher, this.cacheManager, this.podcastManager, this.audioMetadataManager, this.rssFeedManager, this.cronManager, this.notificationManager, this.emitter.bind(this), this.clientEmitter.bind(this))
this.hlsRouter = new HlsRouter(this.db, this.auth, this.playbackSessionManager, this.emitter.bind(this))
this.staticRouter = new StaticRouter(this.db)

View file

@ -4,7 +4,10 @@ class NotificationController {
constructor() { }
get(req, res) {
res.json(this.db.notificationSettings)
res.json({
data: this.notificationManager.getData(),
settings: this.db.notificationSettings
})
}
async update(req, res) {
@ -15,6 +18,28 @@ class NotificationController {
res.sendStatus(200)
}
async createEvent(req, res) {
const success = this.db.notificationSettings.addNewEvent(req.body)
if (success) {
await this.db.updateEntity('settings', this.db.notificationSettings)
}
res.sendStatus(200)
}
async updateEvent(req, res) {
const success = this.db.notificationSettings.updateEvent(req.body)
if (success) {
await this.db.updateEntity('settings', this.db.notificationSettings)
}
res.sendStatus(200)
}
getData(req, res) {
res.json(this.notificationManager.getData())
}
middleware(req, res, next) {
if (!req.user.isAdminOrUp) {
return res.sendStatus(404)

View file

@ -1,5 +1,6 @@
const axios = require('axios')
const Logger = require("../Logger")
const { notificationData } = require('../utils/notifications')
class NotificationManager {
constructor(db) {
@ -8,6 +9,10 @@ class NotificationManager {
this.notificationFailedMap = {}
}
getData() {
return notificationData
}
onPodcastEpisodeDownloaded(libraryItem, episode) {
if (!this.db.notificationSettings.isUseable) return

View file

@ -1,3 +1,5 @@
const { getId } = require('../utils/index')
class Notification {
constructor(notification = null) {
this.id = null
@ -42,6 +44,37 @@ class Notification {
}
}
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'

View file

@ -1,3 +1,5 @@
const Notification = require('../Notification')
class NotificationSettings {
constructor(settings = null) {
this.id = 'notification-settings'
@ -41,5 +43,23 @@ class NotificationSettings {
}
return false
}
addNewEvent(payload) {
if (!payload) return false
// TODO: validate
const notification = new Notification()
notification.setData(payload)
this.notifications.push(notification)
return true
}
updateEvent(payload) {
if (!payload) return false
const notification = this.notifications.find(n => n.id === payload.id)
if (!notification) return false
return notification.update(payload)
}
}
module.exports = NotificationSettings

View file

@ -26,7 +26,7 @@ const Series = require('../objects/entities/Series')
const FileSystemController = require('../controllers/FileSystemController')
class ApiRouter {
constructor(db, auth, scanner, playbackSessionManager, abMergeManager, coverManager, backupManager, watcher, cacheManager, podcastManager, audioMetadataManager, rssFeedManager, cronManager, emitter, clientEmitter) {
constructor(db, auth, scanner, playbackSessionManager, abMergeManager, coverManager, backupManager, watcher, cacheManager, podcastManager, audioMetadataManager, rssFeedManager, cronManager, notificationManager, emitter, clientEmitter) {
this.db = db
this.auth = auth
this.scanner = scanner
@ -40,6 +40,7 @@ class ApiRouter {
this.audioMetadataManager = audioMetadataManager
this.rssFeedManager = rssFeedManager
this.cronManager = cronManager
this.notificationManager = notificationManager
this.emitter = emitter
this.clientEmitter = clientEmitter
@ -205,6 +206,9 @@ class ApiRouter {
//
this.router.get('/notifications', NotificationController.middleware.bind(this), NotificationController.get.bind(this))
this.router.patch('/notifications', NotificationController.middleware.bind(this), NotificationController.update.bind(this))
this.router.post('/notifications/event', NotificationController.middleware.bind(this), NotificationController.createEvent.bind(this))
this.router.patch('/notifications/event', NotificationController.middleware.bind(this), NotificationController.updateEvent.bind(this))
this.router.get('/notificationdata', NotificationController.middleware.bind(this), NotificationController.getData.bind(this))
//
// Misc Routes

View file

@ -0,0 +1,13 @@
module.exports.notificationData = {
events: [
{
name: 'onTest',
requiresLibrary: false,
description: 'Notification for testing',
defaults: {
title: 'Test Title',
body: 'Test Body'
}
}
]
}