]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/comment.ts
Add server hooks
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / comment.ts
index 176ee8bd4dd326e168b4f6a85b29c7558fa8d292..a95392543e1bce89b8b7a608b0460cca57e24e11 100644 (file)
@@ -26,6 +26,7 @@ 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'
 
 const auditLogger = auditLoggerFactory('comments')
 const videoCommentRouter = express.Router()
@@ -76,7 +77,18 @@ async function listVideoThreads (req: express.Request, res: express.Response) {
   let resultList: ResultList<VideoCommentModel>
 
   if (video.commentsEnabled === true) {
-    resultList = await VideoCommentModel.listThreadsForApi(video.id, req.query.start, req.query.count, req.query.sort, user)
+    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')
+
+    resultList = await Hooks.wrapPromise(
+      VideoCommentModel.listThreadsForApi(apiOptions),
+      'filter:api.video-threads.list.result'
+    )
   } else {
     resultList = {
       total: 0,
@@ -94,7 +106,16 @@ async function listVideoThreadComments (req: express.Request, res: express.Respo
   let resultList: ResultList<VideoCommentModel>
 
   if (video.commentsEnabled === true) {
-    resultList = await VideoCommentModel.listThreadCommentsForApi(video.id, res.locals.videoCommentThread.id, user)
+    const apiOptions = await Hooks.wrapObject({
+      videoId: video.id,
+      threadId: res.locals.videoCommentThread.id,
+      user: user
+    }, 'filter:api.video-thread-comments.list.params')
+
+    resultList = await Hooks.wrapPromise(
+      VideoCommentModel.listThreadCommentsForApi(apiOptions),
+      'filter:api.video-thread-comments.list.result'
+    )
   } else {
     resultList = {
       total: 0,
@@ -122,6 +143,8 @@ 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 })
+
   return res.json({
     comment: comment.toFormattedJSON()
   }).end()
@@ -144,6 +167,8 @@ 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 })
+
   return res.json({ comment: comment.toFormattedJSON() }).end()
 }
 
@@ -154,11 +179,10 @@ async function removeVideoComment (req: express.Request, res: express.Response)
     await videoCommentInstance.destroy({ transaction: t })
   })
 
-  auditLogger.delete(
-    getAuditIdFromRes(res),
-    new CommentAuditView(videoCommentInstance.toFormattedJSON())
-  )
+  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()
 }