]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/comment.ts
a95392543e1bce89b8b7a608b0460cca57e24e11
[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 optionalAuthenticate,
13 paginationValidator,
14 setDefaultPagination,
15 setDefaultSort
16 } from '../../../middlewares'
17 import {
18 addVideoCommentReplyValidator,
19 addVideoCommentThreadValidator,
20 listVideoCommentThreadsValidator,
21 listVideoThreadCommentsValidator,
22 removeVideoCommentValidator,
23 videoCommentThreadsSortValidator
24 } from '../../../middlewares/validators'
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 { Notifier } from '../../../lib/notifier'
29 import { Hooks } from '../../../lib/plugins/hooks'
30
31 const auditLogger = auditLoggerFactory('comments')
32 const videoCommentRouter = express.Router()
33
34 videoCommentRouter.get('/:videoId/comment-threads',
35 paginationValidator,
36 videoCommentThreadsSortValidator,
37 setDefaultSort,
38 setDefaultPagination,
39 asyncMiddleware(listVideoCommentThreadsValidator),
40 optionalAuthenticate,
41 asyncMiddleware(listVideoThreads)
42 )
43 videoCommentRouter.get('/:videoId/comment-threads/:threadId',
44 asyncMiddleware(listVideoThreadCommentsValidator),
45 optionalAuthenticate,
46 asyncMiddleware(listVideoThreadComments)
47 )
48
49 videoCommentRouter.post('/:videoId/comment-threads',
50 authenticate,
51 asyncMiddleware(addVideoCommentThreadValidator),
52 asyncRetryTransactionMiddleware(addVideoCommentThread)
53 )
54 videoCommentRouter.post('/:videoId/comments/:commentId',
55 authenticate,
56 asyncMiddleware(addVideoCommentReplyValidator),
57 asyncRetryTransactionMiddleware(addVideoCommentReply)
58 )
59 videoCommentRouter.delete('/:videoId/comments/:commentId',
60 authenticate,
61 asyncMiddleware(removeVideoCommentValidator),
62 asyncRetryTransactionMiddleware(removeVideoComment)
63 )
64
65 // ---------------------------------------------------------------------------
66
67 export {
68 videoCommentRouter
69 }
70
71 // ---------------------------------------------------------------------------
72
73 async function listVideoThreads (req: express.Request, res: express.Response) {
74 const video = res.locals.video
75 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
76
77 let resultList: ResultList<VideoCommentModel>
78
79 if (video.commentsEnabled === true) {
80 const apiOptions = await Hooks.wrapObject({
81 videoId: video.id,
82 start: req.query.start,
83 count: req.query.count,
84 sort: req.query.sort,
85 user: user
86 }, 'filter:api.video-threads.list.params')
87
88 resultList = await Hooks.wrapPromise(
89 VideoCommentModel.listThreadsForApi(apiOptions),
90 'filter:api.video-threads.list.result'
91 )
92 } else {
93 resultList = {
94 total: 0,
95 data: []
96 }
97 }
98
99 return res.json(getFormattedObjects(resultList.data, resultList.total))
100 }
101
102 async function listVideoThreadComments (req: express.Request, res: express.Response) {
103 const video = res.locals.video
104 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
105
106 let resultList: ResultList<VideoCommentModel>
107
108 if (video.commentsEnabled === true) {
109 const apiOptions = await Hooks.wrapObject({
110 videoId: video.id,
111 threadId: res.locals.videoCommentThread.id,
112 user: user
113 }, 'filter:api.video-thread-comments.list.params')
114
115 resultList = await Hooks.wrapPromise(
116 VideoCommentModel.listThreadCommentsForApi(apiOptions),
117 'filter:api.video-thread-comments.list.result'
118 )
119 } else {
120 resultList = {
121 total: 0,
122 data: []
123 }
124 }
125
126 return res.json(buildFormattedCommentTree(resultList))
127 }
128
129 async function addVideoCommentThread (req: express.Request, res: express.Response) {
130 const videoCommentInfo: VideoCommentCreate = req.body
131
132 const comment = await sequelizeTypescript.transaction(async t => {
133 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
134
135 return createVideoComment({
136 text: videoCommentInfo.text,
137 inReplyToComment: null,
138 video: res.locals.video,
139 account
140 }, t)
141 })
142
143 Notifier.Instance.notifyOnNewComment(comment)
144 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
145
146 Hooks.runAction('action:api.video-thread.created', { comment })
147
148 return res.json({
149 comment: comment.toFormattedJSON()
150 }).end()
151 }
152
153 async function addVideoCommentReply (req: express.Request, res: express.Response) {
154 const videoCommentInfo: VideoCommentCreate = req.body
155
156 const comment = await sequelizeTypescript.transaction(async t => {
157 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
158
159 return createVideoComment({
160 text: videoCommentInfo.text,
161 inReplyToComment: res.locals.videoComment,
162 video: res.locals.video,
163 account
164 }, t)
165 })
166
167 Notifier.Instance.notifyOnNewComment(comment)
168 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
169
170 Hooks.runAction('action:api.video-comment-reply.created', { comment })
171
172 return res.json({ comment: comment.toFormattedJSON() }).end()
173 }
174
175 async function removeVideoComment (req: express.Request, res: express.Response) {
176 const videoCommentInstance = res.locals.videoComment
177
178 await sequelizeTypescript.transaction(async t => {
179 await videoCommentInstance.destroy({ transaction: t })
180 })
181
182 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
183 logger.info('Video comment %d deleted.', videoCommentInstance.id)
184
185 Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstance })
186
187 return res.type('json').status(204).end()
188 }