]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/utils.ts
Correctly forward video related activities
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / utils.ts
1 import { Transaction } from 'sequelize'
2 import { Activity } 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 } 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 }
22
23 async function forwardActivity (
24 activity: Activity,
25 t: Transaction,
26 followersException: ActorModel[] = [],
27 additionalFollowerUrls: string[] = []
28 ) {
29 const to = activity.to || []
30 const cc = activity.cc || []
31
32 const followersUrls = additionalFollowerUrls
33 for (const dest of to.concat(cc)) {
34 if (dest.endsWith('/followers')) {
35 followersUrls.push(dest)
36 }
37 }
38
39 const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
40 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
41
42 if (uris.length === 0) {
43 logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
44 return undefined
45 }
46
47 logger.debug('Creating forwarding job.', { uris })
48
49 const payload = {
50 uris,
51 body: activity
52 }
53 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
54 }
55
56 async function broadcastToFollowers (
57 data: any,
58 byActor: ActorModel,
59 toActorFollowers: ActorModel[],
60 t: Transaction,
61 actorsException: ActorModel[] = []
62 ) {
63 const uris = await computeFollowerUris(toActorFollowers, actorsException, t)
64 return broadcastTo(uris, data, byActor)
65 }
66
67 async function broadcastToActors (
68 data: any,
69 byActor: ActorModel,
70 toActors: ActorModel[],
71 actorsException: ActorModel[] = []
72 ) {
73 const uris = await computeUris(toActors, actorsException)
74 return broadcastTo(uris, data, byActor)
75 }
76
77 async function broadcastTo (uris: string[], data: any, byActor: ActorModel) {
78 if (uris.length === 0) return undefined
79
80 logger.debug('Creating broadcast job.', { uris })
81
82 const payload = {
83 uris,
84 signatureActorId: byActor.id,
85 body: data
86 }
87
88 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
89 }
90
91 async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string) {
92 logger.debug('Creating unicast job.', { uri: toActorUrl })
93
94 const payload = {
95 uri: toActorUrl,
96 signatureActorId: byActor.id,
97 body: data
98 }
99
100 return JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
101 }
102
103 // ---------------------------------------------------------------------------
104
105 export {
106 broadcastToFollowers,
107 unicastTo,
108 forwardActivity,
109 broadcastToActors,
110 forwardVideoRelatedActivity
111 }
112
113 // ---------------------------------------------------------------------------
114
115 async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) {
116 const toActorFollowerIds = toActorFollower.map(a => a.id)
117
118 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
119 const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
120 return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
121 }
122
123 async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) {
124 const toActorSharedInboxesSet = new Set(toActors.map(a => a.sharedInboxUrl || a.inboxUrl))
125
126 const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
127 return Array.from(toActorSharedInboxesSet)
128 .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
129 }