]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-delete.ts
Correctly forward comment deletion
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-delete.ts
index 65a4e5bccd87a0f10c729549583f2d410d3242c5..155d2ffcc23b391e973aaf071f7fb420541b9267 100644 (file)
@@ -1,35 +1,51 @@
 import { ActivityDelete } from '../../../../shared/models/activitypub'
-import { logger, retryTransactionWrapper } from '../../../helpers'
+import { retryTransactionWrapper } from '../../../helpers/database-utils'
+import { logger } from '../../../helpers/logger'
 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 { getOrCreateActorAndServerAndModel } from '../actor'
+import { VideoCommentModel } from '../../../models/video/video-comment'
+import { forwardVideoRelatedActivity } from '../send/utils'
 
-async function processDeleteActivity (activity: ActivityDelete) {
-  const actor = await getOrCreateActorAndServerAndModel(activity.actor)
+async function processDeleteActivity (activity: ActivityDelete, byActor: ActorModel) {
+  const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
 
-  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.')
+  if (activity.actor === objectUrl) {
+    // We need more attributes (all the account and channel)
+    const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
 
-      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.')
+    if (byActorFull.type === 'Person') {
+      if (!byActorFull.Account) throw new Error('Actor ' + byActorFull.url + ' is a person but we cannot find it in database.')
 
-      return processDeleteVideoChannel(actor.VideoChannel)
+      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)
     }
   }
 
   {
-    let videoObject = await VideoModel.loadByUrlAndPopulateAccount(activity.id)
-    if (videoObject !== undefined) {
-      return processDeleteVideo(actor, videoObject)
+    const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(objectUrl)
+    if (videoCommentInstance) {
+      return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity)
     }
   }
 
-  return
+  {
+    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)
+    }
+  }
+
+  return undefined
 }
 
 // ---------------------------------------------------------------------------
@@ -41,15 +57,6 @@ export {
 // ---------------------------------------------------------------------------
 
 async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
-  const options = {
-    arguments: [ actor, videoToDelete ],
-    errorMessage: 'Cannot remove the remote video with many retries.'
-  }
-
-  await retryTransactionWrapper(deleteRemoteVideo, options)
-}
-
-async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) {
   logger.debug('Removing remote video "%s".', videoToDelete.uuid)
 
   await sequelizeTypescript.transaction(async t => {
@@ -64,15 +71,6 @@ async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel)
 }
 
 async function processDeleteAccount (accountToRemove: AccountModel) {
-  const options = {
-    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 => {
@@ -83,15 +81,6 @@ async function deleteRemoteAccount (accountToRemove: AccountModel) {
 }
 
 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 (videoChannelToRemove: VideoChannelModel) {
   logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
 
   await sequelizeTypescript.transaction(async t => {
@@ -100,3 +89,23 @@ async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel
 
   logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
 }
+
+function processDeleteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
+  logger.debug('Removing remote video comment "%s".', videoComment.url)
+
+  return sequelizeTypescript.transaction(async t => {
+    if (videoComment.Account.id !== byActor.Account.id) {
+      throw new Error('Account ' + byActor.url + ' does not own video comment ' + videoComment.url)
+    }
+
+    await videoComment.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 video comment %s removed.', videoComment.url)
+  })
+}