]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/send/utils.ts
Merge branch 'develop' into pr/1285
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / utils.ts
index 241db6c8cfc603f4eca27c6b5d6ebbc6508d59f9..69706e620ebcd180a85f7b7a3a6a9e51ee6cdd87 100644 (file)
@@ -1,11 +1,35 @@
 import { Transaction } from 'sequelize'
-import { Activity } from '../../../../shared/models/activitypub'
+import { Activity, ActivityAudience } from '../../../../shared/models/activitypub'
 import { logger } from '../../../helpers/logger'
 import { ActorModel } from '../../../models/activitypub/actor'
 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
 import { JobQueue } from '../../job-queue'
 import { VideoModel } from '../../../models/video/video'
-import { getActorsInvolvedInVideo } from '../audience'
+import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience'
+import { getServerActor } from '../../../helpers/utils'
+
+async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: {
+  byActor: ActorModel,
+  video: VideoModel,
+  transaction?: Transaction
+}) {
+  const actorsInvolvedInVideo = await getActorsInvolvedInVideo(options.video, options.transaction)
+
+  // Send to origin
+  if (options.video.isOwned() === false) {
+    const audience = getRemoteVideoAudience(options.video, actorsInvolvedInVideo)
+    const activity = activityBuilder(audience)
+
+    return unicastTo(activity, options.byActor, options.video.VideoChannel.Account.Actor.sharedInboxUrl)
+  }
+
+  // Send to followers
+  const audience = getAudienceFromFollowersOf(actorsInvolvedInVideo)
+  const activity = activityBuilder(audience)
+
+  const actorsException = [ options.byActor ]
+  return broadcastToFollowers(activity, options.byActor, actorsInvolvedInVideo, options.transaction, actorsException)
+}
 
 async function forwardVideoRelatedActivity (
   activity: Activity,
@@ -26,6 +50,8 @@ async function forwardActivity (
   followersException: ActorModel[] = [],
   additionalFollowerUrls: string[] = []
 ) {
+  logger.info('Forwarding activity %s.', activity.id)
+
   const to = activity.to || []
   const cc = activity.cc || []
 
@@ -56,11 +82,11 @@ async function forwardActivity (
 async function broadcastToFollowers (
   data: any,
   byActor: ActorModel,
-  toActorFollowers: ActorModel[],
+  toFollowersOf: ActorModel[],
   t: Transaction,
   actorsException: ActorModel[] = []
 ) {
-  const uris = await computeFollowerUris(toActorFollowers, actorsException, t)
+  const uris = await computeFollowerUris(toFollowersOf, actorsException, t)
   return broadcastTo(uris, data, byActor)
 }
 
@@ -107,23 +133,38 @@ export {
   unicastTo,
   forwardActivity,
   broadcastToActors,
-  forwardVideoRelatedActivity
+  forwardVideoRelatedActivity,
+  sendVideoRelatedActivity
 }
 
 // ---------------------------------------------------------------------------
 
-async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) {
-  const toActorFollowerIds = toActorFollower.map(a => a.id)
+async function computeFollowerUris (toFollowersOf: ActorModel[], actorsException: ActorModel[], t: Transaction) {
+  const toActorFollowerIds = toFollowersOf.map(a => a.id)
 
   const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
-  const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
+  const sharedInboxesException = await buildSharedInboxesException(actorsException)
+
   return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
 }
 
 async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) {
-  const toActorSharedInboxesSet = new Set(toActors.map(a => a.sharedInboxUrl || a.inboxUrl))
+  const serverActor = await getServerActor()
+  const targetUrls = toActors
+    .filter(a => a.id !== serverActor.id) // Don't send to ourselves
+    .map(a => a.sharedInboxUrl || a.inboxUrl)
+
+  const toActorSharedInboxesSet = new Set(targetUrls)
 
-  const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
+  const sharedInboxesException = await buildSharedInboxesException(actorsException)
   return Array.from(toActorSharedInboxesSet)
               .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
 }
+
+async function buildSharedInboxesException (actorsException: ActorModel[]) {
+  const serverActor = await getServerActor()
+
+  return actorsException
+    .map(f => f.sharedInboxUrl || f.inboxUrl)
+    .concat([ serverActor.sharedInboxUrl ])
+}