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