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, loggerTagsFactory } 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 const lTags = loggerTagsFactory('ap', 'create')
23 async function sendCreateVideo (video: MVideoAP, t: Transaction) {
24 if (!video.hasPrivacyForFederation()) return undefined
26 logger.info('Creating job to send video creation of %s.', video.url, lTags(video.uuid))
28 const byActor = video.VideoChannel.Account.Actor
29 const videoObject = video.toActivityPubObject()
31 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
32 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
34 return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
37 async function sendCreateCacheFile (
39 video: MVideoAccountLight,
40 fileRedundancy: MVideoRedundancyStreamingPlaylistVideo | MVideoRedundancyFileVideo
42 logger.info('Creating job to send file cache of %s.', fileRedundancy.url, lTags(video.uuid))
44 return sendVideoRelatedCreateActivity({
47 url: fileRedundancy.url,
48 object: fileRedundancy.toActivityPubObject(),
49 contextType: 'CacheFile'
53 async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, t: Transaction) {
54 if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
56 logger.info('Creating job to send create video playlist of %s.', playlist.url, lTags(playlist.uuid))
58 const byActor = playlist.OwnerAccount.Actor
59 const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
61 const object = await playlist.toActivityPubObject(null, t)
62 const createActivity = buildCreateActivity(playlist.url, byActor, object, audience)
64 const serverActor = await getServerActor()
65 const toFollowersOf = [ byActor, serverActor ]
67 if (playlist.VideoChannel) toFollowersOf.push(playlist.VideoChannel.Actor)
69 return broadcastToFollowers(createActivity, byActor, toFollowersOf, t)
72 async function sendCreateVideoComment (comment: MCommentOwnerVideo, t: Transaction) {
73 logger.info('Creating job to send comment %s.', comment.url)
75 const isOrigin = comment.Video.isOwned()
77 const byActor = comment.Account.Actor
78 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
79 const commentObject = comment.toActivityPubObject(threadParentComments)
81 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
82 // Add the actor that commented too
83 actorsInvolvedInComment.push(byActor)
85 const parentsCommentActors = threadParentComments.filter(c => !c.isDeleted())
86 .map(c => c.Account.Actor)
88 let audience: ActivityAudience
90 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
92 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
95 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
97 // This was a reply, send it to the parent actors
98 const actorsException = [ byActor ]
99 await broadcastToActors(createActivity, byActor, parentsCommentActors, t, actorsException)
101 // Broadcast to our followers
102 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
104 // Send to actors involved in the comment
105 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
108 t.afterCommit(() => unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.getSharedInbox()))
111 function buildCreateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityCreate {
112 if (!audience) audience = getAudience(byActor)
116 type: 'Create' as 'Create',
117 id: url + '/activity',
119 object: audiencify(object, audience)
125 // ---------------------------------------------------------------------------
130 sendCreateVideoComment,
131 sendCreateVideoPlaylist,
135 // ---------------------------------------------------------------------------
137 async function sendVideoRelatedCreateActivity (options: {
139 video: MVideoAccountLight
142 transaction?: Transaction
143 contextType?: ContextType
145 const activityBuilder = (audience: ActivityAudience) => {
146 return buildCreateActivity(options.url, options.byActor, options.object, audience)
149 return sendVideoRelatedActivity(activityBuilder, options)