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'
10 asyncRetryTransactionMiddleware,
16 } from '../../../middlewares'
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'
31 const auditLogger = auditLoggerFactory('comments')
32 const videoCommentRouter = express.Router()
34 videoCommentRouter.get('/:videoId/comment-threads',
36 videoCommentThreadsSortValidator,
39 asyncMiddleware(listVideoCommentThreadsValidator),
41 asyncMiddleware(listVideoThreads)
43 videoCommentRouter.get('/:videoId/comment-threads/:threadId',
44 asyncMiddleware(listVideoThreadCommentsValidator),
46 asyncMiddleware(listVideoThreadComments)
49 videoCommentRouter.post('/:videoId/comment-threads',
51 asyncMiddleware(addVideoCommentThreadValidator),
52 asyncRetryTransactionMiddleware(addVideoCommentThread)
54 videoCommentRouter.post('/:videoId/comments/:commentId',
56 asyncMiddleware(addVideoCommentReplyValidator),
57 asyncRetryTransactionMiddleware(addVideoCommentReply)
59 videoCommentRouter.delete('/:videoId/comments/:commentId',
61 asyncMiddleware(removeVideoCommentValidator),
62 asyncRetryTransactionMiddleware(removeVideoComment)
65 // ---------------------------------------------------------------------------
71 // ---------------------------------------------------------------------------
73 async function listVideoThreads (req: express.Request, res: express.Response) {
74 const video = res.locals.video
75 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
77 let resultList: ResultList<VideoCommentModel>
79 if (video.commentsEnabled === true) {
80 const apiOptions = await Hooks.wrapObject({
82 start: req.query.start,
83 count: req.query.count,
86 }, 'filter:api.video-threads.list.params')
88 resultList = await Hooks.wrapPromiseFun(
89 VideoCommentModel.listThreadsForApi,
91 'filter:api.video-threads.list.result'
100 return res.json(getFormattedObjects(resultList.data, resultList.total))
103 async function listVideoThreadComments (req: express.Request, res: express.Response) {
104 const video = res.locals.video
105 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
107 let resultList: ResultList<VideoCommentModel>
109 if (video.commentsEnabled === true) {
110 const apiOptions = await Hooks.wrapObject({
112 threadId: res.locals.videoCommentThread.id,
114 }, 'filter:api.video-thread-comments.list.params')
116 resultList = await Hooks.wrapPromiseFun(
117 VideoCommentModel.listThreadCommentsForApi,
119 'filter:api.video-thread-comments.list.result'
128 return res.json(buildFormattedCommentTree(resultList))
131 async function addVideoCommentThread (req: express.Request, res: express.Response) {
132 const videoCommentInfo: VideoCommentCreate = req.body
134 const comment = await sequelizeTypescript.transaction(async t => {
135 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
137 return createVideoComment({
138 text: videoCommentInfo.text,
139 inReplyToComment: null,
140 video: res.locals.video,
145 Notifier.Instance.notifyOnNewComment(comment)
146 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
148 Hooks.runAction('action:api.video-thread.created', { comment })
151 comment: comment.toFormattedJSON()
155 async function addVideoCommentReply (req: express.Request, res: express.Response) {
156 const videoCommentInfo: VideoCommentCreate = req.body
158 const comment = await sequelizeTypescript.transaction(async t => {
159 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
161 return createVideoComment({
162 text: videoCommentInfo.text,
163 inReplyToComment: res.locals.videoComment,
164 video: res.locals.video,
169 Notifier.Instance.notifyOnNewComment(comment)
170 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
172 Hooks.runAction('action:api.video-comment-reply.created', { comment })
174 return res.json({ comment: comment.toFormattedJSON() }).end()
177 async function removeVideoComment (req: express.Request, res: express.Response) {
178 const videoCommentInstance = res.locals.videoComment
180 await sequelizeTypescript.transaction(async t => {
181 await videoCommentInstance.destroy({ transaction: t })
184 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
185 logger.info('Video comment %d deleted.', videoCommentInstance.id)
187 Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstance })
189 return res.type('json').status(204).end()