Detect posting request in our own inbox
[oweals/peertube.git] / server / lib / activitypub / send / send-create.ts
1 import { Transaction } from 'sequelize'
2 import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
3 import { VideoPrivacy } from '../../../../shared/models/videos'
4 import { getServerActor } from '../../../helpers/utils'
5 import { ActorModel } from '../../../models/activitypub/actor'
6 import { VideoModel } from '../../../models/video/video'
7 import { VideoAbuseModel } from '../../../models/video/video-abuse'
8 import { VideoCommentModel } from '../../../models/video/video-comment'
9 import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
10 import {
11   audiencify,
12   broadcastToActors,
13   broadcastToFollowers,
14   getActorsInvolvedInVideo,
15   getAudience,
16   getObjectFollowersAudience,
17   getOriginVideoAudience,
18   getOriginVideoCommentAudience,
19   unicastTo
20 } from './misc'
21
22 async function sendCreateVideo (video: VideoModel, t: Transaction) {
23   if (video.privacy === VideoPrivacy.PRIVATE) return undefined
24
25   const byActor = video.VideoChannel.Account.Actor
26   const videoObject = video.toActivityPubObject()
27
28   const audience = await getAudience(byActor, t, video.privacy === VideoPrivacy.PUBLIC)
29   const data = await createActivityData(video.url, byActor, videoObject, t, audience)
30
31   return broadcastToFollowers(data, byActor, [ byActor ], t)
32 }
33
34 async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) {
35   const url = getVideoAbuseActivityPubUrl(videoAbuse)
36
37   const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
38   const data = await createActivityData(url, byActor, videoAbuse.toActivityPubObject(), t, audience)
39
40   return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
41 }
42
43 async function sendCreateVideoCommentToOrigin (comment: VideoCommentModel, t: Transaction) {
44   const byActor = comment.Account.Actor
45   const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
46   const commentObject = comment.toActivityPubObject(threadParentComments)
47
48   const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
49   actorsInvolvedInComment.push(byActor)
50   const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment)
51
52   const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
53
54   // This was a reply, send it to the parent actors
55   const actorsException = [ byActor ]
56   await broadcastToActors(data, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
57
58   // Broadcast to our followers
59   await broadcastToFollowers(data, byActor, [ byActor ], t)
60
61   // Send to origin
62   return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
63 }
64
65 async function sendCreateVideoCommentToVideoFollowers (comment: VideoCommentModel, t: Transaction) {
66   const byActor = comment.Account.Actor
67   const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
68   const commentObject = comment.toActivityPubObject(threadParentComments)
69
70   const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
71   actorsInvolvedInComment.push(byActor)
72
73   const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment)
74   const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
75
76   // This was a reply, send it to the parent actors
77   const actorsException = [ byActor ]
78   await broadcastToActors(data, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
79
80   // Broadcast to our followers
81   await broadcastToFollowers(data, byActor, [ byActor ], t)
82
83   // Send to actors involved in the comment
84   return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
85 }
86
87 async function sendCreateViewToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
88   const url = getVideoViewActivityPubUrl(byActor, video)
89   const viewActivityData = createViewActivityData(byActor, video)
90
91   const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
92   const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
93   const data = await createActivityData(url, byActor, viewActivityData, t, audience)
94
95   return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
96 }
97
98 async function sendCreateViewToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
99   const url = getVideoViewActivityPubUrl(byActor, video)
100   const viewActivityData = createViewActivityData(byActor, video)
101
102   const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
103   const audience = getObjectFollowersAudience(actorsToForwardView)
104   const data = await createActivityData(url, byActor, viewActivityData, t, audience)
105
106   // Use the server actor to send the view
107   const serverActor = await getServerActor()
108   const actorsException = [ byActor ]
109   return broadcastToFollowers(data, serverActor, actorsToForwardView, t, actorsException)
110 }
111
112 async function sendCreateDislikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
113   const url = getVideoDislikeActivityPubUrl(byActor, video)
114   const dislikeActivityData = createDislikeActivityData(byActor, video)
115
116   const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
117   const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
118   const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
119
120   return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
121 }
122
123 async function sendCreateDislikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
124   const url = getVideoDislikeActivityPubUrl(byActor, video)
125   const dislikeActivityData = createDislikeActivityData(byActor, video)
126
127   const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
128   const audience = getObjectFollowersAudience(actorsToForwardView)
129   const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
130
131   const actorsException = [ byActor ]
132   return broadcastToFollowers(data, byActor, actorsToForwardView, t, actorsException)
133 }
134
135 async function createActivityData (
136   url: string,
137   byActor: ActorModel,
138   object: any,
139   t: Transaction,
140   audience?: ActivityAudience
141 ): Promise<ActivityCreate> {
142   if (!audience) {
143     audience = await getAudience(byActor, t)
144   }
145
146   return audiencify({
147     type: 'Create',
148     id: url,
149     actor: byActor.url,
150     object: audiencify(object, audience)
151   }, audience)
152 }
153
154 function createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
155   return {
156     type: 'Dislike',
157     actor: byActor.url,
158     object: video.url
159   }
160 }
161
162 function createViewActivityData (byActor: ActorModel, video: VideoModel) {
163   return {
164     type: 'View',
165     actor: byActor.url,
166     object: video.url
167   }
168 }
169
170 // ---------------------------------------------------------------------------
171
172 export {
173   sendCreateVideo,
174   sendVideoAbuse,
175   createActivityData,
176   sendCreateViewToOrigin,
177   sendCreateViewToVideoFollowers,
178   sendCreateDislikeToOrigin,
179   sendCreateDislikeToVideoFollowers,
180   createDislikeActivityData,
181   sendCreateVideoCommentToOrigin,
182   sendCreateVideoCommentToVideoFollowers
183 }