comment.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import * as express from 'express'
  2. import { ResultList } from '../../../../shared/models'
  3. import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model'
  4. import { logger } from '../../../helpers/logger'
  5. import { getFormattedObjects } from '../../../helpers/utils'
  6. import { sequelizeTypescript } from '../../../initializers'
  7. import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment'
  8. import {
  9. asyncMiddleware,
  10. asyncRetryTransactionMiddleware,
  11. authenticate,
  12. optionalAuthenticate,
  13. paginationValidator,
  14. setDefaultPagination,
  15. setDefaultSort
  16. } from '../../../middlewares'
  17. import {
  18. addVideoCommentReplyValidator,
  19. addVideoCommentThreadValidator,
  20. listVideoCommentThreadsValidator,
  21. listVideoThreadCommentsValidator,
  22. removeVideoCommentValidator,
  23. videoCommentThreadsSortValidator
  24. } from '../../../middlewares/validators'
  25. import { VideoCommentModel } from '../../../models/video/video-comment'
  26. import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
  27. import { AccountModel } from '../../../models/account/account'
  28. import { Notifier } from '../../../lib/notifier'
  29. const auditLogger = auditLoggerFactory('comments')
  30. const videoCommentRouter = express.Router()
  31. videoCommentRouter.get('/:videoId/comment-threads',
  32. paginationValidator,
  33. videoCommentThreadsSortValidator,
  34. setDefaultSort,
  35. setDefaultPagination,
  36. asyncMiddleware(listVideoCommentThreadsValidator),
  37. optionalAuthenticate,
  38. asyncMiddleware(listVideoThreads)
  39. )
  40. videoCommentRouter.get('/:videoId/comment-threads/:threadId',
  41. asyncMiddleware(listVideoThreadCommentsValidator),
  42. optionalAuthenticate,
  43. asyncMiddleware(listVideoThreadComments)
  44. )
  45. videoCommentRouter.post('/:videoId/comment-threads',
  46. authenticate,
  47. asyncMiddleware(addVideoCommentThreadValidator),
  48. asyncRetryTransactionMiddleware(addVideoCommentThread)
  49. )
  50. videoCommentRouter.post('/:videoId/comments/:commentId',
  51. authenticate,
  52. asyncMiddleware(addVideoCommentReplyValidator),
  53. asyncRetryTransactionMiddleware(addVideoCommentReply)
  54. )
  55. videoCommentRouter.delete('/:videoId/comments/:commentId',
  56. authenticate,
  57. asyncMiddleware(removeVideoCommentValidator),
  58. asyncRetryTransactionMiddleware(removeVideoComment)
  59. )
  60. // ---------------------------------------------------------------------------
  61. export {
  62. videoCommentRouter
  63. }
  64. // ---------------------------------------------------------------------------
  65. async function listVideoThreads (req: express.Request, res: express.Response) {
  66. const video = res.locals.video
  67. const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
  68. let resultList: ResultList<VideoCommentModel>
  69. if (video.commentsEnabled === true) {
  70. resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort, user)
  71. } else {
  72. resultList = {
  73. total: 0,
  74. data: []
  75. }
  76. }
  77. return res.json(getFormattedObjects(resultList.data, resultList.total))
  78. }
  79. async function listVideoThreadComments (req: express.Request, res: express.Response) {
  80. const video = res.locals.video
  81. const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
  82. let resultList: ResultList<VideoCommentModel>
  83. if (video.commentsEnabled === true) {
  84. resultList = await VideoCommentModel.listThreadCommentsForApi(video.id, res.locals.videoCommentThread.id, user)
  85. } else {
  86. resultList = {
  87. total: 0,
  88. data: []
  89. }
  90. }
  91. return res.json(buildFormattedCommentTree(resultList))
  92. }
  93. async function addVideoCommentThread (req: express.Request, res: express.Response) {
  94. const videoCommentInfo: VideoCommentCreate = req.body
  95. const comment = await sequelizeTypescript.transaction(async t => {
  96. const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
  97. return createVideoComment({
  98. text: videoCommentInfo.text,
  99. inReplyToComment: null,
  100. video: res.locals.video,
  101. account
  102. }, t)
  103. })
  104. Notifier.Instance.notifyOnNewComment(comment)
  105. auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
  106. return res.json({
  107. comment: comment.toFormattedJSON()
  108. }).end()
  109. }
  110. async function addVideoCommentReply (req: express.Request, res: express.Response) {
  111. const videoCommentInfo: VideoCommentCreate = req.body
  112. const comment = await sequelizeTypescript.transaction(async t => {
  113. const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
  114. return createVideoComment({
  115. text: videoCommentInfo.text,
  116. inReplyToComment: res.locals.videoComment,
  117. video: res.locals.video,
  118. account
  119. }, t)
  120. })
  121. Notifier.Instance.notifyOnNewComment(comment)
  122. auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
  123. return res.json({ comment: comment.toFormattedJSON() }).end()
  124. }
  125. async function removeVideoComment (req: express.Request, res: express.Response) {
  126. const videoCommentInstance = res.locals.videoComment
  127. await sequelizeTypescript.transaction(async t => {
  128. await videoCommentInstance.destroy({ transaction: t })
  129. })
  130. auditLogger.delete(
  131. getAuditIdFromRes(res),
  132. new CommentAuditView(videoCommentInstance.toFormattedJSON())
  133. )
  134. logger.info('Video comment %d deleted.', videoCommentInstance.id)
  135. return res.type('json').status(204).end()
  136. }