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'
15 async function sendCreateVideo (video: VideoModel, t: Transaction) {
16 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
18 logger.info('Creating job to send video creation of %s.', video.url)
20 const byActor = video.VideoChannel.Account.Actor
21 const videoObject = video.toActivityPubObject()
23 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
24 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
26 return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
29 async function sendCreateCacheFile (byActor: ActorModel, video: VideoModel, fileRedundancy: VideoRedundancyModel) {
30 logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
32 return sendVideoRelatedCreateActivity({
35 url: fileRedundancy.url,
36 object: fileRedundancy.toActivityPubObject()
40 async function sendCreateVideoPlaylist (playlist: VideoPlaylistModel, t: Transaction) {
41 if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
43 logger.info('Creating job to send create video playlist of %s.', playlist.url)
45 const byActor = playlist.OwnerAccount.Actor
46 const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
48 const object = await playlist.toActivityPubObject()
49 const createActivity = buildCreateActivity(playlist.url, byActor, object, audience)
51 const serverActor = await getServerActor()
52 const toFollowersOf = [ byActor, serverActor ]
54 if (playlist.VideoChannel) toFollowersOf.push(playlist.VideoChannel.Actor)
56 return broadcastToFollowers(createActivity, byActor, toFollowersOf, t)
59 async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
60 logger.info('Creating job to send comment %s.', comment.url)
62 const isOrigin = comment.Video.isOwned()
64 const byActor = comment.Account.Actor
65 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
66 const commentObject = comment.toActivityPubObject(threadParentComments)
68 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
69 // Add the actor that commented too
70 actorsInvolvedInComment.push(byActor)
72 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
74 let audience: ActivityAudience
76 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
78 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
81 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
83 // This was a reply, send it to the parent actors
84 const actorsException = [ byActor ]
85 await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException)
87 // Broadcast to our followers
88 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
90 // Send to actors involved in the comment
91 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
94 return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
97 function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
98 if (!audience) audience = getAudience(byActor)
102 type: 'Create' as 'Create',
103 id: url + '/activity',
105 object: audiencify(object, audience)
111 // ---------------------------------------------------------------------------
116 sendCreateVideoComment,
117 sendCreateVideoPlaylist,
121 // ---------------------------------------------------------------------------
123 async function sendVideoRelatedCreateActivity (options: {
128 transaction?: Transaction
130 const activityBuilder = (audience: ActivityAudience) => {
131 return buildCreateActivity(options.url, options.byActor, options.object, audience)
134 return sendVideoRelatedActivity(activityBuilder, options)