]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-create.ts
Refactor AP context builder
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-create.ts
1 import { Transaction } from 'sequelize'
2 import { getServerActor } from '@server/models/application/application'
3 import { ActivityAudience, ActivityCreate, ContextType, VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
4 import { logger, loggerTagsFactory } from '../../../helpers/logger'
5 import { VideoCommentModel } from '../../../models/video/video-comment'
6 import {
7 MActorLight,
8 MCommentOwnerVideo,
9 MVideoAccountLight,
10 MVideoAP,
11 MVideoPlaylistFull,
12 MVideoRedundancyFileVideo,
13 MVideoRedundancyStreamingPlaylistVideo
14 } from '../../../types/models'
15 import { audiencify, getAudience } from '../audience'
16 import {
17 broadcastToActors,
18 broadcastToFollowers,
19 getActorsInvolvedInVideo,
20 getAudienceFromFollowersOf,
21 getVideoCommentAudience,
22 sendVideoRelatedActivity,
23 unicastTo
24 } from './shared'
25
26 const lTags = loggerTagsFactory('ap', 'create')
27
28 async function sendCreateVideo (video: MVideoAP, transaction: Transaction) {
29 if (!video.hasPrivacyForFederation()) return undefined
30
31 logger.info('Creating job to send video creation of %s.', video.url, lTags(video.uuid))
32
33 const byActor = video.VideoChannel.Account.Actor
34 const videoObject = video.toActivityPubObject()
35
36 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
37 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
38
39 return broadcastToFollowers({
40 data: createActivity,
41 byActor,
42 toFollowersOf: [ byActor ],
43 transaction,
44 contextType: 'Video'
45 })
46 }
47
48 async function sendCreateCacheFile (
49 byActor: MActorLight,
50 video: MVideoAccountLight,
51 fileRedundancy: MVideoRedundancyStreamingPlaylistVideo | MVideoRedundancyFileVideo
52 ) {
53 logger.info('Creating job to send file cache of %s.', fileRedundancy.url, lTags(video.uuid))
54
55 return sendVideoRelatedCreateActivity({
56 byActor,
57 video,
58 url: fileRedundancy.url,
59 object: fileRedundancy.toActivityPubObject(),
60 contextType: 'CacheFile'
61 })
62 }
63
64 async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, transaction: Transaction) {
65 if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
66
67 logger.info('Creating job to send create video playlist of %s.', playlist.url, lTags(playlist.uuid))
68
69 const byActor = playlist.OwnerAccount.Actor
70 const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
71
72 const object = await playlist.toActivityPubObject(null, transaction)
73 const createActivity = buildCreateActivity(playlist.url, byActor, object, audience)
74
75 const serverActor = await getServerActor()
76 const toFollowersOf = [ byActor, serverActor ]
77
78 if (playlist.VideoChannel) toFollowersOf.push(playlist.VideoChannel.Actor)
79
80 return broadcastToFollowers({
81 data: createActivity,
82 byActor,
83 toFollowersOf,
84 transaction,
85 contextType: 'Playlist'
86 })
87 }
88
89 async function sendCreateVideoComment (comment: MCommentOwnerVideo, transaction: Transaction) {
90 logger.info('Creating job to send comment %s.', comment.url)
91
92 const isOrigin = comment.Video.isOwned()
93
94 const byActor = comment.Account.Actor
95 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, transaction)
96 const commentObject = comment.toActivityPubObject(threadParentComments)
97
98 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, transaction)
99 // Add the actor that commented too
100 actorsInvolvedInComment.push(byActor)
101
102 const parentsCommentActors = threadParentComments.filter(c => !c.isDeleted())
103 .map(c => c.Account.Actor)
104
105 let audience: ActivityAudience
106 if (isOrigin) {
107 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
108 } else {
109 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
110 }
111
112 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
113
114 // This was a reply, send it to the parent actors
115 const actorsException = [ byActor ]
116 await broadcastToActors({
117 data: createActivity,
118 byActor,
119 toActors: parentsCommentActors,
120 transaction,
121 actorsException,
122 contextType: 'Comment'
123 })
124
125 // Broadcast to our followers
126 await broadcastToFollowers({
127 data: createActivity,
128 byActor,
129 toFollowersOf: [ byActor ],
130 transaction,
131 contextType: 'Comment'
132 })
133
134 // Send to actors involved in the comment
135 if (isOrigin) {
136 return broadcastToFollowers({
137 data: createActivity,
138 byActor,
139 toFollowersOf: actorsInvolvedInComment,
140 transaction,
141 actorsException,
142 contextType: 'Comment'
143 })
144 }
145
146 // Send to origin
147 return transaction.afterCommit(() => {
148 return unicastTo({
149 data: createActivity,
150 byActor,
151 toActorUrl: comment.Video.VideoChannel.Account.Actor.getSharedInbox(),
152 contextType: 'Comment'
153 })
154 })
155 }
156
157 function buildCreateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityCreate {
158 if (!audience) audience = getAudience(byActor)
159
160 return audiencify(
161 {
162 type: 'Create' as 'Create',
163 id: url + '/activity',
164 actor: byActor.url,
165 object: audiencify(object, audience)
166 },
167 audience
168 )
169 }
170
171 // ---------------------------------------------------------------------------
172
173 export {
174 sendCreateVideo,
175 buildCreateActivity,
176 sendCreateVideoComment,
177 sendCreateVideoPlaylist,
178 sendCreateCacheFile
179 }
180
181 // ---------------------------------------------------------------------------
182
183 async function sendVideoRelatedCreateActivity (options: {
184 byActor: MActorLight
185 video: MVideoAccountLight
186 url: string
187 object: any
188 contextType: ContextType
189 transaction?: Transaction
190 }) {
191 const activityBuilder = (audience: ActivityAudience) => {
192 return buildCreateActivity(options.url, options.byActor, options.object, audience)
193 }
194
195 return sendVideoRelatedActivity(activityBuilder, options)
196 }