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[] = []
17 const to = activity.to || []
18 const cc = activity.cc || []
20 const followersUrls: string[] = []
21 for (const dest of to.concat(cc)) {
22 if (dest.endsWith('/followers')) {
23 followersUrls.push(dest)
27 const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
28 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
30 if (uris.length === 0) {
31 logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
35 logger.debug('Creating forwarding job.', { uris })
37 const jobPayload: ActivityPubHttpPayload = {
42 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
45 async function broadcastToFollowers (
48 toActorFollowers: ActorModel[],
50 followersException: ActorModel[] = []
52 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
53 if (uris.length === 0) {
54 logger.info('0 followers for %s, no broadcasting.', toActorFollowers.map(a => a.id).join(', '))
58 logger.debug('Creating broadcast job.', { uris })
60 const jobPayload: ActivityPubHttpPayload = {
62 signatureActorId: byActor.id,
66 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
69 async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string, t: Transaction) {
70 logger.debug('Creating unicast job.', { uri: toActorUrl })
72 const jobPayload: ActivityPubHttpPayload = {
74 signatureActorId: byActor.id,
78 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload)
81 function getOriginVideoAudience (video: VideoModel, actorsInvolvedInVideo: ActorModel[]) {
83 to: [ video.VideoChannel.Account.Actor.url ],
84 cc: actorsInvolvedInVideo.map(a => a.followersUrl)
88 function getOriginVideoCommentAudience (
89 videoComment: VideoCommentModel,
90 threadParentComments: VideoCommentModel[],
91 actorsInvolvedInVideo: ActorModel[],
94 const to = [ ACTIVITY_PUB.PUBLIC ]
97 // Owner of the video we comment
98 if (isOrigin === false) {
99 cc.push(videoComment.Video.VideoChannel.Account.Actor.url)
102 // Followers of the poster
103 cc.push(videoComment.Account.Actor.followersUrl)
105 // Send to actors we reply to
106 for (const parentComment of threadParentComments) {
107 cc.push(parentComment.Account.Actor.url)
112 cc: cc.concat(actorsInvolvedInVideo.map(a => a.followersUrl))
116 function getObjectFollowersAudience (actorsInvolvedInObject: ActorModel[]) {
118 to: actorsInvolvedInObject.map(a => a.followersUrl),
123 async function getActorsInvolvedInVideo (video: VideoModel, t: Transaction) {
124 const actors = await VideoShareModel.loadActorsByShare(video.id, t)
125 actors.push(video.VideoChannel.Account.Actor)
130 async function getAudience (actorSender: ActorModel, t: Transaction, isPublic = true) {
131 const followerInboxUrls = await actorSender.getFollowerSharedInboxUrls(t)
133 // Thanks Mastodon: https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/tag_manager.rb#L47
138 to = [ ACTIVITY_PUB.PUBLIC ]
139 cc = followerInboxUrls
141 to = followerInboxUrls
142 cc = [ ACTIVITY_PUB.PUBLIC ]
148 function audiencify (object: any, audience: ActivityAudience) {
149 return Object.assign(object, audience)
152 async function computeFollowerUris (toActorFollower: ActorModel[], followersException: ActorModel[], t: Transaction) {
153 const toActorFollowerIds = toActorFollower.map(a => a.id)
155 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
156 const followersSharedInboxException = followersException.map(f => f.sharedInboxUrl)
157 return result.data.filter(sharedInbox => followersSharedInboxException.indexOf(sharedInbox) === -1)
160 // ---------------------------------------------------------------------------
163 broadcastToFollowers,
166 getOriginVideoAudience,
167 getActorsInvolvedInVideo,
168 getObjectFollowersAudience,
171 getOriginVideoCommentAudience