]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/comment.ts
Add audit logs module
[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'
4cb6d457 4import { logger } from '../../../helpers/logger'
da854ddd 5import { getFormattedObjects } from '../../../helpers/utils'
bf1f6508
C
6import { sequelizeTypescript } from '../../../initializers'
7import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment'
90d4bb81
C
8import {
9 asyncMiddleware,
10 asyncRetryTransactionMiddleware,
11 authenticate,
12 paginationValidator,
13 setDefaultPagination,
14 setDefaultSort
15} from '../../../middlewares'
bf1f6508
C
16import { videoCommentThreadsSortValidator } from '../../../middlewares/validators'
17import {
90d4bb81
C
18 addVideoCommentReplyValidator,
19 addVideoCommentThreadValidator,
20 listVideoCommentThreadsValidator,
21 listVideoThreadCommentsValidator,
4cb6d457 22 removeVideoCommentValidator
bf1f6508 23} from '../../../middlewares/validators/video-comments'
47564bbe 24import { VideoModel } from '../../../models/video/video'
bf1f6508
C
25import { VideoCommentModel } from '../../../models/video/video-comment'
26
27const videoCommentRouter = express.Router()
28
29videoCommentRouter.get('/:videoId/comment-threads',
30 paginationValidator,
31 videoCommentThreadsSortValidator,
1174a847 32 setDefaultSort,
f05a1c30 33 setDefaultPagination,
bf1f6508
C
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),
90d4bb81 45 asyncRetryTransactionMiddleware(addVideoCommentThread)
bf1f6508
C
46)
47videoCommentRouter.post('/:videoId/comments/:commentId',
48 authenticate,
49 asyncMiddleware(addVideoCommentReplyValidator),
90d4bb81 50 asyncRetryTransactionMiddleware(addVideoCommentReply)
bf1f6508 51)
4cb6d457
C
52videoCommentRouter.delete('/:videoId/comments/:commentId',
53 authenticate,
54 asyncMiddleware(removeVideoCommentValidator),
90d4bb81 55 asyncRetryTransactionMiddleware(removeVideoComment)
4cb6d457 56)
bf1f6508
C
57
58// ---------------------------------------------------------------------------
59
60export {
61 videoCommentRouter
62}
63
64// ---------------------------------------------------------------------------
65
66async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) {
47564bbe
C
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 }
bf1f6508
C
78
79 return res.json(getFormattedObjects(resultList.data, resultList.total))
80}
81
82async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) {
47564bbe
C
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 }
bf1f6508
C
94
95 return res.json(buildFormattedCommentTree(resultList))
96}
97
90d4bb81 98async function addVideoCommentThread (req: express.Request, res: express.Response) {
bf1f6508
C
99 const videoCommentInfo: VideoCommentCreate = req.body
100
90d4bb81 101 const comment = await sequelizeTypescript.transaction(async t => {
bf1f6508
C
102 return createVideoComment({
103 text: videoCommentInfo.text,
ea44f375 104 inReplyToComment: null,
bf1f6508 105 video: res.locals.video,
4635f59d 106 account: res.locals.oauth.token.User.Account
bf1f6508
C
107 }, t)
108 })
bf1f6508 109
90d4bb81 110 return res.json({
4635f59d 111 comment: comment.toFormattedJSON()
bf1f6508
C
112 }).end()
113}
114
90d4bb81 115async function addVideoCommentReply (req: express.Request, res: express.Response) {
bf1f6508
C
116 const videoCommentInfo: VideoCommentCreate = req.body
117
90d4bb81 118 const comment = await sequelizeTypescript.transaction(async t => {
bf1f6508
C
119 return createVideoComment({
120 text: videoCommentInfo.text,
ea44f375 121 inReplyToComment: res.locals.videoComment,
bf1f6508 122 video: res.locals.video,
4635f59d 123 account: res.locals.oauth.token.User.Account
bf1f6508
C
124 }, t)
125 })
4cb6d457 126
90d4bb81
C
127 return res.json({
128 comment: comment.toFormattedJSON()
129 }).end()
4cb6d457
C
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)
90d4bb81
C
140
141 return res.type('json').status(204).end()
4cb6d457 142}