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