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'
12 asyncRetryTransactionMiddleware,
19 } from '../../../middlewares'
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'
33 const auditLogger = auditLoggerFactory('comments')
34 const videoCommentRouter = express.Router()
36 videoCommentRouter.get('/:videoId/comment-threads',
38 videoCommentThreadsSortValidator,
41 asyncMiddleware(listVideoCommentThreadsValidator),
43 asyncMiddleware(listVideoThreads)
45 videoCommentRouter.get('/:videoId/comment-threads/:threadId',
46 asyncMiddleware(listVideoThreadCommentsValidator),
48 asyncMiddleware(listVideoThreadComments)
51 videoCommentRouter.post('/:videoId/comment-threads',
53 asyncMiddleware(addVideoCommentThreadValidator),
54 asyncRetryTransactionMiddleware(addVideoCommentThread)
56 videoCommentRouter.post('/:videoId/comments/:commentId',
58 asyncMiddleware(addVideoCommentReplyValidator),
59 asyncRetryTransactionMiddleware(addVideoCommentReply)
61 videoCommentRouter.delete('/:videoId/comments/:commentId',
63 asyncMiddleware(removeVideoCommentValidator),
64 asyncRetryTransactionMiddleware(removeVideoComment)
67 videoCommentRouter.get('/comments',
69 ensureUserHasRight(UserRight.SEE_ALL_COMMENTS),
71 videoCommentsValidator,
74 listVideoCommentsValidator,
75 asyncMiddleware(listComments)
78 // ---------------------------------------------------------------------------
84 // ---------------------------------------------------------------------------
86 async function listComments (req: express.Request, res: express.Response) {
88 start: req.query.start,
89 count: req.query.count,
92 isLocal: req.query.isLocal,
93 search: req.query.search,
94 searchAccount: req.query.searchAccount,
95 searchVideo: req.query.searchVideo
98 const resultList = await VideoCommentModel.listCommentsForApi(options)
101 total: resultList.total,
102 data: resultList.data.map(c => c.toFormattedAdminJSON())
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
110 let resultList: ResultList<VideoCommentModel>
112 if (video.commentsEnabled === true) {
113 const apiOptions = await Hooks.wrapObject({
115 isVideoOwned: video.isOwned(),
116 start: req.query.start,
117 count: req.query.count,
118 sort: req.query.sort,
120 }, 'filter:api.video-threads.list.params')
122 resultList = await Hooks.wrapPromiseFun(
123 VideoCommentModel.listThreadsForApi,
125 'filter:api.video-threads.list.result'
134 return res.json(getFormattedObjects(resultList.data, resultList.total))
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
141 let resultList: ResultList<VideoCommentModel>
143 if (video.commentsEnabled === true) {
144 const apiOptions = await Hooks.wrapObject({
146 isVideoOwned: video.isOwned(),
147 threadId: res.locals.videoCommentThread.id,
149 }, 'filter:api.video-thread-comments.list.params')
151 resultList = await Hooks.wrapPromiseFun(
152 VideoCommentModel.listThreadCommentsForApi,
154 'filter:api.video-thread-comments.list.result'
163 return res.json(buildFormattedCommentTree(resultList))
166 async function addVideoCommentThread (req: express.Request, res: express.Response) {
167 const videoCommentInfo: VideoCommentCreate = req.body
169 const comment = await sequelizeTypescript.transaction(async t => {
170 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
172 return createVideoComment({
173 text: videoCommentInfo.text,
174 inReplyToComment: null,
175 video: res.locals.videoAll,
180 Notifier.Instance.notifyOnNewComment(comment)
181 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
183 Hooks.runAction('action:api.video-thread.created', { comment })
185 return res.json({ comment: comment.toFormattedJSON() })
188 async function addVideoCommentReply (req: express.Request, res: express.Response) {
189 const videoCommentInfo: VideoCommentCreate = req.body
191 const comment = await sequelizeTypescript.transaction(async t => {
192 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
194 return createVideoComment({
195 text: videoCommentInfo.text,
196 inReplyToComment: res.locals.videoCommentFull,
197 video: res.locals.videoAll,
202 Notifier.Instance.notifyOnNewComment(comment)
203 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
205 Hooks.runAction('action:api.video-comment-reply.created', { comment })
207 return res.json({ comment: comment.toFormattedJSON() })
210 async function removeVideoComment (req: express.Request, res: express.Response) {
211 const videoCommentInstance = res.locals.videoCommentFull
213 await removeComment(videoCommentInstance)
215 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
217 return res.type('json').status(204).end()