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