]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-delete.ts
Add ability to delete comments
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-delete.ts
index 0328d1a7d508c8c4cd6c415743fa88082a09b976..604570e74808e56dc8948377a3cd13e94273028d 100644 (file)
@@ -1,30 +1,40 @@
-import { ActivityDelete } from '../../../../shared/models/activitypub/activity'
+import { ActivityDelete } from '../../../../shared/models/activitypub'
 import { retryTransactionWrapper } from '../../../helpers/database-utils'
 import { logger } from '../../../helpers/logger'
-import { database as db } from '../../../initializers'
-import { AccountInstance } from '../../../models/account/account-interface'
-import { VideoChannelInstance } from '../../../models/video/video-channel-interface'
-import { VideoInstance } from '../../../models/video/video-interface'
-import { getOrCreateAccount } from '../account'
+import { sequelizeTypescript } from '../../../initializers'
+import { AccountModel } from '../../../models/account/account'
+import { ActorModel } from '../../../models/activitypub/actor'
+import { VideoModel } from '../../../models/video/video'
+import { VideoChannelModel } from '../../../models/video/video-channel'
+import { VideoCommentModel } from '../../../models/video/video-comment'
+import { getOrCreateActorAndServerAndModel } from '../actor'
 
 async function processDeleteActivity (activity: ActivityDelete) {
-  const account = await getOrCreateAccount(activity.actor)
+  const actor = await getOrCreateActorAndServerAndModel(activity.actor)
 
-  if (account.url === activity.id) {
-    return processDeleteAccount(account)
+  if (actor.url === activity.id) {
+    if (actor.type === 'Person') {
+      if (!actor.Account) throw new Error('Actor ' + actor.url + ' is a person but we cannot find it in database.')
+
+      return processDeleteAccount(actor.Account)
+    } else if (actor.type === 'Group') {
+      if (!actor.VideoChannel) throw new Error('Actor ' + actor.url + ' is a group but we cannot find it in database.')
+
+      return processDeleteVideoChannel(actor.VideoChannel)
+    }
   }
 
   {
-    let videoObject = await db.Video.loadByUrlAndPopulateAccount(activity.id)
-    if (videoObject !== undefined) {
-      return processDeleteVideo(account, videoObject)
+    const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(activity.id)
+    if (videoCommentInstance) {
+      return processDeleteVideoComment(actor, videoCommentInstance)
     }
   }
 
   {
-    let videoChannelObject = await db.VideoChannel.loadByUrl(activity.id)
-    if (videoChannelObject !== undefined) {
-      return processDeleteVideoChannel(account, videoChannelObject)
+    const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(activity.id)
+    if (videoInstance) {
+      return processDeleteVideo(actor, videoInstance)
     }
   }
 
@@ -39,21 +49,21 @@ export {
 
 // ---------------------------------------------------------------------------
 
-async function processDeleteVideo (account: AccountInstance, videoToDelete: VideoInstance) {
+async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
   const options = {
-    arguments: [ account, videoToDelete ],
+    arguments: [ actor, videoToDelete ],
     errorMessage: 'Cannot remove the remote video with many retries.'
   }
 
   await retryTransactionWrapper(deleteRemoteVideo, options)
 }
 
-async function deleteRemoteVideo (account: AccountInstance, videoToDelete: VideoInstance) {
+async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) {
   logger.debug('Removing remote video "%s".', videoToDelete.uuid)
 
-  await db.sequelize.transaction(async t => {
-    if (videoToDelete.VideoChannel.Account.id !== account.id) {
-      throw new Error('Account ' + account.url + ' does not own video channel ' + videoToDelete.VideoChannel.url)
+  await sequelizeTypescript.transaction(async t => {
+    if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
+      throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
     }
 
     await videoToDelete.destroy({ transaction: t })
@@ -62,44 +72,59 @@ async function deleteRemoteVideo (account: AccountInstance, videoToDelete: Video
   logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
 }
 
-async function processDeleteVideoChannel (account: AccountInstance, videoChannelToRemove: VideoChannelInstance) {
+async function processDeleteAccount (accountToRemove: AccountModel) {
   const options = {
-    arguments: [ account, videoChannelToRemove ],
+    arguments: [ accountToRemove ],
+    errorMessage: 'Cannot remove the remote account with many retries.'
+  }
+
+  await retryTransactionWrapper(deleteRemoteAccount, options)
+}
+
+async function deleteRemoteAccount (accountToRemove: AccountModel) {
+  logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
+
+  await sequelizeTypescript.transaction(async t => {
+    await accountToRemove.destroy({ transaction: t })
+  })
+
+  logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
+}
+
+async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
+  const options = {
+    arguments: [ videoChannelToRemove ],
     errorMessage: 'Cannot remove the remote video channel with many retries.'
   }
 
   await retryTransactionWrapper(deleteRemoteVideoChannel, options)
 }
 
-async function deleteRemoteVideoChannel (account: AccountInstance, videoChannelToRemove: VideoChannelInstance) {
-  logger.debug('Removing remote video channel "%s".', videoChannelToRemove.uuid)
-
-  await db.sequelize.transaction(async t => {
-    if (videoChannelToRemove.Account.id !== account.id) {
-      throw new Error('Account ' + account.url + ' does not own video channel ' + videoChannelToRemove.url)
-    }
+async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel) {
+  logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
 
+  await sequelizeTypescript.transaction(async t => {
     await videoChannelToRemove.destroy({ transaction: t })
   })
 
-  logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.uuid)
+  logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
 }
 
-async function processDeleteAccount (accountToRemove: AccountInstance) {
+async function processDeleteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) {
   const options = {
-    arguments: [ accountToRemove ],
-    errorMessage: 'Cannot remove the remote account with many retries.'
+    arguments: [ actor, videoComment ],
+    errorMessage: 'Cannot remove the remote video comment with many retries.'
   }
 
-  await retryTransactionWrapper(deleteRemoteAccount, options)
+  await retryTransactionWrapper(deleteRemoteVideoComment, options)
 }
 
-async function deleteRemoteAccount (accountToRemove: AccountInstance) {
-  logger.debug('Removing remote account "%s".', accountToRemove.uuid)
+function deleteRemoteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) {
+  logger.debug('Removing remote video comment "%s".', videoComment.url)
 
-  await db.sequelize.transaction(async t => {
-    await accountToRemove.destroy({ transaction: t })
-  })
+  return sequelizeTypescript.transaction(async t => {
+    await videoComment.destroy({ transaction: t })
 
-  logger.info('Remote account with uuid %s removed.', accountToRemove.uuid)
+    logger.info('Remote video comment %s removed.', videoComment.url)
+  })
 }