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