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 { ActorModel } from '../../../models/activitypub/actor'
31 import { VideoChannelModel } from '../../../models/video/video-channel'
32 import { VideoModel } from '../../../models/video/video'
33 import { sendDeleteVideoComment } from '../../../lib/activitypub/send'
35 const auditLogger = auditLoggerFactory('comments')
36 const videoCommentRouter = express.Router()
38 videoCommentRouter.get('/:videoId/comment-threads',
40 videoCommentThreadsSortValidator,
43 asyncMiddleware(listVideoCommentThreadsValidator),
45 asyncMiddleware(listVideoThreads)
47 videoCommentRouter.get('/:videoId/comment-threads/:threadId',
48 asyncMiddleware(listVideoThreadCommentsValidator),
50 asyncMiddleware(listVideoThreadComments)
53 videoCommentRouter.post('/:videoId/comment-threads',
55 asyncMiddleware(addVideoCommentThreadValidator),
56 asyncRetryTransactionMiddleware(addVideoCommentThread)
58 videoCommentRouter.post('/:videoId/comments/:commentId',
60 asyncMiddleware(addVideoCommentReplyValidator),
61 asyncRetryTransactionMiddleware(addVideoCommentReply)
63 videoCommentRouter.delete('/:videoId/comments/:commentId',
65 asyncMiddleware(removeVideoCommentValidator),
66 asyncRetryTransactionMiddleware(removeVideoComment)
69 // ---------------------------------------------------------------------------
75 // ---------------------------------------------------------------------------
77 async function listVideoThreads (req: express.Request, res: express.Response) {
78 const video = res.locals.video
79 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
81 let resultList: ResultList<VideoCommentModel>
83 if (video.commentsEnabled === true) {
84 const apiOptions = await Hooks.wrapObject({
86 start: req.query.start,
87 count: req.query.count,
90 }, 'filter:api.video-threads.list.params')
92 resultList = await Hooks.wrapPromiseFun(
93 VideoCommentModel.listThreadsForApi,
95 'filter:api.video-threads.list.result'
104 return res.json(getFormattedObjects(resultList.data, resultList.total))
107 async function listVideoThreadComments (req: express.Request, res: express.Response) {
108 const video = res.locals.video
109 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
111 let resultList: ResultList<VideoCommentModel>
113 if (video.commentsEnabled === true) {
114 const apiOptions = await Hooks.wrapObject({
116 threadId: res.locals.videoCommentThread.id,
118 }, 'filter:api.video-thread-comments.list.params')
120 resultList = await Hooks.wrapPromiseFun(
121 VideoCommentModel.listThreadCommentsForApi,
123 'filter:api.video-thread-comments.list.result'
132 return res.json(buildFormattedCommentTree(resultList))
135 async function addVideoCommentThread (req: express.Request, res: express.Response) {
136 const videoCommentInfo: VideoCommentCreate = req.body
138 const comment = await sequelizeTypescript.transaction(async t => {
139 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
141 return createVideoComment({
142 text: videoCommentInfo.text,
143 inReplyToComment: null,
144 video: res.locals.video,
149 Notifier.Instance.notifyOnNewComment(comment)
150 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
152 Hooks.runAction('action:api.video-thread.created', { comment })
155 comment: comment.toFormattedJSON()
159 async function addVideoCommentReply (req: express.Request, res: express.Response) {
160 const videoCommentInfo: VideoCommentCreate = req.body
162 const comment = await sequelizeTypescript.transaction(async t => {
163 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
165 return createVideoComment({
166 text: videoCommentInfo.text,
167 inReplyToComment: res.locals.videoComment,
168 video: res.locals.video,
173 Notifier.Instance.notifyOnNewComment(comment)
174 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
176 Hooks.runAction('action:api.video-comment-reply.created', { comment })
178 return res.json({ comment: comment.toFormattedJSON() }).end()
181 async function removeVideoComment (req: express.Request, res: express.Response) {
182 const videoCommentInstance = res.locals.videoComment
184 await sequelizeTypescript.transaction(async t => {
185 await videoCommentInstance.destroy({ transaction: t })
187 if (videoCommentInstance.isOwned() || videoCommentInstance.Video.isOwned()) {
188 await sendDeleteVideoComment(videoCommentInstance, t)
192 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
193 logger.info('Video comment %d deleted.', videoCommentInstance.id)
195 Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstance })
197 return res.type('json').status(204).end()