audiobookshelf/server/controllers/EmailController.js

142 lines
3.6 KiB
JavaScript
Raw Normal View History

2024-08-12 00:01:25 +02:00
const { Request, Response, NextFunction } = require('express')
const Logger = require('../Logger')
const SocketAuthority = require('../SocketAuthority')
2023-07-05 01:14:44 +02:00
const Database = require('../Database')
2024-08-12 00:01:25 +02:00
/**
* @typedef RequestUserObject
* @property {import('../models/User')} user
*
* @typedef {Request & RequestUserObject} RequestWithUser
*/
class EmailController {
constructor() {}
2024-08-12 00:01:25 +02:00
/**
* GET: /api/emails/settings
*
* @param {RequestWithUser} req
* @param {Response} res
*/
getSettings(req, res) {
res.json({
2023-07-05 01:14:44 +02:00
settings: Database.emailSettings
})
}
2024-08-12 00:01:25 +02:00
/**
* PATCH: /api/emails/settings
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async updateSettings(req, res) {
2023-07-05 01:14:44 +02:00
const updated = Database.emailSettings.update(req.body)
if (updated) {
2023-07-05 01:14:44 +02:00
await Database.updateSetting(Database.emailSettings)
}
res.json({
2023-07-05 01:14:44 +02:00
settings: Database.emailSettings
})
}
2024-08-12 00:01:25 +02:00
/**
* POST: /api/emails/test
*
* @this {import('../routers/ApiRouter')}
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async sendTest(req, res) {
this.emailManager.sendTest(res)
}
2024-08-12 00:01:25 +02:00
/**
* POST: /api/emails/ereader-devices
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async updateEReaderDevices(req, res) {
if (!req.body.ereaderDevices || !Array.isArray(req.body.ereaderDevices)) {
return res.status(400).send('Invalid payload. ereaderDevices array required')
}
const ereaderDevices = req.body.ereaderDevices
for (const device of ereaderDevices) {
if (!device.name || !device.email) {
return res.status(400).send('Invalid payload. ereaderDevices array items must have name and email')
}
}
2023-07-05 01:14:44 +02:00
const updated = Database.emailSettings.update({
ereaderDevices
})
if (updated) {
2023-07-05 01:14:44 +02:00
await Database.updateSetting(Database.emailSettings)
SocketAuthority.adminEmitter('ereader-devices-updated', {
2023-07-05 01:14:44 +02:00
ereaderDevices: Database.emailSettings.ereaderDevices
})
}
res.json({
2023-07-05 01:14:44 +02:00
ereaderDevices: Database.emailSettings.ereaderDevices
})
}
/**
2024-08-12 00:01:25 +02:00
* POST: /api/emails/send-ebook-to-device
* Send ebook to device
* User must have access to device and library item
*
2024-08-12 00:01:25 +02:00
* @param {RequestWithUser} req
* @param {Response} res
*/
async sendEBookToDevice(req, res) {
Logger.debug(`[EmailController] Send ebook to device requested by user "${req.user.username}" for libraryItemId=${req.body.libraryItemId}, deviceName=${req.body.deviceName}`)
const device = Database.emailSettings.getEReaderDevice(req.body.deviceName)
if (!device) {
return res.status(404).send('Ereader device not found')
}
// Check user has access to device
if (!Database.emailSettings.checkUserCanAccessDevice(device, req.user)) {
return res.sendStatus(403)
}
const libraryItem = await Database.libraryItemModel.getOldById(req.body.libraryItemId)
if (!libraryItem) {
return res.status(404).send('Library item not found')
}
// Check user has access to library item
if (!req.user.checkCanAccessLibraryItem(libraryItem)) {
return res.sendStatus(403)
}
const ebookFile = libraryItem.media.ebookFile
if (!ebookFile) {
return res.status(404).send('Ebook file not found')
}
this.emailManager.sendEBookToDevice(ebookFile, device, res)
}
2024-08-12 00:01:25 +02:00
/**
*
* @param {RequestWithUser} req
* @param {Response} res
* @param {NextFunction} next
*/
adminMiddleware(req, res, next) {
if (!req.user.isAdminOrUp) {
return res.sendStatus(404)
}
next()
}
}
module.exports = new EmailController()