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'
29 import { Notifier } from '../../../lib/notifier'
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, next: express.NextFunction) {
74 const video = res.locals.video as VideoModel
75 const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : undefined
77 let resultList: ResultList<VideoCommentModel>
79 if (video.commentsEnabled === true) {
80 resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort, user)
88 return res.json(getFormattedObjects(resultList.data, resultList.total))
91 async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) {
92 const video = res.locals.video as VideoModel
93 const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : undefined
95 let resultList: ResultList<VideoCommentModel>
97 if (video.commentsEnabled === true) {
98 resultList = await VideoCommentModel.listThreadCommentsForApi(video.id, res.locals.videoCommentThread.id, user)
106 return res.json(buildFormattedCommentTree(resultList))
109 async function addVideoCommentThread (req: express.Request, res: express.Response) {
110 const videoCommentInfo: VideoCommentCreate = req.body
112 const comment = await sequelizeTypescript.transaction(async t => {
113 const account = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t)
115 return createVideoComment({
116 text: videoCommentInfo.text,
117 inReplyToComment: null,
118 video: res.locals.video,
123 Notifier.Instance.notifyOnNewComment(comment)
124 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
127 comment: comment.toFormattedJSON()
131 async function addVideoCommentReply (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 as UserModel).Account.id, t)
137 return createVideoComment({
138 text: videoCommentInfo.text,
139 inReplyToComment: res.locals.videoComment,
140 video: res.locals.video,
145 Notifier.Instance.notifyOnNewComment(comment)
146 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
148 return res.json({ comment: comment.toFormattedJSON() }).end()
151 async function removeVideoComment (req: express.Request, res: express.Response) {
152 const videoCommentInstance: VideoCommentModel = res.locals.videoComment
154 await sequelizeTypescript.transaction(async t => {
155 await videoCommentInstance.destroy({ transaction: t })
159 getAuditIdFromRes(res),
160 new CommentAuditView(videoCommentInstance.toFormattedJSON())
162 logger.info('Video comment %d deleted.', videoCommentInstance.id)
164 return res.type('json').status(204).end()