]>
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 | ||
73c08093 | 99 | function getVideoCommentAudience ( |
d7e70384 C |
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 { |
9c673970 | 129 | to: [ ACTIVITY_PUB.PUBLIC ].concat(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 | 141 | async function getAudience (actorSender: ActorModel, t: Transaction, isPublic = true) { |
16f29007 | 142 | return buildAudience([ actorSender.followersUrl ], isPublic) |
54e74059 C |
143 | } |
144 | ||
145 | function buildAudience (followerInboxUrls: string[], isPublic = true) { | |
54141398 C |
146 | // Thanks Mastodon: https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/tag_manager.rb#L47 |
147 | let to = [] | |
148 | let cc = [] | |
149 | ||
150 | if (isPublic) { | |
151 | to = [ ACTIVITY_PUB.PUBLIC ] | |
152 | cc = followerInboxUrls | |
153 | } else { // Unlisted | |
276d03ed C |
154 | to = [ ] |
155 | cc = [ ] | |
54141398 C |
156 | } |
157 | ||
158 | return { to, cc } | |
159 | } | |
160 | ||
73c08093 | 161 | function audiencify <T> (object: T, audience: ActivityAudience) { |
e12a0092 C |
162 | return Object.assign(object, audience) |
163 | } | |
164 | ||
93ef8a9d | 165 | async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) { |
50d6de9c | 166 | const toActorFollowerIds = toActorFollower.map(a => a.id) |
63c93323 | 167 | |
50d6de9c | 168 | const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t) |
9a8cbd82 | 169 | const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl) |
93ef8a9d C |
170 | return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1) |
171 | } | |
172 | ||
173 | async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) { | |
9a8cbd82 | 174 | const toActorSharedInboxesSet = new Set(toActors.map(a => a.sharedInboxUrl || a.inboxUrl)) |
93ef8a9d | 175 | |
9a8cbd82 | 176 | const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl) |
93ef8a9d C |
177 | return Array.from(toActorSharedInboxesSet) |
178 | .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1) | |
63c93323 C |
179 | } |
180 | ||
54141398 C |
181 | // --------------------------------------------------------------------------- |
182 | ||
183 | export { | |
184 | broadcastToFollowers, | |
185 | unicastTo, | |
54e74059 | 186 | buildAudience, |
0032ebe9 C |
187 | getAudience, |
188 | getOriginVideoAudience, | |
50d6de9c | 189 | getActorsInvolvedInVideo, |
4e50b6a1 | 190 | getObjectFollowersAudience, |
e12a0092 | 191 | forwardActivity, |
d7e70384 | 192 | audiencify, |
73c08093 | 193 | getVideoCommentAudience, |
93ef8a9d C |
194 | computeUris, |
195 | broadcastToActors | |
54141398 | 196 | } |