2024-08-11 22:15:34 +02:00
|
|
|
const { Request, Response, NextFunction } = require('express')
|
2023-07-05 01:14:44 +02:00
|
|
|
const Database = require('../Database')
|
2022-09-24 23:15:16 +02:00
|
|
|
const { version } = require('../../package.json')
|
2022-09-22 01:01:10 +02:00
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* @typedef RequestUserObjects
|
2024-08-11 23:07:29 +02:00
|
|
|
* @property {import('../models/User')} user
|
2024-08-11 22:15:34 +02:00
|
|
|
*
|
|
|
|
* @typedef {Request & RequestUserObjects} RequestWithUser
|
|
|
|
*/
|
|
|
|
|
2022-09-22 01:01:10 +02:00
|
|
|
class NotificationController {
|
2024-08-11 22:15:34 +02:00
|
|
|
constructor() {}
|
2022-09-22 01:01:10 +02:00
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/notifications
|
|
|
|
* Get notifications, settings and data
|
|
|
|
*
|
|
|
|
* @this {import('../routers/ApiRouter')}
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-09-22 01:01:10 +02:00
|
|
|
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
|
|
|
})
|
2022-09-22 01:01:10 +02:00
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* PATCH: /api/notifications
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-09-22 01:01:10 +02:00
|
|
|
async update(req, res) {
|
2023-07-05 01:14:44 +02:00
|
|
|
const updated = Database.notificationSettings.update(req.body)
|
2022-09-22 01:01:10 +02:00
|
|
|
if (updated) {
|
2023-07-05 01:14:44 +02:00
|
|
|
await Database.updateSetting(Database.notificationSettings)
|
2022-09-22 01:01:10 +02:00
|
|
|
}
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/notificationdata
|
|
|
|
* @deprecated Use /api/notifications
|
|
|
|
*
|
|
|
|
* @this {import('../routers/ApiRouter')}
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-09-24 21:03:14 +02:00
|
|
|
getData(req, res) {
|
|
|
|
res.json(this.notificationManager.getData())
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/notifications/test
|
|
|
|
*
|
|
|
|
* @this {import('../routers/ApiRouter')}
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-09-24 23:15:16 +02:00
|
|
|
async fireTestEvent(req, res) {
|
|
|
|
await this.notificationManager.triggerNotification('onTest', { version: `v${version}` }, req.query.fail === '1')
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/notifications
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-09-24 01:10:03 +02:00
|
|
|
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)
|
2022-09-24 01:10:03 +02:00
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* DELETE: /api/notifications/:id
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-09-24 01:10:03 +02:00
|
|
|
async deleteNotification(req, res) {
|
2023-07-05 01:14:44 +02:00
|
|
|
if (Database.notificationSettings.removeNotification(req.notification.id)) {
|
|
|
|
await Database.updateSetting(Database.notificationSettings)
|
2022-09-24 01:10:03 +02:00
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
res.json(Database.notificationSettings)
|
2022-09-24 01:10:03 +02:00
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* PATCH: /api/notifications/:id
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-09-24 01:10:03 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/notifications/:id/test
|
|
|
|
*
|
|
|
|
* @this {import('../routers/ApiRouter')}
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-09-24 23:15:16 +02:00
|
|
|
async sendNotificationTest(req, res) {
|
2024-08-11 22:15:34 +02:00
|
|
|
if (!Database.notificationSettings.isUseable) return res.status(400).send('Apprise is not configured')
|
2022-09-24 23:15:16 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* Requires admin or up
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
* @param {NextFunction} next
|
|
|
|
*/
|
2022-09-22 01:01:10 +02:00
|
|
|
middleware(req, res, next) {
|
2024-08-11 23:07:29 +02:00
|
|
|
if (!req.user.isAdminOrUp) {
|
2024-08-11 22:15:34 +02:00
|
|
|
return res.sendStatus(403)
|
2022-09-22 01:01:10 +02:00
|
|
|
}
|
2022-09-24 01:10:03 +02:00
|
|
|
|
|
|
|
if (req.params.id) {
|
2023-07-05 01:14:44 +02:00
|
|
|
const notification = Database.notificationSettings.getNotification(req.params.id)
|
2022-09-24 01:10:03 +02:00
|
|
|
if (!notification) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
req.notification = notification
|
|
|
|
}
|
|
|
|
|
2022-09-22 01:01:10 +02:00
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|
2024-08-11 22:15:34 +02:00
|
|
|
module.exports = new NotificationController()
|