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 { VideoAbuseModel } from '../../../models/video/video-abuse'
7 import { VideoCommentModel } from '../../../models/video/video-comment'
8 import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
9 import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
10 import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf, getVideoCommentAudience } from '../audience'
11 import { logger } from '../../../helpers/logger'
12 import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
14 async function sendCreateVideo (video: VideoModel, t: Transaction) {
15 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
17 logger.info('Creating job to send video creation of %s.', video.url)
19 const byActor = video.VideoChannel.Account.Actor
20 const videoObject = video.toActivityPubObject()
22 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
23 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
25 return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
28 async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel) {
29 if (!video.VideoChannel.Account.Actor.serverId) return // Local
31 const url = getVideoAbuseActivityPubUrl(videoAbuse)
33 logger.info('Creating job to send video abuse %s.', url)
35 // Custom audience, we only send the abuse to the origin instance
36 const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
37 const createActivity = buildCreateActivity(url, byActor, videoAbuse.toActivityPubObject(), audience)
39 return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
42 async function sendCreateCacheFile (byActor: ActorModel, fileRedundancy: VideoRedundancyModel) {
43 logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
45 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(fileRedundancy.VideoFile.Video.id)
46 const redundancyObject = fileRedundancy.toActivityPubObject()
48 return sendVideoRelatedCreateActivity({
51 url: fileRedundancy.url,
52 object: redundancyObject
56 async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
57 logger.info('Creating job to send comment %s.', comment.url)
59 const isOrigin = comment.Video.isOwned()
61 const byActor = comment.Account.Actor
62 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
63 const commentObject = comment.toActivityPubObject(threadParentComments)
65 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
66 // Add the actor that commented too
67 actorsInvolvedInComment.push(byActor)
69 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
71 let audience: ActivityAudience
73 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
75 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
78 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
80 // This was a reply, send it to the parent actors
81 const actorsException = [ byActor ]
82 await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException)
84 // Broadcast to our followers
85 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
87 // Send to actors involved in the comment
88 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
91 return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
94 async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) {
95 logger.info('Creating job to send view of %s.', video.url)
97 const url = getVideoViewActivityPubUrl(byActor, video)
98 const viewActivity = buildViewActivity(url, byActor, video)
100 return sendVideoRelatedCreateActivity({
101 // Use the server actor to send the view
105 object: viewActivity,
110 async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
111 logger.info('Creating job to dislike %s.', video.url)
113 const url = getVideoDislikeActivityPubUrl(byActor, video)
114 const dislikeActivity = buildDislikeActivity(url, byActor, video)
116 return sendVideoRelatedCreateActivity({
120 object: dislikeActivity,
125 function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
126 if (!audience) audience = getAudience(byActor)
130 type: 'Create' as 'Create',
131 id: url + '/activity',
133 object: audiencify(object, audience)
139 function buildDislikeActivity (url: string, byActor: ActorModel, video: VideoModel) {
148 function buildViewActivity (url: string, byActor: ActorModel, video: VideoModel) {
157 // ---------------------------------------------------------------------------
165 buildDislikeActivity,
166 sendCreateVideoComment,
170 // ---------------------------------------------------------------------------
172 async function sendVideoRelatedCreateActivity (options: {
177 transaction?: Transaction
179 const activityBuilder = (audience: ActivityAudience) => {
180 return buildCreateActivity(options.url, options.byActor, options.object, audience)
183 return sendVideoRelatedActivity(activityBuilder, options)