import * as express from 'express'
import { ResultList } from '../../../../shared/models'
import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model'
-import { logger } from '../../../helpers/logger'
+import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
import { getFormattedObjects } from '../../../helpers/utils'
-import { sequelizeTypescript } from '../../../initializers'
-import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment'
+import { sequelizeTypescript } from '../../../initializers/database'
+import { Notifier } from '../../../lib/notifier'
+import { Hooks } from '../../../lib/plugins/hooks'
+import { buildFormattedCommentTree, createVideoComment, removeComment } from '../../../lib/video-comment'
import {
asyncMiddleware,
asyncRetryTransactionMiddleware,
removeVideoCommentValidator,
videoCommentThreadsSortValidator
} from '../../../middlewares/validators'
-import { VideoCommentModel } from '../../../models/video/video-comment'
-import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
import { AccountModel } from '../../../models/account/account'
-import { Notifier } from '../../../lib/notifier'
-import { Hooks } from '../../../lib/plugins/hooks'
+import { VideoCommentModel } from '../../../models/video/video-comment'
const auditLogger = auditLoggerFactory('comments')
const videoCommentRouter = express.Router()
// ---------------------------------------------------------------------------
async function listVideoThreads (req: express.Request, res: express.Response) {
- const video = res.locals.video
+ const video = res.locals.onlyVideo
const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
let resultList: ResultList<VideoCommentModel>
if (video.commentsEnabled === true) {
const apiOptions = await Hooks.wrapObject({
videoId: video.id,
+ isVideoOwned: video.isOwned(),
start: req.query.start,
count: req.query.count,
sort: req.query.sort,
- user: user
+ user
}, 'filter:api.video-threads.list.params')
- resultList = await Hooks.wrapPromise(
- VideoCommentModel.listThreadsForApi(apiOptions),
+ resultList = await Hooks.wrapPromiseFun(
+ VideoCommentModel.listThreadsForApi,
+ apiOptions,
'filter:api.video-threads.list.result'
)
} else {
}
async function listVideoThreadComments (req: express.Request, res: express.Response) {
- const video = res.locals.video
+ const video = res.locals.onlyVideo
const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
let resultList: ResultList<VideoCommentModel>
if (video.commentsEnabled === true) {
const apiOptions = await Hooks.wrapObject({
videoId: video.id,
+ isVideoOwned: video.isOwned(),
threadId: res.locals.videoCommentThread.id,
- user: user
+ user
}, 'filter:api.video-thread-comments.list.params')
- resultList = await Hooks.wrapPromise(
- VideoCommentModel.listThreadCommentsForApi(apiOptions),
+ resultList = await Hooks.wrapPromiseFun(
+ VideoCommentModel.listThreadCommentsForApi,
+ apiOptions,
'filter:api.video-thread-comments.list.result'
)
} else {
return createVideoComment({
text: videoCommentInfo.text,
inReplyToComment: null,
- video: res.locals.video,
+ video: res.locals.videoAll,
account
}, t)
})
Hooks.runAction('action:api.video-thread.created', { comment })
- return res.json({
- comment: comment.toFormattedJSON()
- }).end()
+ return res.json({ comment: comment.toFormattedJSON() })
}
async function addVideoCommentReply (req: express.Request, res: express.Response) {
return createVideoComment({
text: videoCommentInfo.text,
- inReplyToComment: res.locals.videoComment,
- video: res.locals.video,
+ inReplyToComment: res.locals.videoCommentFull,
+ video: res.locals.videoAll,
account
}, t)
})
Hooks.runAction('action:api.video-comment-reply.created', { comment })
- return res.json({ comment: comment.toFormattedJSON() }).end()
+ return res.json({ comment: comment.toFormattedJSON() })
}
async function removeVideoComment (req: express.Request, res: express.Response) {
- const videoCommentInstance = res.locals.videoComment
+ const videoCommentInstance = res.locals.videoCommentFull
- await sequelizeTypescript.transaction(async t => {
- await videoCommentInstance.destroy({ transaction: t })
- })
+ await removeComment(videoCommentInstance)
auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
- logger.info('Video comment %d deleted.', videoCommentInstance.id)
-
- Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstance })
return res.type('json').status(204).end()
}