]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/misc.ts
Add maximum to actor follow scores
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / misc.ts
CommitLineData
54141398 1import { Transaction } from 'sequelize'
e12a0092 2import { Activity, ActivityAudience } from '../../../../shared/models/activitypub'
da854ddd 3import { logger } from '../../../helpers/logger'
3fd3ab2d 4import { ACTIVITY_PUB } from '../../../initializers'
50d6de9c
C
5import { ActorModel } from '../../../models/activitypub/actor'
6import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
3fd3ab2d 7import { VideoModel } from '../../../models/video/video'
d7e70384 8import { VideoCommentModel } from '../../../models/video/video-comment'
3fd3ab2d
C
9import { VideoShareModel } from '../../../models/video/video-share'
10import { activitypubHttpJobScheduler, ActivityPubHttpPayload } from '../../jobs/activitypub-http-job-scheduler'
63c93323
C
11
12async function forwardActivity (
13 activity: Activity,
14 t: Transaction,
93ef8a9d
C
15 followersException: ActorModel[] = [],
16 additionalFollowerUrls: string[] = []
63c93323
C
17) {
18 const to = activity.to || []
19 const cc = activity.cc || []
20
93ef8a9d 21 const followersUrls = additionalFollowerUrls
63c93323
C
22 for (const dest of to.concat(cc)) {
23 if (dest.endsWith('/followers')) {
24 followersUrls.push(dest)
25 }
26 }
27
50d6de9c
C
28 const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
29 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
63c93323
C
30
31 if (uris.length === 0) {
50d6de9c 32 logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
df1966c9 33 return undefined
63c93323
C
34 }
35
36 logger.debug('Creating forwarding job.', { uris })
37
38 const jobPayload: ActivityPubHttpPayload = {
39 uris,
40 body: activity
41 }
42
43 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
44}
54141398 45
40ff5707
C
46async function broadcastToFollowers (
47 data: any,
50d6de9c
C
48 byActor: ActorModel,
49 toActorFollowers: ActorModel[],
40ff5707 50 t: Transaction,
93ef8a9d 51 actorsException: ActorModel[] = []
40ff5707 52) {
93ef8a9d
C
53 const uris = await computeFollowerUris(toActorFollowers, actorsException, t)
54 return broadcastTo(uris, data, byActor, t)
55}
56
57async function broadcastToActors (
58 data: any,
59 byActor: ActorModel,
60 toActors: ActorModel[],
61 t: Transaction,
62 actorsException: ActorModel[] = []
63) {
64 const uris = await computeUris(toActors, actorsException)
65 return broadcastTo(uris, data, byActor, t)
66}
67
68async function broadcastTo (uris: string[], data: any, byActor: ActorModel, t: Transaction) {
69 if (uris.length === 0) return undefined
54141398 70
63c93323 71 logger.debug('Creating broadcast job.', { uris })
40ff5707 72
63c93323 73 const jobPayload: ActivityPubHttpPayload = {
40ff5707 74 uris,
50d6de9c 75 signatureActorId: byActor.id,
54141398
C
76 body: data
77 }
78
79 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
80}
81
50d6de9c
C
82async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string, t: Transaction) {
83 logger.debug('Creating unicast job.', { uri: toActorUrl })
63c93323
C
84
85 const jobPayload: ActivityPubHttpPayload = {
50d6de9c
C
86 uris: [ toActorUrl ],
87 signatureActorId: byActor.id,
54141398
C
88 body: data
89 }
90
91 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload)
92}
93
50d6de9c 94function getOriginVideoAudience (video: VideoModel, actorsInvolvedInVideo: ActorModel[]) {
0032ebe9 95 return {
50d6de9c
C
96 to: [ video.VideoChannel.Account.Actor.url ],
97 cc: actorsInvolvedInVideo.map(a => a.followersUrl)
0032ebe9
C
98 }
99}
100
d7e70384
C
101function getOriginVideoCommentAudience (
102 videoComment: VideoCommentModel,
103 threadParentComments: VideoCommentModel[],
104 actorsInvolvedInVideo: ActorModel[],
105 isOrigin = false
106) {
107 const to = [ ACTIVITY_PUB.PUBLIC ]
108 const cc = [ ]
109
110 // Owner of the video we comment
111 if (isOrigin === false) {
112 cc.push(videoComment.Video.VideoChannel.Account.Actor.url)
113 }
114
115 // Followers of the poster
116 cc.push(videoComment.Account.Actor.followersUrl)
117
118 // Send to actors we reply to
119 for (const parentComment of threadParentComments) {
120 cc.push(parentComment.Account.Actor.url)
121 }
122
123 return {
124 to,
125 cc: cc.concat(actorsInvolvedInVideo.map(a => a.followersUrl))
126 }
127}
128
50d6de9c 129function getObjectFollowersAudience (actorsInvolvedInObject: ActorModel[]) {
4e50b6a1 130 return {
50d6de9c 131 to: actorsInvolvedInObject.map(a => a.followersUrl),
0032ebe9
C
132 cc: []
133 }
134}
135
50d6de9c 136async function getActorsInvolvedInVideo (video: VideoModel, t: Transaction) {
d7e70384
C
137 const actors = await VideoShareModel.loadActorsByShare(video.id, t)
138 actors.push(video.VideoChannel.Account.Actor)
4e50b6a1 139
d7e70384 140 return actors
4e50b6a1
C
141}
142
50d6de9c
C
143async function getAudience (actorSender: ActorModel, t: Transaction, isPublic = true) {
144 const followerInboxUrls = await actorSender.getFollowerSharedInboxUrls(t)
54141398
C
145
146 // Thanks Mastodon: https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/tag_manager.rb#L47
147 let to = []
148 let cc = []
149
150 if (isPublic) {
151 to = [ ACTIVITY_PUB.PUBLIC ]
152 cc = followerInboxUrls
153 } else { // Unlisted
154 to = followerInboxUrls
155 cc = [ ACTIVITY_PUB.PUBLIC ]
156 }
157
158 return { to, cc }
159}
160
e12a0092
C
161function audiencify (object: any, audience: ActivityAudience) {
162 return Object.assign(object, audience)
163}
164
93ef8a9d 165async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) {
50d6de9c 166 const toActorFollowerIds = toActorFollower.map(a => a.id)
63c93323 167
50d6de9c 168 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
93ef8a9d
C
169 const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl)
170 return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
171}
172
173async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) {
174 const toActorSharedInboxesSet = new Set(toActors.map(a => a.sharedInboxUrl))
175
176 const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl)
177 return Array.from(toActorSharedInboxesSet)
178 .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
63c93323
C
179}
180
54141398
C
181// ---------------------------------------------------------------------------
182
183export {
184 broadcastToFollowers,
185 unicastTo,
0032ebe9
C
186 getAudience,
187 getOriginVideoAudience,
50d6de9c 188 getActorsInvolvedInVideo,
4e50b6a1 189 getObjectFollowersAudience,
e12a0092 190 forwardActivity,
d7e70384 191 audiencify,
93ef8a9d
C
192 getOriginVideoCommentAudience,
193 computeUris,
194 broadcastToActors
54141398 195}