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,
11 authenticate, optionalAuthenticate,
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),
40 asyncMiddleware(listVideoThreads)
42 videoCommentRouter.get('/:videoId/comment-threads/:threadId',
43 asyncMiddleware(listVideoThreadCommentsValidator),
45 asyncMiddleware(listVideoThreadComments)
48 videoCommentRouter.post('/:videoId/comment-threads',
50 asyncMiddleware(addVideoCommentThreadValidator),
51 asyncRetryTransactionMiddleware(addVideoCommentThread)
53 videoCommentRouter.post('/:videoId/comments/:commentId',
55 asyncMiddleware(addVideoCommentReplyValidator),
56 asyncRetryTransactionMiddleware(addVideoCommentReply)
58 videoCommentRouter.delete('/:videoId/comments/:commentId',
60 asyncMiddleware(removeVideoCommentValidator),
61 asyncRetryTransactionMiddleware(removeVideoComment)
64 // ---------------------------------------------------------------------------
70 // ---------------------------------------------------------------------------
72 async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) {
73 const video = res.locals.video as VideoModel
74 const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : undefined
76 let resultList: ResultList<VideoCommentModel>
78 if (video.commentsEnabled === true) {
79 resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort, user)
87 return res.json(getFormattedObjects(resultList.data, resultList.total))
90 async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) {
91 const video = res.locals.video as VideoModel
92 const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : undefined
94 let resultList: ResultList<VideoCommentModel>
96 if (video.commentsEnabled === true) {
97 resultList = await VideoCommentModel.listThreadCommentsForApi(video.id, res.locals.videoCommentThread.id, user)
105 return res.json(buildFormattedCommentTree(resultList))
108 async function addVideoCommentThread (req: express.Request, res: express.Response) {
109 const videoCommentInfo: VideoCommentCreate = req.body
111 const comment = await sequelizeTypescript.transaction(async t => {
112 const account = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t)
114 return createVideoComment({
115 text: videoCommentInfo.text,
116 inReplyToComment: null,
117 video: res.locals.video,
122 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
125 comment: comment.toFormattedJSON()
129 async function addVideoCommentReply (req: express.Request, res: express.Response) {
130 const videoCommentInfo: VideoCommentCreate = req.body
132 const comment = await sequelizeTypescript.transaction(async t => {
133 const account = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t)
135 return createVideoComment({
136 text: videoCommentInfo.text,
137 inReplyToComment: res.locals.videoComment,
138 video: res.locals.video,
143 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
145 return res.json({ comment: comment.toFormattedJSON() }).end()
148 async function removeVideoComment (req: express.Request, res: express.Response) {
149 const videoCommentInstance: VideoCommentModel = res.locals.videoComment
151 await sequelizeTypescript.transaction(async t => {
152 await videoCommentInstance.destroy({ transaction: t })
156 getAuditIdFromRes(res),
157 new CommentAuditView(videoCommentInstance.toFormattedJSON())
159 logger.info('Video comment %d deleted.', videoCommentInstance.id)
161 return res.type('json').status(204).end()