]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-create.ts
Basic video redundancy implementation
[github/Chocobozzz/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 import { logger } from '../../../helpers/logger'
20 import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
21
22 async function sendCreateVideo (video: VideoModel, t: Transaction) {
23 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
24
25 logger.info('Creating job to send video creation of %s.', video.url)
26
27 const byActor = video.VideoChannel.Account.Actor
28 const videoObject = video.toActivityPubObject()
29
30 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
31 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
32
33 return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
34 }
35
36 async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel) {
37 if (!video.VideoChannel.Account.Actor.serverId) return // Local
38
39 const url = getVideoAbuseActivityPubUrl(videoAbuse)
40
41 logger.info('Creating job to send video abuse %s.', url)
42
43 const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
44 const createActivity = buildCreateActivity(url, byActor, videoAbuse.toActivityPubObject(), audience)
45
46 return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
47 }
48
49 async function sendCreateCacheFile (byActor: ActorModel, fileRedundancy: VideoRedundancyModel) {
50 logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
51
52 const redundancyObject = fileRedundancy.toActivityPubObject()
53
54 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(fileRedundancy.VideoFile.Video.id)
55 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, undefined)
56
57 const audience = getVideoAudience(video, actorsInvolvedInVideo)
58 const createActivity = buildCreateActivity(fileRedundancy.url, byActor, redundancyObject, audience)
59
60 return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
61 }
62
63 async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
64 logger.info('Creating job to send comment %s.', comment.url)
65
66 const isOrigin = comment.Video.isOwned()
67
68 const byActor = comment.Account.Actor
69 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
70 const commentObject = comment.toActivityPubObject(threadParentComments)
71
72 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
73 actorsInvolvedInComment.push(byActor)
74
75 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
76
77 let audience: ActivityAudience
78 if (isOrigin) {
79 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
80 } else {
81 audience = getObjectFollowersAudience(actorsInvolvedInComment.concat(parentsCommentActors))
82 }
83
84 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
85
86 // This was a reply, send it to the parent actors
87 const actorsException = [ byActor ]
88 await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException)
89
90 // Broadcast to our followers
91 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
92
93 // Send to actors involved in the comment
94 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
95
96 // Send to origin
97 return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
98 }
99
100 async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) {
101 logger.info('Creating job to send view of %s.', video.url)
102
103 const url = getVideoViewActivityPubUrl(byActor, video)
104 const viewActivity = buildViewActivity(byActor, video)
105
106 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
107
108 // Send to origin
109 if (video.isOwned() === false) {
110 const audience = getVideoAudience(video, actorsInvolvedInVideo)
111 const createActivity = buildCreateActivity(url, byActor, viewActivity, audience)
112
113 return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
114 }
115
116 // Send to followers
117 const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
118 const createActivity = buildCreateActivity(url, byActor, viewActivity, audience)
119
120 // Use the server actor to send the view
121 const serverActor = await getServerActor()
122 const actorsException = [ byActor ]
123 return broadcastToFollowers(createActivity, serverActor, actorsInvolvedInVideo, t, actorsException)
124 }
125
126 async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
127 logger.info('Creating job to dislike %s.', video.url)
128
129 const url = getVideoDislikeActivityPubUrl(byActor, video)
130 const dislikeActivity = buildDislikeActivity(byActor, video)
131
132 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
133
134 // Send to origin
135 if (video.isOwned() === false) {
136 const audience = getVideoAudience(video, actorsInvolvedInVideo)
137 const createActivity = buildCreateActivity(url, byActor, dislikeActivity, audience)
138
139 return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
140 }
141
142 // Send to followers
143 const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
144 const createActivity = buildCreateActivity(url, byActor, dislikeActivity, audience)
145
146 const actorsException = [ byActor ]
147 return broadcastToFollowers(createActivity, byActor, actorsInvolvedInVideo, t, actorsException)
148 }
149
150 function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
151 if (!audience) audience = getAudience(byActor)
152
153 return audiencify(
154 {
155 type: 'Create' as 'Create',
156 id: url + '/activity',
157 actor: byActor.url,
158 object: audiencify(object, audience)
159 },
160 audience
161 )
162 }
163
164 function buildDislikeActivity (byActor: ActorModel, video: VideoModel) {
165 return {
166 type: 'Dislike',
167 actor: byActor.url,
168 object: video.url
169 }
170 }
171
172 function buildViewActivity (byActor: ActorModel, video: VideoModel) {
173 return {
174 type: 'View',
175 actor: byActor.url,
176 object: video.url
177 }
178 }
179
180 // ---------------------------------------------------------------------------
181
182 export {
183 sendCreateVideo,
184 sendVideoAbuse,
185 buildCreateActivity,
186 sendCreateView,
187 sendCreateDislike,
188 buildDislikeActivity,
189 sendCreateVideoComment,
190 sendCreateCacheFile
191 }