-import * as express from 'express'
-import { ResultList } from '../../../../shared/models'
-import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model'
+import express from 'express'
+import { ResultList, ThreadsResultList, UserRight, VideoCommentCreate } from '../../../../shared/models'
+import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
+import { VideoCommentThreads } from '../../../../shared/models/videos/comment/video-comment.model'
import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
import { getFormattedObjects } from '../../../helpers/utils'
import { sequelizeTypescript } from '../../../initializers/database'
asyncMiddleware,
asyncRetryTransactionMiddleware,
authenticate,
+ ensureUserHasRight,
optionalAuthenticate,
paginationValidator,
setDefaultPagination,
import {
addVideoCommentReplyValidator,
addVideoCommentThreadValidator,
+ listVideoCommentsValidator,
listVideoCommentThreadsValidator,
listVideoThreadCommentsValidator,
removeVideoCommentValidator,
+ videoCommentsValidator,
videoCommentThreadsSortValidator
} from '../../../middlewares/validators'
import { AccountModel } from '../../../models/account/account'
asyncRetryTransactionMiddleware(removeVideoComment)
)
+videoCommentRouter.get('/comments',
+ authenticate,
+ ensureUserHasRight(UserRight.SEE_ALL_COMMENTS),
+ paginationValidator,
+ videoCommentsValidator,
+ setDefaultSort,
+ setDefaultPagination,
+ listVideoCommentsValidator,
+ asyncMiddleware(listComments)
+)
+
// ---------------------------------------------------------------------------
export {
// ---------------------------------------------------------------------------
+async function listComments (req: express.Request, res: express.Response) {
+ const options = {
+ start: req.query.start,
+ count: req.query.count,
+ sort: req.query.sort,
+
+ isLocal: req.query.isLocal,
+ search: req.query.search,
+ searchAccount: req.query.searchAccount,
+ searchVideo: req.query.searchVideo
+ }
+
+ const resultList = await VideoCommentModel.listCommentsForApi(options)
+
+ return res.json({
+ total: resultList.total,
+ data: resultList.data.map(c => c.toFormattedAdminJSON())
+ })
+}
+
async function listVideoThreads (req: express.Request, res: express.Response) {
const video = res.locals.onlyVideo
const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
- let resultList: ResultList<VideoCommentModel>
+ let resultList: ThreadsResultList<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,
} else {
resultList = {
total: 0,
+ totalNotDeletedComments: 0,
data: []
}
}
- return res.json(getFormattedObjects(resultList.data, resultList.total))
+ return res.json({
+ ...getFormattedObjects(resultList.data, resultList.total),
+ totalNotDeletedComments: resultList.totalNotDeletedComments
+ } as VideoCommentThreads)
}
async function listVideoThreadComments (req: express.Request, res: express.Response) {
if (video.commentsEnabled === true) {
const apiOptions = await Hooks.wrapObject({
videoId: video.id,
+ isVideoOwned: video.isOwned(),
threadId: res.locals.videoCommentThread.id,
user
}, 'filter:api.video-thread-comments.list.params')
}
}
+ if (resultList.data.length === 0) {
+ return res.fail({
+ status: HttpStatusCode.NOT_FOUND_404,
+ message: 'No comments were found'
+ })
+ }
+
return res.json(buildFormattedCommentTree(resultList))
}
Notifier.Instance.notifyOnNewComment(comment)
auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
- Hooks.runAction('action:api.video-thread.created', { comment })
+ Hooks.runAction('action:api.video-thread.created', { comment, req, res })
return res.json({ comment: comment.toFormattedJSON() })
}
Notifier.Instance.notifyOnNewComment(comment)
auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
- Hooks.runAction('action:api.video-comment-reply.created', { comment })
+ Hooks.runAction('action:api.video-comment-reply.created', { comment, req, res })
return res.json({ comment: comment.toFormattedJSON() })
}
async function removeVideoComment (req: express.Request, res: express.Response) {
const videoCommentInstance = res.locals.videoCommentFull
- await removeComment(videoCommentInstance)
+ await removeComment(videoCommentInstance, req, res)
auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
- return res.type('json').status(204)
+ return res.type('json')
+ .status(HttpStatusCode.NO_CONTENT_204)
+ .end()
}