1 import express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
4 import { VideoRateType } from '../../../../shared/models/videos'
5 import { isAccountNameValid } from '../../../helpers/custom-validators/accounts'
6 import { isIdValid } from '../../../helpers/custom-validators/misc'
7 import { isRatingValid } from '../../../helpers/custom-validators/video-rates'
8 import { isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos'
9 import { logger } from '../../../helpers/logger'
10 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
11 import { areValidationErrors, checkCanSeeVideo, doesVideoExist, isValidVideoIdParam } from '../shared'
13 const videoUpdateRateValidator = [
14 isValidVideoIdParam('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
24 if (!await checkCanSeeVideo({ req, res, paramId: req.params.id, video: res.locals.videoAll })) return
30 const getAccountVideoRateValidatorFactory = function (rateType: VideoRateType) {
32 param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
33 param('videoId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid videoId'),
35 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
36 logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
38 if (areValidationErrors(req, res)) return
40 const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, +req.params.videoId)
43 status: HttpStatusCode.NOT_FOUND_404,
44 message: 'Video rate not found'
48 res.locals.accountVideoRate = rate
55 const videoRatingValidator = [
56 query('rating').optional().custom(isRatingValid).withMessage('Value must be one of "like" or "dislike"'),
58 (req: express.Request, res: express.Response, next: express.NextFunction) => {
59 logger.debug('Checking rating parameter', { parameters: req.params })
61 if (areValidationErrors(req, res)) return
67 // ---------------------------------------------------------------------------
70 videoUpdateRateValidator,
71 getAccountVideoRateValidatorFactory,