1 import * as express from 'express'
2 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
3 import { ResultList, ThreadsResultList, UserRight } from '../../../../shared/models'
4 import { VideoCommentCreate } from '../../../../shared/models/videos/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 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: ThreadsResultList<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'
131 totalNotDeletedComments: 0,
137 ...getFormattedObjects(resultList.data, resultList.total),
138 totalNotDeletedComments: resultList.totalNotDeletedComments
142 async function listVideoThreadComments (req: express.Request, res: express.Response) {
143 const video = res.locals.onlyVideo
144 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
146 let resultList: ResultList<VideoCommentModel>
148 if (video.commentsEnabled === true) {
149 const apiOptions = await Hooks.wrapObject({
151 isVideoOwned: video.isOwned(),
152 threadId: res.locals.videoCommentThread.id,
154 }, 'filter:api.video-thread-comments.list.params')
156 resultList = await Hooks.wrapPromiseFun(
157 VideoCommentModel.listThreadCommentsForApi,
159 'filter:api.video-thread-comments.list.result'
168 if (resultList.data.length === 0) {
169 return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
172 return res.json(buildFormattedCommentTree(resultList))
175 async function addVideoCommentThread (req: express.Request, res: express.Response) {
176 const videoCommentInfo: VideoCommentCreate = req.body
178 const comment = await sequelizeTypescript.transaction(async t => {
179 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
181 return createVideoComment({
182 text: videoCommentInfo.text,
183 inReplyToComment: null,
184 video: res.locals.videoAll,
189 Notifier.Instance.notifyOnNewComment(comment)
190 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
192 Hooks.runAction('action:api.video-thread.created', { comment })
194 return res.json({ comment: comment.toFormattedJSON() })
197 async function addVideoCommentReply (req: express.Request, res: express.Response) {
198 const videoCommentInfo: VideoCommentCreate = req.body
200 const comment = await sequelizeTypescript.transaction(async t => {
201 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
203 return createVideoComment({
204 text: videoCommentInfo.text,
205 inReplyToComment: res.locals.videoCommentFull,
206 video: res.locals.videoAll,
211 Notifier.Instance.notifyOnNewComment(comment)
212 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
214 Hooks.runAction('action:api.video-comment-reply.created', { comment })
216 return res.json({ comment: comment.toFormattedJSON() })
219 async function removeVideoComment (req: express.Request, res: express.Response) {
220 const videoCommentInstance = res.locals.videoCommentFull
222 await removeComment(videoCommentInstance)
224 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
226 return res.type('json')
227 .status(HttpStatusCode.NO_CONTENT_204)