]>
Commit | Line | Data |
---|---|---|
54141398 | 1 | import { Transaction } from 'sequelize' |
a2377d15 | 2 | import { Activity, ActivityAudience } from '../../../../shared/models/activitypub' |
da854ddd | 3 | import { logger } from '../../../helpers/logger' |
50d6de9c C |
4 | import { ActorModel } from '../../../models/activitypub/actor' |
5 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | |
94a5ff8a | 6 | import { JobQueue } from '../../job-queue' |
9588d4f4 | 7 | import { VideoModel } from '../../../models/video/video' |
a2377d15 | 8 | import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience' |
06a05d5f | 9 | import { getServerActor } from '../../../helpers/utils' |
2284f202 | 10 | import { afterCommitIfTransaction } from '../../../helpers/database-utils' |
9588d4f4 | 11 | |
a2377d15 C |
12 | async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: { |
13 | byActor: ActorModel, | |
14 | video: VideoModel, | |
15 | transaction?: Transaction | |
16 | }) { | |
17 | const actorsInvolvedInVideo = await getActorsInvolvedInVideo(options.video, options.transaction) | |
18 | ||
19 | // Send to origin | |
20 | if (options.video.isOwned() === false) { | |
21 | const audience = getRemoteVideoAudience(options.video, actorsInvolvedInVideo) | |
22 | const activity = activityBuilder(audience) | |
23 | ||
2284f202 C |
24 | return afterCommitIfTransaction(options.transaction, () => { |
25 | return unicastTo(activity, options.byActor, options.video.VideoChannel.Account.Actor.sharedInboxUrl) | |
26 | }) | |
a2377d15 C |
27 | } |
28 | ||
29 | // Send to followers | |
30 | const audience = getAudienceFromFollowersOf(actorsInvolvedInVideo) | |
31 | const activity = activityBuilder(audience) | |
32 | ||
33 | const actorsException = [ options.byActor ] | |
2284f202 | 34 | |
a2377d15 C |
35 | return broadcastToFollowers(activity, options.byActor, actorsInvolvedInVideo, options.transaction, actorsException) |
36 | } | |
37 | ||
9588d4f4 C |
38 | async function forwardVideoRelatedActivity ( |
39 | activity: Activity, | |
40 | t: Transaction, | |
41 | followersException: ActorModel[] = [], | |
42 | video: VideoModel | |
43 | ) { | |
44 | // Mastodon does not add our announces in audience, so we forward to them manually | |
45 | const additionalActors = await getActorsInvolvedInVideo(video, t) | |
46 | const additionalFollowerUrls = additionalActors.map(a => a.followersUrl) | |
47 | ||
48 | return forwardActivity(activity, t, followersException, additionalFollowerUrls) | |
49 | } | |
63c93323 C |
50 | |
51 | async function forwardActivity ( | |
52 | activity: Activity, | |
53 | t: Transaction, | |
93ef8a9d C |
54 | followersException: ActorModel[] = [], |
55 | additionalFollowerUrls: string[] = [] | |
63c93323 | 56 | ) { |
8e0fd45e C |
57 | logger.info('Forwarding activity %s.', activity.id) |
58 | ||
63c93323 C |
59 | const to = activity.to || [] |
60 | const cc = activity.cc || [] | |
61 | ||
93ef8a9d | 62 | const followersUrls = additionalFollowerUrls |
63c93323 C |
63 | for (const dest of to.concat(cc)) { |
64 | if (dest.endsWith('/followers')) { | |
65 | followersUrls.push(dest) | |
66 | } | |
67 | } | |
68 | ||
50d6de9c C |
69 | const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t) |
70 | const uris = await computeFollowerUris(toActorFollowers, followersException, t) | |
63c93323 C |
71 | |
72 | if (uris.length === 0) { | |
50d6de9c | 73 | logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', ')) |
df1966c9 | 74 | return undefined |
63c93323 C |
75 | } |
76 | ||
77 | logger.debug('Creating forwarding job.', { uris }) | |
78 | ||
94a5ff8a | 79 | const payload = { |
63c93323 C |
80 | uris, |
81 | body: activity | |
82 | } | |
2284f202 | 83 | return afterCommitIfTransaction(t, () => JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })) |
63c93323 | 84 | } |
54141398 | 85 | |
40ff5707 C |
86 | async function broadcastToFollowers ( |
87 | data: any, | |
50d6de9c | 88 | byActor: ActorModel, |
c48e82b5 | 89 | toFollowersOf: ActorModel[], |
40ff5707 | 90 | t: Transaction, |
93ef8a9d | 91 | actorsException: ActorModel[] = [] |
40ff5707 | 92 | ) { |
c48e82b5 | 93 | const uris = await computeFollowerUris(toFollowersOf, actorsException, t) |
2284f202 C |
94 | |
95 | return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor)) | |
93ef8a9d C |
96 | } |
97 | ||
98 | async function broadcastToActors ( | |
99 | data: any, | |
100 | byActor: ActorModel, | |
101 | toActors: ActorModel[], | |
2284f202 | 102 | t?: Transaction, |
93ef8a9d C |
103 | actorsException: ActorModel[] = [] |
104 | ) { | |
105 | const uris = await computeUris(toActors, actorsException) | |
2284f202 | 106 | return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor)) |
93ef8a9d C |
107 | } |
108 | ||
2284f202 | 109 | function broadcastTo (uris: string[], data: any, byActor: ActorModel) { |
93ef8a9d | 110 | if (uris.length === 0) return undefined |
54141398 | 111 | |
63c93323 | 112 | logger.debug('Creating broadcast job.', { uris }) |
40ff5707 | 113 | |
94a5ff8a | 114 | const payload = { |
40ff5707 | 115 | uris, |
50d6de9c | 116 | signatureActorId: byActor.id, |
54141398 C |
117 | body: data |
118 | } | |
119 | ||
94a5ff8a | 120 | return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }) |
54141398 C |
121 | } |
122 | ||
2284f202 | 123 | function unicastTo (data: any, byActor: ActorModel, toActorUrl: string) { |
50d6de9c | 124 | logger.debug('Creating unicast job.', { uri: toActorUrl }) |
63c93323 | 125 | |
94a5ff8a C |
126 | const payload = { |
127 | uri: toActorUrl, | |
50d6de9c | 128 | signatureActorId: byActor.id, |
54141398 C |
129 | body: data |
130 | } | |
131 | ||
2284f202 | 132 | JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload }) |
54141398 C |
133 | } |
134 | ||
e251f170 | 135 | // --------------------------------------------------------------------------- |
54141398 | 136 | |
e251f170 C |
137 | export { |
138 | broadcastToFollowers, | |
139 | unicastTo, | |
140 | forwardActivity, | |
9588d4f4 | 141 | broadcastToActors, |
a2377d15 C |
142 | forwardVideoRelatedActivity, |
143 | sendVideoRelatedActivity | |
54141398 C |
144 | } |
145 | ||
e251f170 | 146 | // --------------------------------------------------------------------------- |
e12a0092 | 147 | |
c48e82b5 C |
148 | async function computeFollowerUris (toFollowersOf: ActorModel[], actorsException: ActorModel[], t: Transaction) { |
149 | const toActorFollowerIds = toFollowersOf.map(a => a.id) | |
63c93323 | 150 | |
50d6de9c | 151 | const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t) |
06a05d5f C |
152 | const sharedInboxesException = await buildSharedInboxesException(actorsException) |
153 | ||
93ef8a9d C |
154 | return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1) |
155 | } | |
156 | ||
157 | async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) { | |
06a05d5f C |
158 | const serverActor = await getServerActor() |
159 | const targetUrls = toActors | |
160 | .filter(a => a.id !== serverActor.id) // Don't send to ourselves | |
161 | .map(a => a.sharedInboxUrl || a.inboxUrl) | |
162 | ||
163 | const toActorSharedInboxesSet = new Set(targetUrls) | |
93ef8a9d | 164 | |
06a05d5f | 165 | const sharedInboxesException = await buildSharedInboxesException(actorsException) |
93ef8a9d | 166 | return Array.from(toActorSharedInboxesSet) |
e251f170 | 167 | .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1) |
54141398 | 168 | } |
06a05d5f C |
169 | |
170 | async function buildSharedInboxesException (actorsException: ActorModel[]) { | |
171 | const serverActor = await getServerActor() | |
172 | ||
173 | return actorsException | |
174 | .map(f => f.sharedInboxUrl || f.inboxUrl) | |
175 | .concat([ serverActor.sharedInboxUrl ]) | |
176 | } |