]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/utils.ts
Rephrase account/channel signup description
[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'
598edb8a 11import { ContextType } from '@server/helpers/activitypub'
9588d4f4 12
a2377d15 13async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: {
a1587156
C
14 byActor: MActorLight
15 video: MVideoAccountLight
598edb8a
C
16 transaction?: Transaction,
17 contextType?: ContextType
a2377d15 18}) {
598edb8a 19 const { byActor, video, transaction, contextType } = options
5224c394
C
20
21 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, transaction)
a2377d15
C
22
23 // Send to origin
5224c394
C
24 if (video.isOwned() === false) {
25 const audience = getRemoteVideoAudience(video, actorsInvolvedInVideo)
a2377d15
C
26 const activity = activityBuilder(audience)
27
5224c394 28 return afterCommitIfTransaction(transaction, () => {
598edb8a 29 return unicastTo(activity, byActor, video.VideoChannel.Account.Actor.getSharedInbox(), contextType)
2284f202 30 })
a2377d15
C
31 }
32
33 // Send to followers
34 const audience = getAudienceFromFollowersOf(actorsInvolvedInVideo)
35 const activity = activityBuilder(audience)
36
5224c394 37 const actorsException = [ byActor ]
2284f202 38
598edb8a 39 return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, transaction, actorsException, contextType)
a2377d15
C
40}
41
9588d4f4
C
42async function forwardVideoRelatedActivity (
43 activity: Activity,
44 t: Transaction,
47581df0 45 followersException: MActorWithInboxes[] = [],
453e83ea 46 video: MVideo
9588d4f4
C
47) {
48 // Mastodon does not add our announces in audience, so we forward to them manually
49 const additionalActors = await getActorsInvolvedInVideo(video, t)
50 const additionalFollowerUrls = additionalActors.map(a => a.followersUrl)
51
52 return forwardActivity(activity, t, followersException, additionalFollowerUrls)
53}
63c93323
C
54
55async function forwardActivity (
56 activity: Activity,
57 t: Transaction,
47581df0 58 followersException: MActorWithInboxes[] = [],
93ef8a9d 59 additionalFollowerUrls: string[] = []
63c93323 60) {
8e0fd45e
C
61 logger.info('Forwarding activity %s.', activity.id)
62
63c93323
C
63 const to = activity.to || []
64 const cc = activity.cc || []
65
93ef8a9d 66 const followersUrls = additionalFollowerUrls
63c93323
C
67 for (const dest of to.concat(cc)) {
68 if (dest.endsWith('/followers')) {
69 followersUrls.push(dest)
70 }
71 }
72
50d6de9c
C
73 const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
74 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
63c93323
C
75
76 if (uris.length === 0) {
50d6de9c 77 logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
df1966c9 78 return undefined
63c93323
C
79 }
80
81 logger.debug('Creating forwarding job.', { uris })
82
94a5ff8a 83 const payload = {
63c93323
C
84 uris,
85 body: activity
86 }
2284f202 87 return afterCommitIfTransaction(t, () => JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }))
63c93323 88}
54141398 89
40ff5707
C
90async function broadcastToFollowers (
91 data: any,
453e83ea
C
92 byActor: MActorId,
93 toFollowersOf: MActorId[],
40ff5707 94 t: Transaction,
598edb8a
C
95 actorsException: MActorWithInboxes[] = [],
96 contextType?: ContextType
40ff5707 97) {
c48e82b5 98 const uris = await computeFollowerUris(toFollowersOf, actorsException, t)
2284f202 99
598edb8a 100 return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor, contextType))
93ef8a9d
C
101}
102
103async function broadcastToActors (
104 data: any,
453e83ea
C
105 byActor: MActorId,
106 toActors: MActor[],
2284f202 107 t?: Transaction,
598edb8a
C
108 actorsException: MActorWithInboxes[] = [],
109 contextType?: ContextType
93ef8a9d
C
110) {
111 const uris = await computeUris(toActors, actorsException)
598edb8a 112 return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor, contextType))
93ef8a9d
C
113}
114
598edb8a 115function broadcastTo (uris: string[], data: any, byActor: MActorId, contextType?: ContextType) {
93ef8a9d 116 if (uris.length === 0) return undefined
54141398 117
63c93323 118 logger.debug('Creating broadcast job.', { uris })
40ff5707 119
94a5ff8a 120 const payload = {
40ff5707 121 uris,
50d6de9c 122 signatureActorId: byActor.id,
598edb8a
C
123 body: data,
124 contextType
54141398
C
125 }
126
94a5ff8a 127 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
54141398
C
128}
129
598edb8a 130function unicastTo (data: any, byActor: MActorId, toActorUrl: string, contextType?: ContextType) {
50d6de9c 131 logger.debug('Creating unicast job.', { uri: toActorUrl })
63c93323 132
94a5ff8a
C
133 const payload = {
134 uri: toActorUrl,
50d6de9c 135 signatureActorId: byActor.id,
598edb8a
C
136 body: data,
137 contextType
54141398
C
138 }
139
2284f202 140 JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
54141398
C
141}
142
e251f170 143// ---------------------------------------------------------------------------
54141398 144
e251f170
C
145export {
146 broadcastToFollowers,
147 unicastTo,
148 forwardActivity,
9588d4f4 149 broadcastToActors,
a2377d15
C
150 forwardVideoRelatedActivity,
151 sendVideoRelatedActivity
54141398
C
152}
153
e251f170 154// ---------------------------------------------------------------------------
e12a0092 155
47581df0 156async function computeFollowerUris (toFollowersOf: MActorId[], actorsException: MActorWithInboxes[], t: Transaction) {
c48e82b5 157 const toActorFollowerIds = toFollowersOf.map(a => a.id)
63c93323 158
50d6de9c 159 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
06a05d5f
C
160 const sharedInboxesException = await buildSharedInboxesException(actorsException)
161
93ef8a9d
C
162 return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
163}
164
47581df0 165async function computeUris (toActors: MActor[], actorsException: MActorWithInboxes[] = []) {
06a05d5f
C
166 const serverActor = await getServerActor()
167 const targetUrls = toActors
168 .filter(a => a.id !== serverActor.id) // Don't send to ourselves
47581df0 169 .map(a => a.getSharedInbox())
06a05d5f
C
170
171 const toActorSharedInboxesSet = new Set(targetUrls)
93ef8a9d 172
06a05d5f 173 const sharedInboxesException = await buildSharedInboxesException(actorsException)
93ef8a9d 174 return Array.from(toActorSharedInboxesSet)
e251f170 175 .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
54141398 176}
06a05d5f 177
47581df0 178async function buildSharedInboxesException (actorsException: MActorWithInboxes[]) {
06a05d5f
C
179 const serverActor = await getServerActor()
180
181 return actorsException
47581df0 182 .map(f => f.getSharedInbox())
06a05d5f
C
183 .concat([ serverActor.sharedInboxUrl ])
184}