]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/send-create.ts
Bumped to version v3.1.0
[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 8import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
453e83ea
C
9import {
10 MActorLight,
11 MCommentOwnerVideo,
12 MVideoAccountLight,
13 MVideoAP,
14 MVideoPlaylistFull,
15 MVideoRedundancyFileVideo,
16 MVideoRedundancyStreamingPlaylistVideo
26d6bf65 17} from '../../../types/models'
8dc8a34e 18import { getServerActor } from '@server/models/application/application'
e307e4fc 19import { ContextType } from '@shared/models/activitypub/context'
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
6cb55644
C
83 const parentsCommentActors = threadParentComments.filter(c => !c.isDeleted())
84 .map(c => c.Account.Actor)
ea44f375 85
07197db4
C
86 let audience: ActivityAudience
87 if (isOrigin) {
88 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
89 } else {
a2377d15 90 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
07197db4 91 }
93ef8a9d 92
c48e82b5 93 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
ea44f375 94
93ef8a9d
C
95 // This was a reply, send it to the parent actors
96 const actorsException = [ byActor ]
2284f202 97 await broadcastToActors(createActivity, byActor, parentsCommentActors, t, actorsException)
93ef8a9d
C
98
99 // Broadcast to our followers
c48e82b5 100 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
93ef8a9d
C
101
102 // Send to actors involved in the comment
c48e82b5 103 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
07197db4
C
104
105 // Send to origin
47581df0 106 t.afterCommit(() => unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.getSharedInbox()))
ea44f375
C
107}
108
453e83ea 109function buildCreateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityCreate {
2186386c
C
110 if (!audience) audience = getAudience(byActor)
111
112 return audiencify(
113 {
114 type: 'Create' as 'Create',
115 id: url + '/activity',
116 actor: byActor.url,
117 object: audiencify(object, audience)
118 },
119 audience
120 )
54141398
C
121}
122
123// ---------------------------------------------------------------------------
124
125export {
50d6de9c 126 sendCreateVideo,
c48e82b5 127 buildCreateActivity,
c48e82b5 128 sendCreateVideoComment,
418d092a 129 sendCreateVideoPlaylist,
c48e82b5 130 sendCreateCacheFile
54141398 131}
a2377d15
C
132
133// ---------------------------------------------------------------------------
134
135async function sendVideoRelatedCreateActivity (options: {
a1587156
C
136 byActor: MActorLight
137 video: MVideoAccountLight
138 url: string
139 object: any
a2377d15 140 transaction?: Transaction
084a2cd0 141 contextType?: ContextType
a2377d15
C
142}) {
143 const activityBuilder = (audience: ActivityAudience) => {
144 return buildCreateActivity(options.url, options.byActor, options.object, audience)
145 }
146
147 return sendVideoRelatedActivity(activityBuilder, options)
148}