aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/send/utils.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-08-09 08:17:16 +0200
committerChocobozzz <me@florianbigard.com>2019-08-09 08:17:16 +0200
commit5224c394b3bbac6ec1543e41fa0ec6db436e84fa (patch)
tree36eaecfe095547aca903a8a43fb6e0b0b01899a9 /server/lib/activitypub/send/utils.ts
parent511765c9f86fb07d5d856decd9dbf0ec2092f4fe (diff)
downloadPeerTube-5224c394b3bbac6ec1543e41fa0ec6db436e84fa.tar.gz
PeerTube-5224c394b3bbac6ec1543e41fa0ec6db436e84fa.tar.zst
PeerTube-5224c394b3bbac6ec1543e41fa0ec6db436e84fa.zip
Stronger actor association typing in AP functions
Diffstat (limited to 'server/lib/activitypub/send/utils.ts')
-rw-r--r--server/lib/activitypub/send/utils.ts45
1 files changed, 24 insertions, 21 deletions
diff --git a/server/lib/activitypub/send/utils.ts b/server/lib/activitypub/send/utils.ts
index 1faae1d84..4f69afb00 100644
--- a/server/lib/activitypub/send/utils.ts
+++ b/server/lib/activitypub/send/utils.ts
@@ -8,21 +8,24 @@ import { VideoModel } from '../../../models/video/video'
8import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience' 8import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience'
9import { getServerActor } from '../../../helpers/utils' 9import { getServerActor } from '../../../helpers/utils'
10import { afterCommitIfTransaction } from '../../../helpers/database-utils' 10import { afterCommitIfTransaction } from '../../../helpers/database-utils'
11import { ActorFollowerException, ActorModelId, ActorModelOnly } from '../../../typings/models'
11 12
12async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: { 13async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: {
13 byActor: ActorModel, 14 byActor: ActorModelOnly,
14 video: VideoModel, 15 video: VideoModel,
15 transaction?: Transaction 16 transaction?: Transaction
16}) { 17}) {
17 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(options.video, options.transaction) 18 const { byActor, video, transaction } = options
19
20 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, transaction)
18 21
19 // Send to origin 22 // Send to origin
20 if (options.video.isOwned() === false) { 23 if (video.isOwned() === false) {
21 const audience = getRemoteVideoAudience(options.video, actorsInvolvedInVideo) 24 const audience = getRemoteVideoAudience(video, actorsInvolvedInVideo)
22 const activity = activityBuilder(audience) 25 const activity = activityBuilder(audience)
23 26
24 return afterCommitIfTransaction(options.transaction, () => { 27 return afterCommitIfTransaction(transaction, () => {
25 return unicastTo(activity, options.byActor, options.video.VideoChannel.Account.Actor.sharedInboxUrl) 28 return unicastTo(activity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
26 }) 29 })
27 } 30 }
28 31
@@ -30,15 +33,15 @@ async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAud
30 const audience = getAudienceFromFollowersOf(actorsInvolvedInVideo) 33 const audience = getAudienceFromFollowersOf(actorsInvolvedInVideo)
31 const activity = activityBuilder(audience) 34 const activity = activityBuilder(audience)
32 35
33 const actorsException = [ options.byActor ] 36 const actorsException = [ byActor ]
34 37
35 return broadcastToFollowers(activity, options.byActor, actorsInvolvedInVideo, options.transaction, actorsException) 38 return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, transaction, actorsException)
36} 39}
37 40
38async function forwardVideoRelatedActivity ( 41async function forwardVideoRelatedActivity (
39 activity: Activity, 42 activity: Activity,
40 t: Transaction, 43 t: Transaction,
41 followersException: ActorModel[] = [], 44 followersException: ActorFollowerException[] = [],
42 video: VideoModel 45 video: VideoModel
43) { 46) {
44 // Mastodon does not add our announces in audience, so we forward to them manually 47 // Mastodon does not add our announces in audience, so we forward to them manually
@@ -51,7 +54,7 @@ async function forwardVideoRelatedActivity (
51async function forwardActivity ( 54async function forwardActivity (
52 activity: Activity, 55 activity: Activity,
53 t: Transaction, 56 t: Transaction,
54 followersException: ActorModel[] = [], 57 followersException: ActorFollowerException[] = [],
55 additionalFollowerUrls: string[] = [] 58 additionalFollowerUrls: string[] = []
56) { 59) {
57 logger.info('Forwarding activity %s.', activity.id) 60 logger.info('Forwarding activity %s.', activity.id)
@@ -85,10 +88,10 @@ async function forwardActivity (
85 88
86async function broadcastToFollowers ( 89async function broadcastToFollowers (
87 data: any, 90 data: any,
88 byActor: ActorModel, 91 byActor: ActorModelId,
89 toFollowersOf: ActorModel[], 92 toFollowersOf: ActorModelId[],
90 t: Transaction, 93 t: Transaction,
91 actorsException: ActorModel[] = [] 94 actorsException: ActorFollowerException[] = []
92) { 95) {
93 const uris = await computeFollowerUris(toFollowersOf, actorsException, t) 96 const uris = await computeFollowerUris(toFollowersOf, actorsException, t)
94 97
@@ -97,16 +100,16 @@ async function broadcastToFollowers (
97 100
98async function broadcastToActors ( 101async function broadcastToActors (
99 data: any, 102 data: any,
100 byActor: ActorModel, 103 byActor: ActorModelId,
101 toActors: ActorModel[], 104 toActors: ActorModelOnly[],
102 t?: Transaction, 105 t?: Transaction,
103 actorsException: ActorModel[] = [] 106 actorsException: ActorFollowerException[] = []
104) { 107) {
105 const uris = await computeUris(toActors, actorsException) 108 const uris = await computeUris(toActors, actorsException)
106 return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor)) 109 return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor))
107} 110}
108 111
109function broadcastTo (uris: string[], data: any, byActor: ActorModel) { 112function broadcastTo (uris: string[], data: any, byActor: ActorModelId) {
110 if (uris.length === 0) return undefined 113 if (uris.length === 0) return undefined
111 114
112 logger.debug('Creating broadcast job.', { uris }) 115 logger.debug('Creating broadcast job.', { uris })
@@ -120,7 +123,7 @@ function broadcastTo (uris: string[], data: any, byActor: ActorModel) {
120 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }) 123 return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
121} 124}
122 125
123function unicastTo (data: any, byActor: ActorModel, toActorUrl: string) { 126function unicastTo (data: any, byActor: ActorModelId, toActorUrl: string) {
124 logger.debug('Creating unicast job.', { uri: toActorUrl }) 127 logger.debug('Creating unicast job.', { uri: toActorUrl })
125 128
126 const payload = { 129 const payload = {
@@ -145,7 +148,7 @@ export {
145 148
146// --------------------------------------------------------------------------- 149// ---------------------------------------------------------------------------
147 150
148async function computeFollowerUris (toFollowersOf: ActorModel[], actorsException: ActorModel[], t: Transaction) { 151async function computeFollowerUris (toFollowersOf: ActorModelId[], actorsException: ActorFollowerException[], t: Transaction) {
149 const toActorFollowerIds = toFollowersOf.map(a => a.id) 152 const toActorFollowerIds = toFollowersOf.map(a => a.id)
150 153
151 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t) 154 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
@@ -154,7 +157,7 @@ async function computeFollowerUris (toFollowersOf: ActorModel[], actorsException
154 return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1) 157 return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
155} 158}
156 159
157async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) { 160async function computeUris (toActors: ActorModelOnly[], actorsException: ActorFollowerException[] = []) {
158 const serverActor = await getServerActor() 161 const serverActor = await getServerActor()
159 const targetUrls = toActors 162 const targetUrls = toActors
160 .filter(a => a.id !== serverActor.id) // Don't send to ourselves 163 .filter(a => a.id !== serverActor.id) // Don't send to ourselves
@@ -167,7 +170,7 @@ async function computeUris (toActors: ActorModel[], actorsException: ActorModel[
167 .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1) 170 .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
168} 171}
169 172
170async function buildSharedInboxesException (actorsException: ActorModel[]) { 173async function buildSharedInboxesException (actorsException: ActorFollowerException[]) {
171 const serverActor = await getServerActor() 174 const serverActor = await getServerActor()
172 175
173 return actorsException 176 return actorsException