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