]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/comment.ts
Merge branch 'release/4.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / comment.ts
index 2dcb85ecf6a4f8ff77d412320e0ac24a57550fbc..47fa2f2e20b2d5ec43221b655d624bccae6a6048 100644 (file)
@@ -1,6 +1,7 @@
-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'
@@ -11,6 +12,7 @@ import {
   asyncMiddleware,
   asyncRetryTransactionMiddleware,
   authenticate,
+  ensureUserHasRight,
   optionalAuthenticate,
   paginationValidator,
   setDefaultPagination,
@@ -19,9 +21,11 @@ import {
 import {
   addVideoCommentReplyValidator,
   addVideoCommentThreadValidator,
+  listVideoCommentsValidator,
   listVideoCommentThreadsValidator,
   listVideoThreadCommentsValidator,
   removeVideoCommentValidator,
+  videoCommentsValidator,
   videoCommentThreadsSortValidator
 } from '../../../middlewares/validators'
 import { AccountModel } from '../../../models/account/account'
@@ -61,6 +65,17 @@ videoCommentRouter.delete('/:videoId/comments/:commentId',
   asyncRetryTransactionMiddleware(removeVideoComment)
 )
 
+videoCommentRouter.get('/comments',
+  authenticate,
+  ensureUserHasRight(UserRight.SEE_ALL_COMMENTS),
+  paginationValidator,
+  videoCommentsValidator,
+  setDefaultSort,
+  setDefaultPagination,
+  listVideoCommentsValidator,
+  asyncMiddleware(listComments)
+)
+
 // ---------------------------------------------------------------------------
 
 export {
@@ -69,15 +84,36 @@ 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,
@@ -92,11 +128,15 @@ async function listVideoThreads (req: express.Request, res: express.Response) {
   } 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) {
@@ -108,6 +148,7 @@ async function listVideoThreadComments (req: express.Request, res: express.Respo
   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')
@@ -124,6 +165,13 @@ async function listVideoThreadComments (req: express.Request, res: express.Respo
     }
   }
 
+  if (resultList.data.length === 0) {
+    return res.fail({
+      status: HttpStatusCode.NOT_FOUND_404,
+      message: 'No comments were found'
+    })
+  }
+
   return res.json(buildFormattedCommentTree(resultList))
 }
 
@@ -144,7 +192,7 @@ async function addVideoCommentThread (req: express.Request, res: express.Respons
   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() })
 }
@@ -166,7 +214,7 @@ async function addVideoCommentReply (req: express.Request, res: express.Response
   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() })
 }
@@ -174,9 +222,11 @@ async function addVideoCommentReply (req: express.Request, res: express.Response
 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).end()
+  return res.type('json')
+            .status(HttpStatusCode.NO_CONTENT_204)
+            .end()
 }