]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-delete.ts
Fix delete comment federation
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-delete.ts
index 604570e74808e56dc8948377a3cd13e94273028d..b58f6c04a28a686f1fddb4ab365d16750e48cc6f 100644 (file)
@@ -8,31 +8,38 @@ import { VideoModel } from '../../../models/video/video'
 import { VideoChannelModel } from '../../../models/video/video-channel'
 import { VideoCommentModel } from '../../../models/video/video-comment'
 import { getOrCreateActorAndServerAndModel } from '../actor'
+import { forwardActivity } from '../send/misc'
 
 async function processDeleteActivity (activity: ActivityDelete) {
-  const actor = await getOrCreateActorAndServerAndModel(activity.actor)
+  const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
+
+  if (activity.actor === objectUrl) {
+    let actor = await ActorModel.loadByUrl(activity.actor)
+    if (!actor) return
 
-  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.')
 
+      actor.Account.Actor = await actor.Account.$get('Actor') as ActorModel
       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.')
 
+      actor.VideoChannel.Actor = await actor.VideoChannel.$get('Actor') as ActorModel
       return processDeleteVideoChannel(actor.VideoChannel)
     }
   }
 
+  const actor = await getOrCreateActorAndServerAndModel(activity.actor)
   {
-    const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(activity.id)
+    const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(objectUrl)
     if (videoCommentInstance) {
-      return processDeleteVideoComment(actor, videoCommentInstance)
+      return processDeleteVideoComment(actor, videoCommentInstance, activity)
     }
   }
 
   {
-    const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(activity.id)
+    const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
     if (videoInstance) {
       return processDeleteVideo(actor, videoInstance)
     }
@@ -110,21 +117,27 @@ async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel
   logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
 }
 
-async function processDeleteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) {
+async function processDeleteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
   const options = {
-    arguments: [ actor, videoComment ],
+    arguments: [ byActor, videoComment, activity ],
     errorMessage: 'Cannot remove the remote video comment with many retries.'
   }
 
   await retryTransactionWrapper(deleteRemoteVideoComment, options)
 }
 
-function deleteRemoteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) {
+function deleteRemoteVideoComment (byActor: ActorModel, videoComment: VideoCommentModel, activity: ActivityDelete) {
   logger.debug('Removing remote video comment "%s".', videoComment.url)
 
   return sequelizeTypescript.transaction(async t => {
     await videoComment.destroy({ transaction: t })
 
+    if (videoComment.Video.isOwned()) {
+      // Don't resend the activity to the sender
+      const exceptions = [ byActor ]
+      await forwardActivity(activity, t, exceptions)
+    }
+
     logger.info('Remote video comment %s removed.', videoComment.url)
   })
 }