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