diff options
author | Chocobozzz <me@florianbigard.com> | 2019-08-09 08:17:16 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-08-09 08:17:16 +0200 |
commit | 5224c394b3bbac6ec1543e41fa0ec6db436e84fa (patch) | |
tree | 36eaecfe095547aca903a8a43fb6e0b0b01899a9 /server/lib/activitypub/send | |
parent | 511765c9f86fb07d5d856decd9dbf0ec2092f4fe (diff) | |
download | PeerTube-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.ts | 8 | ||||
-rw-r--r-- | server/lib/activitypub/send/send-announce.ts | 17 | ||||
-rw-r--r-- | server/lib/activitypub/send/send-follow.ts | 4 | ||||
-rw-r--r-- | server/lib/activitypub/send/send-reject.ts | 3 | ||||
-rw-r--r-- | server/lib/activitypub/send/utils.ts | 45 |
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 @@ | |||
1 | import { ActivityAccept, ActivityFollow } from '../../../../shared/models/activitypub' | 1 | import { ActivityAccept, ActivityFollow } from '../../../../shared/models/activitypub' |
2 | import { ActorModel } from '../../../models/activitypub/actor' | ||
3 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | ||
4 | import { getActorFollowAcceptActivityPubUrl, getActorFollowActivityPubUrl } from '../url' | 2 | import { getActorFollowAcceptActivityPubUrl, getActorFollowActivityPubUrl } from '../url' |
5 | import { unicastTo } from './utils' | 3 | import { unicastTo } from './utils' |
6 | import { buildFollowActivity } from './send-follow' | 4 | import { buildFollowActivity } from './send-follow' |
7 | import { logger } from '../../../helpers/logger' | 5 | import { logger } from '../../../helpers/logger' |
6 | import { ActorFollowModelLight } from '../../../typings/models/actor-follow' | ||
7 | import { ActorModelOnly } from '../../../typings/models' | ||
8 | 8 | ||
9 | async function sendAccept (actorFollow: ActorFollowModel) { | 9 | async 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 | ||
37 | function buildAcceptActivity (url: string, byActor: ActorModel, followActivityData: ActivityFollow): ActivityAccept { | 37 | function 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 @@ | |||
1 | import { Transaction } from 'sequelize' | 1 | import { Transaction } from 'sequelize' |
2 | import { ActivityAnnounce, ActivityAudience } from '../../../../shared/models/activitypub' | 2 | import { ActivityAnnounce, ActivityAudience } from '../../../../shared/models/activitypub' |
3 | import { ActorModel } from '../../../models/activitypub/actor' | ||
4 | import { VideoModel } from '../../../models/video/video' | 3 | import { VideoModel } from '../../../models/video/video' |
5 | import { VideoShareModel } from '../../../models/video/video-share' | ||
6 | import { broadcastToFollowers } from './utils' | 4 | import { broadcastToFollowers } from './utils' |
7 | import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf } from '../audience' | 5 | import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf } from '../audience' |
8 | import { logger } from '../../../helpers/logger' | 6 | import { logger } from '../../../helpers/logger' |
9 | 7 | import { ActorModelOnly } from '../../../typings/models' | |
10 | async function buildAnnounceWithVideoAudience (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) { | 8 | import { VideoShareModelOnly } from '../../../typings/models/video-share' |
9 | |||
10 | async 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 | ||
21 | async function sendVideoAnnounce (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) { | 26 | async 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 | ||
30 | function buildAnnounceActivity (url: string, byActor: ActorModel, object: string, audience?: ActivityAudience): ActivityAnnounce { | 35 | function 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 @@ | |||
1 | import { ActivityFollow } from '../../../../shared/models/activitypub' | 1 | import { ActivityFollow } from '../../../../shared/models/activitypub' |
2 | import { ActorModel } from '../../../models/activitypub/actor' | ||
3 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | 2 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' |
4 | import { getActorFollowActivityPubUrl } from '../url' | 3 | import { getActorFollowActivityPubUrl } from '../url' |
5 | import { unicastTo } from './utils' | 4 | import { unicastTo } from './utils' |
6 | import { logger } from '../../../helpers/logger' | 5 | import { logger } from '../../../helpers/logger' |
7 | import { Transaction } from 'sequelize' | 6 | import { Transaction } from 'sequelize' |
7 | import { ActorModelOnly } from '../../../typings/models' | ||
8 | 8 | ||
9 | function sendFollow (actorFollow: ActorFollowModel, t: Transaction) { | 9 | function 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 | ||
24 | function buildFollowActivity (url: string, byActor: ActorModel, targetActor: ActorModel): ActivityFollow { | 24 | function 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 | |||
4 | import { unicastTo } from './utils' | 4 | import { unicastTo } from './utils' |
5 | import { buildFollowActivity } from './send-follow' | 5 | import { buildFollowActivity } from './send-follow' |
6 | import { logger } from '../../../helpers/logger' | 6 | import { logger } from '../../../helpers/logger' |
7 | import { SignatureActorModel } from '../../../typings/models' | ||
7 | 8 | ||
8 | async function sendReject (follower: ActorModel, following: ActorModel) { | 9 | async 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' | |||
8 | import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience' | 8 | import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience' |
9 | import { getServerActor } from '../../../helpers/utils' | 9 | import { getServerActor } from '../../../helpers/utils' |
10 | import { afterCommitIfTransaction } from '../../../helpers/database-utils' | 10 | import { afterCommitIfTransaction } from '../../../helpers/database-utils' |
11 | import { ActorFollowerException, ActorModelId, ActorModelOnly } from '../../../typings/models' | ||
11 | 12 | ||
12 | async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: { | 13 | async 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 | ||
38 | async function forwardVideoRelatedActivity ( | 41 | async 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 ( | |||
51 | async function forwardActivity ( | 54 | async 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 | ||
86 | async function broadcastToFollowers ( | 89 | async 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 | ||
98 | async function broadcastToActors ( | 101 | async 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 | ||
109 | function broadcastTo (uris: string[], data: any, byActor: ActorModel) { | 112 | function 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 | ||
123 | function unicastTo (data: any, byActor: ActorModel, toActorUrl: string) { | 126 | function 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 | ||
148 | async function computeFollowerUris (toFollowersOf: ActorModel[], actorsException: ActorModel[], t: Transaction) { | 151 | async 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 | ||
157 | async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) { | 160 | async 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 | ||
170 | async function buildSharedInboxesException (actorsException: ActorModel[]) { | 173 | async function buildSharedInboxesException (actorsException: ActorFollowerException[]) { |
171 | const serverActor = await getServerActor() | 174 | const serverActor = await getServerActor() |
172 | 175 | ||
173 | return actorsException | 176 | return actorsException |