1 import * as express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc'
4 import { isRatingValid } from '../../../helpers/custom-validators/video-rates'
5 import { isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos'
6 import { logger } from '../../../helpers/logger'
7 import { areValidationErrors } from '../utils'
8 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
9 import { VideoRateType } from '../../../../shared/models/videos'
10 import { isAccountNameValid } from '../../../helpers/custom-validators/accounts'
11 import { doesVideoExist } from '../../../helpers/middlewares'
12 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
14 const videoUpdateRateValidator = [
15 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
16 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
18 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
19 logger.debug('Checking videoRate parameters', { parameters: req.body })
21 if (areValidationErrors(req, res)) return
22 if (!await doesVideoExist(req.params.id, res)) return
28 const getAccountVideoRateValidatorFactory = function (rateType: VideoRateType) {
30 param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
31 param('videoId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid videoId'),
33 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
34 logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
36 if (areValidationErrors(req, res)) return
38 const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, +req.params.videoId)
40 return res.status(HttpStatusCode.NOT_FOUND_404)
41 .json({ error: 'Video rate not found' })
44 res.locals.accountVideoRate = rate
51 const videoRatingValidator = [
52 query('rating').optional().custom(isRatingValid).withMessage('Value must be one of "like" or "dislike"'),
54 (req: express.Request, res: express.Response, next: express.NextFunction) => {
55 logger.debug('Checking rating parameter', { parameters: req.params })
57 if (areValidationErrors(req, res)) return
63 // ---------------------------------------------------------------------------
66 videoUpdateRateValidator,
67 getAccountVideoRateValidatorFactory,