aboutsummaryrefslogblamecommitdiffhomepage
path: root/server/middlewares/validators/videos/video-blacklist.ts
blob: 77ad29cbbd05c9ac178c453fa5ee09a7c8a42b71 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                                  
                                                     
                                                                                         
                                                                          

                                                
                                                                                                                         
                                                        

                                        
                                                                                                      
 
                                                                                      

                                                                                    
                                             

                                                                        

                 



                                     
                                                                                                      



                                                                                  


                                                                                   
 
                                                                                      
                                                                                      
 
                                             
                                                              
 







                                                                 
                 


   




                                                                                                      
 

                                                                                         
 
                                             

                                                                        
 
                 
   
 
 
                                                                              
 



                                 
 
import * as express from 'express'
import { body, param } from 'express-validator/check'
import { isBooleanValid, isIdOrUUIDValid } from '../../../helpers/custom-validators/misc'
import { doesVideoExist } from '../../../helpers/custom-validators/videos'
import { logger } from '../../../helpers/logger'
import { areValidationErrors } from '../utils'
import { doesVideoBlacklistExist, isVideoBlacklistReasonValid } from '../../../helpers/custom-validators/video-blacklist'
import { VideoModel } from '../../../models/video/video'

const videosBlacklistRemoveValidator = [
  param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })

    if (areValidationErrors(req, res)) return
    if (!await doesVideoExist(req.params.videoId, res)) return
    if (!await doesVideoBlacklistExist(res.locals.video.id, res)) return

    return next()
  }
]

const videosBlacklistAddValidator = [
  param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
  body('unfederate')
    .optional()
    .toBoolean()
    .custom(isBooleanValid).withMessage('Should have a valid unfederate boolean'),
  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 })

    if (areValidationErrors(req, res)) return
    if (!await doesVideoExist(req.params.videoId, res)) return

    const video: VideoModel = res.locals.video
    if (req.body.unfederate === true && video.remote === true) {
      return res
        .status(409)
        .send({ error: 'You cannot unfederate a remote video.' })
        .end()
    }

    return next()
  }
]

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'),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    logger.debug('Checking videosBlacklistUpdate parameters', { parameters: req.params })

    if (areValidationErrors(req, res)) return
    if (!await doesVideoExist(req.params.videoId, res)) return
    if (!await doesVideoBlacklistExist(res.locals.video.id, res)) return

    return next()
  }
]

// ---------------------------------------------------------------------------

export {
  videosBlacklistAddValidator,
  videosBlacklistRemoveValidator,
  videosBlacklistUpdateValidator
}