audiobookshelf/server/controllers/NotificationController.js

154 lines
3.7 KiB
JavaScript
Raw Normal View History

const { Request, Response, NextFunction } = require('express')
2023-07-05 01:14:44 +02:00
const Database = require('../Database')
const { version } = require('../../package.json')
const NotificationManager = require('../managers/NotificationManager')
/**
2024-08-12 00:01:25 +02:00
* @typedef RequestUserObject
* @property {import('../models/User')} user
*
2024-08-12 00:01:25 +02:00
* @typedef {Request & RequestUserObject} RequestWithUser
*/
class NotificationController {
constructor() {}
/**
* GET: /api/notifications
* Get notifications, settings and data
*
* @this {import('../routers/ApiRouter')}
*
* @param {RequestWithUser} req
* @param {Response} res
*/
get(req, res) {
2022-09-23 01:12:48 +02:00
res.json({
data: NotificationManager.getData(),
2023-07-05 01:14:44 +02:00
settings: Database.notificationSettings
2022-09-23 01:12:48 +02:00
})
}
/**
* PATCH: /api/notifications
*
* @param {RequestWithUser} req
* @param {Response} res
*/
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)
}
/**
* GET: /api/notificationdata
* @deprecated Use /api/notifications
*
* @this {import('../routers/ApiRouter')}
*
* @param {RequestWithUser} req
* @param {Response} res
*/
getData(req, res) {
res.json(NotificationManager.getData())
}
/**
* GET: /api/notifications/test
*
* @this {import('../routers/ApiRouter')}
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async fireTestEvent(req, res) {
await NotificationManager.triggerNotification('onTest', { version: `v${version}` }, req.query.fail === '1')
res.sendStatus(200)
}
/**
* POST: /api/notifications
*
* @param {RequestWithUser} req
* @param {Response} res
*/
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)
}
/**
* DELETE: /api/notifications/:id
*
* @param {RequestWithUser} req
* @param {Response} res
*/
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)
}
/**
* PATCH: /api/notifications/:id
*
* @param {RequestWithUser} req
* @param {Response} res
*/
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
}
/**
* GET: /api/notifications/:id/test
*
* @this {import('../routers/ApiRouter')}
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async sendNotificationTest(req, res) {
if (!Database.notificationSettings.isUseable) return res.status(400).send('Apprise is not configured')
const success = await NotificationManager.sendTestNotification(req.notification)
if (success) res.sendStatus(200)
else res.sendStatus(500)
2022-09-23 01:12:48 +02:00
}
/**
* Requires admin or up
*
* @param {RequestWithUser} req
* @param {Response} res
* @param {NextFunction} next
*/
middleware(req, res, next) {
if (!req.user.isAdminOrUp) {
return res.sendStatus(403)
}
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()