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