]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/blocklist.ts
Cleanup useless express validator messages
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / blocklist.ts
index b7749e204c8bac8bc6ba164a78876ddcc04bb36f..3de614522da6e8dc4d4af8126a4155e8634de2f1 100644 (file)
@@ -1,8 +1,10 @@
 import express from 'express'
-import { body, param } from 'express-validator'
+import { body, param, query } from 'express-validator'
+import { areValidActorHandles } from '@server/helpers/custom-validators/activitypub/actor'
+import { toArray } from '@server/helpers/custom-validators/misc'
 import { getServerActor } from '@server/models/application/application'
 import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
-import { isHostValid } from '../../helpers/custom-validators/servers'
+import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
 import { logger } from '../../helpers/logger'
 import { WEBSERVER } from '../../initializers/constants'
 import { AccountBlocklistModel } from '../../models/account/account-blocklist'
@@ -11,7 +13,8 @@ import { ServerBlocklistModel } from '../../models/server/server-blocklist'
 import { areValidationErrors, doesAccountNameWithHostExist } from './shared'
 
 const blockAccountValidator = [
-  body('accountName').exists().withMessage('Should have an account name with host'),
+  body('accountName')
+    .exists(),
 
   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
     logger.debug('Checking blockAccountByAccountValidator parameters', { parameters: req.body })
@@ -35,7 +38,8 @@ const blockAccountValidator = [
 ]
 
 const unblockAccountByAccountValidator = [
-  param('accountName').exists().withMessage('Should have an account name with host'),
+  param('accountName')
+    .exists(),
 
   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
     logger.debug('Checking unblockAccountByAccountValidator parameters', { parameters: req.params })
@@ -52,7 +56,8 @@ const unblockAccountByAccountValidator = [
 ]
 
 const unblockAccountByServerValidator = [
-  param('accountName').exists().withMessage('Should have an account name with host'),
+  param('accountName')
+    .exists(),
 
   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
     logger.debug('Checking unblockAccountByServerValidator parameters', { parameters: req.params })
@@ -69,7 +74,8 @@ const unblockAccountByServerValidator = [
 ]
 
 const blockServerValidator = [
-  body('host').custom(isHostValid).withMessage('Should have a valid host'),
+  body('host')
+    .custom(isHostValid),
 
   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
     logger.debug('Checking serverGetValidator parameters', { parameters: req.body })
@@ -94,7 +100,8 @@ const blockServerValidator = [
 ]
 
 const unblockServerByAccountValidator = [
-  param('host').custom(isHostValid).withMessage('Should have an account name with host'),
+  param('host')
+    .custom(isHostValid),
 
   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
     logger.debug('Checking unblockServerByAccountValidator parameters', { parameters: req.params })
@@ -109,7 +116,8 @@ const unblockServerByAccountValidator = [
 ]
 
 const unblockServerByServerValidator = [
-  param('host').custom(isHostValid).withMessage('Should have an account name with host'),
+  param('host')
+    .custom(isHostValid),
 
   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
     logger.debug('Checking unblockServerByServerValidator parameters', { parameters: req.params })
@@ -123,6 +131,26 @@ const unblockServerByServerValidator = [
   }
 ]
 
+const blocklistStatusValidator = [
+  query('hosts')
+    .optional()
+    .customSanitizer(toArray)
+    .custom(isEachUniqueHostValid).withMessage('Should have a valid hosts array'),
+
+  query('accounts')
+    .optional()
+    .customSanitizer(toArray)
+    .custom(areValidActorHandles).withMessage('Should have a valid accounts array'),
+
+  (req: express.Request, res: express.Response, next: express.NextFunction) => {
+    logger.debug('Checking blocklistStatusValidator parameters', { query: req.query })
+
+    if (areValidationErrors(req, res)) return
+
+    return next()
+  }
+]
+
 // ---------------------------------------------------------------------------
 
 export {
@@ -131,7 +159,8 @@ export {
   unblockAccountByAccountValidator,
   unblockServerByAccountValidator,
   unblockAccountByServerValidator,
-  unblockServerByServerValidator
+  unblockServerByServerValidator,
+  blocklistStatusValidator
 }
 
 // ---------------------------------------------------------------------------