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, video: VideoModel, fileRedundancy: VideoRedundancyModel) {
27 logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
29 return sendVideoRelatedCreateActivity({
32 url: fileRedundancy.url,
33 object: fileRedundancy.toActivityPubObject()
37 async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
38 logger.info('Creating job to send comment %s.', comment.url)
40 const isOrigin = comment.Video.isOwned()
42 const byActor = comment.Account.Actor
43 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
44 const commentObject = comment.toActivityPubObject(threadParentComments)
46 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
47 // Add the actor that commented too
48 actorsInvolvedInComment.push(byActor)
50 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
52 let audience: ActivityAudience
54 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
56 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
59 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
61 // This was a reply, send it to the parent actors
62 const actorsException = [ byActor ]
63 await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException)
65 // Broadcast to our followers
66 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
68 // Send to actors involved in the comment
69 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
72 return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
75 function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
76 if (!audience) audience = getAudience(byActor)
80 type: 'Create' as 'Create',
81 id: url + '/activity',
83 object: audiencify(object, audience)
89 // ---------------------------------------------------------------------------
94 sendCreateVideoComment,
98 // ---------------------------------------------------------------------------
100 async function sendVideoRelatedCreateActivity (options: {
105 transaction?: Transaction
107 const activityBuilder = (audience: ActivityAudience) => {
108 return buildCreateActivity(options.url, options.byActor, options.object, audience)
111 return sendVideoRelatedActivity(activityBuilder, options)