]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-create.ts
Merge branch 'feature/correctly-send-activities' into develop
[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 { VideoCommentModel } from '../../../models/video/video-comment'
7 import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
8 import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf, getVideoCommentAudience } from '../audience'
9 import { logger } from '../../../helpers/logger'
10 import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
11
12 async function sendCreateVideo (video: VideoModel, t: Transaction) {
13 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
14
15 logger.info('Creating job to send video creation of %s.', video.url)
16
17 const byActor = video.VideoChannel.Account.Actor
18 const videoObject = video.toActivityPubObject()
19
20 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
21 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
22
23 return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
24 }
25
26 async function sendCreateCacheFile (byActor: ActorModel, video: VideoModel, fileRedundancy: VideoRedundancyModel) {
27 logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
28
29 return sendVideoRelatedCreateActivity({
30 byActor,
31 video,
32 url: fileRedundancy.url,
33 object: fileRedundancy.toActivityPubObject()
34 })
35 }
36
37 async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
38 logger.info('Creating job to send comment %s.', comment.url)
39
40 const isOrigin = comment.Video.isOwned()
41
42 const byActor = comment.Account.Actor
43 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
44 const commentObject = comment.toActivityPubObject(threadParentComments)
45
46 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
47 // Add the actor that commented too
48 actorsInvolvedInComment.push(byActor)
49
50 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
51
52 let audience: ActivityAudience
53 if (isOrigin) {
54 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
55 } else {
56 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
57 }
58
59 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
60
61 // This was a reply, send it to the parent actors
62 const actorsException = [ byActor ]
63 await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException)
64
65 // Broadcast to our followers
66 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
67
68 // Send to actors involved in the comment
69 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
70
71 // Send to origin
72 return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
73 }
74
75 function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
76 if (!audience) audience = getAudience(byActor)
77
78 return audiencify(
79 {
80 type: 'Create' as 'Create',
81 id: url + '/activity',
82 actor: byActor.url,
83 object: audiencify(object, audience)
84 },
85 audience
86 )
87 }
88
89 // ---------------------------------------------------------------------------
90
91 export {
92 sendCreateVideo,
93 buildCreateActivity,
94 sendCreateVideoComment,
95 sendCreateCacheFile
96 }
97
98 // ---------------------------------------------------------------------------
99
100 async function sendVideoRelatedCreateActivity (options: {
101 byActor: ActorModel,
102 video: VideoModel,
103 url: string,
104 object: any,
105 transaction?: Transaction
106 }) {
107 const activityBuilder = (audience: ActivityAudience) => {
108 return buildCreateActivity(options.url, options.byActor, options.object, audience)
109 }
110
111 return sendVideoRelatedActivity(activityBuilder, options)
112 }