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'
11 import { VideoPlaylistModel } from '../../../models/video/video-playlist'
12 import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
13 import { getServerActor } from '../../../helpers/utils'
14 import * as Bluebird from 'bluebird'
16 async function sendCreateVideo (video: VideoModel, t: Transaction) {
17 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
19 logger.info('Creating job to send video creation of %s.', video.url)
21 const byActor = video.VideoChannel.Account.Actor
22 const videoObject = video.toActivityPubObject()
24 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
25 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
27 return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
30 async function sendCreateCacheFile (byActor: ActorModel, video: VideoModel, fileRedundancy: VideoRedundancyModel) {
31 logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
33 return sendVideoRelatedCreateActivity({
36 url: fileRedundancy.url,
37 object: fileRedundancy.toActivityPubObject()
41 async function sendCreateVideoPlaylist (playlist: VideoPlaylistModel, t: Transaction) {
42 if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
44 logger.info('Creating job to send create video playlist of %s.', playlist.url)
46 const byActor = playlist.OwnerAccount.Actor
47 const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
49 const object = await playlist.toActivityPubObject(null, t)
50 const createActivity = buildCreateActivity(playlist.url, byActor, object, audience)
52 const serverActor = await getServerActor()
53 const toFollowersOf = [ byActor, serverActor ]
55 if (playlist.VideoChannel) toFollowersOf.push(playlist.VideoChannel.Actor)
57 return broadcastToFollowers(createActivity, byActor, toFollowersOf, t)
60 async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
61 logger.info('Creating job to send comment %s.', comment.url)
63 const isOrigin = comment.Video.isOwned()
65 const byActor = comment.Account.Actor
66 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
67 const commentObject = comment.toActivityPubObject(threadParentComments)
69 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
70 // Add the actor that commented too
71 actorsInvolvedInComment.push(byActor)
73 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
75 let audience: ActivityAudience
77 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
79 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
82 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
84 // This was a reply, send it to the parent actors
85 const actorsException = [ byActor ]
86 await broadcastToActors(createActivity, byActor, parentsCommentActors, t, actorsException)
88 // Broadcast to our followers
89 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
91 // Send to actors involved in the comment
92 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
95 t.afterCommit(() => unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl))
98 function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
99 if (!audience) audience = getAudience(byActor)
103 type: 'Create' as 'Create',
104 id: url + '/activity',
106 object: audiencify(object, audience)
112 // ---------------------------------------------------------------------------
117 sendCreateVideoComment,
118 sendCreateVideoPlaylist,
122 // ---------------------------------------------------------------------------
124 async function sendVideoRelatedCreateActivity (options: {
129 transaction?: Transaction
131 const activityBuilder = (audience: ActivityAudience) => {
132 return buildCreateActivity(options.url, options.byActor, options.object, audience)
135 return sendVideoRelatedActivity(activityBuilder, options)