2018-11-14 09:01:28 -05:00
|
|
|
import * as express from 'express'
|
2019-07-25 10:23:44 -04:00
|
|
|
import { body, param, query } from 'express-validator'
|
2021-02-26 10:26:27 -05:00
|
|
|
import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc'
|
2019-04-09 05:02:02 -04:00
|
|
|
import { isRatingValid } from '../../../helpers/custom-validators/video-rates'
|
2019-07-23 04:40:39 -04:00
|
|
|
import { isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos'
|
2018-11-14 09:01:28 -05:00
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { areValidationErrors } from '../utils'
|
|
|
|
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
|
|
|
|
import { VideoRateType } from '../../../../shared/models/videos'
|
|
|
|
import { isAccountNameValid } from '../../../helpers/custom-validators/accounts'
|
2019-07-23 04:40:39 -04:00
|
|
|
import { doesVideoExist } from '../../../helpers/middlewares'
|
2020-12-07 08:32:36 -05:00
|
|
|
import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
|
2018-11-14 09:01:28 -05:00
|
|
|
|
|
|
|
const videoUpdateRateValidator = [
|
|
|
|
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
|
|
|
body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videoRate parameters', { parameters: req.body })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
2019-03-19 04:26:50 -04:00
|
|
|
if (!await doesVideoExist(req.params.id, res)) return
|
2018-11-14 09:01:28 -05:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2020-01-29 09:17:42 -05:00
|
|
|
const getAccountVideoRateValidatorFactory = function (rateType: VideoRateType) {
|
2018-11-14 09:01:28 -05:00
|
|
|
return [
|
|
|
|
param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
|
2021-02-26 10:26:27 -05:00
|
|
|
param('videoId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid videoId'),
|
2018-11-14 09:01:28 -05:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
2021-02-26 10:26:27 -05:00
|
|
|
const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, +req.params.videoId)
|
2018-11-14 09:01:28 -05:00
|
|
|
if (!rate) {
|
2021-05-31 19:36:53 -04:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.NOT_FOUND_404,
|
|
|
|
message: 'Video rate not found'
|
|
|
|
})
|
2018-11-14 09:01:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.accountVideoRate = rate
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2019-04-09 05:02:02 -04:00
|
|
|
const videoRatingValidator = [
|
|
|
|
query('rating').optional().custom(isRatingValid).withMessage('Value must be one of "like" or "dislike"'),
|
|
|
|
|
2020-01-31 10:56:52 -05:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2019-04-09 05:02:02 -04:00
|
|
|
logger.debug('Checking rating parameter', { parameters: req.params })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-11-14 09:01:28 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
videoUpdateRateValidator,
|
2020-01-29 09:17:42 -05:00
|
|
|
getAccountVideoRateValidatorFactory,
|
2019-04-09 05:02:02 -04:00
|
|
|
videoRatingValidator
|
2018-11-14 09:01:28 -05:00
|
|
|
}
|