Split files in activitypub server
[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 { broadcastToActors, broadcastToFollowers, unicastTo } from './utils'
11 import {
12   audiencify,
13   getActorsInvolvedInVideo,
14   getAudience,
15   getObjectFollowersAudience,
16   getVideoAudience,
17   getVideoCommentAudience
18 } from '../audience'
19
20 async function sendCreateVideo (video: VideoModel, t: Transaction) {
21   if (video.privacy === VideoPrivacy.PRIVATE) return undefined
22
23   const byActor = video.VideoChannel.Account.Actor
24   const videoObject = video.toActivityPubObject()
25
26   const audience = await getAudience(byActor, t, video.privacy === VideoPrivacy.PUBLIC)
27   const data = await createActivityData(video.url, byActor, videoObject, t, audience)
28
29   return broadcastToFollowers(data, byActor, [ byActor ], t)
30 }
31
32 async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) {
33   const url = getVideoAbuseActivityPubUrl(videoAbuse)
34
35   const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
36   const data = await createActivityData(url, byActor, videoAbuse.toActivityPubObject(), t, audience)
37
38   return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
39 }
40
41 async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
42   const isOrigin = comment.Video.isOwned()
43
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
51   const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
52
53   let audience: ActivityAudience
54   if (isOrigin) {
55     audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
56   } else {
57     audience = getObjectFollowersAudience(actorsInvolvedInComment.concat(parentsCommentActors))
58   }
59
60   const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
61
62   // This was a reply, send it to the parent actors
63   const actorsException = [ byActor ]
64   await broadcastToActors(data, byActor, parentsCommentActors, actorsException)
65
66   // Broadcast to our followers
67   await broadcastToFollowers(data, byActor, [ byActor ], t)
68
69   // Send to actors involved in the comment
70   if (isOrigin) return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
71
72   // Send to origin
73   return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
74 }
75
76 async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) {
77   const url = getVideoViewActivityPubUrl(byActor, video)
78   const viewActivityData = createViewActivityData(byActor, video)
79
80   const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
81
82   // Send to origin
83   if (video.isOwned() === false) {
84     const audience = getVideoAudience(video, actorsInvolvedInVideo)
85     const data = await createActivityData(url, byActor, viewActivityData, t, audience)
86
87     return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
88   }
89
90   // Send to followers
91   const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
92   const data = await createActivityData(url, byActor, viewActivityData, t, audience)
93
94   // Use the server actor to send the view
95   const serverActor = await getServerActor()
96   const actorsException = [ byActor ]
97   return broadcastToFollowers(data, serverActor, actorsInvolvedInVideo, t, actorsException)
98 }
99
100 async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
101   const url = getVideoDislikeActivityPubUrl(byActor, video)
102   const dislikeActivityData = createDislikeActivityData(byActor, video)
103
104   const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
105
106   // Send to origin
107   if (video.isOwned() === false) {
108     const audience = getVideoAudience(video, actorsInvolvedInVideo)
109     const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
110
111     return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
112   }
113
114   // Send to followers
115   const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
116   const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
117
118   const actorsException = [ byActor ]
119   return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, actorsException)
120 }
121
122 async function createActivityData (url: string,
123                                    byActor: ActorModel,
124                                    object: any,
125                                    t: Transaction,
126                                    audience?: ActivityAudience): Promise<ActivityCreate> {
127   if (!audience) {
128     audience = await getAudience(byActor, t)
129   }
130
131   return audiencify({
132     type: 'Create' as 'Create',
133     id: url + '/activity',
134     actor: byActor.url,
135     object: audiencify(object, audience)
136   }, audience)
137 }
138
139 function createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
140   return {
141     type: 'Dislike',
142     actor: byActor.url,
143     object: video.url
144   }
145 }
146
147 function createViewActivityData (byActor: ActorModel, video: VideoModel) {
148   return {
149     type: 'View',
150     actor: byActor.url,
151     object: video.url
152   }
153 }
154
155 // ---------------------------------------------------------------------------
156
157 export {
158   sendCreateVideo,
159   sendVideoAbuse,
160   createActivityData,
161   sendCreateView,
162   sendCreateDislike,
163   createDislikeActivityData,
164   sendCreateVideoComment
165 }