1 import express from 'express'
2 import { body, query } from 'express-validator'
3 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
4 import { isBooleanValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
5 import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../../helpers/custom-validators/video-blacklist'
6 import { logger } from '../../../helpers/logger'
7 import { areValidationErrors, doesVideoBlacklistExist, doesVideoExist, isValidVideoIdParam } from '../shared'
9 const videosBlacklistRemoveValidator = [
10 isValidVideoIdParam('videoId'),
12 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
13 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
15 if (areValidationErrors(req, res)) return
16 if (!await doesVideoExist(req.params.videoId, res)) return
17 if (!await doesVideoBlacklistExist(res.locals.videoAll.id, res)) return
23 const videosBlacklistAddValidator = [
24 isValidVideoIdParam('videoId'),
28 .customSanitizer(toBooleanOrNull)
29 .custom(isBooleanValid).withMessage('Should have a valid unfederate boolean'),
32 .custom(isVideoBlacklistReasonValid),
34 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
35 logger.debug('Checking videosBlacklistAdd parameters', { parameters: req.params })
37 if (areValidationErrors(req, res)) return
38 if (!await doesVideoExist(req.params.videoId, res)) return
40 const video = res.locals.videoAll
41 if (req.body.unfederate === true && video.remote === true) {
43 status: HttpStatusCode.CONFLICT_409,
44 message: 'You cannot unfederate a remote video.'
52 const videosBlacklistUpdateValidator = [
53 isValidVideoIdParam('videoId'),
57 .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
59 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
60 logger.debug('Checking videosBlacklistUpdate parameters', { parameters: req.params })
62 if (areValidationErrors(req, res)) return
63 if (!await doesVideoExist(req.params.videoId, res)) return
64 if (!await doesVideoBlacklistExist(res.locals.videoAll.id, res)) return
70 const videosBlacklistFiltersValidator = [
73 .customSanitizer(toIntOrNull)
74 .custom(isVideoBlacklistTypeValid).withMessage('Should have a valid video blacklist type attribute'),
78 .isEmpty().withMessage('Should have a valid search'),
80 (req: express.Request, res: express.Response, next: express.NextFunction) => {
81 logger.debug('Checking videos blacklist filters query', { parameters: req.query })
83 if (areValidationErrors(req, res)) return
89 // ---------------------------------------------------------------------------
92 videosBlacklistAddValidator,
93 videosBlacklistRemoveValidator,
94 videosBlacklistUpdateValidator,
95 videosBlacklistFiltersValidator