]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/utils.ts
Merge branch 'develop' into pr/1285
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / utils.ts
1 import { Transaction } from 'sequelize'
2 import { Activity, ActivityAudience } from '../../../../shared/models/activitypub'
3 import { logger } from '../../../helpers/logger'
4 import { ActorModel } from '../../../models/activitypub/actor'
5 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
6 import { JobQueue } from '../../job-queue'
7 import { VideoModel } from '../../../models/video/video'
8 import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience'
9 import { getServerActor } from '../../../helpers/utils'
10
11 async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: {
12 byActor: ActorModel,
13 video: VideoModel,
14 transaction?: Transaction
15 }) {
16 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(options.video, options.transaction)
17
18 // Send to origin
19 if (options.video.isOwned() === false) {
20 const audience = getRemoteVideoAudience(options.video, actorsInvolvedInVideo)
21 const activity = activityBuilder(audience)
22
23 return unicastTo(activity, options.byActor, options.video.VideoChannel.Account.Actor.sharedInboxUrl)
24 }
25
26 // Send to followers
27 const audience = getAudienceFromFollowersOf(actorsInvolvedInVideo)
28 const activity = activityBuilder(audience)
29
30 const actorsException = [ options.byActor ]
31 return broadcastToFollowers(activity, options.byActor, actorsInvolvedInVideo, options.transaction, actorsException)
32 }
33
34 async function forwardVideoRelatedActivity (
35 activity: Activity,
36 t: Transaction,
37 followersException: ActorModel[] = [],
38 video: VideoModel
39 ) {
40 // Mastodon does not add our announces in audience, so we forward to them manually
41 const additionalActors = await getActorsInvolvedInVideo(video, t)
42 const additionalFollowerUrls = additionalActors.map(a => a.followersUrl)
43
44 return forwardActivity(activity, t, followersException, additionalFollowerUrls)
45 }
46
47 async function forwardActivity (
48 activity: Activity,
49 t: Transaction,
50 followersException: ActorModel[] = [],
51 additionalFollowerUrls: string[] = []
52 ) {
53 logger.info('Forwarding activity %s.', activity.id)
54
55 const to = activity.to || []
56 const cc = activity.cc || []
57
58 const followersUrls = additionalFollowerUrls
59 for (const dest of to.concat(cc)) {
60 if (dest.endsWith('/followers')) {
61 followersUrls.push(dest)
62 }
63 }
64
65 const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
66 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
67
68 if (uris.length === 0) {
69 logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
70 return undefined
71 }
72
73 logger.debug('Creating forwarding job.', { uris })
74
75 const payload = {
76 uris,
77 body: activity
78 }
79 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
80 }
81
82 async function broadcastToFollowers (
83 data: any,
84 byActor: ActorModel,
85 toFollowersOf: ActorModel[],
86 t: Transaction,
87 actorsException: ActorModel[] = []
88 ) {
89 const uris = await computeFollowerUris(toFollowersOf, actorsException, t)
90 return broadcastTo(uris, data, byActor)
91 }
92
93 async function broadcastToActors (
94 data: any,
95 byActor: ActorModel,
96 toActors: ActorModel[],
97 actorsException: ActorModel[] = []
98 ) {
99 const uris = await computeUris(toActors, actorsException)
100 return broadcastTo(uris, data, byActor)
101 }
102
103 async function broadcastTo (uris: string[], data: any, byActor: ActorModel) {
104 if (uris.length === 0) return undefined
105
106 logger.debug('Creating broadcast job.', { uris })
107
108 const payload = {
109 uris,
110 signatureActorId: byActor.id,
111 body: data
112 }
113
114 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
115 }
116
117 async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string) {
118 logger.debug('Creating unicast job.', { uri: toActorUrl })
119
120 const payload = {
121 uri: toActorUrl,
122 signatureActorId: byActor.id,
123 body: data
124 }
125
126 return JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
127 }
128
129 // ---------------------------------------------------------------------------
130
131 export {
132 broadcastToFollowers,
133 unicastTo,
134 forwardActivity,
135 broadcastToActors,
136 forwardVideoRelatedActivity,
137 sendVideoRelatedActivity
138 }
139
140 // ---------------------------------------------------------------------------
141
142 async function computeFollowerUris (toFollowersOf: ActorModel[], actorsException: ActorModel[], t: Transaction) {
143 const toActorFollowerIds = toFollowersOf.map(a => a.id)
144
145 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
146 const sharedInboxesException = await buildSharedInboxesException(actorsException)
147
148 return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
149 }
150
151 async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) {
152 const serverActor = await getServerActor()
153 const targetUrls = toActors
154 .filter(a => a.id !== serverActor.id) // Don't send to ourselves
155 .map(a => a.sharedInboxUrl || a.inboxUrl)
156
157 const toActorSharedInboxesSet = new Set(targetUrls)
158
159 const sharedInboxesException = await buildSharedInboxesException(actorsException)
160 return Array.from(toActorSharedInboxesSet)
161 .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
162 }
163
164 async function buildSharedInboxesException (actorsException: ActorModel[]) {
165 const serverActor = await getServerActor()
166
167 return actorsException
168 .map(f => f.sharedInboxUrl || f.inboxUrl)
169 .concat([ serverActor.sharedInboxUrl ])
170 }