comment.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. import { Hooks } from '../../../lib/plugins/hooks'
  30. const auditLogger = auditLoggerFactory('comments')
  31. const videoCommentRouter = express.Router()
  32. videoCommentRouter.get('/:videoId/comment-threads',
  33. paginationValidator,
  34. videoCommentThreadsSortValidator,
  35. setDefaultSort,
  36. setDefaultPagination,
  37. asyncMiddleware(listVideoCommentThreadsValidator),
  38. optionalAuthenticate,
  39. asyncMiddleware(listVideoThreads)
  40. )
  41. videoCommentRouter.get('/:videoId/comment-threads/:threadId',
  42. asyncMiddleware(listVideoThreadCommentsValidator),
  43. optionalAuthenticate,
  44. asyncMiddleware(listVideoThreadComments)
  45. )
  46. videoCommentRouter.post('/:videoId/comment-threads',
  47. authenticate,
  48. asyncMiddleware(addVideoCommentThreadValidator),
  49. asyncRetryTransactionMiddleware(addVideoCommentThread)
  50. )
  51. videoCommentRouter.post('/:videoId/comments/:commentId',
  52. authenticate,
  53. asyncMiddleware(addVideoCommentReplyValidator),
  54. asyncRetryTransactionMiddleware(addVideoCommentReply)
  55. )
  56. videoCommentRouter.delete('/:videoId/comments/:commentId',
  57. authenticate,
  58. asyncMiddleware(removeVideoCommentValidator),
  59. asyncRetryTransactionMiddleware(removeVideoComment)
  60. )
  61. // ---------------------------------------------------------------------------
  62. export {
  63. videoCommentRouter
  64. }
  65. // ---------------------------------------------------------------------------
  66. async function listVideoThreads (req: express.Request, res: express.Response) {
  67. const video = res.locals.video
  68. const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
  69. let resultList: ResultList<VideoCommentModel>
  70. if (video.commentsEnabled === true) {
  71. const apiOptions = await Hooks.wrapObject({
  72. videoId: video.id,
  73. start: req.query.start,
  74. count: req.query.count,
  75. sort: req.query.sort,
  76. user: user
  77. }, 'filter:api.video-threads.list.params')
  78. resultList = await Hooks.wrapPromise(
  79. VideoCommentModel.listThreadsForApi(apiOptions),
  80. 'filter:api.video-threads.list.result'
  81. )
  82. } else {
  83. resultList = {
  84. total: 0,
  85. data: []
  86. }
  87. }
  88. return res.json(getFormattedObjects(resultList.data, resultList.total))
  89. }
  90. async function listVideoThreadComments (req: express.Request, res: express.Response) {
  91. const video = res.locals.video
  92. const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
  93. let resultList: ResultList<VideoCommentModel>
  94. if (video.commentsEnabled === true) {
  95. const apiOptions = await Hooks.wrapObject({
  96. videoId: video.id,
  97. threadId: res.locals.videoCommentThread.id,
  98. user: user
  99. }, 'filter:api.video-thread-comments.list.params')
  100. resultList = await Hooks.wrapPromise(
  101. VideoCommentModel.listThreadCommentsForApi(apiOptions),
  102. 'filter:api.video-thread-comments.list.result'
  103. )
  104. } else {
  105. resultList = {
  106. total: 0,
  107. data: []
  108. }
  109. }
  110. return res.json(buildFormattedCommentTree(resultList))
  111. }
  112. async function addVideoCommentThread (req: express.Request, res: express.Response) {
  113. const videoCommentInfo: VideoCommentCreate = req.body
  114. const comment = await sequelizeTypescript.transaction(async t => {
  115. const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
  116. return createVideoComment({
  117. text: videoCommentInfo.text,
  118. inReplyToComment: null,
  119. video: res.locals.video,
  120. account
  121. }, t)
  122. })
  123. Notifier.Instance.notifyOnNewComment(comment)
  124. auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
  125. Hooks.runAction('action:api.video-thread.created', { comment })
  126. return res.json({
  127. comment: comment.toFormattedJSON()
  128. }).end()
  129. }
  130. async function addVideoCommentReply (req: express.Request, res: express.Response) {
  131. const videoCommentInfo: VideoCommentCreate = req.body
  132. const comment = await sequelizeTypescript.transaction(async t => {
  133. const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
  134. return createVideoComment({
  135. text: videoCommentInfo.text,
  136. inReplyToComment: res.locals.videoComment,
  137. video: res.locals.video,
  138. account
  139. }, t)
  140. })
  141. Notifier.Instance.notifyOnNewComment(comment)
  142. auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
  143. Hooks.runAction('action:api.video-comment-reply.created', { comment })
  144. return res.json({ comment: comment.toFormattedJSON() }).end()
  145. }
  146. async function removeVideoComment (req: express.Request, res: express.Response) {
  147. const videoCommentInstance = res.locals.videoComment
  148. await sequelizeTypescript.transaction(async t => {
  149. await videoCommentInstance.destroy({ transaction: t })
  150. })
  151. auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
  152. logger.info('Video comment %d deleted.', videoCommentInstance.id)
  153. Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstance })
  154. return res.type('json').status(204).end()
  155. }