]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/send/send-create.ts
Fix announce activities
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-create.ts
index ca50460be444229ae89703268b334c137309e53a..b92615e9bc89cea7b6b74af8da7d9ab41a05c38b 100644 (file)
@@ -1,14 +1,21 @@
 import { Transaction } from 'sequelize'
 import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
 import { VideoPrivacy } from '../../../../shared/models/videos'
-import { getServerActor } from '../../../helpers'
+import { getServerActor } from '../../../helpers/utils'
 import { ActorModel } from '../../../models/activitypub/actor'
 import { VideoModel } from '../../../models/video/video'
 import { VideoAbuseModel } from '../../../models/video/video-abuse'
 import { VideoCommentModel } from '../../../models/video/video-comment'
 import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
 import {
-  audiencify, broadcastToFollowers, getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience, getOriginVideoAudience,
+  audiencify,
+  broadcastToActors,
+  broadcastToFollowers,
+  getActorsInvolvedInVideo,
+  getAudience,
+  getObjectFollowersAudience,
+  getOriginVideoAudience,
+  getOriginVideoCommentAudience,
   unicastTo
 } from './misc'
 
@@ -30,32 +37,51 @@ async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel,
   const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
   const data = await createActivityData(url, byActor, videoAbuse.toActivityPubObject(), t, audience)
 
-  return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
+  return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
 }
 
 async function sendCreateVideoCommentToOrigin (comment: VideoCommentModel, t: Transaction) {
   const byActor = comment.Account.Actor
+  const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
+  const commentObject = comment.toActivityPubObject(threadParentComments)
 
-  const actorsInvolvedInVideo = await getActorsInvolvedInVideo(comment.Video, t)
-  const audience = getOriginVideoAudience(comment.Video, actorsInvolvedInVideo)
+  const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
+  actorsInvolvedInComment.push(byActor)
+  const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment)
 
-  const commentObject = comment.toActivityPubObject()
   const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
 
-  return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl, t)
+  // This was a reply, send it to the parent actors
+  const actorsException = [ byActor ]
+  await broadcastToActors(data, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
+
+  // Broadcast to our followers
+  await broadcastToFollowers(data, byActor, [ byActor ], t)
+
+  // Send to origin
+  return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
 }
 
 async function sendCreateVideoCommentToVideoFollowers (comment: VideoCommentModel, t: Transaction) {
   const byActor = comment.Account.Actor
+  const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
+  const commentObject = comment.toActivityPubObject(threadParentComments)
 
-  const actorsToForwardView = await getActorsInvolvedInVideo(comment.Video, t)
-  const audience = getObjectFollowersAudience(actorsToForwardView)
+  const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
+  actorsInvolvedInComment.push(byActor)
 
-  const commentObject = comment.toActivityPubObject()
+  const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment)
   const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
 
-  const followersException = [ byActor ]
-  return broadcastToFollowers(data, byActor, actorsToForwardView, t, followersException)
+  // This was a reply, send it to the parent actors
+  const actorsException = [ byActor ]
+  await broadcastToActors(data, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
+
+  // Broadcast to our followers
+  await broadcastToFollowers(data, byActor, [ byActor ], t)
+
+  // Send to actors involved in the comment
+  return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
 }
 
 async function sendCreateViewToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
@@ -66,7 +92,7 @@ async function sendCreateViewToOrigin (byActor: ActorModel, video: VideoModel, t
   const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
   const data = await createActivityData(url, byActor, viewActivityData, t, audience)
 
-  return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
+  return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
 }
 
 async function sendCreateViewToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
@@ -79,8 +105,8 @@ async function sendCreateViewToVideoFollowers (byActor: ActorModel, video: Video
 
   // Use the server actor to send the view
   const serverActor = await getServerActor()
-  const followersException = [ byActor ]
-  return broadcastToFollowers(data, serverActor, actorsToForwardView, t, followersException)
+  const actorsException = [ byActor ]
+  return broadcastToFollowers(data, serverActor, actorsToForwardView, t, actorsException)
 }
 
 async function sendCreateDislikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
@@ -91,7 +117,7 @@ async function sendCreateDislikeToOrigin (byActor: ActorModel, video: VideoModel
   const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
   const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
 
-  return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
+  return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
 }
 
 async function sendCreateDislikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
@@ -102,8 +128,8 @@ async function sendCreateDislikeToVideoFollowers (byActor: ActorModel, video: Vi
   const audience = getObjectFollowersAudience(actorsToForwardView)
   const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
 
-  const followersException = [ byActor ]
-  return broadcastToFollowers(data, byActor, actorsToForwardView, t, followersException)
+  const actorsException = [ byActor ]
+  return broadcastToFollowers(data, byActor, actorsToForwardView, t, actorsException)
 }
 
 async function createActivityData (