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