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'),
17 .custom(isVideoRatingTypeValid),
19 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
20 logger.debug('Checking videoRate parameters', { parameters: req.body })
22 if (areValidationErrors(req, res)) return
23 if (!await doesVideoExist(req.params.id, res)) return
25 if (!await checkCanSeeVideo({ req, res, paramId: req.params.id, video: res.locals.videoAll })) return
31 const getAccountVideoRateValidatorFactory = function (rateType: VideoRateType) {
34 .custom(isAccountNameValid),
38 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
39 logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
41 if (areValidationErrors(req, res)) return
43 const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, +req.params.videoId)
46 status: HttpStatusCode.NOT_FOUND_404,
47 message: 'Video rate not found'
51 res.locals.accountVideoRate = rate
58 const videoRatingValidator = [
61 .custom(isRatingValid).withMessage('Value must be one of "like" or "dislike"'),
63 (req: express.Request, res: express.Response, next: express.NextFunction) => {
64 logger.debug('Checking rating parameter', { parameters: req.params })
66 if (areValidationErrors(req, res)) return
72 // ---------------------------------------------------------------------------
75 videoUpdateRateValidator,
76 getAccountVideoRateValidatorFactory,