1 import * as express from 'express'
2 import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model'
3 import { getFormattedObjects, retryTransactionWrapper } from '../../../helpers'
4 import { sequelizeTypescript } from '../../../initializers'
5 import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment'
6 import { asyncMiddleware, authenticate, paginationValidator, setPagination, setVideoCommentThreadsSort } from '../../../middlewares'
7 import { videoCommentThreadsSortValidator } from '../../../middlewares/validators'
9 addVideoCommentReplyValidator, addVideoCommentThreadValidator, listVideoCommentThreadsValidator,
10 listVideoThreadCommentsValidator
11 } from '../../../middlewares/validators/video-comments'
12 import { VideoCommentModel } from '../../../models/video/video-comment'
14 const videoCommentRouter = express.Router()
16 videoCommentRouter.get('/:videoId/comment-threads',
18 videoCommentThreadsSortValidator,
19 setVideoCommentThreadsSort,
21 asyncMiddleware(listVideoCommentThreadsValidator),
22 asyncMiddleware(listVideoThreads)
24 videoCommentRouter.get('/:videoId/comment-threads/:threadId',
25 asyncMiddleware(listVideoThreadCommentsValidator),
26 asyncMiddleware(listVideoThreadComments)
29 videoCommentRouter.post('/:videoId/comment-threads',
31 asyncMiddleware(addVideoCommentThreadValidator),
32 asyncMiddleware(addVideoCommentThreadRetryWrapper)
34 videoCommentRouter.post('/:videoId/comments/:commentId',
36 asyncMiddleware(addVideoCommentReplyValidator),
37 asyncMiddleware(addVideoCommentReplyRetryWrapper)
40 // ---------------------------------------------------------------------------
46 // ---------------------------------------------------------------------------
48 async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) {
49 const resultList = await VideoCommentModel.listThreadsForApi(res.locals.video.id, req.query.start, req.query.count, req.query.sort)
51 return res.json(getFormattedObjects(resultList.data, resultList.total))
54 async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) {
55 const resultList = await VideoCommentModel.listThreadCommentsForApi(res.locals.video.id, res.locals.videoCommentThread.id)
57 return res.json(buildFormattedCommentTree(resultList))
60 async function addVideoCommentThreadRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
62 arguments: [ req, res ],
63 errorMessage: 'Cannot insert the video comment thread with many retries.'
66 const comment = await retryTransactionWrapper(addVideoCommentThread, options)
75 function addVideoCommentThread (req: express.Request, res: express.Response) {
76 const videoCommentInfo: VideoCommentCreate = req.body
78 return sequelizeTypescript.transaction(async t => {
79 return createVideoComment({
80 text: videoCommentInfo.text,
81 inReplyToComment: null,
82 video: res.locals.video,
83 actorId: res.locals.oauth.token.User.Account.Actor.id
88 async function addVideoCommentReplyRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
90 arguments: [ req, res ],
91 errorMessage: 'Cannot insert the video comment reply with many retries.'
94 const comment = await retryTransactionWrapper(addVideoCommentReply, options)
103 function addVideoCommentReply (req: express.Request, res: express.Response, next: express.NextFunction) {
104 const videoCommentInfo: VideoCommentCreate = req.body
106 return sequelizeTypescript.transaction(async t => {
107 return createVideoComment({
108 text: videoCommentInfo.text,
109 inReplyToComment: res.locals.videoComment.id,
110 video: res.locals.video,
111 actorId: res.locals.oauth.token.User.Account.Actor.id