]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/comment.ts
Add ability to disable video comments
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / comment.ts
CommitLineData
bf1f6508 1import * as express from 'express'
47564bbe 2import { ResultList } from '../../../../shared/models'
bf1f6508 3import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model'
da854ddd
C
4import { retryTransactionWrapper } from '../../../helpers/database-utils'
5import { getFormattedObjects } from '../../../helpers/utils'
bf1f6508
C
6import { sequelizeTypescript } from '../../../initializers'
7import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment'
8import { asyncMiddleware, authenticate, paginationValidator, setPagination, setVideoCommentThreadsSort } from '../../../middlewares'
9import { videoCommentThreadsSortValidator } from '../../../middlewares/validators'
10import {
11 addVideoCommentReplyValidator, addVideoCommentThreadValidator, listVideoCommentThreadsValidator,
12 listVideoThreadCommentsValidator
13} from '../../../middlewares/validators/video-comments'
47564bbe 14import { VideoModel } from '../../../models/video/video'
bf1f6508
C
15import { VideoCommentModel } from '../../../models/video/video-comment'
16
17const videoCommentRouter = express.Router()
18
19videoCommentRouter.get('/:videoId/comment-threads',
20 paginationValidator,
21 videoCommentThreadsSortValidator,
22 setVideoCommentThreadsSort,
23 setPagination,
24 asyncMiddleware(listVideoCommentThreadsValidator),
25 asyncMiddleware(listVideoThreads)
26)
27videoCommentRouter.get('/:videoId/comment-threads/:threadId',
28 asyncMiddleware(listVideoThreadCommentsValidator),
29 asyncMiddleware(listVideoThreadComments)
30)
31
32videoCommentRouter.post('/:videoId/comment-threads',
33 authenticate,
34 asyncMiddleware(addVideoCommentThreadValidator),
35 asyncMiddleware(addVideoCommentThreadRetryWrapper)
36)
37videoCommentRouter.post('/:videoId/comments/:commentId',
38 authenticate,
39 asyncMiddleware(addVideoCommentReplyValidator),
40 asyncMiddleware(addVideoCommentReplyRetryWrapper)
41)
42
43// ---------------------------------------------------------------------------
44
45export {
46 videoCommentRouter
47}
48
49// ---------------------------------------------------------------------------
50
51async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) {
47564bbe
C
52 const video = res.locals.video as VideoModel
53 let resultList: ResultList<VideoCommentModel>
54
55 if (video.commentsEnabled === true) {
56 resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort)
57 } else {
58 resultList = {
59 total: 0,
60 data: []
61 }
62 }
bf1f6508
C
63
64 return res.json(getFormattedObjects(resultList.data, resultList.total))
65}
66
67async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) {
47564bbe
C
68 const video = res.locals.video as VideoModel
69 let resultList: ResultList<VideoCommentModel>
70
71 if (video.commentsEnabled === true) {
72 resultList = await VideoCommentModel.listThreadCommentsForApi(res.locals.video.id, res.locals.videoCommentThread.id)
73 } else {
74 resultList = {
75 total: 0,
76 data: []
77 }
78 }
bf1f6508
C
79
80 return res.json(buildFormattedCommentTree(resultList))
81}
82
83async function addVideoCommentThreadRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
84 const options = {
85 arguments: [ req, res ],
86 errorMessage: 'Cannot insert the video comment thread with many retries.'
87 }
88
89 const comment = await retryTransactionWrapper(addVideoCommentThread, options)
90
91 res.json({
4635f59d 92 comment: comment.toFormattedJSON()
bf1f6508
C
93 }).end()
94}
95
96function addVideoCommentThread (req: express.Request, res: express.Response) {
97 const videoCommentInfo: VideoCommentCreate = req.body
98
99 return sequelizeTypescript.transaction(async t => {
100 return createVideoComment({
101 text: videoCommentInfo.text,
ea44f375 102 inReplyToComment: null,
bf1f6508 103 video: res.locals.video,
4635f59d 104 account: res.locals.oauth.token.User.Account
bf1f6508
C
105 }, t)
106 })
107}
108
109async function addVideoCommentReplyRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
110 const options = {
111 arguments: [ req, res ],
112 errorMessage: 'Cannot insert the video comment reply with many retries.'
113 }
114
115 const comment = await retryTransactionWrapper(addVideoCommentReply, options)
116
117 res.json({
4635f59d 118 comment: comment.toFormattedJSON()
bf1f6508
C
119 }).end()
120}
121
122function addVideoCommentReply (req: express.Request, res: express.Response, next: express.NextFunction) {
123 const videoCommentInfo: VideoCommentCreate = req.body
124
125 return sequelizeTypescript.transaction(async t => {
126 return createVideoComment({
127 text: videoCommentInfo.text,
ea44f375 128 inReplyToComment: res.locals.videoComment,
bf1f6508 129 video: res.locals.video,
4635f59d 130 account: res.locals.oauth.token.User.Account
bf1f6508
C
131 }, t)
132 })
133}