2017-12-22 10:50:07 +01:00
|
|
|
import * as express from 'express'
|
2018-01-03 10:12:36 +01:00
|
|
|
import { ResultList } from '../../../../shared/models'
|
2017-12-22 10:50:07 +01:00
|
|
|
import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model'
|
2018-01-04 11:19:16 +01:00
|
|
|
import { logger } from '../../../helpers/logger'
|
2017-12-28 11:16:08 +01:00
|
|
|
import { getFormattedObjects } from '../../../helpers/utils'
|
2017-12-22 10:50:07 +01:00
|
|
|
import { sequelizeTypescript } from '../../../initializers'
|
|
|
|
import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment'
|
2018-06-13 14:27:40 +02:00
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
asyncRetryTransactionMiddleware,
|
2018-10-12 15:26:04 +02:00
|
|
|
authenticate, optionalAuthenticate,
|
2018-06-13 14:27:40 +02:00
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
setDefaultSort
|
|
|
|
} from '../../../middlewares'
|
2017-12-22 10:50:07 +01:00
|
|
|
import {
|
2018-06-13 14:27:40 +02:00
|
|
|
addVideoCommentReplyValidator,
|
|
|
|
addVideoCommentThreadValidator,
|
|
|
|
listVideoCommentThreadsValidator,
|
|
|
|
listVideoThreadCommentsValidator,
|
2018-10-05 11:15:06 +02:00
|
|
|
removeVideoCommentValidator,
|
|
|
|
videoCommentThreadsSortValidator
|
|
|
|
} from '../../../middlewares/validators'
|
2018-01-03 10:12:36 +01:00
|
|
|
import { VideoModel } from '../../../models/video/video'
|
2017-12-22 10:50:07 +01:00
|
|
|
import { VideoCommentModel } from '../../../models/video/video-comment'
|
2018-09-19 17:02:16 +02:00
|
|
|
import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
|
2018-09-20 10:13:13 +02:00
|
|
|
import { AccountModel } from '../../../models/account/account'
|
|
|
|
import { UserModel } from '../../../models/account/user'
|
2018-12-26 10:36:24 +01:00
|
|
|
import { Notifier } from '../../../lib/notifier'
|
2017-12-22 10:50:07 +01:00
|
|
|
|
2018-07-31 14:04:26 +02:00
|
|
|
const auditLogger = auditLoggerFactory('comments')
|
2017-12-22 10:50:07 +01:00
|
|
|
const videoCommentRouter = express.Router()
|
|
|
|
|
|
|
|
videoCommentRouter.get('/:videoId/comment-threads',
|
|
|
|
paginationValidator,
|
|
|
|
videoCommentThreadsSortValidator,
|
2018-01-17 10:50:33 +01:00
|
|
|
setDefaultSort,
|
2018-01-18 10:53:54 +01:00
|
|
|
setDefaultPagination,
|
2017-12-22 10:50:07 +01:00
|
|
|
asyncMiddleware(listVideoCommentThreadsValidator),
|
2018-10-12 15:26:04 +02:00
|
|
|
optionalAuthenticate,
|
2017-12-22 10:50:07 +01:00
|
|
|
asyncMiddleware(listVideoThreads)
|
|
|
|
)
|
|
|
|
videoCommentRouter.get('/:videoId/comment-threads/:threadId',
|
|
|
|
asyncMiddleware(listVideoThreadCommentsValidator),
|
2018-10-12 15:26:04 +02:00
|
|
|
optionalAuthenticate,
|
2017-12-22 10:50:07 +01:00
|
|
|
asyncMiddleware(listVideoThreadComments)
|
|
|
|
)
|
|
|
|
|
|
|
|
videoCommentRouter.post('/:videoId/comment-threads',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(addVideoCommentThreadValidator),
|
2018-06-13 14:27:40 +02:00
|
|
|
asyncRetryTransactionMiddleware(addVideoCommentThread)
|
2017-12-22 10:50:07 +01:00
|
|
|
)
|
|
|
|
videoCommentRouter.post('/:videoId/comments/:commentId',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(addVideoCommentReplyValidator),
|
2018-06-13 14:27:40 +02:00
|
|
|
asyncRetryTransactionMiddleware(addVideoCommentReply)
|
2017-12-22 10:50:07 +01:00
|
|
|
)
|
2018-01-04 11:19:16 +01:00
|
|
|
videoCommentRouter.delete('/:videoId/comments/:commentId',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(removeVideoCommentValidator),
|
2018-06-13 14:27:40 +02:00
|
|
|
asyncRetryTransactionMiddleware(removeVideoComment)
|
2018-01-04 11:19:16 +01:00
|
|
|
)
|
2017-12-22 10:50:07 +01:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
videoCommentRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2018-01-03 10:12:36 +01:00
|
|
|
const video = res.locals.video as VideoModel
|
2018-10-12 15:26:04 +02:00
|
|
|
const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : undefined
|
|
|
|
|
2018-01-03 10:12:36 +01:00
|
|
|
let resultList: ResultList<VideoCommentModel>
|
|
|
|
|
|
|
|
if (video.commentsEnabled === true) {
|
2018-10-12 15:26:04 +02:00
|
|
|
resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort, user)
|
2018-01-03 10:12:36 +01:00
|
|
|
} else {
|
|
|
|
resultList = {
|
|
|
|
total: 0,
|
|
|
|
data: []
|
|
|
|
}
|
|
|
|
}
|
2017-12-22 10:50:07 +01:00
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
|
|
|
|
|
|
|
async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2018-01-03 10:12:36 +01:00
|
|
|
const video = res.locals.video as VideoModel
|
2018-10-12 15:26:04 +02:00
|
|
|
const user: UserModel = res.locals.oauth ? res.locals.oauth.token.User : undefined
|
|
|
|
|
2018-01-03 10:12:36 +01:00
|
|
|
let resultList: ResultList<VideoCommentModel>
|
|
|
|
|
|
|
|
if (video.commentsEnabled === true) {
|
2018-10-12 15:26:04 +02:00
|
|
|
resultList = await VideoCommentModel.listThreadCommentsForApi(video.id, res.locals.videoCommentThread.id, user)
|
2018-01-03 10:12:36 +01:00
|
|
|
} else {
|
|
|
|
resultList = {
|
|
|
|
total: 0,
|
|
|
|
data: []
|
|
|
|
}
|
|
|
|
}
|
2017-12-22 10:50:07 +01:00
|
|
|
|
|
|
|
return res.json(buildFormattedCommentTree(resultList))
|
|
|
|
}
|
|
|
|
|
2018-06-13 14:27:40 +02:00
|
|
|
async function addVideoCommentThread (req: express.Request, res: express.Response) {
|
2017-12-22 10:50:07 +01:00
|
|
|
const videoCommentInfo: VideoCommentCreate = req.body
|
|
|
|
|
2018-06-13 14:27:40 +02:00
|
|
|
const comment = await sequelizeTypescript.transaction(async t => {
|
2018-09-20 10:13:13 +02:00
|
|
|
const account = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t)
|
|
|
|
|
2017-12-22 10:50:07 +01:00
|
|
|
return createVideoComment({
|
|
|
|
text: videoCommentInfo.text,
|
2017-12-27 10:39:31 +01:00
|
|
|
inReplyToComment: null,
|
2017-12-22 10:50:07 +01:00
|
|
|
video: res.locals.video,
|
2018-09-20 10:13:13 +02:00
|
|
|
account
|
2017-12-22 10:50:07 +01:00
|
|
|
}, t)
|
|
|
|
})
|
|
|
|
|
2018-12-26 10:36:24 +01:00
|
|
|
Notifier.Instance.notifyOnNewComment(comment)
|
2018-09-19 17:02:16 +02:00
|
|
|
auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
|
2018-07-31 14:04:26 +02:00
|
|
|
|
2018-06-13 14:27:40 +02:00
|
|
|
return res.json({
|
2017-12-27 16:11:53 +01:00
|
|
|
comment: comment.toFormattedJSON()
|
2017-12-22 10:50:07 +01:00
|
|
|
}).end()
|
|
|
|
}
|
|
|
|
|
2018-06-13 14:27:40 +02:00
|
|
|
async function addVideoCommentReply (req: express.Request, res: express.Response) {
|
2017-12-22 10:50:07 +01:00
|
|
|
const videoCommentInfo: VideoCommentCreate = req.body
|
|
|
|
|
2018-06-13 14:27:40 +02:00
|
|
|
const comment = await sequelizeTypescript.transaction(async t => {
|
2018-09-20 10:13:13 +02:00
|
|
|
const account = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t)
|
|
|
|
|
2017-12-22 10:50:07 +01:00
|
|
|
return createVideoComment({
|
|
|
|
text: videoCommentInfo.text,
|
2017-12-27 10:39:31 +01:00
|
|
|
inReplyToComment: res.locals.videoComment,
|
2017-12-22 10:50:07 +01:00
|
|
|
video: res.locals.video,
|
2018-09-20 10:13:13 +02:00
|
|
|
account
|
2017-12-22 10:50:07 +01:00
|
|
|
}, t)
|
|
|
|
})
|
2018-01-04 11:19:16 +01:00
|
|
|
|
2018-12-26 10:36:24 +01:00
|
|
|
Notifier.Instance.notifyOnNewComment(comment)
|
2018-09-19 17:02:16 +02:00
|
|
|
auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
|
2018-07-31 14:04:26 +02:00
|
|
|
|
2018-09-20 10:13:13 +02:00
|
|
|
return res.json({ comment: comment.toFormattedJSON() }).end()
|
2018-01-04 11:19:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function removeVideoComment (req: express.Request, res: express.Response) {
|
|
|
|
const videoCommentInstance: VideoCommentModel = res.locals.videoComment
|
|
|
|
|
|
|
|
await sequelizeTypescript.transaction(async t => {
|
|
|
|
await videoCommentInstance.destroy({ transaction: t })
|
|
|
|
})
|
|
|
|
|
2018-07-31 14:04:26 +02:00
|
|
|
auditLogger.delete(
|
2018-09-19 17:02:16 +02:00
|
|
|
getAuditIdFromRes(res),
|
2018-07-31 14:04:26 +02:00
|
|
|
new CommentAuditView(videoCommentInstance.toFormattedJSON())
|
|
|
|
)
|
2018-01-04 11:19:16 +01:00
|
|
|
logger.info('Video comment %d deleted.', videoCommentInstance.id)
|
2018-06-13 14:27:40 +02:00
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
2018-01-04 11:19:16 +01:00
|
|
|
}
|