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, t: 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(createActivity, byActor, [ byActor ], t)
42 async function sendCreateCacheFile (
44 video: MVideoAccountLight,
45 fileRedundancy: MVideoRedundancyStreamingPlaylistVideo | MVideoRedundancyFileVideo
47 logger.info('Creating job to send file cache of %s.', fileRedundancy.url, lTags(video.uuid))
49 return sendVideoRelatedCreateActivity({
52 url: fileRedundancy.url,
53 object: fileRedundancy.toActivityPubObject(),
54 contextType: 'CacheFile'
58 async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, t: Transaction) {
59 if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
61 logger.info('Creating job to send create video playlist of %s.', playlist.url, lTags(playlist.uuid))
63 const byActor = playlist.OwnerAccount.Actor
64 const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
66 const object = await playlist.toActivityPubObject(null, t)
67 const createActivity = buildCreateActivity(playlist.url, byActor, object, audience)
69 const serverActor = await getServerActor()
70 const toFollowersOf = [ byActor, serverActor ]
72 if (playlist.VideoChannel) toFollowersOf.push(playlist.VideoChannel.Actor)
74 return broadcastToFollowers(createActivity, byActor, toFollowersOf, t)
77 async function sendCreateVideoComment (comment: MCommentOwnerVideo, t: Transaction) {
78 logger.info('Creating job to send comment %s.', comment.url)
80 const isOrigin = comment.Video.isOwned()
82 const byActor = comment.Account.Actor
83 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
84 const commentObject = comment.toActivityPubObject(threadParentComments)
86 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
87 // Add the actor that commented too
88 actorsInvolvedInComment.push(byActor)
90 const parentsCommentActors = threadParentComments.filter(c => !c.isDeleted())
91 .map(c => c.Account.Actor)
93 let audience: ActivityAudience
95 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
97 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
100 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
102 // This was a reply, send it to the parent actors
103 const actorsException = [ byActor ]
104 await broadcastToActors(createActivity, byActor, parentsCommentActors, t, actorsException)
106 // Broadcast to our followers
107 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
109 // Send to actors involved in the comment
110 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
113 t.afterCommit(() => unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.getSharedInbox()))
116 function buildCreateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityCreate {
117 if (!audience) audience = getAudience(byActor)
121 type: 'Create' as 'Create',
122 id: url + '/activity',
124 object: audiencify(object, audience)
130 // ---------------------------------------------------------------------------
135 sendCreateVideoComment,
136 sendCreateVideoPlaylist,
140 // ---------------------------------------------------------------------------
142 async function sendVideoRelatedCreateActivity (options: {
144 video: MVideoAccountLight
147 transaction?: Transaction
148 contextType?: ContextType
150 const activityBuilder = (audience: ActivityAudience) => {
151 return buildCreateActivity(options.url, options.byActor, options.object, audience)
154 return sendVideoRelatedActivity(activityBuilder, options)