]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/config.ts
Refactor email enabled function
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / config.ts
index d015fa6fece80d738ee553f477ed50c27c6ba050..dfa549e763dc6508a8da2527fecc1981b2e6498d 100644 (file)
@@ -1,10 +1,12 @@
 import * as express from 'express'
-import { body } from 'express-validator/check'
-import { isUserNSFWPolicyValid, isUserVideoQuotaValid, isUserVideoQuotaDailyValid } from '../../helpers/custom-validators/users'
+import { body } from 'express-validator'
+import { isUserNSFWPolicyValid, isUserVideoQuotaDailyValid, isUserVideoQuotaValid } from '../../helpers/custom-validators/users'
 import { logger } from '../../helpers/logger'
 import { CustomConfig } from '../../../shared/models/server/custom-config.model'
-import { Emailer } from '../../lib/emailer'
 import { areValidationErrors } from './utils'
+import { isThemeNameValid } from '../../helpers/custom-validators/plugins'
+import { isThemeRegistered } from '../../lib/plugins/theme-utils'
+import { isEmailEnabled } from '@server/initializers/config'
 
 const customConfigUpdateValidator = [
   body('instance.name').exists().withMessage('Should have a valid instance name'),
@@ -35,23 +37,30 @@ const customConfigUpdateValidator = [
   body('transcoding.enabled').isBoolean().withMessage('Should have a valid transcoding enabled boolean'),
   body('transcoding.allowAdditionalExtensions').isBoolean().withMessage('Should have a valid additional extensions boolean'),
   body('transcoding.threads').isInt().withMessage('Should have a valid transcoding threads number'),
+  body('transcoding.resolutions.0p').isBoolean().withMessage('Should have a valid transcoding 0p resolution enabled boolean'),
   body('transcoding.resolutions.240p').isBoolean().withMessage('Should have a valid transcoding 240p resolution enabled boolean'),
   body('transcoding.resolutions.360p').isBoolean().withMessage('Should have a valid transcoding 360p resolution enabled boolean'),
   body('transcoding.resolutions.480p').isBoolean().withMessage('Should have a valid transcoding 480p resolution enabled boolean'),
   body('transcoding.resolutions.720p').isBoolean().withMessage('Should have a valid transcoding 720p resolution enabled boolean'),
   body('transcoding.resolutions.1080p').isBoolean().withMessage('Should have a valid transcoding 1080p resolution enabled boolean'),
 
+  body('transcoding.webtorrent.enabled').isBoolean().withMessage('Should have a valid webtorrent transcoding enabled boolean'),
+  body('transcoding.hls.enabled').isBoolean().withMessage('Should have a valid hls transcoding enabled boolean'),
+
   body('import.videos.http.enabled').isBoolean().withMessage('Should have a valid import video http enabled boolean'),
   body('import.videos.torrent.enabled').isBoolean().withMessage('Should have a valid import video torrent enabled boolean'),
 
   body('followers.instance.enabled').isBoolean().withMessage('Should have a valid followers of instance boolean'),
   body('followers.instance.manualApproval').isBoolean().withMessage('Should have a valid manual approval boolean'),
 
-  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
+  body('theme.default').custom(v => isThemeNameValid(v) && isThemeRegistered(v)).withMessage('Should have a valid theme'),
+
+  (req: express.Request, res: express.Response, next: express.NextFunction) => {
     logger.debug('Checking customConfigUpdateValidator parameters', { parameters: req.body })
 
     if (areValidationErrors(req, res)) return
     if (!checkInvalidConfigIfEmailDisabled(req.body as CustomConfig, res)) return
+    if (!checkInvalidTranscodingConfig(req.body as CustomConfig, res)) return
 
     return next()
   }
@@ -64,7 +73,7 @@ export {
 }
 
 function checkInvalidConfigIfEmailDisabled (customConfig: CustomConfig, res: express.Response) {
-  if (Emailer.isEnabled()) return true
+  if (isEmailEnabled()) return true
 
   if (customConfig.signup.requiresEmailVerification === true) {
     res.status(400)
@@ -75,3 +84,16 @@ function checkInvalidConfigIfEmailDisabled (customConfig: CustomConfig, res: exp
 
   return true
 }
+
+function checkInvalidTranscodingConfig (customConfig: CustomConfig, res: express.Response) {
+  if (customConfig.transcoding.enabled === false) return true
+
+  if (customConfig.transcoding.webtorrent.enabled === false && customConfig.transcoding.hls.enabled === false) {
+    res.status(400)
+       .send({ error: 'You need to enable at least webtorrent transcoding or hls transcoding' })
+       .end()
+    return false
+  }
+
+  return true
+}