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