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'
32 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
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 search: req.query.search,
95 searchAccount: req.query.searchAccount,
96 searchVideo: req.query.searchVideo
99 const resultList = await VideoCommentModel.listCommentsForApi(options)
102 total: resultList.total,
103 data: resultList.data.map(c => c.toFormattedAdminJSON())
107 async function listVideoThreads (req: express.Request, res: express.Response) {
108 const video = res.locals.onlyVideo
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 isVideoOwned: video.isOwned(),
117 start: req.query.start,
118 count: req.query.count,
119 sort: req.query.sort,
121 }, 'filter:api.video-threads.list.params')
123 resultList = await Hooks.wrapPromiseFun(
124 VideoCommentModel.listThreadsForApi,
126 'filter:api.video-threads.list.result'
135 return res.json(getFormattedObjects(resultList.data, resultList.total))
138 async function listVideoThreadComments (req: express.Request, res: express.Response) {
139 const video = res.locals.onlyVideo
140 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
142 let resultList: ResultList<VideoCommentModel>
144 if (video.commentsEnabled === true) {
145 const apiOptions = await Hooks.wrapObject({
147 isVideoOwned: video.isOwned(),
148 threadId: res.locals.videoCommentThread.id,
150 }, 'filter:api.video-thread-comments.list.params')
152 resultList = await Hooks.wrapPromiseFun(
153 VideoCommentModel.listThreadCommentsForApi,
155 'filter:api.video-thread-comments.list.result'
164 if (resultList.data.length === 0) {
165 return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
168 return res.json(buildFormattedCommentTree(resultList))
171 async function addVideoCommentThread (req: express.Request, res: express.Response) {
172 const videoCommentInfo: VideoCommentCreate = req.body
174 const comment = await sequelizeTypescript.transaction(async t => {
175 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
177 return createVideoComment({
178 text: videoCommentInfo.text,
179 inReplyToComment: null,
180 video: res.locals.videoAll,
185 Notifier.Instance.notifyOnNewComment(comment)
186 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
188 Hooks.runAction('action:api.video-thread.created', { comment })
190 return res.json({ comment: comment.toFormattedJSON() })
193 async function addVideoCommentReply (req: express.Request, res: express.Response) {
194 const videoCommentInfo: VideoCommentCreate = req.body
196 const comment = await sequelizeTypescript.transaction(async t => {
197 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
199 return createVideoComment({
200 text: videoCommentInfo.text,
201 inReplyToComment: res.locals.videoCommentFull,
202 video: res.locals.videoAll,
207 Notifier.Instance.notifyOnNewComment(comment)
208 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
210 Hooks.runAction('action:api.video-comment-reply.created', { comment })
212 return res.json({ comment: comment.toFormattedJSON() })
215 async function removeVideoComment (req: express.Request, res: express.Response) {
216 const videoCommentInstance = res.locals.videoCommentFull
218 await removeComment(videoCommentInstance)
220 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
222 return res.type('json')
223 .status(HttpStatusCode.NO_CONTENT_204)