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'
12 MVideoRedundancyFileVideo,
13 MVideoRedundancyStreamingPlaylistVideo
14 } from '../../../types/models'
15 import { audiencify, getAudience } from '../audience'
19 getActorsInvolvedInVideo,
20 getAudienceFromFollowersOf,
21 getVideoCommentAudience,
22 sendVideoRelatedActivity,
26 const lTags = loggerTagsFactory('ap', 'create')
28 async function sendCreateVideo (video: MVideoAP, transaction: Transaction) {
29 if (!video.hasPrivacyForFederation()) return undefined
31 logger.info('Creating job to send video creation of %s.', video.url, lTags(video.uuid))
33 const byActor = video.VideoChannel.Account.Actor
34 const videoObject = video.toActivityPubObject()
36 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
37 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
39 return broadcastToFollowers({
42 toFollowersOf: [ byActor ],
48 async function sendCreateCacheFile (
50 video: MVideoAccountLight,
51 fileRedundancy: MVideoRedundancyStreamingPlaylistVideo | MVideoRedundancyFileVideo
53 logger.info('Creating job to send file cache of %s.', fileRedundancy.url, lTags(video.uuid))
55 return sendVideoRelatedCreateActivity({
58 url: fileRedundancy.url,
59 object: fileRedundancy.toActivityPubObject(),
60 contextType: 'CacheFile'
64 async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, transaction: Transaction) {
65 if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
67 logger.info('Creating job to send create video playlist of %s.', playlist.url, lTags(playlist.uuid))
69 const byActor = playlist.OwnerAccount.Actor
70 const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
72 const object = await playlist.toActivityPubObject(null, transaction)
73 const createActivity = buildCreateActivity(playlist.url, byActor, object, audience)
75 const serverActor = await getServerActor()
76 const toFollowersOf = [ byActor, serverActor ]
78 if (playlist.VideoChannel) toFollowersOf.push(playlist.VideoChannel.Actor)
80 return broadcastToFollowers({
85 contextType: 'Playlist'
89 async function sendCreateVideoComment (comment: MCommentOwnerVideo, transaction: Transaction) {
90 logger.info('Creating job to send comment %s.', comment.url)
92 const isOrigin = comment.Video.isOwned()
94 const byActor = comment.Account.Actor
95 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, transaction)
96 const commentObject = comment.toActivityPubObject(threadParentComments)
98 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, transaction)
99 // Add the actor that commented too
100 actorsInvolvedInComment.push(byActor)
102 const parentsCommentActors = threadParentComments.filter(c => !c.isDeleted())
103 .map(c => c.Account.Actor)
105 let audience: ActivityAudience
107 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
109 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
112 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
114 // This was a reply, send it to the parent actors
115 const actorsException = [ byActor ]
116 await broadcastToActors({
117 data: createActivity,
119 toActors: parentsCommentActors,
122 contextType: 'Comment'
125 // Broadcast to our followers
126 await broadcastToFollowers({
127 data: createActivity,
129 toFollowersOf: [ byActor ],
131 contextType: 'Comment'
134 // Send to actors involved in the comment
136 return broadcastToFollowers({
137 data: createActivity,
139 toFollowersOf: actorsInvolvedInComment,
142 contextType: 'Comment'
147 return transaction.afterCommit(() => {
149 data: createActivity,
151 toActorUrl: comment.Video.VideoChannel.Account.Actor.getSharedInbox(),
152 contextType: 'Comment'
157 function buildCreateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityCreate {
158 if (!audience) audience = getAudience(byActor)
162 type: 'Create' as 'Create',
163 id: url + '/activity',
165 object: audiencify(object, audience)
171 // ---------------------------------------------------------------------------
176 sendCreateVideoComment,
177 sendCreateVideoPlaylist,
181 // ---------------------------------------------------------------------------
183 async function sendVideoRelatedCreateActivity (options: {
185 video: MVideoAccountLight
188 contextType: ContextType
189 transaction?: Transaction
191 const activityBuilder = (audience: ActivityAudience) => {
192 return buildCreateActivity(options.url, options.byActor, options.object, audience)
195 return sendVideoRelatedActivity(activityBuilder, options)