]>
Commit | Line | Data |
---|---|---|
54141398 | 1 | import { Transaction } from 'sequelize' |
e251f170 | 2 | import { Activity } 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 C |
7 | import { VideoModel } from '../../../models/video/video' |
8 | import { getActorsInvolvedInVideo } from '../audience' | |
9 | ||
10 | async function forwardVideoRelatedActivity ( | |
11 | activity: Activity, | |
12 | t: Transaction, | |
13 | followersException: ActorModel[] = [], | |
14 | video: VideoModel | |
15 | ) { | |
16 | // Mastodon does not add our announces in audience, so we forward to them manually | |
17 | const additionalActors = await getActorsInvolvedInVideo(video, t) | |
18 | const additionalFollowerUrls = additionalActors.map(a => a.followersUrl) | |
19 | ||
20 | return forwardActivity(activity, t, followersException, additionalFollowerUrls) | |
21 | } | |
63c93323 C |
22 | |
23 | async function forwardActivity ( | |
24 | activity: Activity, | |
25 | t: Transaction, | |
93ef8a9d C |
26 | followersException: ActorModel[] = [], |
27 | additionalFollowerUrls: string[] = [] | |
63c93323 | 28 | ) { |
8e0fd45e C |
29 | logger.info('Forwarding activity %s.', activity.id) |
30 | ||
63c93323 C |
31 | const to = activity.to || [] |
32 | const cc = activity.cc || [] | |
33 | ||
93ef8a9d | 34 | const followersUrls = additionalFollowerUrls |
63c93323 C |
35 | for (const dest of to.concat(cc)) { |
36 | if (dest.endsWith('/followers')) { | |
37 | followersUrls.push(dest) | |
38 | } | |
39 | } | |
40 | ||
50d6de9c C |
41 | const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t) |
42 | const uris = await computeFollowerUris(toActorFollowers, followersException, t) | |
63c93323 C |
43 | |
44 | if (uris.length === 0) { | |
50d6de9c | 45 | logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', ')) |
df1966c9 | 46 | return undefined |
63c93323 C |
47 | } |
48 | ||
49 | logger.debug('Creating forwarding job.', { uris }) | |
50 | ||
94a5ff8a | 51 | const payload = { |
63c93323 C |
52 | uris, |
53 | body: activity | |
54 | } | |
94a5ff8a | 55 | return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }) |
63c93323 | 56 | } |
54141398 | 57 | |
40ff5707 C |
58 | async function broadcastToFollowers ( |
59 | data: any, | |
50d6de9c C |
60 | byActor: ActorModel, |
61 | toActorFollowers: ActorModel[], | |
40ff5707 | 62 | t: Transaction, |
93ef8a9d | 63 | actorsException: ActorModel[] = [] |
40ff5707 | 64 | ) { |
93ef8a9d | 65 | const uris = await computeFollowerUris(toActorFollowers, actorsException, t) |
94a5ff8a | 66 | return broadcastTo(uris, data, byActor) |
93ef8a9d C |
67 | } |
68 | ||
69 | async function broadcastToActors ( | |
70 | data: any, | |
71 | byActor: ActorModel, | |
72 | toActors: ActorModel[], | |
93ef8a9d C |
73 | actorsException: ActorModel[] = [] |
74 | ) { | |
75 | const uris = await computeUris(toActors, actorsException) | |
94a5ff8a | 76 | return broadcastTo(uris, data, byActor) |
93ef8a9d C |
77 | } |
78 | ||
94a5ff8a | 79 | async function broadcastTo (uris: string[], data: any, byActor: ActorModel) { |
93ef8a9d | 80 | if (uris.length === 0) return undefined |
54141398 | 81 | |
63c93323 | 82 | logger.debug('Creating broadcast job.', { uris }) |
40ff5707 | 83 | |
94a5ff8a | 84 | const payload = { |
40ff5707 | 85 | uris, |
50d6de9c | 86 | signatureActorId: byActor.id, |
54141398 C |
87 | body: data |
88 | } | |
89 | ||
94a5ff8a | 90 | return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }) |
54141398 C |
91 | } |
92 | ||
94a5ff8a | 93 | async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string) { |
50d6de9c | 94 | logger.debug('Creating unicast job.', { uri: toActorUrl }) |
63c93323 | 95 | |
94a5ff8a C |
96 | const payload = { |
97 | uri: toActorUrl, | |
50d6de9c | 98 | signatureActorId: byActor.id, |
54141398 C |
99 | body: data |
100 | } | |
101 | ||
94a5ff8a | 102 | return JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload }) |
54141398 C |
103 | } |
104 | ||
e251f170 | 105 | // --------------------------------------------------------------------------- |
54141398 | 106 | |
e251f170 C |
107 | export { |
108 | broadcastToFollowers, | |
109 | unicastTo, | |
110 | forwardActivity, | |
9588d4f4 C |
111 | broadcastToActors, |
112 | forwardVideoRelatedActivity | |
54141398 C |
113 | } |
114 | ||
e251f170 | 115 | // --------------------------------------------------------------------------- |
e12a0092 | 116 | |
93ef8a9d | 117 | async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) { |
50d6de9c | 118 | const toActorFollowerIds = toActorFollower.map(a => a.id) |
63c93323 | 119 | |
50d6de9c | 120 | const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t) |
9a8cbd82 | 121 | const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl) |
93ef8a9d C |
122 | return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1) |
123 | } | |
124 | ||
125 | async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) { | |
9a8cbd82 | 126 | const toActorSharedInboxesSet = new Set(toActors.map(a => a.sharedInboxUrl || a.inboxUrl)) |
93ef8a9d | 127 | |
9a8cbd82 | 128 | const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl) |
93ef8a9d | 129 | return Array.from(toActorSharedInboxesSet) |
e251f170 | 130 | .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1) |
54141398 | 131 | } |