]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-undo.ts
Add federation to ownership change
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-undo.ts
index 9cad592334b13bf3f9dd830fa0265910eff89b6d..1c1de8827c9da037708111eb8af73981ab68c11c 100644 (file)
@@ -1,5 +1,6 @@
-import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub'
+import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub'
 import { DislikeObject } from '../../../../shared/models/activitypub/objects'
+import { getActorUrl } from '../../../helpers/activitypub'
 import { retryTransactionWrapper } from '../../../helpers/database-utils'
 import { logger } from '../../../helpers/logger'
 import { sequelizeTypescript } from '../../../initializers'
@@ -7,18 +8,23 @@ import { AccountModel } from '../../../models/account/account'
 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
 import { ActorModel } from '../../../models/activitypub/actor'
 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
-import { VideoModel } from '../../../models/video/video'
-import { forwardActivity } from '../send/misc'
+import { forwardVideoRelatedActivity } from '../send/utils'
+import { getOrCreateVideoAndAccountAndChannel } from '../videos'
+import { VideoShareModel } from '../../../models/video/video-share'
 
 async function processUndoActivity (activity: ActivityUndo) {
   const activityToUndo = activity.object
 
+  const actorUrl = getActorUrl(activity.actor)
+
   if (activityToUndo.type === 'Like') {
-    return processUndoLike(activity.actor, activity)
+    return retryTransactionWrapper(processUndoLike, actorUrl, activity)
   } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') {
-    return processUndoDislike(activity.actor, activity)
+    return retryTransactionWrapper(processUndoDislike, actorUrl, activity)
   } else if (activityToUndo.type === 'Follow') {
-    return processUndoFollow(activity.actor, activityToUndo)
+    return retryTransactionWrapper(processUndoFollow, actorUrl, activityToUndo)
+  } else if (activityToUndo.type === 'Announce') {
+    return retryTransactionWrapper(processUndoAnnounce, actorUrl, activityToUndo)
   }
 
   logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
@@ -34,25 +40,15 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function processUndoLike (actorUrl: string, activity: ActivityUndo) {
-  const options = {
-    arguments: [ actorUrl, activity ],
-    errorMessage: 'Cannot undo like with many retries.'
-  }
-
-  return retryTransactionWrapper(undoLike, options)
-}
-
-function undoLike (actorUrl: string, activity: ActivityUndo) {
+async function processUndoLike (actorUrl: string, activity: ActivityUndo) {
   const likeActivity = activity.object as ActivityLike
 
+  const { video } = await getOrCreateVideoAndAccountAndChannel(likeActivity.object)
+
   return sequelizeTypescript.transaction(async t => {
     const byAccount = await AccountModel.loadByUrl(actorUrl, t)
     if (!byAccount) throw new Error('Unknown account ' + actorUrl)
 
-    const video = await VideoModel.loadByUrlAndPopulateAccount(likeActivity.object, t)
-    if (!video) throw new Error('Unknown video ' + likeActivity.actor)
-
     const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
     if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
 
@@ -62,30 +58,21 @@ function undoLike (actorUrl: string, activity: ActivityUndo) {
     if (video.isOwned()) {
       // Don't resend the activity to the sender
       const exceptions = [ byAccount.Actor ]
-      await forwardActivity(activity, t, exceptions)
+
+      await forwardVideoRelatedActivity(activity, t, exceptions, video)
     }
   })
 }
 
-function processUndoDislike (actorUrl: string, activity: ActivityUndo) {
-  const options = {
-    arguments: [ actorUrl, activity ],
-    errorMessage: 'Cannot undo dislike with many retries.'
-  }
-
-  return retryTransactionWrapper(undoDislike, options)
-}
-
-function undoDislike (actorUrl: string, activity: ActivityUndo) {
+async function processUndoDislike (actorUrl: string, activity: ActivityUndo) {
   const dislike = activity.object.object as DislikeObject
 
+  const { video } = await getOrCreateVideoAndAccountAndChannel(dislike.object)
+
   return sequelizeTypescript.transaction(async t => {
     const byAccount = await AccountModel.loadByUrl(actorUrl, t)
     if (!byAccount) throw new Error('Unknown account ' + actorUrl)
 
-    const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t)
-    if (!video) throw new Error('Unknown video ' + dislike.actor)
-
     const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t)
     if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
 
@@ -95,21 +82,13 @@ function undoDislike (actorUrl: string, activity: ActivityUndo) {
     if (video.isOwned()) {
       // Don't resend the activity to the sender
       const exceptions = [ byAccount.Actor ]
-      await forwardActivity(activity, t, exceptions)
+
+      await forwardVideoRelatedActivity(activity, t, exceptions, video)
     }
   })
 }
 
 function processUndoFollow (actorUrl: string, followActivity: ActivityFollow) {
-  const options = {
-    arguments: [ actorUrl, followActivity ],
-    errorMessage: 'Cannot undo follow with many retries.'
-  }
-
-  return retryTransactionWrapper(undoFollow, options)
-}
-
-function undoFollow (actorUrl: string, followActivity: ActivityFollow) {
   return sequelizeTypescript.transaction(async t => {
     const follower = await ActorModel.loadByUrl(actorUrl, t)
     const following = await ActorModel.loadByUrl(followActivity.object, t)
@@ -122,3 +101,24 @@ function undoFollow (actorUrl: string, followActivity: ActivityFollow) {
     return undefined
   })
 }
+
+function processUndoAnnounce (actorUrl: string, announceActivity: ActivityAnnounce) {
+  return sequelizeTypescript.transaction(async t => {
+    const byActor = await ActorModel.loadByUrl(actorUrl, t)
+    if (!byActor) throw new Error('Unknown actor ' + actorUrl)
+
+    const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
+    if (!share) throw new Error(`Unknown video share ${announceActivity.id}.`)
+
+    if (share.actorId !== byActor.id) throw new Error(`${share.url} is not shared by ${byActor.url}.`)
+
+    await share.destroy({ transaction: t })
+
+    if (share.Video.isOwned()) {
+      // Don't resend the activity to the sender
+      const exceptions = [ byActor ]
+
+      await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
+    }
+  })
+}