1 import { Transaction } from 'sequelize'
2 import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
3 import { Video, 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, video: VideoModel, fileRedundancy: VideoRedundancyModel) {
43 logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
45 return sendVideoRelatedCreateActivity({
48 url: fileRedundancy.url,
49 object: fileRedundancy.toActivityPubObject()
53 async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
54 logger.info('Creating job to send comment %s.', comment.url)
56 const isOrigin = comment.Video.isOwned()
58 const byActor = comment.Account.Actor
59 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
60 const commentObject = comment.toActivityPubObject(threadParentComments)
62 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
63 // Add the actor that commented too
64 actorsInvolvedInComment.push(byActor)
66 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
68 let audience: ActivityAudience
70 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
72 audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
75 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
77 // This was a reply, send it to the parent actors
78 const actorsException = [ byActor ]
79 await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException)
81 // Broadcast to our followers
82 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
84 // Send to actors involved in the comment
85 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
88 return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
91 async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) {
92 logger.info('Creating job to send view of %s.', video.url)
94 const url = getVideoViewActivityPubUrl(byActor, video)
95 const viewActivity = buildViewActivity(url, byActor, video)
97 return sendVideoRelatedCreateActivity({
98 // Use the server actor to send the view
102 object: viewActivity,
107 async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
108 logger.info('Creating job to dislike %s.', video.url)
110 const url = getVideoDislikeActivityPubUrl(byActor, video)
111 const dislikeActivity = buildDislikeActivity(url, byActor, video)
113 return sendVideoRelatedCreateActivity({
117 object: dislikeActivity,
122 function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
123 if (!audience) audience = getAudience(byActor)
127 type: 'Create' as 'Create',
128 id: url + '/activity',
130 object: audiencify(object, audience)
136 function buildDislikeActivity (url: string, byActor: ActorModel, video: VideoModel) {
145 function buildViewActivity (url: string, byActor: ActorModel, video: VideoModel) {
154 // ---------------------------------------------------------------------------
162 buildDislikeActivity,
163 sendCreateVideoComment,
167 // ---------------------------------------------------------------------------
169 async function sendVideoRelatedCreateActivity (options: {
174 transaction?: Transaction
176 const activityBuilder = (audience: ActivityAudience) => {
177 return buildCreateActivity(options.url, options.byActor, options.object, audience)
180 return sendVideoRelatedActivity(activityBuilder, options)