]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/send-create.ts
Merge branch 'develop' into pr/1285
[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'
50d6de9c 4import { ActorModel } from '../../../models/activitypub/actor'
3fd3ab2d 5import { VideoModel } from '../../../models/video/video'
ea44f375 6import { VideoCommentModel } from '../../../models/video/video-comment'
a2377d15
C
7import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
8import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf, getVideoCommentAudience } from '../audience'
8e0fd45e 9import { logger } from '../../../helpers/logger'
c48e82b5 10import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
54141398 11
50d6de9c 12async function sendCreateVideo (video: VideoModel, t: Transaction) {
54b38063 13 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
54141398 14
8e0fd45e
C
15 logger.info('Creating job to send video creation of %s.', video.url)
16
e12a0092 17 const byActor = video.VideoChannel.Account.Actor
50d6de9c 18 const videoObject = video.toActivityPubObject()
e12a0092 19
2186386c 20 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
c48e82b5 21 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
54141398 22
c48e82b5 23 return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
54141398
C
24}
25
09209296 26async function sendCreateCacheFile (byActor: ActorModel, video: VideoModel, fileRedundancy: VideoRedundancyModel) {
c48e82b5
C
27 logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
28
a2377d15
C
29 return sendVideoRelatedCreateActivity({
30 byActor,
31 video,
32 url: fileRedundancy.url,
09209296 33 object: fileRedundancy.toActivityPubObject()
a2377d15 34 })
40ff5707
C
35}
36
07197db4 37async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
8e0fd45e
C
38 logger.info('Creating job to send comment %s.', comment.url)
39
07197db4
C
40 const isOrigin = comment.Video.isOwned()
41
ea44f375 42 const byActor = comment.Account.Actor
d7e70384
C
43 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
44 const commentObject = comment.toActivityPubObject(threadParentComments)
ea44f375 45
93ef8a9d 46 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
a2377d15 47 // Add the actor that commented too
93ef8a9d 48 actorsInvolvedInComment.push(byActor)
93ef8a9d 49
07197db4 50 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
ea44f375 51
07197db4
C
52 let audience: ActivityAudience
53 if (isOrigin) {
54 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
55 } else {
a2377d15 56 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
07197db4 57 }
93ef8a9d 58
c48e82b5 59 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
ea44f375 60
93ef8a9d
C
61 // This was a reply, send it to the parent actors
62 const actorsException = [ byActor ]
c48e82b5 63 await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException)
93ef8a9d
C
64
65 // Broadcast to our followers
c48e82b5 66 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
93ef8a9d
C
67
68 // Send to actors involved in the comment
c48e82b5 69 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
07197db4
C
70
71 // Send to origin
c48e82b5 72 return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
ea44f375
C
73}
74
c48e82b5 75function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
2186386c
C
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 )
54141398
C
87}
88
89// ---------------------------------------------------------------------------
90
91export {
50d6de9c 92 sendCreateVideo,
c48e82b5 93 buildCreateActivity,
c48e82b5
C
94 sendCreateVideoComment,
95 sendCreateCacheFile
54141398 96}
a2377d15
C
97
98// ---------------------------------------------------------------------------
99
100async 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}