Detect posting request in our own inbox
[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 { JobQueue } from '../../job-queue'
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 payload = {
39     uris,
40     body: activity
41   }
42   return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
43 }
44
45 async 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
56 async 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
66 async 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
80 async 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
92 function getOriginVideoAudience (video: VideoModel, actorsInvolvedInVideo: ActorModel[]) {
93   return {
94     to: [ video.VideoChannel.Account.Actor.url ],
95     cc: actorsInvolvedInVideo.map(a => a.followersUrl)
96   }
97 }
98
99 function getOriginVideoCommentAudience (
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
127 function getObjectFollowersAudience (actorsInvolvedInObject: ActorModel[]) {
128   return {
129     to: actorsInvolvedInObject.map(a => a.followersUrl),
130     cc: []
131   }
132 }
133
134 async 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
141 async function getAudience (actorSender: ActorModel, t: Transaction, isPublic = true) {
142   const followerInboxUrls = await actorSender.getFollowerSharedInboxUrls(t)
143
144   return buildAudience(followerInboxUrls, isPublic)
145 }
146
147 function buildAudience (followerInboxUrls: string[], isPublic = true) {
148   // Thanks Mastodon: https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/tag_manager.rb#L47
149   let to = []
150   let cc = []
151
152   if (isPublic) {
153     to = [ ACTIVITY_PUB.PUBLIC ]
154     cc = followerInboxUrls
155   } else { // Unlisted
156     to = [ ]
157     cc = [ ]
158   }
159
160   return { to, cc }
161 }
162
163 function audiencify (object: any, audience: ActivityAudience) {
164   return Object.assign(object, audience)
165 }
166
167 async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) {
168   const toActorFollowerIds = toActorFollower.map(a => a.id)
169
170   const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
171   const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
172   return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
173 }
174
175 async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) {
176   const toActorSharedInboxesSet = new Set(toActors.map(a => a.sharedInboxUrl || a.inboxUrl))
177
178   const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
179   return Array.from(toActorSharedInboxesSet)
180     .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
181 }
182
183 // ---------------------------------------------------------------------------
184
185 export {
186   broadcastToFollowers,
187   unicastTo,
188   buildAudience,
189   getAudience,
190   getOriginVideoAudience,
191   getActorsInvolvedInVideo,
192   getObjectFollowersAudience,
193   forwardActivity,
194   audiencify,
195   getOriginVideoCommentAudience,
196   computeUris,
197   broadcastToActors
198 }