]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/send/send-undo.ts
Fix incorrect IDs in AP federation
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-undo.ts
index a50673c7975cb042f1a8d2d5fa4dce467ce4d2fa..352c158fd6be237e7fce9befe1a3147b7d3fcb43 100644 (file)
@@ -3,25 +3,33 @@ import {
   ActivityAnnounce,
   ActivityAudience,
   ActivityCreate,
+  ActivityDislike,
   ActivityFollow,
   ActivityLike,
   ActivityUndo
 } from '../../../../shared/models/activitypub'
-import { ActorModel } from '../../../models/activitypub/actor'
-import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
+import { logger } from '../../../helpers/logger'
 import { VideoModel } from '../../../models/video/video'
-import { getActorFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url'
-import { broadcastToFollowers, unicastTo } from './utils'
-import { audiencify, getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience, getVideoAudience } from '../audience'
-import { buildCreateActivity, buildDislikeActivity } from './send-create'
+import {
+  MActor,
+  MActorAudience,
+  MActorFollowActors,
+  MActorLight,
+  MVideo,
+  MVideoAccountLight,
+  MVideoRedundancyVideo,
+  MVideoShare
+} from '../../../types/models'
+import { audiencify, getAudience } from '../audience'
+import { getUndoActivityPubUrl, getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from '../url'
+import { buildAnnounceWithVideoAudience } from './send-announce'
+import { buildCreateActivity } from './send-create'
+import { buildDislikeActivity } from './send-dislike'
 import { buildFollowActivity } from './send-follow'
 import { buildLikeActivity } from './send-like'
-import { VideoShareModel } from '../../../models/video/video-share'
-import { buildAnnounceWithVideoAudience } from './send-announce'
-import { logger } from '../../../helpers/logger'
-import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
+import { broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
 
-async function sendUndoFollow (actorFollow: ActorFollowModel, t: Transaction) {
+function sendUndoFollow (actorFollow: MActorFollowActors, t: Transaction) {
   const me = actorFollow.ActorFollower
   const following = actorFollow.ActorFollowing
 
@@ -30,88 +38,52 @@ async function sendUndoFollow (actorFollow: ActorFollowModel, t: Transaction) {
 
   logger.info('Creating job to send an unfollow request to %s.', following.url)
 
-  const followUrl = getActorFollowActivityPubUrl(actorFollow)
-  const undoUrl = getUndoActivityPubUrl(followUrl)
+  const undoUrl = getUndoActivityPubUrl(actorFollow.url)
 
-  const followActivity = buildFollowActivity(followUrl, me, following)
+  const followActivity = buildFollowActivity(actorFollow.url, me, following)
   const undoActivity = undoActivityData(undoUrl, me, followActivity)
 
-  return unicastTo(undoActivity, me, following.inboxUrl)
+  t.afterCommit(() => unicastTo(undoActivity, me, following.inboxUrl))
 }
 
-async function sendUndoLike (byActor: ActorModel, video: VideoModel, t: Transaction) {
-  logger.info('Creating job to undo a like of video %s.', video.url)
-
-  const likeUrl = getVideoLikeActivityPubUrl(byActor, video)
-  const undoUrl = getUndoActivityPubUrl(likeUrl)
-
-  const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
-  const likeActivity = buildLikeActivity(likeUrl, byActor, video)
-
-  // Send to origin
-  if (video.isOwned() === false) {
-    const audience = getVideoAudience(video, actorsInvolvedInVideo)
-    const undoActivity = undoActivityData(undoUrl, byActor, likeActivity, audience)
+async function sendUndoAnnounce (byActor: MActorLight, videoShare: MVideoShare, video: MVideo, t: Transaction) {
+  logger.info('Creating job to undo announce %s.', videoShare.url)
 
-    return unicastTo(undoActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
-  }
+  const undoUrl = getUndoActivityPubUrl(videoShare.url)
 
-  const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
-  const undoActivity = undoActivityData(undoUrl, byActor, likeActivity, audience)
+  const { activity: announceActivity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t)
+  const undoActivity = undoActivityData(undoUrl, byActor, announceActivity)
 
   const followersException = [ byActor ]
   return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException)
 }
 
-async function sendUndoDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
-  logger.info('Creating job to undo a dislike of video %s.', video.url)
-
-  const dislikeUrl = getVideoDislikeActivityPubUrl(byActor, video)
-  const undoUrl = getUndoActivityPubUrl(dislikeUrl)
-
-  const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
-  const dislikeActivity = buildDislikeActivity(byActor, video)
-  const createDislikeActivity = buildCreateActivity(dislikeUrl, byActor, dislikeActivity)
-
-  if (video.isOwned() === false) {
-    const audience = getVideoAudience(video, actorsInvolvedInVideo)
-    const undoActivity = undoActivityData(undoUrl, byActor, createDislikeActivity, audience)
-
-    return unicastTo(undoActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
-  }
+async function sendUndoLike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
+  logger.info('Creating job to undo a like of video %s.', video.url)
 
-  const undoActivity = undoActivityData(undoUrl, byActor, createDislikeActivity)
+  const likeUrl = getVideoLikeActivityPubUrlByLocalActor(byActor, video)
+  const likeActivity = buildLikeActivity(likeUrl, byActor, video)
 
-  const followersException = [ byActor ]
-  return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException)
+  return sendUndoVideoRelatedActivity({ byActor, video, url: likeUrl, activity: likeActivity, transaction: t })
 }
 
-async function sendUndoAnnounce (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) {
-  logger.info('Creating job to undo announce %s.', videoShare.url)
-
-  const undoUrl = getUndoActivityPubUrl(videoShare.url)
+async function sendUndoDislike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
+  logger.info('Creating job to undo a dislike of video %s.', video.url)
 
-  const { activity: announceActivity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t)
-  const undoActivity = undoActivityData(undoUrl, byActor, announceActivity)
+  const dislikeUrl = getVideoDislikeActivityPubUrlByLocalActor(byActor, video)
+  const dislikeActivity = buildDislikeActivity(dislikeUrl, byActor, video)
 
-  const followersException = [ byActor ]
-  return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException)
+  return sendUndoVideoRelatedActivity({ byActor, video, url: dislikeUrl, activity: dislikeActivity, transaction: t })
 }
 
-async function sendUndoCacheFile (byActor: ActorModel, redundancyModel: VideoRedundancyModel, t: Transaction) {
+async function sendUndoCacheFile (byActor: MActor, redundancyModel: MVideoRedundancyVideo, t: Transaction) {
   logger.info('Creating job to undo cache file %s.', redundancyModel.url)
 
-  const undoUrl = getUndoActivityPubUrl(redundancyModel.url)
-
-  const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(redundancyModel.VideoFile.Video.id)
-  const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
-
-  const audience = getVideoAudience(video, actorsInvolvedInVideo)
+  const videoId = redundancyModel.getVideo().id
+  const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
   const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject())
 
-  const undoActivity = undoActivityData(undoUrl, byActor, createActivity, audience)
-
-  return unicastTo(undoActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
+  return sendUndoVideoRelatedActivity({ byActor, video, url: redundancyModel.url, activity: createActivity, transaction: t })
 }
 
 // ---------------------------------------------------------------------------
@@ -128,8 +100,8 @@ export {
 
 function undoActivityData (
   url: string,
-  byActor: ActorModel,
-  object: ActivityFollow | ActivityLike | ActivityCreate | ActivityAnnounce,
+  byActor: MActorAudience,
+  object: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce,
   audience?: ActivityAudience
 ): ActivityUndo {
   if (!audience) audience = getAudience(byActor)
@@ -144,3 +116,19 @@ function undoActivityData (
     audience
   )
 }
+
+async function sendUndoVideoRelatedActivity (options: {
+  byActor: MActor
+  video: MVideoAccountLight
+  url: string
+  activity: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce
+  transaction: Transaction
+}) {
+  const activityBuilder = (audience: ActivityAudience) => {
+    const undoUrl = getUndoActivityPubUrl(options.url)
+
+    return undoActivityData(undoUrl, options.byActor, options.activity, audience)
+  }
+
+  return sendVideoRelatedActivity(activityBuilder, options)
+}