video-rates.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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, checkCanSeeVideoIfPrivate, doesVideoExist, isValidVideoIdParam } from '../shared'
  12. const videoUpdateRateValidator = [
  13. isValidVideoIdParam('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. if (!await checkCanSeeVideoIfPrivate(req, res, res.locals.videoAll)) return
  20. return next()
  21. }
  22. ]
  23. const getAccountVideoRateValidatorFactory = function (rateType: VideoRateType) {
  24. return [
  25. param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
  26. param('videoId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid videoId'),
  27. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  28. logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
  29. if (areValidationErrors(req, res)) return
  30. const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, +req.params.videoId)
  31. if (!rate) {
  32. return res.fail({
  33. status: HttpStatusCode.NOT_FOUND_404,
  34. message: 'Video rate not found'
  35. })
  36. }
  37. res.locals.accountVideoRate = rate
  38. return next()
  39. }
  40. ]
  41. }
  42. const videoRatingValidator = [
  43. query('rating').optional().custom(isRatingValid).withMessage('Value must be one of "like" or "dislike"'),
  44. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  45. logger.debug('Checking rating parameter', { parameters: req.params })
  46. if (areValidationErrors(req, res)) return
  47. return next()
  48. }
  49. ]
  50. // ---------------------------------------------------------------------------
  51. export {
  52. videoUpdateRateValidator,
  53. getAccountVideoRateValidatorFactory,
  54. videoRatingValidator
  55. }