1 import { Transaction } from 'sequelize'
2 import { Activity, ActivityAudience } from '../../../../shared/models/activitypub'
3 import { logger } from '../../../helpers/logger'
4 import { ACTIVITY_PUB } from '../../../initializers'
5 import { ActorModel } from '../../../models/activitypub/actor'
6 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
7 import { VideoModel } from '../../../models/video/video'
8 import { VideoCommentModel } from '../../../models/video/video-comment'
9 import { VideoShareModel } from '../../../models/video/video-share'
10 import { activitypubHttpJobScheduler, ActivityPubHttpPayload } from '../../jobs/activitypub-http-job-scheduler'
12 async function forwardActivity (
15 followersException: ActorModel[] = [],
16 additionalFollowerUrls: string[] = []
18 const to = activity.to || []
19 const cc = activity.cc || []
21 const followersUrls = additionalFollowerUrls
22 for (const dest of to.concat(cc)) {
23 if (dest.endsWith('/followers')) {
24 followersUrls.push(dest)
28 const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
29 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
31 if (uris.length === 0) {
32 logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
36 logger.debug('Creating forwarding job.', { uris })
38 const jobPayload: ActivityPubHttpPayload = {
43 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
46 async function broadcastToFollowers (
49 toActorFollowers: ActorModel[],
51 actorsException: ActorModel[] = []
53 const uris = await computeFollowerUris(toActorFollowers, actorsException, t)
54 return broadcastTo(uris, data, byActor, t)
57 async function broadcastToActors (
60 toActors: ActorModel[],
62 actorsException: ActorModel[] = []
64 const uris = await computeUris(toActors, actorsException)
65 return broadcastTo(uris, data, byActor, t)
68 async function broadcastTo (uris: string[], data: any, byActor: ActorModel, t: Transaction) {
69 if (uris.length === 0) return undefined
71 logger.debug('Creating broadcast job.', { uris })
73 const jobPayload: ActivityPubHttpPayload = {
75 signatureActorId: byActor.id,
79 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
82 async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string, t: Transaction) {
83 logger.debug('Creating unicast job.', { uri: toActorUrl })
85 const jobPayload: ActivityPubHttpPayload = {
87 signatureActorId: byActor.id,
91 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload)
94 function getOriginVideoAudience (video: VideoModel, actorsInvolvedInVideo: ActorModel[]) {
96 to: [ video.VideoChannel.Account.Actor.url ],
97 cc: actorsInvolvedInVideo.map(a => a.followersUrl)
101 function getOriginVideoCommentAudience (
102 videoComment: VideoCommentModel,
103 threadParentComments: VideoCommentModel[],
104 actorsInvolvedInVideo: ActorModel[],
107 const to = [ ACTIVITY_PUB.PUBLIC ]
110 // Owner of the video we comment
111 if (isOrigin === false) {
112 cc.push(videoComment.Video.VideoChannel.Account.Actor.url)
115 // Followers of the poster
116 cc.push(videoComment.Account.Actor.followersUrl)
118 // Send to actors we reply to
119 for (const parentComment of threadParentComments) {
120 cc.push(parentComment.Account.Actor.url)
125 cc: cc.concat(actorsInvolvedInVideo.map(a => a.followersUrl))
129 function getObjectFollowersAudience (actorsInvolvedInObject: ActorModel[]) {
131 to: actorsInvolvedInObject.map(a => a.followersUrl),
136 async function getActorsInvolvedInVideo (video: VideoModel, t: Transaction) {
137 const actors = await VideoShareModel.loadActorsByShare(video.id, t)
138 actors.push(video.VideoChannel.Account.Actor)
143 async function getAudience (actorSender: ActorModel, t: Transaction, isPublic = true) {
144 const followerInboxUrls = await actorSender.getFollowerSharedInboxUrls(t)
146 // Thanks Mastodon: https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/tag_manager.rb#L47
151 to = [ ACTIVITY_PUB.PUBLIC ]
152 cc = followerInboxUrls
154 to = followerInboxUrls
155 cc = [ ACTIVITY_PUB.PUBLIC ]
161 function audiencify (object: any, audience: ActivityAudience) {
162 return Object.assign(object, audience)
165 async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) {
166 const toActorFollowerIds = toActorFollower.map(a => a.id)
168 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
169 const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
170 return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
173 async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) {
174 const toActorSharedInboxesSet = new Set(toActors.map(a => a.sharedInboxUrl || a.inboxUrl))
176 const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
177 return Array.from(toActorSharedInboxesSet)
178 .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
181 // ---------------------------------------------------------------------------
184 broadcastToFollowers,
187 getOriginVideoAudience,
188 getActorsInvolvedInVideo,
189 getObjectFollowersAudience,
192 getOriginVideoCommentAudience,