video-comments.ts 2.2 KB

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