1 import express from 'express'
2 import { ResultList, ThreadsResultList, UserRight, VideoCommentCreate } from '../../../../shared/models'
3 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
4 import { VideoCommentThreads } from '../../../../shared/models/videos/comment/video-comment.model'
5 import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
6 import { getFormattedObjects } from '../../../helpers/utils'
7 import { sequelizeTypescript } from '../../../initializers/database'
8 import { Notifier } from '../../../lib/notifier'
9 import { Hooks } from '../../../lib/plugins/hooks'
10 import { buildFormattedCommentTree, createVideoComment, removeComment } from '../../../lib/video-comment'
13 asyncRetryTransactionMiddleware,
20 } from '../../../middlewares'
22 addVideoCommentReplyValidator,
23 addVideoCommentThreadValidator,
24 listVideoCommentsValidator,
25 listVideoCommentThreadsValidator,
26 listVideoThreadCommentsValidator,
27 removeVideoCommentValidator,
28 videoCommentsValidator,
29 videoCommentThreadsSortValidator
30 } from '../../../middlewares/validators'
31 import { AccountModel } from '../../../models/account/account'
32 import { VideoCommentModel } from '../../../models/video/video-comment'
34 const auditLogger = auditLoggerFactory('comments')
35 const videoCommentRouter = express.Router()
37 videoCommentRouter.get('/:videoId/comment-threads',
39 videoCommentThreadsSortValidator,
42 asyncMiddleware(listVideoCommentThreadsValidator),
44 asyncMiddleware(listVideoThreads)
46 videoCommentRouter.get('/:videoId/comment-threads/:threadId',
47 asyncMiddleware(listVideoThreadCommentsValidator),
49 asyncMiddleware(listVideoThreadComments)
52 videoCommentRouter.post('/:videoId/comment-threads',
54 asyncMiddleware(addVideoCommentThreadValidator),
55 asyncRetryTransactionMiddleware(addVideoCommentThread)
57 videoCommentRouter.post('/:videoId/comments/:commentId',
59 asyncMiddleware(addVideoCommentReplyValidator),
60 asyncRetryTransactionMiddleware(addVideoCommentReply)
62 videoCommentRouter.delete('/:videoId/comments/:commentId',
64 asyncMiddleware(removeVideoCommentValidator),
65 asyncRetryTransactionMiddleware(removeVideoComment)
68 videoCommentRouter.get('/comments',
70 ensureUserHasRight(UserRight.SEE_ALL_COMMENTS),
72 videoCommentsValidator,
75 listVideoCommentsValidator,
76 asyncMiddleware(listComments)
79 // ---------------------------------------------------------------------------
85 // ---------------------------------------------------------------------------
87 async function listComments (req: express.Request, res: express.Response) {
89 start: req.query.start,
90 count: req.query.count,
93 isLocal: req.query.isLocal,
94 onLocalVideo: req.query.onLocalVideo,
95 search: req.query.search,
96 searchAccount: req.query.searchAccount,
97 searchVideo: req.query.searchVideo
100 const resultList = await VideoCommentModel.listCommentsForApi(options)
103 total: resultList.total,
104 data: resultList.data.map(c => c.toFormattedAdminJSON())
108 async function listVideoThreads (req: express.Request, res: express.Response) {
109 const video = res.locals.onlyVideo
110 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
112 let resultList: ThreadsResultList<VideoCommentModel>
114 if (video.commentsEnabled === true) {
115 const apiOptions = await Hooks.wrapObject({
117 isVideoOwned: video.isOwned(),
118 start: req.query.start,
119 count: req.query.count,
120 sort: req.query.sort,
122 }, 'filter:api.video-threads.list.params')
124 resultList = await Hooks.wrapPromiseFun(
125 VideoCommentModel.listThreadsForApi,
127 'filter:api.video-threads.list.result'
132 totalNotDeletedComments: 0,
138 ...getFormattedObjects(resultList.data, resultList.total),
139 totalNotDeletedComments: resultList.totalNotDeletedComments
140 } as VideoCommentThreads)
143 async function listVideoThreadComments (req: express.Request, res: express.Response) {
144 const video = res.locals.onlyVideo
145 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
147 let resultList: ResultList<VideoCommentModel>
149 if (video.commentsEnabled === true) {
150 const apiOptions = await Hooks.wrapObject({
152 isVideoOwned: video.isOwned(),
153 threadId: res.locals.videoCommentThread.id,
155 }, 'filter:api.video-thread-comments.list.params')
157 resultList = await Hooks.wrapPromiseFun(
158 VideoCommentModel.listThreadCommentsForApi,
160 'filter:api.video-thread-comments.list.result'
169 if (resultList.data.length === 0) {
171 status: HttpStatusCode.NOT_FOUND_404,
172 message: 'No comments were found'
176 return res.json(buildFormattedCommentTree(resultList))
179 async function addVideoCommentThread (req: express.Request, res: express.Response) {
180 const videoCommentInfo: VideoCommentCreate = req.body
182 const comment = await sequelizeTypescript.transaction(async t => {
183 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
185 return createVideoComment({
186 text: videoCommentInfo.text,
187 inReplyToComment: null,
188 video: res.locals.videoAll,
193 Notifier.Instance.notifyOnNewComment(comment)
194 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
196 Hooks.runAction('action:api.video-thread.created', { comment, req, res })
198 return res.json({ comment: comment.toFormattedJSON() })
201 async function addVideoCommentReply (req: express.Request, res: express.Response) {
202 const videoCommentInfo: VideoCommentCreate = req.body
204 const comment = await sequelizeTypescript.transaction(async t => {
205 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
207 return createVideoComment({
208 text: videoCommentInfo.text,
209 inReplyToComment: res.locals.videoCommentFull,
210 video: res.locals.videoAll,
215 Notifier.Instance.notifyOnNewComment(comment)
216 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
218 Hooks.runAction('action:api.video-comment-reply.created', { comment, req, res })
220 return res.json({ comment: comment.toFormattedJSON() })
223 async function removeVideoComment (req: express.Request, res: express.Response) {
224 const videoCommentInstance = res.locals.videoCommentFull
226 await removeComment(videoCommentInstance, req, res)
228 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
230 return res.type('json')
231 .status(HttpStatusCode.NO_CONTENT_204)