1 import * as express from 'express'
2 import { ResultList } 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,
18 } from '../../../middlewares'
20 addVideoCommentReplyValidator,
21 addVideoCommentThreadValidator,
22 listVideoCommentThreadsValidator,
23 listVideoThreadCommentsValidator,
24 removeVideoCommentValidator,
25 videoCommentThreadsSortValidator
26 } from '../../../middlewares/validators'
27 import { AccountModel } from '../../../models/account/account'
28 import { VideoCommentModel } from '../../../models/video/video-comment'
30 const auditLogger = auditLoggerFactory('comments')
31 const videoCommentRouter = express.Router()
33 videoCommentRouter.get('/:videoId/comment-threads',
35 videoCommentThreadsSortValidator,
38 asyncMiddleware(listVideoCommentThreadsValidator),
40 asyncMiddleware(listVideoThreads)
42 videoCommentRouter.get('/:videoId/comment-threads/:threadId',
43 asyncMiddleware(listVideoThreadCommentsValidator),
45 asyncMiddleware(listVideoThreadComments)
48 videoCommentRouter.post('/:videoId/comment-threads',
50 asyncMiddleware(addVideoCommentThreadValidator),
51 asyncRetryTransactionMiddleware(addVideoCommentThread)
53 videoCommentRouter.post('/:videoId/comments/:commentId',
55 asyncMiddleware(addVideoCommentReplyValidator),
56 asyncRetryTransactionMiddleware(addVideoCommentReply)
58 videoCommentRouter.delete('/:videoId/comments/:commentId',
60 asyncMiddleware(removeVideoCommentValidator),
61 asyncRetryTransactionMiddleware(removeVideoComment)
64 // ---------------------------------------------------------------------------
70 // ---------------------------------------------------------------------------
72 async function listVideoThreads (req: express.Request, res: express.Response) {
73 const video = res.locals.onlyVideo
74 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
76 let resultList: ResultList<VideoCommentModel>
78 if (video.commentsEnabled === true) {
79 const apiOptions = await Hooks.wrapObject({
81 isVideoOwned: video.isOwned(),
82 start: req.query.start,
83 count: req.query.count,
86 }, 'filter:api.video-threads.list.params')
88 resultList = await Hooks.wrapPromiseFun(
89 VideoCommentModel.listThreadsForApi,
91 'filter:api.video-threads.list.result'
100 return res.json(getFormattedObjects(resultList.data, resultList.total))
103 async function listVideoThreadComments (req: express.Request, res: express.Response) {
104 const video = res.locals.onlyVideo
105 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
107 let resultList: ResultList<VideoCommentModel>
109 if (video.commentsEnabled === true) {
110 const apiOptions = await Hooks.wrapObject({
112 isVideoOwned: video.isOwned(),
113 threadId: res.locals.videoCommentThread.id,
115 }, 'filter:api.video-thread-comments.list.params')
117 resultList = await Hooks.wrapPromiseFun(
118 VideoCommentModel.listThreadCommentsForApi,
120 'filter:api.video-thread-comments.list.result'
129 return res.json(buildFormattedCommentTree(resultList))
132 async function addVideoCommentThread (req: express.Request, res: express.Response) {
133 const videoCommentInfo: VideoCommentCreate = req.body
135 const comment = await sequelizeTypescript.transaction(async t => {
136 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
138 return createVideoComment({
139 text: videoCommentInfo.text,
140 inReplyToComment: null,
141 video: res.locals.videoAll,
146 Notifier.Instance.notifyOnNewComment(comment)
147 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
149 Hooks.runAction('action:api.video-thread.created', { comment })
151 return res.json({ comment: comment.toFormattedJSON() })
154 async function addVideoCommentReply (req: express.Request, res: express.Response) {
155 const videoCommentInfo: VideoCommentCreate = req.body
157 const comment = await sequelizeTypescript.transaction(async t => {
158 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
160 return createVideoComment({
161 text: videoCommentInfo.text,
162 inReplyToComment: res.locals.videoCommentFull,
163 video: res.locals.videoAll,
168 Notifier.Instance.notifyOnNewComment(comment)
169 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
171 Hooks.runAction('action:api.video-comment-reply.created', { comment })
173 return res.json({ comment: comment.toFormattedJSON() })
176 async function removeVideoComment (req: express.Request, res: express.Response) {
177 const videoCommentInstance = res.locals.videoCommentFull
179 await removeComment(videoCommentInstance)
181 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
183 return res.type('json').status(204).end()