]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-delete.ts
Remove comment federation by video owner
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-delete.ts
index 41cdc236d1baacdd25e3cbbe9659957b4d288257..9fcfd9e3adc5001c6b064a4abc288754b9c4d9c3 100644 (file)
@@ -1,34 +1,64 @@
-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 { getOrCreateAccountAndServer } from '../account'
-
-async function processDeleteActivity (activity: ActivityDelete) {
-  const account = await getOrCreateAccountAndServer(activity.actor)
+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 { forwardVideoRelatedActivity } from '../send/utils'
+import { VideoPlaylistModel } from '../../../models/video/video-playlist'
+import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
+
+async function processDeleteActivity (options: APProcessorOptions<ActivityDelete>) {
+  const { activity, byActor } = options
+
+  const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
+
+  if (activity.actor === objectUrl) {
+    // We need more attributes (all the account and channel)
+    const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
+
+    if (byActorFull.type === 'Person') {
+      if (!byActorFull.Account) throw new Error('Actor ' + byActorFull.url + ' is a person but we cannot find it in database.')
+
+      byActorFull.Account.Actor = await byActorFull.Account.$get('Actor') as ActorModel
+      return retryTransactionWrapper(processDeleteAccount, byActorFull.Account)
+    } else if (byActorFull.type === 'Group') {
+      if (!byActorFull.VideoChannel) throw new Error('Actor ' + byActorFull.url + ' is a group but we cannot find it in database.')
+
+      byActorFull.VideoChannel.Actor = await byActorFull.VideoChannel.$get('Actor') as ActorModel
+      return retryTransactionWrapper(processDeleteVideoChannel, byActorFull.VideoChannel)
+    }
+  }
 
-  if (account.url === activity.id) {
-    return processDeleteAccount(account)
+  {
+    const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(objectUrl)
+    if (videoCommentInstance) {
+      return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity)
+    }
   }
 
   {
-    let videoObject = await db.Video.loadByUrlAndPopulateAccount(activity.id)
-    if (videoObject !== undefined) {
-      return processDeleteVideo(account, videoObject)
+    const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
+    if (videoInstance) {
+      if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`)
+
+      return retryTransactionWrapper(processDeleteVideo, byActor, videoInstance)
     }
   }
 
   {
-    let videoChannelObject = await db.VideoChannel.loadByUrl(activity.id)
-    if (videoChannelObject !== undefined) {
-      return processDeleteVideoChannel(account, videoChannelObject)
+    const videoPlaylist = await VideoPlaylistModel.loadByUrlAndPopulateAccount(objectUrl)
+    if (videoPlaylist) {
+      if (videoPlaylist.isOwned()) throw new Error(`Remote instance cannot delete owned playlist ${videoPlaylist.url}.`)
+
+      return retryTransactionWrapper(processDeleteVideoPlaylist, byActor, videoPlaylist)
     }
   }
 
-  return
+  return undefined
 }
 
 // ---------------------------------------------------------------------------
@@ -39,21 +69,12 @@ export {
 
 // ---------------------------------------------------------------------------
 
-async function processDeleteVideo (account: AccountInstance, videoToDelete: VideoInstance) {
-  const options = {
-    arguments: [ account, videoToDelete ],
-    errorMessage: 'Cannot remove the remote video with many retries.'
-  }
-
-  await retryTransactionWrapper(deleteRemoteVideo, options)
-}
-
-async function deleteRemoteVideo (account: AccountInstance, videoToDelete: VideoInstance) {
+async function processDeleteVideo (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 +83,56 @@ async function deleteRemoteVideo (account: AccountInstance, videoToDelete: Video
   logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
 }
 
-async function processDeleteVideoChannel (account: AccountInstance, videoChannelToRemove: VideoChannelInstance) {
-  const options = {
-    arguments: [ account, videoChannelToRemove ],
-    errorMessage: 'Cannot remove the remote video channel with many retries.'
-  }
+async function processDeleteVideoPlaylist (actor: ActorModel, playlistToDelete: VideoPlaylistModel) {
+  logger.debug('Removing remote video playlist "%s".', playlistToDelete.uuid)
 
-  await retryTransactionWrapper(deleteRemoteVideoChannel, options)
+  await sequelizeTypescript.transaction(async t => {
+    if (playlistToDelete.OwnerAccount.Actor.id !== actor.id) {
+      throw new Error('Account ' + actor.url + ' does not own video playlist ' + playlistToDelete.url)
+    }
+
+    await playlistToDelete.destroy({ transaction: t })
+  })
+
+  logger.info('Remote video playlist with uuid %s removed.', playlistToDelete.uuid)
 }
 
-async function deleteRemoteVideoChannel (account: AccountInstance, videoChannelToRemove: VideoChannelInstance) {
-  logger.debug('Removing remote video channel "%s".', videoChannelToRemove.uuid)
+async function processDeleteAccount (accountToRemove: AccountModel) {
+  logger.debug('Removing remote account "%s".', accountToRemove.Actor.url)
 
-  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)
-    }
+  await sequelizeTypescript.transaction(async t => {
+    await accountToRemove.destroy({ transaction: t })
+  })
+
+  logger.info('Remote account %s removed.', accountToRemove.Actor.url)
+}
+
+async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
+  logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.url)
 
+  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 %s removed.', videoChannelToRemove.Actor.url)
 }
 
-async function processDeleteAccount (accountToRemove: AccountInstance) {
-  const options = {
-    arguments: [ accountToRemove ],
-    errorMessage: 'Cannot remove the remote account with many retries.'
-  }
+function processDeleteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
+  logger.debug('Removing remote video comment "%s".', videoComment.url)
 
-  await retryTransactionWrapper(deleteRemoteAccount, options)
-}
+  return sequelizeTypescript.transaction(async t => {
+    if (byActor.Account.id !== videoComment.Account.id && byActor.Account.id !== videoComment.Video.VideoChannel.accountId) {
+      throw new Error(`Account ${byActor.url} does not own video comment ${videoComment.url} or video ${videoComment.Video.url}`)
+    }
 
-async function deleteRemoteAccount (accountToRemove: AccountInstance) {
-  logger.debug('Removing remote account "%s".', accountToRemove.uuid)
+    await videoComment.destroy({ transaction: t })
 
-  await db.sequelize.transaction(async t => {
-    await accountToRemove.destroy({ transaction: t })
-  })
+    if (videoComment.Video.isOwned()) {
+      // Don't resend the activity to the sender
+      const exceptions = [ byActor ]
+      await forwardVideoRelatedActivity(activity, t, exceptions, videoComment.Video)
+    }
 
-  logger.info('Remote account with uuid %s removed.', accountToRemove.uuid)
+    logger.info('Remote video comment %s removed.', videoComment.url)
+  })
 }