Fix lint
[oweals/peertube.git] / server / lib / activitypub / send / misc.ts
1 import { Transaction } from 'sequelize'
2 import { Activity, ActivityAudience } from '../../../../shared/models/activitypub'
3 import { logger } from '../../../helpers/logger'
4 import { ACTIVITY_PUB } from '../../../initializers'
5 import { ActorModel } from '../../../models/activitypub/actor'
6 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
7 import { VideoModel } from '../../../models/video/video'
8 import { VideoCommentModel } from '../../../models/video/video-comment'
9 import { VideoShareModel } from '../../../models/video/video-share'
10 import { activitypubHttpJobScheduler, ActivityPubHttpPayload } from '../../jobs/activitypub-http-job-scheduler'
11
12 async 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 jobPayload: ActivityPubHttpPayload = {
39     uris,
40     body: activity
41   }
42
43   return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
44 }
45
46 async function broadcastToFollowers (
47   data: any,
48   byActor: ActorModel,
49   toActorFollowers: ActorModel[],
50   t: Transaction,
51   actorsException: ActorModel[] = []
52 ) {
53   const uris = await computeFollowerUris(toActorFollowers, actorsException, t)
54   return broadcastTo(uris, data, byActor, t)
55 }
56
57 async 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
68 async function broadcastTo (uris: string[], data: any, byActor: ActorModel, t: Transaction) {
69   if (uris.length === 0) return undefined
70
71   logger.debug('Creating broadcast job.', { uris })
72
73   const jobPayload: ActivityPubHttpPayload = {
74     uris,
75     signatureActorId: byActor.id,
76     body: data
77   }
78
79   return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
80 }
81
82 async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string, t: Transaction) {
83   logger.debug('Creating unicast job.', { uri: toActorUrl })
84
85   const jobPayload: ActivityPubHttpPayload = {
86     uris: [ toActorUrl ],
87     signatureActorId: byActor.id,
88     body: data
89   }
90
91   return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload)
92 }
93
94 function getOriginVideoAudience (video: VideoModel, actorsInvolvedInVideo: ActorModel[]) {
95   return {
96     to: [ video.VideoChannel.Account.Actor.url ],
97     cc: actorsInvolvedInVideo.map(a => a.followersUrl)
98   }
99 }
100
101 function 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
129 function getObjectFollowersAudience (actorsInvolvedInObject: ActorModel[]) {
130   return {
131     to: actorsInvolvedInObject.map(a => a.followersUrl),
132     cc: []
133   }
134 }
135
136 async function getActorsInvolvedInVideo (video: VideoModel, t: Transaction) {
137   const actors = await VideoShareModel.loadActorsByShare(video.id, t)
138   actors.push(video.VideoChannel.Account.Actor)
139
140   return actors
141 }
142
143 async function getAudience (actorSender: ActorModel, t: Transaction, isPublic = true) {
144   const followerInboxUrls = await actorSender.getFollowerSharedInboxUrls(t)
145
146   return buildAudience(followerInboxUrls, isPublic)
147 }
148
149 function buildAudience (followerInboxUrls: string[], isPublic = true) {
150   // Thanks Mastodon: https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/tag_manager.rb#L47
151   let to = []
152   let cc = []
153
154   if (isPublic) {
155     to = [ ACTIVITY_PUB.PUBLIC ]
156     cc = followerInboxUrls
157   } else { // Unlisted
158     to = followerInboxUrls
159     cc = [ ACTIVITY_PUB.PUBLIC ]
160   }
161
162   return { to, cc }
163 }
164
165 function audiencify (object: any, audience: ActivityAudience) {
166   return Object.assign(object, audience)
167 }
168
169 async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) {
170   const toActorFollowerIds = toActorFollower.map(a => a.id)
171
172   const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
173   const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
174   return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
175 }
176
177 async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) {
178   const toActorSharedInboxesSet = new Set(toActors.map(a => a.sharedInboxUrl || a.inboxUrl))
179
180   const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
181   return Array.from(toActorSharedInboxesSet)
182     .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
183 }
184
185 // ---------------------------------------------------------------------------
186
187 export {
188   broadcastToFollowers,
189   unicastTo,
190   buildAudience,
191   getAudience,
192   getOriginVideoAudience,
193   getActorsInvolvedInVideo,
194   getObjectFollowersAudience,
195   forwardActivity,
196   audiencify,
197   getOriginVideoCommentAudience,
198   computeUris,
199   broadcastToActors
200 }