]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/server.ts
Add ability for admins to set default p2p policy
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / server.ts
index d85afc2ffee8875356422d7ad5ae5ec8b0469b60..10bbefe38866ee95d16be212f8f12197b23a83fe 100644 (file)
@@ -1,13 +1,13 @@
-import * as express from 'express'
-import { logger } from '../../helpers/logger'
-import { areValidationErrors } from './utils'
+import express from 'express'
+import { body } from 'express-validator'
+import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
 import { isHostValid, isValidContactBody } from '../../helpers/custom-validators/servers'
-import { ServerModel } from '../../models/server/server'
-import { body } from 'express-validator/check'
 import { isUserDisplayNameValid } from '../../helpers/custom-validators/users'
-import { Emailer } from '../../lib/emailer'
+import { logger } from '../../helpers/logger'
+import { CONFIG, isEmailEnabled } from '../../initializers/config'
 import { Redis } from '../../lib/redis'
-import { CONFIG } from '../../initializers/constants'
+import { ServerModel } from '../../models/server/server'
+import { areValidationErrors } from './shared'
 
 const serverGetValidator = [
   body('host').custom(isHostValid).withMessage('Should have a valid host'),
@@ -19,9 +19,10 @@ const serverGetValidator = [
 
     const server = await ServerModel.loadByHost(req.body.host)
     if (!server) {
-      return res.status(404)
-         .send({ error: 'Server host not found.' })
-         .end()
+      return res.fail({
+        status: HttpStatusCode.NOT_FOUND_404,
+        message: 'Server host not found.'
+      })
     }
 
     res.locals.server = server
@@ -44,26 +45,26 @@ const contactAdministratorValidator = [
     if (areValidationErrors(req, res)) return
 
     if (CONFIG.CONTACT_FORM.ENABLED === false) {
-      return res
-        .status(409)
-        .send({ error: 'Contact form is not enabled on this instance.' })
-        .end()
+      return res.fail({
+        status: HttpStatusCode.CONFLICT_409,
+        message: 'Contact form is not enabled on this instance.'
+      })
     }
 
-    if (Emailer.isEnabled() === false) {
-      return res
-        .status(409)
-        .send({ error: 'Emailer is not enabled on this instance.' })
-        .end()
+    if (isEmailEnabled() === false) {
+      return res.fail({
+        status: HttpStatusCode.CONFLICT_409,
+        message: 'Emailer is not enabled on this instance.'
+      })
     }
 
-    if (await Redis.Instance.isContactFormIpExists(req.ip)) {
+    if (await Redis.Instance.doesContactFormIpExist(req.ip)) {
       logger.info('Refusing a contact form by %s: already sent one recently.', req.ip)
 
-      return res
-        .status(403)
-        .send({ error: 'You already sent a contact form recently.' })
-        .end()
+      return res.fail({
+        status: HttpStatusCode.FORBIDDEN_403,
+        message: 'You already sent a contact form recently.'
+      })
     }
 
     return next()