2024-08-12 00:01:25 +02:00
const { Request , Response , NextFunction } = require ( 'express' )
2023-05-30 00:38:38 +02:00
const Logger = require ( '../Logger' )
const SocketAuthority = require ( '../SocketAuthority' )
2023-07-05 01:14:44 +02:00
const Database = require ( '../Database' )
2023-05-30 00:38:38 +02:00
2024-08-12 00:01:25 +02:00
/ * *
* @ typedef RequestUserObject
* @ property { import ( '../models/User' ) } user
*
* @ typedef { Request & RequestUserObject } RequestWithUser
* /
2023-05-30 00:38:38 +02:00
class EmailController {
2024-08-11 00:15:21 +02:00
constructor ( ) { }
2023-05-30 00:38:38 +02:00
2024-08-12 00:01:25 +02:00
/ * *
* GET : / a p i / e m a i l s / s e t t i n g s
*
* @ param { RequestWithUser } req
* @ param { Response } res
* /
2023-05-30 00:38:38 +02:00
getSettings ( req , res ) {
res . json ( {
2023-07-05 01:14:44 +02:00
settings : Database . emailSettings
2023-05-30 00:38:38 +02:00
} )
}
2024-08-12 00:01:25 +02:00
/ * *
* PATCH : / a p i / e m a i l s / s e t t i n g s
*
* @ param { RequestWithUser } req
* @ param { Response } res
* /
2023-05-30 00:38:38 +02:00
async updateSettings ( req , res ) {
2023-07-05 01:14:44 +02:00
const updated = Database . emailSettings . update ( req . body )
2023-05-30 00:38:38 +02:00
if ( updated ) {
2023-07-05 01:14:44 +02:00
await Database . updateSetting ( Database . emailSettings )
2023-05-30 00:38:38 +02:00
}
res . json ( {
2023-07-05 01:14:44 +02:00
settings : Database . emailSettings
2023-05-30 00:38:38 +02:00
} )
}
2024-08-12 00:01:25 +02:00
/ * *
* POST : / a p i / e m a i l s / t e s t
*
* @ this { import ( '../routers/ApiRouter' ) }
*
* @ param { RequestWithUser } req
* @ param { Response } res
* /
2023-05-30 00:38:38 +02:00
async sendTest ( req , res ) {
this . emailManager . sendTest ( res )
}
2024-08-12 00:01:25 +02:00
/ * *
* POST : / a p i / e m a i l s / e r e a d e r - d e v i c e s
*
* @ param { RequestWithUser } req
* @ param { Response } res
* /
2023-05-30 00:38:38 +02:00
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 ( {
2023-05-30 00:38:38 +02:00
ereaderDevices
} )
if ( updated ) {
2023-07-05 01:14:44 +02:00
await Database . updateSetting ( Database . emailSettings )
2023-05-30 00:38:38 +02:00
SocketAuthority . adminEmitter ( 'ereader-devices-updated' , {
2023-07-05 01:14:44 +02:00
ereaderDevices : Database . emailSettings . ereaderDevices
2023-05-30 00:38:38 +02:00
} )
}
res . json ( {
2023-07-05 01:14:44 +02:00
ereaderDevices : Database . emailSettings . ereaderDevices
2023-05-30 00:38:38 +02:00
} )
}
2023-10-29 17:28:34 +01:00
/ * *
2024-08-12 00:01:25 +02:00
* POST : / a p i / e m a i l s / s e n d - e b o o k - t o - d e v i c e
2023-10-29 17:28:34 +01:00
* Send ebook to device
* User must have access to device and library item
2024-08-11 00:15:21 +02:00
*
2024-08-12 00:01:25 +02:00
* @ param { RequestWithUser } req
* @ param { Response } res
2023-10-29 17:28:34 +01:00
* /
2023-05-30 00:38:38 +02:00
async sendEBookToDevice ( req , res ) {
2024-08-11 23:07:29 +02:00
Logger . debug ( ` [EmailController] Send ebook to device requested by user " ${ req . user . username } " for libraryItemId= ${ req . body . libraryItemId } , deviceName= ${ req . body . deviceName } ` )
2023-10-29 17:28:34 +01:00
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
2024-08-11 23:07:29 +02:00
if ( ! Database . emailSettings . checkUserCanAccessDevice ( device , req . user ) ) {
2023-10-29 17:28:34 +01:00
return res . sendStatus ( 403 )
}
2023-05-30 00:38:38 +02:00
2023-09-04 23:33:55 +02:00
const libraryItem = await Database . libraryItemModel . getOldById ( req . body . libraryItemId )
2023-05-30 00:38:38 +02:00
if ( ! libraryItem ) {
return res . status ( 404 ) . send ( 'Library item not found' )
}
2023-10-29 17:28:34 +01:00
// Check user has access to library item
2024-08-11 23:07:29 +02:00
if ( ! req . user . checkCanAccessLibraryItem ( libraryItem ) ) {
2023-05-30 00:38:38 +02:00
return res . sendStatus ( 403 )
}
const ebookFile = libraryItem . media . ebookFile
if ( ! ebookFile ) {
2023-10-29 17:28:34 +01:00
return res . status ( 404 ) . send ( 'Ebook file not found' )
2023-05-30 00:38:38 +02:00
}
this . emailManager . sendEBookToDevice ( ebookFile , device , res )
}
2024-08-12 00:01:25 +02:00
/ * *
*
* @ param { RequestWithUser } req
* @ param { Response } res
* @ param { NextFunction } next
* /
2023-10-29 17:28:34 +01:00
adminMiddleware ( req , res , next ) {
2024-08-11 23:07:29 +02:00
if ( ! req . user . isAdminOrUp ) {
2023-05-30 00:38:38 +02:00
return res . sendStatus ( 404 )
}
next ( )
}
}
2024-08-11 00:15:21 +02:00
module . exports = new EmailController ( )