aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/send/shared
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-03-23 16:14:33 +0100
committerChocobozzz <me@florianbigard.com>2022-03-24 09:40:46 +0100
commita219c9100b3ce8774d454497d46be87465bf664e (patch)
treecaa869e47919a9e23cc86dcece1100e239683b8c /server/lib/activitypub/send/shared
parent7e98a7df7d04e19ba67163a86c7b876d78d76839 (diff)
downloadPeerTube-a219c9100b3ce8774d454497d46be87465bf664e.tar.gz
PeerTube-a219c9100b3ce8774d454497d46be87465bf664e.tar.zst
PeerTube-a219c9100b3ce8774d454497d46be87465bf664e.zip
Refactor AP context builder
Diffstat (limited to 'server/lib/activitypub/send/shared')
-rw-r--r--server/lib/activitypub/send/shared/send-utils.ts104
1 files changed, 76 insertions, 28 deletions
diff --git a/server/lib/activitypub/send/shared/send-utils.ts b/server/lib/activitypub/send/shared/send-utils.ts
index 9e8f12fa8..dbcde91ee 100644
--- a/server/lib/activitypub/send/shared/send-utils.ts
+++ b/server/lib/activitypub/send/shared/send-utils.ts
@@ -1,7 +1,7 @@
1import { Transaction } from 'sequelize' 1import { Transaction } from 'sequelize'
2import { ActorFollowHealthCache } from '@server/lib/actor-follow-health-cache' 2import { ActorFollowHealthCache } from '@server/lib/actor-follow-health-cache'
3import { getServerActor } from '@server/models/application/application' 3import { getServerActor } from '@server/models/application/application'
4import { Activity, ActivityAudience } from '@shared/models' 4import { Activity, ActivityAudience, ActivitypubHttpBroadcastPayload } from '@shared/models'
5import { ContextType } from '@shared/models/activitypub/context' 5import { ContextType } from '@shared/models/activitypub/context'
6import { afterCommitIfTransaction } from '../../../../helpers/database-utils' 6import { afterCommitIfTransaction } from '../../../../helpers/database-utils'
7import { logger } from '../../../../helpers/logger' 7import { logger } from '../../../../helpers/logger'
@@ -14,8 +14,8 @@ import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getOriginVideoAud
14async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: { 14async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: {
15 byActor: MActorLight 15 byActor: MActorLight
16 video: MVideoImmutable | MVideoAccountLight 16 video: MVideoImmutable | MVideoAccountLight
17 contextType: ContextType
17 transaction?: Transaction 18 transaction?: Transaction
18 contextType?: ContextType
19}) { 19}) {
20 const { byActor, video, transaction, contextType } = options 20 const { byActor, video, transaction, contextType } = options
21 21
@@ -32,15 +32,23 @@ async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAud
32 32
33 const actorsException = [ byActor ] 33 const actorsException = [ byActor ]
34 34
35 return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, transaction, actorsException, contextType) 35 return broadcastToFollowers({
36 data: activity,
37 byActor,
38 toFollowersOf: actorsInvolvedInVideo,
39 transaction,
40 actorsException,
41 contextType
42 })
36} 43}
37 44
38async function sendVideoActivityToOrigin (activityBuilder: (audience: ActivityAudience) => Activity, options: { 45async function sendVideoActivityToOrigin (activityBuilder: (audience: ActivityAudience) => Activity, options: {
39 byActor: MActorLight 46 byActor: MActorLight
40 video: MVideoImmutable | MVideoAccountLight 47 video: MVideoImmutable | MVideoAccountLight
48 contextType: ContextType
49
41 actorsInvolvedInVideo?: MActorLight[] 50 actorsInvolvedInVideo?: MActorLight[]
42 transaction?: Transaction 51 transaction?: Transaction
43 contextType?: ContextType
44}) { 52}) {
45 const { byActor, video, actorsInvolvedInVideo, transaction, contextType } = options 53 const { byActor, video, actorsInvolvedInVideo, transaction, contextType } = options
46 54
@@ -53,7 +61,12 @@ async function sendVideoActivityToOrigin (activityBuilder: (audience: ActivityAu
53 const activity = activityBuilder(audience) 61 const activity = activityBuilder(audience)
54 62
55 return afterCommitIfTransaction(transaction, () => { 63 return afterCommitIfTransaction(transaction, () => {
56 return unicastTo(activity, byActor, accountActor.getSharedInbox(), contextType) 64 return unicastTo({
65 data: activity,
66 byActor,
67 toActorUrl: accountActor.getSharedInbox(),
68 contextType
69 })
57 }) 70 })
58} 71}
59 72
@@ -100,41 +113,69 @@ async function forwardActivity (
100 113
101 logger.debug('Creating forwarding job.', { uris }) 114 logger.debug('Creating forwarding job.', { uris })
102 115
103 const payload = { 116 const payload: ActivitypubHttpBroadcastPayload = {
104 uris, 117 uris,
105 body: activity 118 body: activity,
119 contextType: null
106 } 120 }
107 return afterCommitIfTransaction(t, () => JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })) 121 return afterCommitIfTransaction(t, () => JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }))
108} 122}
109 123
110// --------------------------------------------------------------------------- 124// ---------------------------------------------------------------------------
111 125
112async function broadcastToFollowers ( 126async function broadcastToFollowers (options: {
113 data: any, 127 data: any
114 byActor: MActorId, 128 byActor: MActorId
115 toFollowersOf: MActorId[], 129 toFollowersOf: MActorId[]
116 t: Transaction, 130 transaction: Transaction
117 actorsException: MActorWithInboxes[] = [], 131 contextType: ContextType
118 contextType?: ContextType 132
119) { 133 actorsException?: MActorWithInboxes[]
120 const uris = await computeFollowerUris(toFollowersOf, actorsException, t) 134}) {
135 const { data, byActor, toFollowersOf, transaction, contextType, actorsException = [] } = options
121 136
122 return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor, contextType)) 137 const uris = await computeFollowerUris(toFollowersOf, actorsException, transaction)
138
139 return afterCommitIfTransaction(transaction, () => {
140 return broadcastTo({
141 uris,
142 data,
143 byActor,
144 contextType
145 })
146 })
123} 147}
124 148
125async function broadcastToActors ( 149async function broadcastToActors (options: {
126 data: any, 150 data: any
127 byActor: MActorId, 151 byActor: MActorId
128 toActors: MActor[], 152 toActors: MActor[]
129 t?: Transaction, 153 transaction: Transaction
130 actorsException: MActorWithInboxes[] = [], 154 contextType: ContextType
131 contextType?: ContextType 155 actorsException?: MActorWithInboxes[]
132) { 156}) {
157 const { data, byActor, toActors, transaction, contextType, actorsException = [] } = options
158
133 const uris = await computeUris(toActors, actorsException) 159 const uris = await computeUris(toActors, actorsException)
134 return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor, contextType)) 160
161 return afterCommitIfTransaction(transaction, () => {
162 return broadcastTo({
163 uris,
164 data,
165 byActor,
166 contextType
167 })
168 })
135} 169}
136 170
137function broadcastTo (uris: string[], data: any, byActor: MActorId, contextType?: ContextType) { 171function broadcastTo (options: {
172 uris: string[]
173 data: any
174 byActor: MActorId
175 contextType: ContextType
176}) {
177 const { uris, data, byActor, contextType } = options
178
138 if (uris.length === 0) return undefined 179 if (uris.length === 0) return undefined
139 180
140 const broadcastUris: string[] = [] 181 const broadcastUris: string[] = []
@@ -174,7 +215,14 @@ function broadcastTo (uris: string[], data: any, byActor: MActorId, contextType?
174 } 215 }
175} 216}
176 217
177function unicastTo (data: any, byActor: MActorId, toActorUrl: string, contextType?: ContextType) { 218function unicastTo (options: {
219 data: any
220 byActor: MActorId
221 toActorUrl: string
222 contextType: ContextType
223}) {
224 const { data, byActor, toActorUrl, contextType } = options
225
178 logger.debug('Creating unicast job.', { uri: toActorUrl }) 226 logger.debug('Creating unicast job.', { uri: toActorUrl })
179 227
180 const payload = { 228 const payload = {