]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/comment.ts
Add registered setting CLI plugin install test
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / comment.ts
1 import * as express from 'express'
2 import { ResultList, UserRight } from '../../../../shared/models'
3 import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model'
4 import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
5 import { getFormattedObjects } from '../../../helpers/utils'
6 import { sequelizeTypescript } from '../../../initializers/database'
7 import { Notifier } from '../../../lib/notifier'
8 import { Hooks } from '../../../lib/plugins/hooks'
9 import { buildFormattedCommentTree, createVideoComment, removeComment } from '../../../lib/video-comment'
10 import {
11 asyncMiddleware,
12 asyncRetryTransactionMiddleware,
13 authenticate,
14 ensureUserHasRight,
15 optionalAuthenticate,
16 paginationValidator,
17 setDefaultPagination,
18 setDefaultSort
19 } from '../../../middlewares'
20 import {
21 addVideoCommentReplyValidator,
22 addVideoCommentThreadValidator,
23 listVideoCommentsValidator,
24 listVideoCommentThreadsValidator,
25 listVideoThreadCommentsValidator,
26 removeVideoCommentValidator,
27 videoCommentsValidator,
28 videoCommentThreadsSortValidator
29 } from '../../../middlewares/validators'
30 import { AccountModel } from '../../../models/account/account'
31 import { VideoCommentModel } from '../../../models/video/video-comment'
32
33 const auditLogger = auditLoggerFactory('comments')
34 const videoCommentRouter = express.Router()
35
36 videoCommentRouter.get('/:videoId/comment-threads',
37 paginationValidator,
38 videoCommentThreadsSortValidator,
39 setDefaultSort,
40 setDefaultPagination,
41 asyncMiddleware(listVideoCommentThreadsValidator),
42 optionalAuthenticate,
43 asyncMiddleware(listVideoThreads)
44 )
45 videoCommentRouter.get('/:videoId/comment-threads/:threadId',
46 asyncMiddleware(listVideoThreadCommentsValidator),
47 optionalAuthenticate,
48 asyncMiddleware(listVideoThreadComments)
49 )
50
51 videoCommentRouter.post('/:videoId/comment-threads',
52 authenticate,
53 asyncMiddleware(addVideoCommentThreadValidator),
54 asyncRetryTransactionMiddleware(addVideoCommentThread)
55 )
56 videoCommentRouter.post('/:videoId/comments/:commentId',
57 authenticate,
58 asyncMiddleware(addVideoCommentReplyValidator),
59 asyncRetryTransactionMiddleware(addVideoCommentReply)
60 )
61 videoCommentRouter.delete('/:videoId/comments/:commentId',
62 authenticate,
63 asyncMiddleware(removeVideoCommentValidator),
64 asyncRetryTransactionMiddleware(removeVideoComment)
65 )
66
67 videoCommentRouter.get('/comments',
68 authenticate,
69 ensureUserHasRight(UserRight.SEE_ALL_COMMENTS),
70 paginationValidator,
71 videoCommentsValidator,
72 setDefaultSort,
73 setDefaultPagination,
74 listVideoCommentsValidator,
75 asyncMiddleware(listComments)
76 )
77
78 // ---------------------------------------------------------------------------
79
80 export {
81 videoCommentRouter
82 }
83
84 // ---------------------------------------------------------------------------
85
86 async function listComments (req: express.Request, res: express.Response) {
87 const options = {
88 start: req.query.start,
89 count: req.query.count,
90 sort: req.query.sort,
91
92 isLocal: req.query.isLocal,
93 search: req.query.search,
94 searchAccount: req.query.searchAccount,
95 searchVideo: req.query.searchVideo
96 }
97
98 const resultList = await VideoCommentModel.listCommentsForApi(options)
99
100 return res.json({
101 total: resultList.total,
102 data: resultList.data.map(c => c.toFormattedAdminJSON())
103 })
104 }
105
106 async function listVideoThreads (req: express.Request, res: express.Response) {
107 const video = res.locals.onlyVideo
108 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
109
110 let resultList: ResultList<VideoCommentModel>
111
112 if (video.commentsEnabled === true) {
113 const apiOptions = await Hooks.wrapObject({
114 videoId: video.id,
115 isVideoOwned: video.isOwned(),
116 start: req.query.start,
117 count: req.query.count,
118 sort: req.query.sort,
119 user
120 }, 'filter:api.video-threads.list.params')
121
122 resultList = await Hooks.wrapPromiseFun(
123 VideoCommentModel.listThreadsForApi,
124 apiOptions,
125 'filter:api.video-threads.list.result'
126 )
127 } else {
128 resultList = {
129 total: 0,
130 data: []
131 }
132 }
133
134 return res.json(getFormattedObjects(resultList.data, resultList.total))
135 }
136
137 async function listVideoThreadComments (req: express.Request, res: express.Response) {
138 const video = res.locals.onlyVideo
139 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
140
141 let resultList: ResultList<VideoCommentModel>
142
143 if (video.commentsEnabled === true) {
144 const apiOptions = await Hooks.wrapObject({
145 videoId: video.id,
146 isVideoOwned: video.isOwned(),
147 threadId: res.locals.videoCommentThread.id,
148 user
149 }, 'filter:api.video-thread-comments.list.params')
150
151 resultList = await Hooks.wrapPromiseFun(
152 VideoCommentModel.listThreadCommentsForApi,
153 apiOptions,
154 'filter:api.video-thread-comments.list.result'
155 )
156 } else {
157 resultList = {
158 total: 0,
159 data: []
160 }
161 }
162
163 return res.json(buildFormattedCommentTree(resultList))
164 }
165
166 async function addVideoCommentThread (req: express.Request, res: express.Response) {
167 const videoCommentInfo: VideoCommentCreate = req.body
168
169 const comment = await sequelizeTypescript.transaction(async t => {
170 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
171
172 return createVideoComment({
173 text: videoCommentInfo.text,
174 inReplyToComment: null,
175 video: res.locals.videoAll,
176 account
177 }, t)
178 })
179
180 Notifier.Instance.notifyOnNewComment(comment)
181 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
182
183 Hooks.runAction('action:api.video-thread.created', { comment })
184
185 return res.json({ comment: comment.toFormattedJSON() })
186 }
187
188 async function addVideoCommentReply (req: express.Request, res: express.Response) {
189 const videoCommentInfo: VideoCommentCreate = req.body
190
191 const comment = await sequelizeTypescript.transaction(async t => {
192 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
193
194 return createVideoComment({
195 text: videoCommentInfo.text,
196 inReplyToComment: res.locals.videoCommentFull,
197 video: res.locals.videoAll,
198 account
199 }, t)
200 })
201
202 Notifier.Instance.notifyOnNewComment(comment)
203 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
204
205 Hooks.runAction('action:api.video-comment-reply.created', { comment })
206
207 return res.json({ comment: comment.toFormattedJSON() })
208 }
209
210 async function removeVideoComment (req: express.Request, res: express.Response) {
211 const videoCommentInstance = res.locals.videoCommentFull
212
213 await removeComment(videoCommentInstance)
214
215 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
216
217 return res.type('json').status(204).end()
218 }