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