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