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'
9 MLocalVideoViewerWithWatchSections,
13 MVideoRedundancyFileVideo,
14 MVideoRedundancyStreamingPlaylistVideo
15 } from '../../../types/models'
16 import { audiencify, getAudience } from '../audience'
20 getActorsInvolvedInVideo,
21 getAudienceFromFollowersOf,
22 getVideoCommentAudience,
23 sendVideoActivityToOrigin,
24 sendVideoRelatedActivity,
28 const lTags = loggerTagsFactory('ap', 'create')
30 async function sendCreateVideo (video: MVideoAP, transaction: Transaction) {
31 if (!video.hasPrivacyForFederation()) return undefined
33 logger.info('Creating job to send video creation of %s.', video.url, lTags(video.uuid))
35 const byActor = video.VideoChannel.Account.Actor
36 const videoObject = video.toActivityPubObject()
38 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
39 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
41 return broadcastToFollowers({
44 toFollowersOf: [ byActor ],
50 async function sendCreateCacheFile (
52 video: MVideoAccountLight,
53 fileRedundancy: MVideoRedundancyStreamingPlaylistVideo | MVideoRedundancyFileVideo
55 logger.info('Creating job to send file cache of %s.', fileRedundancy.url, lTags(video.uuid))
57 return sendVideoRelatedCreateActivity({
60 url: fileRedundancy.url,
61 object: fileRedundancy.toActivityPubObject(),
62 contextType: 'CacheFile'
66 async function sendCreateWatchAction (stats: MLocalVideoViewerWithWatchSections, transaction: Transaction) {
67 logger.info('Creating job to send create watch action %s.', stats.url, lTags(stats.uuid))
69 const byActor = await getServerActor()
71 const activityBuilder = (audience: ActivityAudience) => {
72 return buildCreateActivity(stats.url, byActor, stats.toActivityPubObject(), audience)
75 return sendVideoActivityToOrigin(activityBuilder, { byActor, video: stats.Video, transaction, contextType: 'WatchAction' })
78 async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, transaction: Transaction) {
79 if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
81 logger.info('Creating job to send create video playlist of %s.', playlist.url, lTags(playlist.uuid))
83 const byActor = playlist.OwnerAccount.Actor
84 const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
86 const object = await playlist.toActivityPubObject(null, transaction)
87 const createActivity = buildCreateActivity(playlist.url, byActor, object, audience)
89 const serverActor = await getServerActor()
90 const toFollowersOf = [ byActor, serverActor ]
92 if (playlist.VideoChannel) toFollowersOf.push(playlist.VideoChannel.Actor)
94 return broadcastToFollowers({
99 contextType: 'Playlist'
103 async function sendCreateVideoComment (comment: MCommentOwnerVideo, transaction: Transaction) {
104 logger.info('Creating job to send comment %s.', comment.url)
106 const isOrigin = comment.Video.isOwned()
108 const byActor = comment.Account.Actor
109 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, transaction)
110 const commentObject = comment.toActivityPubObject(threadParentComments)
112 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, transaction)
113 // Add the actor that commented too
114 actorsInvolvedInComment.push(byActor)
116 const parentsCommentActors = threadParentComments.filter(c => !c.isDeleted())
117 .map(c => c.Account.Actor)
119 let audience: ActivityAudience
121 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
123 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
126 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
128 // This was a reply, send it to the parent actors
129 const actorsException = [ byActor ]
130 await broadcastToActors({
131 data: createActivity,
133 toActors: parentsCommentActors,
136 contextType: 'Comment'
139 // Broadcast to our followers
140 await broadcastToFollowers({
141 data: createActivity,
143 toFollowersOf: [ byActor ],
145 contextType: 'Comment'
148 // Send to actors involved in the comment
150 return broadcastToFollowers({
151 data: createActivity,
153 toFollowersOf: actorsInvolvedInComment,
156 contextType: 'Comment'
161 return transaction.afterCommit(() => {
163 data: createActivity,
165 toActorUrl: comment.Video.VideoChannel.Account.Actor.getSharedInbox(),
166 contextType: 'Comment'
171 function buildCreateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityCreate {
172 if (!audience) audience = getAudience(byActor)
176 type: 'Create' as 'Create',
177 id: url + '/activity',
179 object: audiencify(object, audience)
185 // ---------------------------------------------------------------------------
190 sendCreateVideoComment,
191 sendCreateVideoPlaylist,
193 sendCreateWatchAction
196 // ---------------------------------------------------------------------------
198 async function sendVideoRelatedCreateActivity (options: {
200 video: MVideoAccountLight
203 contextType: ContextType
204 transaction?: Transaction
206 const activityBuilder = (audience: ActivityAudience) => {
207 return buildCreateActivity(options.url, options.byActor, options.object, audience)
210 return sendVideoRelatedActivity(activityBuilder, options)