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