video-rates.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import * as express from 'express'
  2. import 'express-validator'
  3. import { body, param, query } from 'express-validator/check'
  4. import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc'
  5. import { isRatingValid } from '../../../helpers/custom-validators/video-rates'
  6. import { doesVideoExist, isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos'
  7. import { logger } from '../../../helpers/logger'
  8. import { areValidationErrors } from '../utils'
  9. import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
  10. import { VideoRateType } from '../../../../shared/models/videos'
  11. import { isAccountNameValid } from '../../../helpers/custom-validators/accounts'
  12. const videoUpdateRateValidator = [
  13. param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
  14. body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
  15. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  16. logger.debug('Checking videoRate parameters', { parameters: req.body })
  17. if (areValidationErrors(req, res)) return
  18. if (!await doesVideoExist(req.params.id, res)) return
  19. return next()
  20. }
  21. ]
  22. const getAccountVideoRateValidator = function (rateType: VideoRateType) {
  23. return [
  24. param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
  25. param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
  26. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  27. logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
  28. if (areValidationErrors(req, res)) return
  29. const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, req.params.videoId)
  30. if (!rate) {
  31. return res.status(404)
  32. .json({ error: 'Video rate not found' })
  33. .end()
  34. }
  35. res.locals.accountVideoRate = rate
  36. return next()
  37. }
  38. ]
  39. }
  40. const videoRatingValidator = [
  41. query('rating').optional().custom(isRatingValid).withMessage('Value must be one of "like" or "dislike"'),
  42. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  43. logger.debug('Checking rating parameter', { parameters: req.params })
  44. if (areValidationErrors(req, res)) return
  45. return next()
  46. }
  47. ]
  48. // ---------------------------------------------------------------------------
  49. export {
  50. videoUpdateRateValidator,
  51. getAccountVideoRateValidator,
  52. videoRatingValidator
  53. }