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