mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-06-22 08:53:41 +02:00
Update more API endpoints to use new user model
This commit is contained in:
parent
9facf77ff1
commit
afc16358ca
23 changed files with 856 additions and 404 deletions
|
@ -1,10 +1,27 @@
|
|||
const Logger = require('../Logger')
|
||||
const { Request, Response, NextFunction } = require('express')
|
||||
const Database = require('../Database')
|
||||
const { version } = require('../../package.json')
|
||||
|
||||
class NotificationController {
|
||||
constructor() { }
|
||||
/**
|
||||
* @typedef RequestUserObjects
|
||||
* @property {import('../models/User')} userNew
|
||||
* @property {import('../objects/user/User')} user
|
||||
*
|
||||
* @typedef {Request & RequestUserObjects} 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) {
|
||||
res.json({
|
||||
data: this.notificationManager.getData(),
|
||||
|
@ -12,6 +29,12 @@ class NotificationController {
|
|||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH: /api/notifications
|
||||
*
|
||||
* @param {RequestWithUser} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
async update(req, res) {
|
||||
const updated = Database.notificationSettings.update(req.body)
|
||||
if (updated) {
|
||||
|
@ -20,15 +43,38 @@ class NotificationController {
|
|||
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(this.notificationManager.getData())
|
||||
}
|
||||
|
||||
/**
|
||||
* GET: /api/notifications/test
|
||||
*
|
||||
* @this {import('../routers/ApiRouter')}
|
||||
*
|
||||
* @param {RequestWithUser} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
async fireTestEvent(req, res) {
|
||||
await this.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) {
|
||||
const success = Database.notificationSettings.createNotification(req.body)
|
||||
|
||||
|
@ -38,6 +84,12 @@ class NotificationController {
|
|||
res.json(Database.notificationSettings)
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE: /api/notifications/:id
|
||||
*
|
||||
* @param {RequestWithUser} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
async deleteNotification(req, res) {
|
||||
if (Database.notificationSettings.removeNotification(req.notification.id)) {
|
||||
await Database.updateSetting(Database.notificationSettings)
|
||||
|
@ -45,6 +97,12 @@ class NotificationController {
|
|||
res.json(Database.notificationSettings)
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH: /api/notifications/:id
|
||||
*
|
||||
* @param {RequestWithUser} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
async updateNotification(req, res) {
|
||||
const success = Database.notificationSettings.updateNotification(req.body)
|
||||
if (success) {
|
||||
|
@ -53,17 +111,32 @@ class NotificationController {
|
|||
res.json(Database.notificationSettings)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(500).send('Apprise is not configured')
|
||||
if (!Database.notificationSettings.isUseable) return res.status(400).send('Apprise is not configured')
|
||||
|
||||
const success = await this.notificationManager.sendTestNotification(req.notification)
|
||||
if (success) res.sendStatus(200)
|
||||
else res.sendStatus(500)
|
||||
}
|
||||
|
||||
/**
|
||||
* Requires admin or up
|
||||
*
|
||||
* @param {RequestWithUser} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
middleware(req, res, next) {
|
||||
if (!req.user.isAdminOrUp) {
|
||||
return res.sendStatus(404)
|
||||
if (!req.userNew.isAdminOrUp) {
|
||||
return res.sendStatus(403)
|
||||
}
|
||||
|
||||
if (req.params.id) {
|
||||
|
@ -77,4 +150,4 @@ class NotificationController {
|
|||
next()
|
||||
}
|
||||
}
|
||||
module.exports = new NotificationController()
|
||||
module.exports = new NotificationController()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue