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,
15 } from '../../../middlewares'
16 import { videoCommentThreadsSortValidator } from '../../../middlewares/validators'
18 addVideoCommentReplyValidator,
19 addVideoCommentThreadValidator,
20 listVideoCommentThreadsValidator,
21 listVideoThreadCommentsValidator,
22 removeVideoCommentValidator
23 } from '../../../middlewares/validators/video-comments'
24 import { VideoModel } from '../../../models/video/video'
25 import { VideoCommentModel } from '../../../models/video/video-comment'
26 import { auditLoggerFactory, CommentAuditView } from '../../../helpers/audit-logger'
28 const auditLogger = auditLoggerFactory('comments')
29 const videoCommentRouter = express.Router()
31 videoCommentRouter.get('/:videoId/comment-threads',
33 videoCommentThreadsSortValidator,
36 asyncMiddleware(listVideoCommentThreadsValidator),
37 asyncMiddleware(listVideoThreads)
39 videoCommentRouter.get('/:videoId/comment-threads/:threadId',
40 asyncMiddleware(listVideoThreadCommentsValidator),
41 asyncMiddleware(listVideoThreadComments)
44 videoCommentRouter.post('/:videoId/comment-threads',
46 asyncMiddleware(addVideoCommentThreadValidator),
47 asyncRetryTransactionMiddleware(addVideoCommentThread)
49 videoCommentRouter.post('/:videoId/comments/:commentId',
51 asyncMiddleware(addVideoCommentReplyValidator),
52 asyncRetryTransactionMiddleware(addVideoCommentReply)
54 videoCommentRouter.delete('/:videoId/comments/:commentId',
56 asyncMiddleware(removeVideoCommentValidator),
57 asyncRetryTransactionMiddleware(removeVideoComment)
60 // ---------------------------------------------------------------------------
66 // ---------------------------------------------------------------------------
68 async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) {
69 const video = res.locals.video as VideoModel
70 let resultList: ResultList<VideoCommentModel>
72 if (video.commentsEnabled === true) {
73 resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort)
81 return res.json(getFormattedObjects(resultList.data, resultList.total))
84 async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) {
85 const video = res.locals.video as VideoModel
86 let resultList: ResultList<VideoCommentModel>
88 if (video.commentsEnabled === true) {
89 resultList = await VideoCommentModel.listThreadCommentsForApi(res.locals.video.id, res.locals.videoCommentThread.id)
97 return res.json(buildFormattedCommentTree(resultList))
100 async function addVideoCommentThread (req: express.Request, res: express.Response) {
101 const videoCommentInfo: VideoCommentCreate = req.body
103 const comment = await sequelizeTypescript.transaction(async t => {
104 return createVideoComment({
105 text: videoCommentInfo.text,
106 inReplyToComment: null,
107 video: res.locals.video,
108 account: res.locals.oauth.token.User.Account
112 auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new CommentAuditView(comment.toFormattedJSON()))
115 comment: comment.toFormattedJSON()
119 async function addVideoCommentReply (req: express.Request, res: express.Response) {
120 const videoCommentInfo: VideoCommentCreate = req.body
122 const comment = await sequelizeTypescript.transaction(async t => {
123 return createVideoComment({
124 text: videoCommentInfo.text,
125 inReplyToComment: res.locals.videoComment,
126 video: res.locals.video,
127 account: res.locals.oauth.token.User.Account
131 auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new CommentAuditView(comment.toFormattedJSON()))
134 comment: comment.toFormattedJSON()
138 async function removeVideoComment (req: express.Request, res: express.Response) {
139 const videoCommentInstance: VideoCommentModel = res.locals.videoComment
141 await sequelizeTypescript.transaction(async t => {
142 await videoCommentInstance.destroy({ transaction: t })
146 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
147 new CommentAuditView(videoCommentInstance.toFormattedJSON())
149 logger.info('Video comment %d deleted.', videoCommentInstance.id)
151 return res.type('json').status(204).end()