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'
17 addVideoCommentReplyValidator,
18 addVideoCommentThreadValidator,
19 listVideoCommentThreadsValidator,
20 listVideoThreadCommentsValidator,
21 removeVideoCommentValidator,
22 videoCommentThreadsSortValidator
23 } from '../../../middlewares/validators'
24 import { VideoModel } from '../../../models/video/video'
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 { UserModel } from '../../../models/account/user'
30 const auditLogger = auditLoggerFactory('comments')
31 const videoCommentRouter = express.Router()
33 videoCommentRouter.get('/:videoId/comment-threads',
35 videoCommentThreadsSortValidator,
38 asyncMiddleware(listVideoCommentThreadsValidator),
39 asyncMiddleware(listVideoThreads)
41 videoCommentRouter.get('/:videoId/comment-threads/:threadId',
42 asyncMiddleware(listVideoThreadCommentsValidator),
43 asyncMiddleware(listVideoThreadComments)
46 videoCommentRouter.post('/:videoId/comment-threads',
48 asyncMiddleware(addVideoCommentThreadValidator),
49 asyncRetryTransactionMiddleware(addVideoCommentThread)
51 videoCommentRouter.post('/:videoId/comments/:commentId',
53 asyncMiddleware(addVideoCommentReplyValidator),
54 asyncRetryTransactionMiddleware(addVideoCommentReply)
56 videoCommentRouter.delete('/:videoId/comments/:commentId',
58 asyncMiddleware(removeVideoCommentValidator),
59 asyncRetryTransactionMiddleware(removeVideoComment)
62 // ---------------------------------------------------------------------------
68 // ---------------------------------------------------------------------------
70 async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) {
71 const video = res.locals.video as VideoModel
72 let resultList: ResultList<VideoCommentModel>
74 if (video.commentsEnabled === true) {
75 resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort)
83 return res.json(getFormattedObjects(resultList.data, resultList.total))
86 async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) {
87 const video = res.locals.video as VideoModel
88 let resultList: ResultList<VideoCommentModel>
90 if (video.commentsEnabled === true) {
91 resultList = await VideoCommentModel.listThreadCommentsForApi(video.id, res.locals.videoCommentThread.id)
99 return res.json(buildFormattedCommentTree(resultList))
102 async function addVideoCommentThread (req: express.Request, res: express.Response) {
103 const videoCommentInfo: VideoCommentCreate = req.body
105 const comment = await sequelizeTypescript.transaction(async t => {
106 const account = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t)
108 return createVideoComment({
109 text: videoCommentInfo.text,
110 inReplyToComment: null,
111 video: res.locals.video,
116 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
119 comment: comment.toFormattedJSON()
123 async function addVideoCommentReply (req: express.Request, res: express.Response) {
124 const videoCommentInfo: VideoCommentCreate = req.body
126 const comment = await sequelizeTypescript.transaction(async t => {
127 const account = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t)
129 return createVideoComment({
130 text: videoCommentInfo.text,
131 inReplyToComment: res.locals.videoComment,
132 video: res.locals.video,
137 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
139 return res.json({ comment: comment.toFormattedJSON() }).end()
142 async function removeVideoComment (req: express.Request, res: express.Response) {
143 const videoCommentInstance: VideoCommentModel = res.locals.videoComment
145 await sequelizeTypescript.transaction(async t => {
146 await videoCommentInstance.destroy({ transaction: t })
150 getAuditIdFromRes(res),
151 new CommentAuditView(videoCommentInstance.toFormattedJSON())
153 logger.info('Video comment %d deleted.', videoCommentInstance.id)
155 return res.type('json').status(204).end()