]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/send-create.ts
Basic video redundancy implementation
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-create.ts
CommitLineData
54141398 1import { Transaction } from 'sequelize'
3fd3ab2d 2import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
50d6de9c 3import { VideoPrivacy } from '../../../../shared/models/videos'
da854ddd 4import { getServerActor } from '../../../helpers/utils'
50d6de9c 5import { ActorModel } from '../../../models/activitypub/actor'
3fd3ab2d
C
6import { VideoModel } from '../../../models/video/video'
7import { VideoAbuseModel } from '../../../models/video/video-abuse'
ea44f375 8import { VideoCommentModel } from '../../../models/video/video-comment'
0032ebe9 9import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
e251f170 10import { broadcastToActors, broadcastToFollowers, unicastTo } from './utils'
0032ebe9 11import {
94a5ff8a 12 audiencify,
94a5ff8a
C
13 getActorsInvolvedInVideo,
14 getAudience,
15 getObjectFollowersAudience,
e251f170
C
16 getVideoAudience,
17 getVideoCommentAudience
18} from '../audience'
8e0fd45e 19import { logger } from '../../../helpers/logger'
c48e82b5 20import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
54141398 21
50d6de9c 22async function sendCreateVideo (video: VideoModel, t: Transaction) {
54b38063 23 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
54141398 24
8e0fd45e
C
25 logger.info('Creating job to send video creation of %s.', video.url)
26
e12a0092 27 const byActor = video.VideoChannel.Account.Actor
50d6de9c 28 const videoObject = video.toActivityPubObject()
e12a0092 29
2186386c 30 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
c48e82b5 31 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
54141398 32
c48e82b5 33 return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
54141398
C
34}
35
c48e82b5 36async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel) {
06a05d5f
C
37 if (!video.VideoChannel.Account.Actor.serverId) return // Local
38
54141398 39 const url = getVideoAbuseActivityPubUrl(videoAbuse)
40ff5707 40
8e0fd45e
C
41 logger.info('Creating job to send video abuse %s.', url)
42
50d6de9c 43 const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
c48e82b5 44 const createActivity = buildCreateActivity(url, byActor, videoAbuse.toActivityPubObject(), audience)
40ff5707 45
c48e82b5
C
46 return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
47}
48
49async 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)
40ff5707
C
61}
62
07197db4 63async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
8e0fd45e
C
64 logger.info('Creating job to send comment %s.', comment.url)
65
07197db4
C
66 const isOrigin = comment.Video.isOwned()
67
ea44f375 68 const byActor = comment.Account.Actor
d7e70384
C
69 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
70 const commentObject = comment.toActivityPubObject(threadParentComments)
ea44f375 71
93ef8a9d
C
72 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
73 actorsInvolvedInComment.push(byActor)
93ef8a9d 74
07197db4 75 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
ea44f375 76
07197db4
C
77 let audience: ActivityAudience
78 if (isOrigin) {
79 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
80 } else {
81 audience = getObjectFollowersAudience(actorsInvolvedInComment.concat(parentsCommentActors))
82 }
93ef8a9d 83
c48e82b5 84 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
ea44f375 85
93ef8a9d
C
86 // This was a reply, send it to the parent actors
87 const actorsException = [ byActor ]
c48e82b5 88 await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException)
93ef8a9d
C
89
90 // Broadcast to our followers
c48e82b5 91 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
93ef8a9d
C
92
93 // Send to actors involved in the comment
c48e82b5 94 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
07197db4
C
95
96 // Send to origin
c48e82b5 97 return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
ea44f375
C
98}
99
07197db4 100async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) {
8e0fd45e
C
101 logger.info('Creating job to send view of %s.', video.url)
102
50d6de9c 103 const url = getVideoViewActivityPubUrl(byActor, video)
c48e82b5 104 const viewActivity = buildViewActivity(byActor, video)
40ff5707 105
50d6de9c 106 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
54141398 107
07197db4
C
108 // Send to origin
109 if (video.isOwned() === false) {
e251f170 110 const audience = getVideoAudience(video, actorsInvolvedInVideo)
c48e82b5 111 const createActivity = buildCreateActivity(url, byActor, viewActivity, audience)
54141398 112
c48e82b5 113 return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
07197db4 114 }
40ff5707 115
07197db4
C
116 // Send to followers
117 const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
c48e82b5 118 const createActivity = buildCreateActivity(url, byActor, viewActivity, audience)
40ff5707 119
50d6de9c
C
120 // Use the server actor to send the view
121 const serverActor = await getServerActor()
93ef8a9d 122 const actorsException = [ byActor ]
c48e82b5 123 return broadcastToFollowers(createActivity, serverActor, actorsInvolvedInVideo, t, actorsException)
0032ebe9
C
124}
125
07197db4 126async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
8e0fd45e
C
127 logger.info('Creating job to dislike %s.', video.url)
128
50d6de9c 129 const url = getVideoDislikeActivityPubUrl(byActor, video)
c48e82b5 130 const dislikeActivity = buildDislikeActivity(byActor, video)
0032ebe9 131
50d6de9c 132 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
0032ebe9 133
07197db4
C
134 // Send to origin
135 if (video.isOwned() === false) {
e251f170 136 const audience = getVideoAudience(video, actorsInvolvedInVideo)
c48e82b5 137 const createActivity = buildCreateActivity(url, byActor, dislikeActivity, audience)
40ff5707 138
c48e82b5 139 return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
07197db4 140 }
0032ebe9 141
07197db4
C
142 // Send to followers
143 const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
c48e82b5 144 const createActivity = buildCreateActivity(url, byActor, dislikeActivity, audience)
0032ebe9 145
93ef8a9d 146 const actorsException = [ byActor ]
c48e82b5 147 return broadcastToFollowers(createActivity, byActor, actorsInvolvedInVideo, t, actorsException)
40ff5707
C
148}
149
c48e82b5 150function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
2186386c
C
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 )
54141398
C
162}
163
c48e82b5 164function buildDislikeActivity (byActor: ActorModel, video: VideoModel) {
3fd3ab2d 165 return {
0032ebe9 166 type: 'Dislike',
50d6de9c 167 actor: byActor.url,
0032ebe9
C
168 object: video.url
169 }
0032ebe9
C
170}
171
c48e82b5 172function buildViewActivity (byActor: ActorModel, video: VideoModel) {
ea44f375
C
173 return {
174 type: 'View',
175 actor: byActor.url,
176 object: video.url
177 }
178}
179
54141398
C
180// ---------------------------------------------------------------------------
181
182export {
50d6de9c 183 sendCreateVideo,
54141398 184 sendVideoAbuse,
c48e82b5 185 buildCreateActivity,
07197db4
C
186 sendCreateView,
187 sendCreateDislike,
c48e82b5
C
188 buildDislikeActivity,
189 sendCreateVideoComment,
190 sendCreateCacheFile
54141398 191}