2022-09-22 01:01:10 +02:00
|
|
|
const Logger = require('../Logger')
|
|
|
|
|
|
|
|
class NotificationController {
|
|
|
|
constructor() { }
|
|
|
|
|
|
|
|
get(req, res) {
|
2022-09-23 01:12:48 +02:00
|
|
|
res.json({
|
|
|
|
data: this.notificationManager.getData(),
|
|
|
|
settings: this.db.notificationSettings
|
|
|
|
})
|
2022-09-22 01:01:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async update(req, res) {
|
|
|
|
const updated = this.db.notificationSettings.update(req.body)
|
|
|
|
if (updated) {
|
|
|
|
await this.db.updateEntity('settings', this.db.notificationSettings)
|
|
|
|
}
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2022-09-24 21:03:14 +02:00
|
|
|
getData(req, res) {
|
|
|
|
res.json(this.notificationManager.getData())
|
|
|
|
}
|
|
|
|
|
2022-09-24 01:10:03 +02:00
|
|
|
async createNotification(req, res) {
|
|
|
|
const success = this.db.notificationSettings.createNotification(req.body)
|
2022-09-23 01:12:48 +02:00
|
|
|
|
|
|
|
if (success) {
|
|
|
|
await this.db.updateEntity('settings', this.db.notificationSettings)
|
|
|
|
}
|
2022-09-24 21:03:14 +02:00
|
|
|
res.json(this.db.notificationSettings)
|
2022-09-24 01:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async deleteNotification(req, res) {
|
|
|
|
if (this.db.notificationSettings.removeNotification(req.notification.id)) {
|
|
|
|
await this.db.updateEntity('settings', this.db.notificationSettings)
|
|
|
|
}
|
2022-09-24 21:03:14 +02:00
|
|
|
res.json(this.db.notificationSettings)
|
2022-09-24 01:10:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async updateNotification(req, res) {
|
|
|
|
const success = this.db.notificationSettings.updateNotification(req.body)
|
2022-09-24 21:03:14 +02:00
|
|
|
console.log('Update notification', success, req.body)
|
2022-09-23 01:12:48 +02:00
|
|
|
if (success) {
|
|
|
|
await this.db.updateEntity('settings', this.db.notificationSettings)
|
|
|
|
}
|
2022-09-24 21:03:14 +02:00
|
|
|
res.json(this.db.notificationSettings)
|
2022-09-23 01:12:48 +02:00
|
|
|
}
|
|
|
|
|
2022-09-24 01:10:03 +02:00
|
|
|
sendNotificationTest(req, res) {
|
2022-09-24 21:03:14 +02:00
|
|
|
if (!this.db.notificationSettings.isUsable) return res.status(500).send('Apprise is not configured')
|
2022-09-24 01:10:03 +02:00
|
|
|
this.notificationManager.onTest()
|
|
|
|
res.sendStatus(200)
|
2022-09-23 01:12:48 +02:00
|
|
|
}
|
|
|
|
|
2022-09-22 01:01:10 +02:00
|
|
|
middleware(req, res, next) {
|
|
|
|
if (!req.user.isAdminOrUp) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
2022-09-24 01:10:03 +02:00
|
|
|
|
|
|
|
if (req.params.id) {
|
|
|
|
const notification = this.db.notificationSettings.getNotification(req.params.id)
|
|
|
|
if (!notification) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
req.notification = notification
|
|
|
|
}
|
|
|
|
|
2022-09-22 01:01:10 +02:00
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = new NotificationController()
|