2
1

video-comments.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import * as express from 'express'
  2. import validator from 'validator'
  3. import { VideoCommentModel } from '@server/models/video/video-comment'
  4. import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
  5. import { MVideoId } from '@server/types/models'
  6. import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
  7. const VIDEO_COMMENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_COMMENTS
  8. function isValidVideoCommentText (value: string) {
  9. return value === null || validator.isLength(value, VIDEO_COMMENTS_CONSTRAINTS_FIELDS.TEXT)
  10. }
  11. async function doesVideoCommentThreadExist (idArg: number | string, video: MVideoId, res: express.Response) {
  12. const id = parseInt(idArg + '', 10)
  13. const videoComment = await VideoCommentModel.loadById(id)
  14. if (!videoComment) {
  15. res.status(HttpStatusCode.NOT_FOUND_404)
  16. .json({ error: 'Video comment thread not found' })
  17. .end()
  18. return false
  19. }
  20. if (videoComment.videoId !== video.id) {
  21. res.status(HttpStatusCode.BAD_REQUEST_400)
  22. .json({ error: 'Video comment is not associated to this video.' })
  23. .end()
  24. return false
  25. }
  26. if (videoComment.inReplyToCommentId !== null) {
  27. res.status(HttpStatusCode.BAD_REQUEST_400)
  28. .json({ error: 'Video comment is not a thread.' })
  29. .end()
  30. return false
  31. }
  32. res.locals.videoCommentThread = videoComment
  33. return true
  34. }
  35. async function doesVideoCommentExist (idArg: number | string, video: MVideoId, res: express.Response) {
  36. const id = parseInt(idArg + '', 10)
  37. const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
  38. if (!videoComment) {
  39. res.status(HttpStatusCode.NOT_FOUND_404)
  40. .json({ error: 'Video comment thread not found' })
  41. .end()
  42. return false
  43. }
  44. if (videoComment.videoId !== video.id) {
  45. res.status(HttpStatusCode.BAD_REQUEST_400)
  46. .json({ error: 'Video comment is not associated to this video.' })
  47. .end()
  48. return false
  49. }
  50. res.locals.videoCommentFull = videoComment
  51. return true
  52. }
  53. async function doesCommentIdExist (idArg: number | string, res: express.Response) {
  54. const id = parseInt(idArg + '', 10)
  55. const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
  56. if (!videoComment) {
  57. res.status(HttpStatusCode.NOT_FOUND_404)
  58. .json({ error: 'Video comment thread not found' })
  59. return false
  60. }
  61. res.locals.videoCommentFull = videoComment
  62. return true
  63. }
  64. // ---------------------------------------------------------------------------
  65. export {
  66. isValidVideoCommentText,
  67. doesVideoCommentThreadExist,
  68. doesVideoCommentExist,
  69. doesCommentIdExist
  70. }