2021-08-27 07:32:44 -05:00
|
|
|
import express from 'express'
|
2019-07-25 09:23:44 -05:00
|
|
|
import { body, param, query } from 'express-validator'
|
2021-07-16 03:42:24 -05:00
|
|
|
import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
|
2021-06-03 10:33:44 -05:00
|
|
|
import { VideoRateType } from '../../../../shared/models/videos'
|
|
|
|
import { isAccountNameValid } from '../../../helpers/custom-validators/accounts'
|
2021-06-28 10:30:59 -05:00
|
|
|
import { isIdValid } from '../../../helpers/custom-validators/misc'
|
2019-04-09 04:02:02 -05:00
|
|
|
import { isRatingValid } from '../../../helpers/custom-validators/video-rates'
|
2019-07-23 03:40:39 -05:00
|
|
|
import { isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos'
|
2018-11-14 08:01:28 -06:00
|
|
|
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
|
2023-06-29 02:48:55 -05:00
|
|
|
import { areValidationErrors, checkCanSeeVideo, doesVideoExist, isValidVideoIdParam, isValidVideoPasswordHeader } from '../shared'
|
2018-11-14 08:01:28 -06:00
|
|
|
|
|
|
|
const videoUpdateRateValidator = [
|
2021-06-28 10:30:59 -05:00
|
|
|
isValidVideoIdParam('id'),
|
|
|
|
|
2022-08-17 07:27:04 -05:00
|
|
|
body('rating')
|
|
|
|
.custom(isVideoRatingTypeValid),
|
2023-06-29 02:48:55 -05:00
|
|
|
isValidVideoPasswordHeader(),
|
2018-11-14 08:01:28 -06:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
2019-03-19 03:26:50 -05:00
|
|
|
if (!await doesVideoExist(req.params.id, res)) return
|
2018-11-14 08:01:28 -06:00
|
|
|
|
2022-06-22 02:44:08 -05:00
|
|
|
if (!await checkCanSeeVideo({ req, res, paramId: req.params.id, video: res.locals.videoAll })) return
|
2022-02-22 07:16:34 -06:00
|
|
|
|
2018-11-14 08:01:28 -06:00
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2020-01-29 08:17:42 -06:00
|
|
|
const getAccountVideoRateValidatorFactory = function (rateType: VideoRateType) {
|
2018-11-14 08:01:28 -06:00
|
|
|
return [
|
2022-08-17 07:27:04 -05:00
|
|
|
param('name')
|
|
|
|
.custom(isAccountNameValid),
|
|
|
|
param('videoId')
|
|
|
|
.custom(isIdValid),
|
2018-11-14 08:01:28 -06:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
2021-02-26 09:26:27 -06:00
|
|
|
const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, +req.params.videoId)
|
2018-11-14 08:01:28 -06:00
|
|
|
if (!rate) {
|
2021-05-31 18:36:53 -05:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.NOT_FOUND_404,
|
|
|
|
message: 'Video rate not found'
|
|
|
|
})
|
2018-11-14 08:01:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.accountVideoRate = rate
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2019-04-09 04:02:02 -05:00
|
|
|
const videoRatingValidator = [
|
2022-08-17 07:27:04 -05:00
|
|
|
query('rating')
|
|
|
|
.optional()
|
|
|
|
.custom(isRatingValid).withMessage('Value must be one of "like" or "dislike"'),
|
2019-04-09 04:02:02 -05:00
|
|
|
|
2020-01-31 09:56:52 -06:00
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2019-04-09 04:02:02 -05:00
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-11-14 08:01:28 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
videoUpdateRateValidator,
|
2020-01-29 08:17:42 -06:00
|
|
|
getAccountVideoRateValidatorFactory,
|
2019-04-09 04:02:02 -05:00
|
|
|
videoRatingValidator
|
2018-11-14 08:01:28 -06:00
|
|
|
}
|