1 import { Transaction } from 'sequelize'
2 import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
3 import { VideoPrivacy } from '../../../../shared/models/videos'
4 import { VideoCommentModel } from '../../../models/video/video-comment'
5 import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
6 import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf, getVideoCommentAudience } from '../audience'
7 import { logger } from '../../../helpers/logger'
8 import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
15 MVideoRedundancyFileVideo,
16 MVideoRedundancyStreamingPlaylistVideo
17 } from '../../../types/models'
18 import { getServerActor } from '@server/models/application/application'
19 import { ContextType } from '@shared/models/activitypub/context'
21 async function sendCreateVideo (video: MVideoAP, t: Transaction) {
22 if (!video.hasPrivacyForFederation()) return undefined
24 logger.info('Creating job to send video creation of %s.', video.url)
26 const byActor = video.VideoChannel.Account.Actor
27 const videoObject = video.toActivityPubObject()
29 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
30 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
32 return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
35 async function sendCreateCacheFile (
37 video: MVideoAccountLight,
38 fileRedundancy: MVideoRedundancyStreamingPlaylistVideo | MVideoRedundancyFileVideo
40 logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
42 return sendVideoRelatedCreateActivity({
45 url: fileRedundancy.url,
46 object: fileRedundancy.toActivityPubObject(),
47 contextType: 'CacheFile'
51 async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, t: Transaction) {
52 if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
54 logger.info('Creating job to send create video playlist of %s.', playlist.url)
56 const byActor = playlist.OwnerAccount.Actor
57 const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
59 const object = await playlist.toActivityPubObject(null, t)
60 const createActivity = buildCreateActivity(playlist.url, byActor, object, audience)
62 const serverActor = await getServerActor()
63 const toFollowersOf = [ byActor, serverActor ]
65 if (playlist.VideoChannel) toFollowersOf.push(playlist.VideoChannel.Actor)
67 return broadcastToFollowers(createActivity, byActor, toFollowersOf, t)
70 async function sendCreateVideoComment (comment: MCommentOwnerVideo, t: Transaction) {
71 logger.info('Creating job to send comment %s.', comment.url)
73 const isOrigin = comment.Video.isOwned()
75 const byActor = comment.Account.Actor
76 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
77 const commentObject = comment.toActivityPubObject(threadParentComments)
79 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
80 // Add the actor that commented too
81 actorsInvolvedInComment.push(byActor)
83 const parentsCommentActors = threadParentComments.filter(c => !c.isDeleted())
84 .map(c => c.Account.Actor)
86 let audience: ActivityAudience
88 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
90 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
93 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
95 // This was a reply, send it to the parent actors
96 const actorsException = [ byActor ]
97 await broadcastToActors(createActivity, byActor, parentsCommentActors, t, actorsException)
99 // Broadcast to our followers
100 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
102 // Send to actors involved in the comment
103 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
106 t.afterCommit(() => unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.getSharedInbox()))
109 function buildCreateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityCreate {
110 if (!audience) audience = getAudience(byActor)
114 type: 'Create' as 'Create',
115 id: url + '/activity',
117 object: audiencify(object, audience)
123 // ---------------------------------------------------------------------------
128 sendCreateVideoComment,
129 sendCreateVideoPlaylist,
133 // ---------------------------------------------------------------------------
135 async function sendVideoRelatedCreateActivity (options: {
137 video: MVideoAccountLight
140 transaction?: Transaction
141 contextType?: ContextType
143 const activityBuilder = (audience: ActivityAudience) => {
144 return buildCreateActivity(options.url, options.byActor, options.object, audience)
147 return sendVideoRelatedActivity(activityBuilder, options)