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