]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/video-comment.ts
Add ability to bulk delete comments
[github/Chocobozzz/PeerTube.git] / server / lib / video-comment.ts
index 516c912a90a30f0f3e679e65e37384ee6a3c3542..97aa639fbdc278475cd6c738cc7b5df2b9f5c85e 100644 (file)
@@ -1,10 +1,32 @@
+import { cloneDeep } from 'lodash'
 import * as Sequelize from 'sequelize'
+import { logger } from '@server/helpers/logger'
+import { sequelizeTypescript } from '@server/initializers/database'
 import { ResultList } from '../../shared/models'
 import { VideoCommentThreadTree } from '../../shared/models/videos/video-comment.model'
 import { VideoCommentModel } from '../models/video/video-comment'
+import { MAccountDefault, MComment, MCommentOwnerVideoReply, MVideoFullLight, MCommentOwnerVideo } from '../typings/models'
+import { sendCreateVideoComment, sendDeleteVideoComment } from './activitypub/send'
 import { getVideoCommentActivityPubUrl } from './activitypub/url'
-import { sendCreateVideoComment } from './activitypub/send'
-import { MAccountDefault, MComment, MCommentOwnerVideoReply, MVideoFullLight } from '../typings/models'
+import { Hooks } from './plugins/hooks'
+
+async function removeComment (videoCommentInstance: MCommentOwnerVideo) {
+  const videoCommentInstanceBefore = cloneDeep(videoCommentInstance)
+
+  await sequelizeTypescript.transaction(async t => {
+    if (videoCommentInstance.isOwned() || videoCommentInstance.Video.isOwned()) {
+      await sendDeleteVideoComment(videoCommentInstance, t)
+    }
+
+    markCommentAsDeleted(videoCommentInstance)
+
+    await videoCommentInstance.save()
+  })
+
+  logger.info('Video comment %d deleted.', videoCommentInstance.id)
+
+  Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstanceBefore })
+}
 
 async function createVideoComment (obj: {
   text: string
@@ -73,7 +95,7 @@ function buildFormattedCommentTree (resultList: ResultList<VideoCommentModel>):
   return thread
 }
 
-function markCommentAsDeleted (comment: MCommentOwnerVideoReply): void {
+function markCommentAsDeleted (comment: MComment): void {
   comment.text = ''
   comment.deletedAt = new Date()
   comment.accountId = null
@@ -82,6 +104,7 @@ function markCommentAsDeleted (comment: MCommentOwnerVideoReply): void {
 // ---------------------------------------------------------------------------
 
 export {
+  removeComment,
   createVideoComment,
   buildFormattedCommentTree,
   markCommentAsDeleted