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