aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/send
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
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')
-rw-r--r--server/lib/activitypub/send/send-accept.ts8
-rw-r--r--server/lib/activitypub/send/send-announce.ts17
-rw-r--r--server/lib/activitypub/send/send-follow.ts4
-rw-r--r--server/lib/activitypub/send/send-reject.ts3
-rw-r--r--server/lib/activitypub/send/utils.ts45
5 files changed, 43 insertions, 34 deletions
diff --git a/server/lib/activitypub/send/send-accept.ts b/server/lib/activitypub/send/send-accept.ts
index 388a9ed23..813c42e15 100644
--- a/server/lib/activitypub/send/send-accept.ts
+++ b/server/lib/activitypub/send/send-accept.ts
@@ -1,12 +1,12 @@
1import { ActivityAccept, ActivityFollow } from '../../../../shared/models/activitypub' 1import { ActivityAccept, ActivityFollow } from '../../../../shared/models/activitypub'
2import { ActorModel } from '../../../models/activitypub/actor'
3import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
4import { getActorFollowAcceptActivityPubUrl, getActorFollowActivityPubUrl } from '../url' 2import { getActorFollowAcceptActivityPubUrl, getActorFollowActivityPubUrl } from '../url'
5import { unicastTo } from './utils' 3import { unicastTo } from './utils'
6import { buildFollowActivity } from './send-follow' 4import { buildFollowActivity } from './send-follow'
7import { logger } from '../../../helpers/logger' 5import { logger } from '../../../helpers/logger'
6import { ActorFollowModelLight } from '../../../typings/models/actor-follow'
7import { ActorModelOnly } from '../../../typings/models'
8 8
9async function sendAccept (actorFollow: ActorFollowModel) { 9async function sendAccept (actorFollow: ActorFollowModelLight) {
10 const follower = actorFollow.ActorFollower 10 const follower = actorFollow.ActorFollower
11 const me = actorFollow.ActorFollowing 11 const me = actorFollow.ActorFollowing
12 12
@@ -34,7 +34,7 @@ export {
34 34
35// --------------------------------------------------------------------------- 35// ---------------------------------------------------------------------------
36 36
37function buildAcceptActivity (url: string, byActor: ActorModel, followActivityData: ActivityFollow): ActivityAccept { 37function buildAcceptActivity (url: string, byActor: ActorModelOnly, followActivityData: ActivityFollow): ActivityAccept {
38 return { 38 return {
39 type: 'Accept', 39 type: 'Accept',
40 id: url, 40 id: url,
diff --git a/server/lib/activitypub/send/send-announce.ts b/server/lib/activitypub/send/send-announce.ts
index cd0cab7ee..7fe4ca180 100644
--- a/server/lib/activitypub/send/send-announce.ts
+++ b/server/lib/activitypub/send/send-announce.ts
@@ -1,13 +1,18 @@
1import { Transaction } from 'sequelize' 1import { Transaction } from 'sequelize'
2import { ActivityAnnounce, ActivityAudience } from '../../../../shared/models/activitypub' 2import { ActivityAnnounce, ActivityAudience } from '../../../../shared/models/activitypub'
3import { ActorModel } from '../../../models/activitypub/actor'
4import { VideoModel } from '../../../models/video/video' 3import { VideoModel } from '../../../models/video/video'
5import { VideoShareModel } from '../../../models/video/video-share'
6import { broadcastToFollowers } from './utils' 4import { broadcastToFollowers } from './utils'
7import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf } from '../audience' 5import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf } from '../audience'
8import { logger } from '../../../helpers/logger' 6import { logger } from '../../../helpers/logger'
9 7import { ActorModelOnly } from '../../../typings/models'
10async function buildAnnounceWithVideoAudience (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) { 8import { VideoShareModelOnly } from '../../../typings/models/video-share'
9
10async function buildAnnounceWithVideoAudience (
11 byActor: ActorModelOnly,
12 videoShare: VideoShareModelOnly,
13 video: VideoModel,
14 t: Transaction
15) {
11 const announcedObject = video.url 16 const announcedObject = video.url
12 17
13 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t) 18 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
@@ -18,7 +23,7 @@ async function buildAnnounceWithVideoAudience (byActor: ActorModel, videoShare:
18 return { activity, actorsInvolvedInVideo } 23 return { activity, actorsInvolvedInVideo }
19} 24}
20 25
21async function sendVideoAnnounce (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) { 26async function sendVideoAnnounce (byActor: ActorModelOnly, videoShare: VideoShareModelOnly, video: VideoModel, t: Transaction) {
22 const { activity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t) 27 const { activity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t)
23 28
24 logger.info('Creating job to send announce %s.', videoShare.url) 29 logger.info('Creating job to send announce %s.', videoShare.url)
@@ -27,7 +32,7 @@ async function sendVideoAnnounce (byActor: ActorModel, videoShare: VideoShareMod
27 return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, t, followersException) 32 return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, t, followersException)
28} 33}
29 34
30function buildAnnounceActivity (url: string, byActor: ActorModel, object: string, audience?: ActivityAudience): ActivityAnnounce { 35function buildAnnounceActivity (url: string, byActor: ActorModelOnly, object: string, audience?: ActivityAudience): ActivityAnnounce {
31 if (!audience) audience = getAudience(byActor) 36 if (!audience) audience = getAudience(byActor)
32 37
33 return audiencify({ 38 return audiencify({
diff --git a/server/lib/activitypub/send/send-follow.ts b/server/lib/activitypub/send/send-follow.ts
index c6e7fe83d..a59ed50cf 100644
--- a/server/lib/activitypub/send/send-follow.ts
+++ b/server/lib/activitypub/send/send-follow.ts
@@ -1,10 +1,10 @@
1import { ActivityFollow } from '../../../../shared/models/activitypub' 1import { ActivityFollow } from '../../../../shared/models/activitypub'
2import { ActorModel } from '../../../models/activitypub/actor'
3import { ActorFollowModel } from '../../../models/activitypub/actor-follow' 2import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
4import { getActorFollowActivityPubUrl } from '../url' 3import { getActorFollowActivityPubUrl } from '../url'
5import { unicastTo } from './utils' 4import { unicastTo } from './utils'
6import { logger } from '../../../helpers/logger' 5import { logger } from '../../../helpers/logger'
7import { Transaction } from 'sequelize' 6import { Transaction } from 'sequelize'
7import { ActorModelOnly } from '../../../typings/models'
8 8
9function sendFollow (actorFollow: ActorFollowModel, t: Transaction) { 9function sendFollow (actorFollow: ActorFollowModel, t: Transaction) {
10 const me = actorFollow.ActorFollower 10 const me = actorFollow.ActorFollower
@@ -21,7 +21,7 @@ function sendFollow (actorFollow: ActorFollowModel, t: Transaction) {
21 t.afterCommit(() => unicastTo(data, me, following.inboxUrl)) 21 t.afterCommit(() => unicastTo(data, me, following.inboxUrl))
22} 22}
23 23
24function buildFollowActivity (url: string, byActor: ActorModel, targetActor: ActorModel): ActivityFollow { 24function buildFollowActivity (url: string, byActor: ActorModelOnly, targetActor: ActorModelOnly): ActivityFollow {
25 return { 25 return {
26 type: 'Follow', 26 type: 'Follow',
27 id: url, 27 id: url,
diff --git a/server/lib/activitypub/send/send-reject.ts b/server/lib/activitypub/send/send-reject.ts
index bac7ff556..63110b433 100644
--- a/server/lib/activitypub/send/send-reject.ts
+++ b/server/lib/activitypub/send/send-reject.ts
@@ -4,8 +4,9 @@ import { getActorFollowActivityPubUrl, getActorFollowRejectActivityPubUrl } from
4import { unicastTo } from './utils' 4import { unicastTo } from './utils'
5import { buildFollowActivity } from './send-follow' 5import { buildFollowActivity } from './send-follow'
6import { logger } from '../../../helpers/logger' 6import { logger } from '../../../helpers/logger'
7import { SignatureActorModel } from '../../../typings/models'
7 8
8async function sendReject (follower: ActorModel, following: ActorModel) { 9async function sendReject (follower: SignatureActorModel, following: ActorModel) {
9 if (!follower.serverId) { // This should never happen 10 if (!follower.serverId) { // This should never happen
10 logger.warn('Do not sending reject to local follower.') 11 logger.warn('Do not sending reject to local follower.')
11 return 12 return
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