]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/send-create.ts
Merge branch 'release/1.4.0' into develop
[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'
ea44f375 4import { VideoCommentModel } from '../../../models/video/video-comment'
a2377d15
C
5import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
6import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf, getVideoCommentAudience } from '../audience'
8e0fd45e 7import { logger } from '../../../helpers/logger'
418d092a
C
8import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
9import { getServerActor } from '../../../helpers/utils'
453e83ea
C
10import {
11 MActorLight,
12 MCommentOwnerVideo,
13 MVideoAccountLight,
14 MVideoAP,
15 MVideoPlaylistFull,
16 MVideoRedundancyFileVideo,
17 MVideoRedundancyStreamingPlaylistVideo
18} from '../../../typings/models'
19
20async function sendCreateVideo (video: MVideoAP, t: Transaction) {
54b38063 21 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
54141398 22
8e0fd45e
C
23 logger.info('Creating job to send video creation of %s.', video.url)
24
e12a0092 25 const byActor = video.VideoChannel.Account.Actor
50d6de9c 26 const videoObject = video.toActivityPubObject()
e12a0092 27
2186386c 28 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
c48e82b5 29 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
54141398 30
c48e82b5 31 return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
54141398
C
32}
33
453e83ea
C
34async function sendCreateCacheFile (
35 byActor: MActorLight,
36 video: MVideoAccountLight,
37 fileRedundancy: MVideoRedundancyStreamingPlaylistVideo | MVideoRedundancyFileVideo
38) {
c48e82b5
C
39 logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
40
a2377d15
C
41 return sendVideoRelatedCreateActivity({
42 byActor,
43 video,
44 url: fileRedundancy.url,
09209296 45 object: fileRedundancy.toActivityPubObject()
a2377d15 46 })
40ff5707
C
47}
48
453e83ea 49async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, t: Transaction) {
418d092a
C
50 if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
51
52 logger.info('Creating job to send create video playlist of %s.', playlist.url)
53
54 const byActor = playlist.OwnerAccount.Actor
55 const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
56
df0b219d 57 const object = await playlist.toActivityPubObject(null, t)
418d092a
C
58 const createActivity = buildCreateActivity(playlist.url, byActor, object, audience)
59
60 const serverActor = await getServerActor()
61 const toFollowersOf = [ byActor, serverActor ]
62
63 if (playlist.VideoChannel) toFollowersOf.push(playlist.VideoChannel.Actor)
64
65 return broadcastToFollowers(createActivity, byActor, toFollowersOf, t)
66}
67
453e83ea 68async function sendCreateVideoComment (comment: MCommentOwnerVideo, t: Transaction) {
8e0fd45e
C
69 logger.info('Creating job to send comment %s.', comment.url)
70
07197db4
C
71 const isOrigin = comment.Video.isOwned()
72
ea44f375 73 const byActor = comment.Account.Actor
d7e70384
C
74 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
75 const commentObject = comment.toActivityPubObject(threadParentComments)
ea44f375 76
93ef8a9d 77 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
a2377d15 78 // Add the actor that commented too
93ef8a9d 79 actorsInvolvedInComment.push(byActor)
93ef8a9d 80
07197db4 81 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
ea44f375 82
07197db4
C
83 let audience: ActivityAudience
84 if (isOrigin) {
85 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
86 } else {
a2377d15 87 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
07197db4 88 }
93ef8a9d 89
c48e82b5 90 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
ea44f375 91
93ef8a9d
C
92 // This was a reply, send it to the parent actors
93 const actorsException = [ byActor ]
2284f202 94 await broadcastToActors(createActivity, byActor, parentsCommentActors, t, actorsException)
93ef8a9d
C
95
96 // Broadcast to our followers
c48e82b5 97 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
93ef8a9d
C
98
99 // Send to actors involved in the comment
c48e82b5 100 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
07197db4
C
101
102 // Send to origin
2284f202 103 t.afterCommit(() => unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl))
ea44f375
C
104}
105
453e83ea 106function buildCreateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityCreate {
2186386c
C
107 if (!audience) audience = getAudience(byActor)
108
109 return audiencify(
110 {
111 type: 'Create' as 'Create',
112 id: url + '/activity',
113 actor: byActor.url,
114 object: audiencify(object, audience)
115 },
116 audience
117 )
54141398
C
118}
119
120// ---------------------------------------------------------------------------
121
122export {
50d6de9c 123 sendCreateVideo,
c48e82b5 124 buildCreateActivity,
c48e82b5 125 sendCreateVideoComment,
418d092a 126 sendCreateVideoPlaylist,
c48e82b5 127 sendCreateCacheFile
54141398 128}
a2377d15
C
129
130// ---------------------------------------------------------------------------
131
132async function sendVideoRelatedCreateActivity (options: {
453e83ea
C
133 byActor: MActorLight,
134 video: MVideoAccountLight,
a2377d15
C
135 url: string,
136 object: any,
137 transaction?: Transaction
138}) {
139 const activityBuilder = (audience: ActivityAudience) => {
140 return buildCreateActivity(options.url, options.byActor, options.object, audience)
141 }
142
143 return sendVideoRelatedActivity(activityBuilder, options)
144}