]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/video-blacklist.ts
add user account email verificiation (#977)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / video-blacklist.ts
index 3c8c315195bbd24dc06ac4c0f527009f8e8129f7..95a2b9f170824919f27658380d6dfff863ddbf20 100644 (file)
@@ -1,67 +1,62 @@
-import { param } from 'express-validator/check'
 import * as express from 'express'
-
-import { database as db } from '../../initializers/database'
-import { checkErrors } from './utils'
-import { logger, isIdOrUUIDValid, checkVideoExists } from '../../helpers'
+import { body, param } from 'express-validator/check'
+import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
+import { isVideoExist } from '../../helpers/custom-validators/videos'
+import { logger } from '../../helpers/logger'
+import { areValidationErrors } from './utils'
+import { isVideoBlacklistExist, isVideoBlacklistReasonValid } from '../../helpers/custom-validators/video-blacklist'
 
 const videosBlacklistRemoveValidator = [
   param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
 
-  (req: express.Request, res: express.Response, next: express.NextFunction) => {
+  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
     logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
 
-    checkErrors(req, res, () => {
-      checkVideoExists(req.params.videoId, res, () => {
-        checkVideoIsBlacklisted(req, res, next)
-      })
-    })
+    if (areValidationErrors(req, res)) return
+    if (!await isVideoExist(req.params.videoId, res)) return
+    if (!await isVideoBlacklistExist(res.locals.video.id, res)) return
+
+    return next()
   }
 ]
 
 const videosBlacklistAddValidator = [
   param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
+  body('reason')
+    .optional()
+    .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
+
+  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
+    logger.debug('Checking videosBlacklistAdd parameters', { parameters: req.params })
 
-  (req: express.Request, res: express.Response, next: express.NextFunction) => {
-    logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
+    if (areValidationErrors(req, res)) return
+    if (!await isVideoExist(req.params.videoId, res)) return
 
-    checkErrors(req, res, () => {
-      checkVideoExists(req.params.videoId, res, () => {
-        checkVideoIsBlacklistable(req, res, next)
-      })
-    })
+    return next()
   }
 ]
 
-// ---------------------------------------------------------------------------
-
-export {
-  videosBlacklistAddValidator,
-  videosBlacklistRemoveValidator
-}
-// ---------------------------------------------------------------------------
+const videosBlacklistUpdateValidator = [
+  param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
+  body('reason')
+    .optional()
+    .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
 
-function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) {
-  if (res.locals.video.isOwned() === true) {
-    return res.status(403)
-              .json({ error: 'Cannot blacklist a local video' })
-              .end()
-  }
+  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
+    logger.debug('Checking videosBlacklistUpdate parameters', { parameters: req.params })
 
-  callback()
-}
+    if (areValidationErrors(req, res)) return
+    if (!await isVideoExist(req.params.videoId, res)) return
+    if (!await isVideoBlacklistExist(res.locals.video.id, res)) return
 
-function checkVideoIsBlacklisted (req: express.Request, res: express.Response, callback: () => void) {
-  db.BlacklistedVideo.loadByVideoId(res.locals.video.id)
-    .then(blacklistedVideo => {
-      if (!blacklistedVideo) return res.status(404).send('Blacklisted video not found')
+    return next()
+  }
+]
 
-      res.locals.blacklistedVideo = blacklistedVideo
+// ---------------------------------------------------------------------------
 
-      callback()
-    })
-    .catch(err => {
-      logger.error('Error in blacklistRemove request validator', { error: err })
-      return res.sendStatus(500)
-    })
+export {
+  videosBlacklistAddValidator,
+  videosBlacklistRemoveValidator,
+  videosBlacklistUpdateValidator
 }