]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/comment.ts
Add ability to disable video comments
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / comment.ts
index 81c9e7d161b689d6ff4b064a427982bd716edef5..e09b242ed9d688faba8912fe915fd996529fa38f 100644 (file)
@@ -1,6 +1,8 @@
 import * as express from 'express'
+import { ResultList } from '../../../../shared/models'
 import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model'
-import { getFormattedObjects, retryTransactionWrapper } from '../../../helpers'
+import { retryTransactionWrapper } from '../../../helpers/database-utils'
+import { getFormattedObjects } from '../../../helpers/utils'
 import { sequelizeTypescript } from '../../../initializers'
 import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment'
 import { asyncMiddleware, authenticate, paginationValidator, setPagination, setVideoCommentThreadsSort } from '../../../middlewares'
@@ -9,6 +11,7 @@ import {
   addVideoCommentReplyValidator, addVideoCommentThreadValidator, listVideoCommentThreadsValidator,
   listVideoThreadCommentsValidator
 } from '../../../middlewares/validators/video-comments'
+import { VideoModel } from '../../../models/video/video'
 import { VideoCommentModel } from '../../../models/video/video-comment'
 
 const videoCommentRouter = express.Router()
@@ -46,13 +49,33 @@ export {
 // ---------------------------------------------------------------------------
 
 async function listVideoThreads (req: express.Request, res: express.Response, next: express.NextFunction) {
-  const resultList = await VideoCommentModel.listThreadsForApi(res.locals.video.id, req.query.start, req.query.count, req.query.sort)
+  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: []
+    }
+  }
 
   return res.json(getFormattedObjects(resultList.data, resultList.total))
 }
 
 async function listVideoThreadComments (req: express.Request, res: express.Response, next: express.NextFunction) {
-  const resultList = await VideoCommentModel.listThreadCommentsForApi(res.locals.video.id, res.locals.videoCommentThread.id)
+  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: []
+    }
+  }
 
   return res.json(buildFormattedCommentTree(resultList))
 }
@@ -66,9 +89,7 @@ async function addVideoCommentThreadRetryWrapper (req: express.Request, res: exp
   const comment = await retryTransactionWrapper(addVideoCommentThread, options)
 
   res.json({
-    comment: {
-      id: comment.id
-    }
+    comment: comment.toFormattedJSON()
   }).end()
 }
 
@@ -80,7 +101,7 @@ function addVideoCommentThread (req: express.Request, res: express.Response) {
       text: videoCommentInfo.text,
       inReplyToComment: null,
       video: res.locals.video,
-      actorId: res.locals.oauth.token.User.Account.Actor.id
+      account: res.locals.oauth.token.User.Account
     }, t)
   })
 }
@@ -94,9 +115,7 @@ async function addVideoCommentReplyRetryWrapper (req: express.Request, res: expr
   const comment = await retryTransactionWrapper(addVideoCommentReply, options)
 
   res.json({
-    comment: {
-      id: comment.id
-    }
+    comment: comment.toFormattedJSON()
   }).end()
 }
 
@@ -106,9 +125,9 @@ function addVideoCommentReply (req: express.Request, res: express.Response, next
   return sequelizeTypescript.transaction(async t => {
     return createVideoComment({
       text: videoCommentInfo.text,
-      inReplyToComment: res.locals.videoComment.id,
+      inReplyToComment: res.locals.videoComment,
       video: res.locals.video,
-      actorId: res.locals.oauth.token.User.Account.Actor.id
+      account: res.locals.oauth.token.User.Account
     }, t)
   })
 }