]>
Commit | Line | Data |
---|---|---|
54141398 | 1 | import { Transaction } from 'sequelize' |
e12a0092 | 2 | import { Activity, ActivityAudience } from '../../../../shared/models/activitypub' |
da854ddd | 3 | import { logger } from '../../../helpers/logger' |
3fd3ab2d | 4 | import { ACTIVITY_PUB } from '../../../initializers' |
50d6de9c C |
5 | import { ActorModel } from '../../../models/activitypub/actor' |
6 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | |
3fd3ab2d | 7 | import { VideoModel } from '../../../models/video/video' |
d7e70384 | 8 | import { VideoCommentModel } from '../../../models/video/video-comment' |
3fd3ab2d | 9 | import { VideoShareModel } from '../../../models/video/video-share' |
94a5ff8a | 10 | import { JobQueue } from '../../job-queue' |
63c93323 C |
11 | |
12 | async function forwardActivity ( | |
13 | activity: Activity, | |
14 | t: Transaction, | |
93ef8a9d C |
15 | followersException: ActorModel[] = [], |
16 | additionalFollowerUrls: string[] = [] | |
63c93323 C |
17 | ) { |
18 | const to = activity.to || [] | |
19 | const cc = activity.cc || [] | |
20 | ||
93ef8a9d | 21 | const followersUrls = additionalFollowerUrls |
63c93323 C |
22 | for (const dest of to.concat(cc)) { |
23 | if (dest.endsWith('/followers')) { | |
24 | followersUrls.push(dest) | |
25 | } | |
26 | } | |
27 | ||
50d6de9c C |
28 | const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t) |
29 | const uris = await computeFollowerUris(toActorFollowers, followersException, t) | |
63c93323 C |
30 | |
31 | if (uris.length === 0) { | |
50d6de9c | 32 | logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', ')) |
df1966c9 | 33 | return undefined |
63c93323 C |
34 | } |
35 | ||
36 | logger.debug('Creating forwarding job.', { uris }) | |
37 | ||
94a5ff8a | 38 | const payload = { |
63c93323 C |
39 | uris, |
40 | body: activity | |
41 | } | |
94a5ff8a | 42 | return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }) |
63c93323 | 43 | } |
54141398 | 44 | |
40ff5707 C |
45 | async function broadcastToFollowers ( |
46 | data: any, | |
50d6de9c C |
47 | byActor: ActorModel, |
48 | toActorFollowers: ActorModel[], | |
40ff5707 | 49 | t: Transaction, |
93ef8a9d | 50 | actorsException: ActorModel[] = [] |
40ff5707 | 51 | ) { |
93ef8a9d | 52 | const uris = await computeFollowerUris(toActorFollowers, actorsException, t) |
94a5ff8a | 53 | return broadcastTo(uris, data, byActor) |
93ef8a9d C |
54 | } |
55 | ||
56 | async function broadcastToActors ( | |
57 | data: any, | |
58 | byActor: ActorModel, | |
59 | toActors: ActorModel[], | |
93ef8a9d C |
60 | actorsException: ActorModel[] = [] |
61 | ) { | |
62 | const uris = await computeUris(toActors, actorsException) | |
94a5ff8a | 63 | return broadcastTo(uris, data, byActor) |
93ef8a9d C |
64 | } |
65 | ||
94a5ff8a | 66 | async function broadcastTo (uris: string[], data: any, byActor: ActorModel) { |
93ef8a9d | 67 | if (uris.length === 0) return undefined |
54141398 | 68 | |
63c93323 | 69 | logger.debug('Creating broadcast job.', { uris }) |
40ff5707 | 70 | |
94a5ff8a | 71 | const payload = { |
40ff5707 | 72 | uris, |
50d6de9c | 73 | signatureActorId: byActor.id, |
54141398 C |
74 | body: data |
75 | } | |
76 | ||
94a5ff8a | 77 | return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }) |
54141398 C |
78 | } |
79 | ||
94a5ff8a | 80 | async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string) { |
50d6de9c | 81 | logger.debug('Creating unicast job.', { uri: toActorUrl }) |
63c93323 | 82 | |
94a5ff8a C |
83 | const payload = { |
84 | uri: toActorUrl, | |
50d6de9c | 85 | signatureActorId: byActor.id, |
54141398 C |
86 | body: data |
87 | } | |
88 | ||
94a5ff8a | 89 | return JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload }) |
54141398 C |
90 | } |
91 | ||
50d6de9c | 92 | function getOriginVideoAudience (video: VideoModel, actorsInvolvedInVideo: ActorModel[]) { |
0032ebe9 | 93 | return { |
50d6de9c C |
94 | to: [ video.VideoChannel.Account.Actor.url ], |
95 | cc: actorsInvolvedInVideo.map(a => a.followersUrl) | |
0032ebe9 C |
96 | } |
97 | } | |
98 | ||
d7e70384 C |
99 | function getOriginVideoCommentAudience ( |
100 | videoComment: VideoCommentModel, | |
101 | threadParentComments: VideoCommentModel[], | |
102 | actorsInvolvedInVideo: ActorModel[], | |
103 | isOrigin = false | |
104 | ) { | |
105 | const to = [ ACTIVITY_PUB.PUBLIC ] | |
106 | const cc = [ ] | |
107 | ||
108 | // Owner of the video we comment | |
109 | if (isOrigin === false) { | |
110 | cc.push(videoComment.Video.VideoChannel.Account.Actor.url) | |
111 | } | |
112 | ||
113 | // Followers of the poster | |
114 | cc.push(videoComment.Account.Actor.followersUrl) | |
115 | ||
116 | // Send to actors we reply to | |
117 | for (const parentComment of threadParentComments) { | |
118 | cc.push(parentComment.Account.Actor.url) | |
119 | } | |
120 | ||
121 | return { | |
122 | to, | |
123 | cc: cc.concat(actorsInvolvedInVideo.map(a => a.followersUrl)) | |
124 | } | |
125 | } | |
126 | ||
50d6de9c | 127 | function getObjectFollowersAudience (actorsInvolvedInObject: ActorModel[]) { |
4e50b6a1 | 128 | return { |
50d6de9c | 129 | to: actorsInvolvedInObject.map(a => a.followersUrl), |
0032ebe9 C |
130 | cc: [] |
131 | } | |
132 | } | |
133 | ||
50d6de9c | 134 | async function getActorsInvolvedInVideo (video: VideoModel, t: Transaction) { |
d7e70384 C |
135 | const actors = await VideoShareModel.loadActorsByShare(video.id, t) |
136 | actors.push(video.VideoChannel.Account.Actor) | |
4e50b6a1 | 137 | |
d7e70384 | 138 | return actors |
4e50b6a1 C |
139 | } |
140 | ||
50d6de9c C |
141 | async function getAudience (actorSender: ActorModel, t: Transaction, isPublic = true) { |
142 | const followerInboxUrls = await actorSender.getFollowerSharedInboxUrls(t) | |
54141398 | 143 | |
54e74059 C |
144 | return buildAudience(followerInboxUrls, isPublic) |
145 | } | |
146 | ||
147 | function buildAudience (followerInboxUrls: string[], isPublic = true) { | |
54141398 C |
148 | // Thanks Mastodon: https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/tag_manager.rb#L47 |
149 | let to = [] | |
150 | let cc = [] | |
151 | ||
152 | if (isPublic) { | |
153 | to = [ ACTIVITY_PUB.PUBLIC ] | |
154 | cc = followerInboxUrls | |
155 | } else { // Unlisted | |
156 | to = followerInboxUrls | |
157 | cc = [ ACTIVITY_PUB.PUBLIC ] | |
158 | } | |
159 | ||
160 | return { to, cc } | |
161 | } | |
162 | ||
e12a0092 C |
163 | function audiencify (object: any, audience: ActivityAudience) { |
164 | return Object.assign(object, audience) | |
165 | } | |
166 | ||
93ef8a9d | 167 | async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) { |
50d6de9c | 168 | const toActorFollowerIds = toActorFollower.map(a => a.id) |
63c93323 | 169 | |
50d6de9c | 170 | const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t) |
9a8cbd82 | 171 | const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl) |
93ef8a9d C |
172 | return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1) |
173 | } | |
174 | ||
175 | async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) { | |
9a8cbd82 | 176 | const toActorSharedInboxesSet = new Set(toActors.map(a => a.sharedInboxUrl || a.inboxUrl)) |
93ef8a9d | 177 | |
9a8cbd82 | 178 | const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl) |
93ef8a9d C |
179 | return Array.from(toActorSharedInboxesSet) |
180 | .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1) | |
63c93323 C |
181 | } |
182 | ||
54141398 C |
183 | // --------------------------------------------------------------------------- |
184 | ||
185 | export { | |
186 | broadcastToFollowers, | |
187 | unicastTo, | |
54e74059 | 188 | buildAudience, |
0032ebe9 C |
189 | getAudience, |
190 | getOriginVideoAudience, | |
50d6de9c | 191 | getActorsInvolvedInVideo, |
4e50b6a1 | 192 | getObjectFollowersAudience, |
e12a0092 | 193 | forwardActivity, |
d7e70384 | 194 | audiencify, |
93ef8a9d C |
195 | getOriginVideoCommentAudience, |
196 | computeUris, | |
197 | broadcastToActors | |
54141398 | 198 | } |