audiobookshelf/server/controllers/NotificationController.js

80 lines
2.2 KiB
JavaScript
Raw Normal View History

const Logger = require('../Logger')
2023-07-05 01:14:44 +02:00
const Database = require('../Database')
const { version } = require('../../package.json')
class NotificationController {
constructor() { }
get(req, res) {
2022-09-23 01:12:48 +02:00
res.json({
data: this.notificationManager.getData(),
2023-07-05 01:14:44 +02:00
settings: Database.notificationSettings
2022-09-23 01:12:48 +02:00
})
}
async update(req, res) {
2023-07-05 01:14:44 +02:00
const updated = Database.notificationSettings.update(req.body)
if (updated) {
2023-07-05 01:14:44 +02:00
await Database.updateSetting(Database.notificationSettings)
}
res.sendStatus(200)
}
getData(req, res) {
res.json(this.notificationManager.getData())
}
async fireTestEvent(req, res) {
await this.notificationManager.triggerNotification('onTest', { version: `v${version}` }, req.query.fail === '1')
res.sendStatus(200)
}
async createNotification(req, res) {
2023-07-05 01:14:44 +02:00
const success = Database.notificationSettings.createNotification(req.body)
2022-09-23 01:12:48 +02:00
if (success) {
2023-07-05 01:14:44 +02:00
await Database.updateSetting(Database.notificationSettings)
2022-09-23 01:12:48 +02:00
}
2023-07-05 01:14:44 +02:00
res.json(Database.notificationSettings)
}
async deleteNotification(req, res) {
2023-07-05 01:14:44 +02:00
if (Database.notificationSettings.removeNotification(req.notification.id)) {
await Database.updateSetting(Database.notificationSettings)
}
2023-07-05 01:14:44 +02:00
res.json(Database.notificationSettings)
}
async updateNotification(req, res) {
2023-07-05 01:14:44 +02:00
const success = Database.notificationSettings.updateNotification(req.body)
2022-09-23 01:12:48 +02:00
if (success) {
2023-07-05 01:14:44 +02:00
await Database.updateSetting(Database.notificationSettings)
2022-09-23 01:12:48 +02:00
}
2023-07-05 01:14:44 +02:00
res.json(Database.notificationSettings)
2022-09-23 01:12:48 +02:00
}
async sendNotificationTest(req, res) {
2023-07-05 01:14:44 +02:00
if (!Database.notificationSettings.isUseable) return res.status(500).send('Apprise is not configured')
const success = await this.notificationManager.sendTestNotification(req.notification)
if (success) res.sendStatus(200)
else res.sendStatus(500)
2022-09-23 01:12:48 +02:00
}
middleware(req, res, next) {
if (!req.user.isAdminOrUp) {
return res.sendStatus(404)
}
if (req.params.id) {
2023-07-05 01:14:44 +02:00
const notification = Database.notificationSettings.getNotification(req.params.id)
if (!notification) {
return res.sendStatus(404)
}
req.notification = notification
}
next()
}
}
module.exports = new NotificationController()