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 { JobQueue } from '../../job-queue'
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 })
42 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
45 async function broadcastToFollowers (
48 toActorFollowers: ActorModel[],
50 actorsException: ActorModel[] = []
52 const uris = await computeFollowerUris(toActorFollowers, actorsException, t)
53 return broadcastTo(uris, data, byActor)
56 async function broadcastToActors (
59 toActors: ActorModel[],
60 actorsException: ActorModel[] = []
62 const uris = await computeUris(toActors, actorsException)
63 return broadcastTo(uris, data, byActor)
66 async function broadcastTo (uris: string[], data: any, byActor: ActorModel) {
67 if (uris.length === 0) return undefined
69 logger.debug('Creating broadcast job.', { uris })
73 signatureActorId: byActor.id,
77 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
80 async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string) {
81 logger.debug('Creating unicast job.', { uri: toActorUrl })
85 signatureActorId: byActor.id,
89 return JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
92 function getOriginVideoAudience (video: VideoModel, actorsInvolvedInVideo: ActorModel[]) {
94 to: [ video.VideoChannel.Account.Actor.url ],
95 cc: actorsInvolvedInVideo.map(a => a.followersUrl)
99 function getOriginVideoCommentAudience (
100 videoComment: VideoCommentModel,
101 threadParentComments: VideoCommentModel[],
102 actorsInvolvedInVideo: ActorModel[],
105 const to = [ ACTIVITY_PUB.PUBLIC ]
108 // Owner of the video we comment
109 if (isOrigin === false) {
110 cc.push(videoComment.Video.VideoChannel.Account.Actor.url)
113 // Followers of the poster
114 cc.push(videoComment.Account.Actor.followersUrl)
116 // Send to actors we reply to
117 for (const parentComment of threadParentComments) {
118 cc.push(parentComment.Account.Actor.url)
123 cc: cc.concat(actorsInvolvedInVideo.map(a => a.followersUrl))
127 function getObjectFollowersAudience (actorsInvolvedInObject: ActorModel[]) {
129 to: actorsInvolvedInObject.map(a => a.followersUrl),
134 async function getActorsInvolvedInVideo (video: VideoModel, t: Transaction) {
135 const actors = await VideoShareModel.loadActorsByShare(video.id, t)
136 actors.push(video.VideoChannel.Account.Actor)
141 async function getAudience (actorSender: ActorModel, t: Transaction, isPublic = true) {
142 const followerInboxUrls = await actorSender.getFollowerSharedInboxUrls(t)
144 return buildAudience(followerInboxUrls, isPublic)
147 function buildAudience (followerInboxUrls: string[], isPublic = true) {
148 // Thanks Mastodon: https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/tag_manager.rb#L47
153 to = [ ACTIVITY_PUB.PUBLIC ]
154 cc = followerInboxUrls
163 function audiencify (object: any, audience: ActivityAudience) {
164 return Object.assign(object, audience)
167 async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) {
168 const toActorFollowerIds = toActorFollower.map(a => a.id)
170 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
171 const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
172 return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
175 async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) {
176 const toActorSharedInboxesSet = new Set(toActors.map(a => a.sharedInboxUrl || a.inboxUrl))
178 const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
179 return Array.from(toActorSharedInboxesSet)
180 .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
183 // ---------------------------------------------------------------------------
186 broadcastToFollowers,
190 getOriginVideoAudience,
191 getActorsInvolvedInVideo,
192 getObjectFollowersAudience,
195 getOriginVideoCommentAudience,