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'
12 async function sendCreateVideo (video: VideoModel, t: Transaction) {
13 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
15 logger.info('Creating job to send video creation of %s.', video.url)
17 const byActor = video.VideoChannel.Account.Actor
18 const videoObject = video.toActivityPubObject()
20 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
21 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
23 return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
26 async function sendCreateCacheFile (byActor: ActorModel, fileRedundancy: VideoRedundancyModel) {
27 logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
29 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(fileRedundancy.VideoFile.Video.id)
30 const redundancyObject = fileRedundancy.toActivityPubObject()
32 return sendVideoRelatedCreateActivity({
35 url: fileRedundancy.url,
36 object: redundancyObject
40 async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
41 logger.info('Creating job to send comment %s.', comment.url)
43 const isOrigin = comment.Video.isOwned()
45 const byActor = comment.Account.Actor
46 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
47 const commentObject = comment.toActivityPubObject(threadParentComments)
49 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
50 // Add the actor that commented too
51 actorsInvolvedInComment.push(byActor)
53 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
55 let audience: ActivityAudience
57 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
59 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
62 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
64 // This was a reply, send it to the parent actors
65 const actorsException = [ byActor ]
66 await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException)
68 // Broadcast to our followers
69 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
71 // Send to actors involved in the comment
72 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
75 return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
78 function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
79 if (!audience) audience = getAudience(byActor)
83 type: 'Create' as 'Create',
84 id: url + '/activity',
86 object: audiencify(object, audience)
92 // ---------------------------------------------------------------------------
97 sendCreateVideoComment,
101 // ---------------------------------------------------------------------------
103 async function sendVideoRelatedCreateActivity (options: {
108 transaction?: Transaction
110 const activityBuilder = (audience: ActivityAudience) => {
111 return buildCreateActivity(options.url, options.byActor, options.object, audience)
114 return sendVideoRelatedActivity(activityBuilder, options)