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'
10 asyncRetryTransactionMiddleware,
16 } from '../../../middlewares'
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'
32 const auditLogger = auditLoggerFactory('comments')
33 const videoCommentRouter = express.Router()
35 videoCommentRouter.get('/:videoId/comment-threads',
37 videoCommentThreadsSortValidator,
40 asyncMiddleware(listVideoCommentThreadsValidator),
42 asyncMiddleware(listVideoThreads)
44 videoCommentRouter.get('/:videoId/comment-threads/:threadId',
45 asyncMiddleware(listVideoThreadCommentsValidator),
47 asyncMiddleware(listVideoThreadComments)
50 videoCommentRouter.post('/:videoId/comment-threads',
52 asyncMiddleware(addVideoCommentThreadValidator),
53 asyncRetryTransactionMiddleware(addVideoCommentThread)
55 videoCommentRouter.post('/:videoId/comments/:commentId',
57 asyncMiddleware(addVideoCommentReplyValidator),
58 asyncRetryTransactionMiddleware(addVideoCommentReply)
60 videoCommentRouter.delete('/:videoId/comments/:commentId',
62 asyncMiddleware(removeVideoCommentValidator),
63 asyncRetryTransactionMiddleware(removeVideoComment)
66 // ---------------------------------------------------------------------------
72 // ---------------------------------------------------------------------------
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
78 let resultList: ResultList<VideoCommentModel>
80 if (video.commentsEnabled === true) {
81 const apiOptions = await Hooks.wrapObject({
83 start: req.query.start,
84 count: req.query.count,
87 }, 'filter:api.video-threads.list.params')
89 resultList = await Hooks.wrapPromiseFun(
90 VideoCommentModel.listThreadsForApi,
92 'filter:api.video-threads.list.result'
101 return res.json(getFormattedObjects(resultList.data, resultList.total))
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
108 let resultList: ResultList<VideoCommentModel>
110 if (video.commentsEnabled === true) {
111 const apiOptions = await Hooks.wrapObject({
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 })
152 comment: comment.toFormattedJSON()
156 async function addVideoCommentReply (req: express.Request, res: express.Response) {
157 const videoCommentInfo: VideoCommentCreate = req.body
159 const comment = await sequelizeTypescript.transaction(async t => {
160 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
162 return createVideoComment({
163 text: videoCommentInfo.text,
164 inReplyToComment: res.locals.videoCommentFull,
165 video: res.locals.videoAll,
170 Notifier.Instance.notifyOnNewComment(comment)
171 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
173 Hooks.runAction('action:api.video-comment-reply.created', { comment })
175 return res.json({ comment: comment.toFormattedJSON() }).end()
178 async function removeVideoComment (req: express.Request, res: express.Response) {
179 const videoCommentInstance = res.locals.videoCommentFull
181 await sequelizeTypescript.transaction(async t => {
182 await videoCommentInstance.destroy({ transaction: t })
184 if (videoCommentInstance.isOwned() || videoCommentInstance.Video.isOwned()) {
185 await sendDeleteVideoComment(videoCommentInstance, t)
189 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
190 logger.info('Video comment %d deleted.', videoCommentInstance.id)
192 Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstance })
194 return res.type('json').status(204).end()