1 import * as express from 'express'
2 import { ResultList, ThreadsResultList, 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'
33 import { logger } from '@server/helpers/logger'
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 videoCommentRouter.get('/comments',
71 ensureUserHasRight(UserRight.SEE_ALL_COMMENTS),
73 videoCommentsValidator,
76 listVideoCommentsValidator,
77 asyncMiddleware(listComments)
80 // ---------------------------------------------------------------------------
86 // ---------------------------------------------------------------------------
88 async function listComments (req: express.Request, res: express.Response) {
90 start: req.query.start,
91 count: req.query.count,
94 isLocal: req.query.isLocal,
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
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 logger.info('coucou', { resultList })
171 if (resultList.data.length === 0) {
172 return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
175 return res.json(buildFormattedCommentTree(resultList))
178 async function addVideoCommentThread (req: express.Request, res: express.Response) {
179 const videoCommentInfo: VideoCommentCreate = req.body
181 const comment = await sequelizeTypescript.transaction(async t => {
182 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
184 return createVideoComment({
185 text: videoCommentInfo.text,
186 inReplyToComment: null,
187 video: res.locals.videoAll,
192 Notifier.Instance.notifyOnNewComment(comment)
193 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
195 Hooks.runAction('action:api.video-thread.created', { comment })
197 return res.json({ comment: comment.toFormattedJSON() })
200 async function addVideoCommentReply (req: express.Request, res: express.Response) {
201 const videoCommentInfo: VideoCommentCreate = req.body
203 const comment = await sequelizeTypescript.transaction(async t => {
204 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
206 return createVideoComment({
207 text: videoCommentInfo.text,
208 inReplyToComment: res.locals.videoCommentFull,
209 video: res.locals.videoAll,
214 Notifier.Instance.notifyOnNewComment(comment)
215 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
217 Hooks.runAction('action:api.video-comment-reply.created', { comment })
219 return res.json({ comment: comment.toFormattedJSON() })
222 async function removeVideoComment (req: express.Request, res: express.Response) {
223 const videoCommentInstance = res.locals.videoCommentFull
225 await removeComment(videoCommentInstance)
227 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
229 return res.type('json')
230 .status(HttpStatusCode.NO_CONTENT_204)