]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-create.ts
Add hls support on server
[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 { Video, 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, video: VideoModel, fileRedundancy: VideoRedundancyModel) {
43 logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
44
45 return sendVideoRelatedCreateActivity({
46 byActor,
47 video,
48 url: fileRedundancy.url,
49 object: fileRedundancy.toActivityPubObject()
50 })
51 }
52
53 async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
54 logger.info('Creating job to send comment %s.', comment.url)
55
56 const isOrigin = comment.Video.isOwned()
57
58 const byActor = comment.Account.Actor
59 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
60 const commentObject = comment.toActivityPubObject(threadParentComments)
61
62 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
63 // Add the actor that commented too
64 actorsInvolvedInComment.push(byActor)
65
66 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
67
68 let audience: ActivityAudience
69 if (isOrigin) {
70 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
71 } else {
72 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
73 }
74
75 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
76
77 // This was a reply, send it to the parent actors
78 const actorsException = [ byActor ]
79 await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException)
80
81 // Broadcast to our followers
82 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
83
84 // Send to actors involved in the comment
85 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
86
87 // Send to origin
88 return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
89 }
90
91 async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) {
92 logger.info('Creating job to send view of %s.', video.url)
93
94 const url = getVideoViewActivityPubUrl(byActor, video)
95 const viewActivity = buildViewActivity(url, byActor, video)
96
97 return sendVideoRelatedCreateActivity({
98 // Use the server actor to send the view
99 byActor,
100 video,
101 url,
102 object: viewActivity,
103 transaction: t
104 })
105 }
106
107 async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
108 logger.info('Creating job to dislike %s.', video.url)
109
110 const url = getVideoDislikeActivityPubUrl(byActor, video)
111 const dislikeActivity = buildDislikeActivity(url, byActor, video)
112
113 return sendVideoRelatedCreateActivity({
114 byActor,
115 video,
116 url,
117 object: dislikeActivity,
118 transaction: t
119 })
120 }
121
122 function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
123 if (!audience) audience = getAudience(byActor)
124
125 return audiencify(
126 {
127 type: 'Create' as 'Create',
128 id: url + '/activity',
129 actor: byActor.url,
130 object: audiencify(object, audience)
131 },
132 audience
133 )
134 }
135
136 function buildDislikeActivity (url: string, byActor: ActorModel, video: VideoModel) {
137 return {
138 id: url,
139 type: 'Dislike',
140 actor: byActor.url,
141 object: video.url
142 }
143 }
144
145 function buildViewActivity (url: string, byActor: ActorModel, video: VideoModel) {
146 return {
147 id: url,
148 type: 'View',
149 actor: byActor.url,
150 object: video.url
151 }
152 }
153
154 // ---------------------------------------------------------------------------
155
156 export {
157 sendCreateVideo,
158 sendVideoAbuse,
159 buildCreateActivity,
160 sendCreateView,
161 sendCreateDislike,
162 buildDislikeActivity,
163 sendCreateVideoComment,
164 sendCreateCacheFile
165 }
166
167 // ---------------------------------------------------------------------------
168
169 async function sendVideoRelatedCreateActivity (options: {
170 byActor: ActorModel,
171 video: VideoModel,
172 url: string,
173 object: any,
174 transaction?: Transaction
175 }) {
176 const activityBuilder = (audience: ActivityAudience) => {
177 return buildCreateActivity(options.url, options.byActor, options.object, audience)
178 }
179
180 return sendVideoRelatedActivity(activityBuilder, options)
181 }