]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/utils.ts
Remove progress bar transition
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / utils.ts
CommitLineData
54141398 1import { Transaction } from 'sequelize'
a2377d15 2import { Activity, ActivityAudience } from '../../../../shared/models/activitypub'
da854ddd 3import { logger } from '../../../helpers/logger'
50d6de9c
C
4import { ActorModel } from '../../../models/activitypub/actor'
5import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
94a5ff8a 6import { JobQueue } from '../../job-queue'
a2377d15 7import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience'
06a05d5f 8import { getServerActor } from '../../../helpers/utils'
2284f202 9import { afterCommitIfTransaction } from '../../../helpers/database-utils'
47581df0 10import { MActorWithInboxes, MActor, MActorId, MActorLight, MVideo, MVideoAccountLight } from '../../../typings/models'
9588d4f4 11
a2377d15 12async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: {
a1587156
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 26 return afterCommitIfTransaction(transaction, () => {
47581df0 27 return unicastTo(activity, byActor, video.VideoChannel.Account.Actor.getSharedInbox())
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
40async function forwardVideoRelatedActivity (
41 activity: Activity,
42 t: Transaction,
47581df0 43 followersException: MActorWithInboxes[] = [],
453e83ea 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
53async function forwardActivity (
54 activity: Activity,
55 t: Transaction,
47581df0 56 followersException: MActorWithInboxes[] = [],
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
88async function broadcastToFollowers (
89 data: any,
453e83ea
C
90 byActor: MActorId,
91 toFollowersOf: MActorId[],
40ff5707 92 t: Transaction,
47581df0 93 actorsException: MActorWithInboxes[] = []
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
100async function broadcastToActors (
101 data: any,
453e83ea
C
102 byActor: MActorId,
103 toActors: MActor[],
2284f202 104 t?: Transaction,
47581df0 105 actorsException: MActorWithInboxes[] = []
93ef8a9d
C
106) {
107 const uris = await computeUris(toActors, actorsException)
2284f202 108 return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor))
93ef8a9d
C
109}
110
453e83ea 111function 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 125function 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
139export {
140 broadcastToFollowers,
141 unicastTo,
142 forwardActivity,
9588d4f4 143 broadcastToActors,
a2377d15
C
144 forwardVideoRelatedActivity,
145 sendVideoRelatedActivity
54141398
C
146}
147
e251f170 148// ---------------------------------------------------------------------------
e12a0092 149
47581df0 150async function computeFollowerUris (toFollowersOf: MActorId[], actorsException: MActorWithInboxes[], 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
47581df0 159async function computeUris (toActors: MActor[], actorsException: MActorWithInboxes[] = []) {
06a05d5f
C
160 const serverActor = await getServerActor()
161 const targetUrls = toActors
162 .filter(a => a.id !== serverActor.id) // Don't send to ourselves
47581df0 163 .map(a => a.getSharedInbox())
06a05d5f
C
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
47581df0 172async function buildSharedInboxesException (actorsException: MActorWithInboxes[]) {
06a05d5f
C
173 const serverActor = await getServerActor()
174
175 return actorsException
47581df0 176 .map(f => f.getSharedInbox())
06a05d5f
C
177 .concat([ serverActor.sharedInboxUrl ])
178}