PeerTube/server/controllers/api/videos/comment.ts

199 lines
6.4 KiB
TypeScript
Raw Normal View History

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