video-comments.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import * as express from 'express'
  2. import { body, param } from 'express-validator/check'
  3. import { UserRight } from '../../../../shared'
  4. import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc'
  5. import { isValidVideoCommentText } from '../../../helpers/custom-validators/video-comments'
  6. import { doesVideoExist } from '../../../helpers/custom-validators/videos'
  7. import { logger } from '../../../helpers/logger'
  8. import { UserModel } from '../../../models/account/user'
  9. import { VideoModel } from '../../../models/video/video'
  10. import { VideoCommentModel } from '../../../models/video/video-comment'
  11. import { areValidationErrors } from '../utils'
  12. const listVideoCommentThreadsValidator = [
  13. param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
  14. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  15. logger.debug('Checking listVideoCommentThreads parameters.', { parameters: req.params })
  16. if (areValidationErrors(req, res)) return
  17. if (!await doesVideoExist(req.params.videoId, res, 'only-video')) return
  18. return next()
  19. }
  20. ]
  21. const listVideoThreadCommentsValidator = [
  22. param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
  23. param('threadId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid threadId'),
  24. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  25. logger.debug('Checking listVideoThreadComments parameters.', { parameters: req.params })
  26. if (areValidationErrors(req, res)) return
  27. if (!await doesVideoExist(req.params.videoId, res, 'only-video')) return
  28. if (!await doesVideoCommentThreadExist(req.params.threadId, res.locals.video, res)) return
  29. return next()
  30. }
  31. ]
  32. const addVideoCommentThreadValidator = [
  33. param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
  34. body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'),
  35. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  36. logger.debug('Checking addVideoCommentThread parameters.', { parameters: req.params, body: req.body })
  37. if (areValidationErrors(req, res)) return
  38. if (!await doesVideoExist(req.params.videoId, res)) return
  39. if (!isVideoCommentsEnabled(res.locals.video, res)) return
  40. return next()
  41. }
  42. ]
  43. const addVideoCommentReplyValidator = [
  44. param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
  45. param('commentId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'),
  46. body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'),
  47. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  48. logger.debug('Checking addVideoCommentReply parameters.', { parameters: req.params, body: req.body })
  49. if (areValidationErrors(req, res)) return
  50. if (!await doesVideoExist(req.params.videoId, res)) return
  51. if (!isVideoCommentsEnabled(res.locals.video, res)) return
  52. if (!await doesVideoCommentExist(req.params.commentId, res.locals.video, res)) return
  53. return next()
  54. }
  55. ]
  56. const videoCommentGetValidator = [
  57. param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
  58. param('commentId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'),
  59. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  60. logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
  61. if (areValidationErrors(req, res)) return
  62. if (!await doesVideoExist(req.params.videoId, res, 'id')) return
  63. if (!await doesVideoCommentExist(req.params.commentId, res.locals.video, res)) return
  64. return next()
  65. }
  66. ]
  67. const removeVideoCommentValidator = [
  68. param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
  69. param('commentId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'),
  70. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  71. logger.debug('Checking removeVideoCommentValidator parameters.', { parameters: req.params })
  72. if (areValidationErrors(req, res)) return
  73. if (!await doesVideoExist(req.params.videoId, res)) return
  74. if (!await doesVideoCommentExist(req.params.commentId, res.locals.video, res)) return
  75. // Check if the user who did the request is able to delete the video
  76. if (!checkUserCanDeleteVideoComment(res.locals.oauth.token.User, res.locals.videoComment, res)) return
  77. return next()
  78. }
  79. ]
  80. // ---------------------------------------------------------------------------
  81. export {
  82. listVideoCommentThreadsValidator,
  83. listVideoThreadCommentsValidator,
  84. addVideoCommentThreadValidator,
  85. addVideoCommentReplyValidator,
  86. videoCommentGetValidator,
  87. removeVideoCommentValidator
  88. }
  89. // ---------------------------------------------------------------------------
  90. async function doesVideoCommentThreadExist (id: number, video: VideoModel, res: express.Response) {
  91. const videoComment = await VideoCommentModel.loadById(id)
  92. if (!videoComment) {
  93. res.status(404)
  94. .json({ error: 'Video comment thread not found' })
  95. .end()
  96. return false
  97. }
  98. if (videoComment.videoId !== video.id) {
  99. res.status(400)
  100. .json({ error: 'Video comment is associated to this video.' })
  101. .end()
  102. return false
  103. }
  104. if (videoComment.inReplyToCommentId !== null) {
  105. res.status(400)
  106. .json({ error: 'Video comment is not a thread.' })
  107. .end()
  108. return false
  109. }
  110. res.locals.videoCommentThread = videoComment
  111. return true
  112. }
  113. async function doesVideoCommentExist (id: number, video: VideoModel, res: express.Response) {
  114. const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
  115. if (!videoComment) {
  116. res.status(404)
  117. .json({ error: 'Video comment thread not found' })
  118. .end()
  119. return false
  120. }
  121. if (videoComment.videoId !== video.id) {
  122. res.status(400)
  123. .json({ error: 'Video comment is associated to this video.' })
  124. .end()
  125. return false
  126. }
  127. res.locals.videoComment = videoComment
  128. return true
  129. }
  130. function isVideoCommentsEnabled (video: VideoModel, res: express.Response) {
  131. if (video.commentsEnabled !== true) {
  132. res.status(409)
  133. .json({ error: 'Video comments are disabled for this video.' })
  134. .end()
  135. return false
  136. }
  137. return true
  138. }
  139. function checkUserCanDeleteVideoComment (user: UserModel, videoComment: VideoCommentModel, res: express.Response) {
  140. const account = videoComment.Account
  141. if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT) === false && account.userId !== user.id) {
  142. res.status(403)
  143. .json({ error: 'Cannot remove video comment of another user' })
  144. .end()
  145. return false
  146. }
  147. return true
  148. }