]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-undo.ts
Improve tests when waiting pending jobs
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-undo.ts
index 4a0181137f465a2a87aa43fced11772ccdbcf3bd..37db58e1a06780f1bb84fbc29269b5b13b725511 100644 (file)
@@ -1,23 +1,30 @@
-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 { logger, retryTransactionWrapper } from '../../../helpers'
+import { getActorUrl } from '../../../helpers/activitypub'
+import { retryTransactionWrapper } from '../../../helpers/database-utils'
+import { logger } from '../../../helpers/logger'
 import { sequelizeTypescript } from '../../../initializers'
 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 { getOrCreateAccountAndVideoAndChannel } 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 processUndoLike(actorUrl, activity)
   } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') {
-    return processUndoDislike(activity.actor, activity)
+    return processUndoDislike(actorUrl, activity)
   } else if (activityToUndo.type === 'Follow') {
-    return processUndoFollow(activity.actor, activityToUndo)
+    return processUndoFollow(actorUrl, activityToUndo)
+  } else if (activityToUndo.type === 'Announce') {
+    return processUndoAnnounce(actorUrl, activityToUndo)
   }
 
   logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
@@ -42,16 +49,15 @@ function processUndoLike (actorUrl: string, activity: ActivityUndo) {
   return retryTransactionWrapper(undoLike, options)
 }
 
-function undoLike (actorUrl: string, activity: ActivityUndo) {
+async function undoLike (actorUrl: string, activity: ActivityUndo) {
   const likeActivity = activity.object as ActivityLike
 
+  const { video } = await getOrCreateAccountAndVideoAndChannel(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}.`)
 
@@ -61,7 +67,8 @@ 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)
     }
   })
 }
@@ -75,16 +82,15 @@ function processUndoDislike (actorUrl: string, activity: ActivityUndo) {
   return retryTransactionWrapper(undoDislike, options)
 }
 
-function undoDislike (actorUrl: string, activity: ActivityUndo) {
+async function undoDislike (actorUrl: string, activity: ActivityUndo) {
   const dislike = activity.object.object as DislikeObject
 
+  const { video } = await getOrCreateAccountAndVideoAndChannel(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}.`)
 
@@ -94,7 +100,8 @@ 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)
     }
   })
 }
@@ -121,3 +128,31 @@ function undoFollow (actorUrl: string, followActivity: ActivityFollow) {
     return undefined
   })
 }
+
+function processUndoAnnounce (actorUrl: string, announceActivity: ActivityAnnounce) {
+  const options = {
+    arguments: [ actorUrl, announceActivity ],
+    errorMessage: 'Cannot undo announce with many retries.'
+  }
+
+  return retryTransactionWrapper(undoAnnounce, options)
+}
+
+function undoAnnounce (actorUrl: string, announceActivity: ActivityAnnounce) {
+  return sequelizeTypescript.transaction(async t => {
+    const byAccount = await AccountModel.loadByUrl(actorUrl, t)
+    if (!byAccount) throw new Error('Unknown account ' + actorUrl)
+
+    const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
+    if (!share) throw new Error(`'Unknown video share ${announceActivity.id}.`)
+
+    await share.destroy({ transaction: t })
+
+    if (share.Video.isOwned()) {
+      // Don't resend the activity to the sender
+      const exceptions = [ byAccount.Actor ]
+
+      await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
+    }
+  })
+}