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