1
0
Fork 0
peertube/server/controllers/api/videos/comment.ts

153 lines
5.1 KiB
TypeScript
Raw Normal View History

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