]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/comment.ts
Move youtubeDL upgrader in helpers/
[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 25import { VideoCommentModel } from '../../../models/video/video-comment'
80e36cd9 26import { auditLoggerFactory, CommentAuditView } from '../../../helpers/audit-logger'
bf1f6508 27
80e36cd9 28const auditLogger = auditLoggerFactory('comments')
bf1f6508
C
29const videoCommentRouter = express.Router()
30
31videoCommentRouter.get('/:videoId/comment-threads',
32 paginationValidator,
33 videoCommentThreadsSortValidator,
1174a847 34 setDefaultSort,
f05a1c30 35 setDefaultPagination,
bf1f6508
C
36 asyncMiddleware(listVideoCommentThreadsValidator),
37 asyncMiddleware(listVideoThreads)
38)
39videoCommentRouter.get('/:videoId/comment-threads/:threadId',
40 asyncMiddleware(listVideoThreadCommentsValidator),
41 asyncMiddleware(listVideoThreadComments)
42)
43
44videoCommentRouter.post('/:videoId/comment-threads',
45 authenticate,
46 asyncMiddleware(addVideoCommentThreadValidator),
90d4bb81 47 asyncRetryTransactionMiddleware(addVideoCommentThread)
bf1f6508
C
48)
49videoCommentRouter.post('/:videoId/comments/:commentId',
50 authenticate,
51 asyncMiddleware(addVideoCommentReplyValidator),
90d4bb81 52 asyncRetryTransactionMiddleware(addVideoCommentReply)
bf1f6508 53)
4cb6d457
C
54videoCommentRouter.delete('/:videoId/comments/:commentId',
55 authenticate,
56 asyncMiddleware(removeVideoCommentValidator),
90d4bb81 57 asyncRetryTransactionMiddleware(removeVideoComment)
4cb6d457 58)
bf1f6508
C
59
60// ---------------------------------------------------------------------------
61
62export {
63 videoCommentRouter
64}
65
66// ---------------------------------------------------------------------------
67
68async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) {
47564bbe
C
69 const video = res.locals.video as VideoModel
70 let resultList: ResultList<VideoCommentModel>
71
72 if (video.commentsEnabled === true) {
73 resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort)
74 } else {
75 resultList = {
76 total: 0,
77 data: []
78 }
79 }
bf1f6508
C
80
81 return res.json(getFormattedObjects(resultList.data, resultList.total))
82}
83
84async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) {
47564bbe
C
85 const video = res.locals.video as VideoModel
86 let resultList: ResultList<VideoCommentModel>
87
88 if (video.commentsEnabled === true) {
627621c1 89 resultList = await VideoCommentModel.listThreadCommentsForApi(video.id, res.locals.videoCommentThread.id)
47564bbe
C
90 } else {
91 resultList = {
92 total: 0,
93 data: []
94 }
95 }
bf1f6508
C
96
97 return res.json(buildFormattedCommentTree(resultList))
98}
99
90d4bb81 100async function addVideoCommentThread (req: express.Request, res: express.Response) {
bf1f6508
C
101 const videoCommentInfo: VideoCommentCreate = req.body
102
90d4bb81 103 const comment = await sequelizeTypescript.transaction(async t => {
bf1f6508
C
104 return createVideoComment({
105 text: videoCommentInfo.text,
ea44f375 106 inReplyToComment: null,
bf1f6508 107 video: res.locals.video,
4635f59d 108 account: res.locals.oauth.token.User.Account
bf1f6508
C
109 }, t)
110 })
bf1f6508 111
80e36cd9
AB
112 auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new CommentAuditView(comment.toFormattedJSON()))
113
90d4bb81 114 return res.json({
4635f59d 115 comment: comment.toFormattedJSON()
bf1f6508
C
116 }).end()
117}
118
90d4bb81 119async function addVideoCommentReply (req: express.Request, res: express.Response) {
bf1f6508
C
120 const videoCommentInfo: VideoCommentCreate = req.body
121
90d4bb81 122 const comment = await sequelizeTypescript.transaction(async t => {
bf1f6508
C
123 return createVideoComment({
124 text: videoCommentInfo.text,
ea44f375 125 inReplyToComment: res.locals.videoComment,
bf1f6508 126 video: res.locals.video,
4635f59d 127 account: res.locals.oauth.token.User.Account
bf1f6508
C
128 }, t)
129 })
4cb6d457 130
80e36cd9
AB
131 auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new CommentAuditView(comment.toFormattedJSON()))
132
90d4bb81
C
133 return res.json({
134 comment: comment.toFormattedJSON()
135 }).end()
4cb6d457
C
136}
137
138async function removeVideoComment (req: express.Request, res: express.Response) {
139 const videoCommentInstance: VideoCommentModel = res.locals.videoComment
140
141 await sequelizeTypescript.transaction(async t => {
142 await videoCommentInstance.destroy({ transaction: t })
143 })
144
80e36cd9
AB
145 auditLogger.delete(
146 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
147 new CommentAuditView(videoCommentInstance.toFormattedJSON())
148 )
4cb6d457 149 logger.info('Video comment %d deleted.', videoCommentInstance.id)
90d4bb81
C
150
151 return res.type('json').status(204).end()
4cb6d457 152}