1 import { Transaction } from 'sequelize'
2 import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
3 import { VideoPrivacy } from '../../../../shared/models/videos'
4 import { getServerActor } from '../../../helpers/utils'
5 import { ActorModel } from '../../../models/activitypub/actor'
6 import { VideoModel } from '../../../models/video/video'
7 import { VideoAbuseModel } from '../../../models/video/video-abuse'
8 import { VideoCommentModel } from '../../../models/video/video-comment'
9 import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
10 import { broadcastToActors, broadcastToFollowers, unicastTo } from './utils'
13 getActorsInvolvedInVideo,
15 getObjectFollowersAudience,
17 getVideoCommentAudience
19 import { logger } from '../../../helpers/logger'
20 import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
22 async function sendCreateVideo (video: VideoModel, t: Transaction) {
23 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
25 logger.info('Creating job to send video creation of %s.', video.url)
27 const byActor = video.VideoChannel.Account.Actor
28 const videoObject = video.toActivityPubObject()
30 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
31 const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
33 return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
36 async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel) {
37 if (!video.VideoChannel.Account.Actor.serverId) return // Local
39 const url = getVideoAbuseActivityPubUrl(videoAbuse)
41 logger.info('Creating job to send video abuse %s.', url)
43 const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
44 const createActivity = buildCreateActivity(url, byActor, videoAbuse.toActivityPubObject(), audience)
46 return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
49 async function sendCreateCacheFile (byActor: ActorModel, fileRedundancy: VideoRedundancyModel) {
50 logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
52 const redundancyObject = fileRedundancy.toActivityPubObject()
54 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(fileRedundancy.VideoFile.Video.id)
55 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, undefined)
57 const audience = getVideoAudience(video, actorsInvolvedInVideo)
58 const createActivity = buildCreateActivity(fileRedundancy.url, byActor, redundancyObject, audience)
60 return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
63 async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
64 logger.info('Creating job to send comment %s.', comment.url)
66 const isOrigin = comment.Video.isOwned()
68 const byActor = comment.Account.Actor
69 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
70 const commentObject = comment.toActivityPubObject(threadParentComments)
72 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
73 actorsInvolvedInComment.push(byActor)
75 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
77 let audience: ActivityAudience
79 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
81 audience = getObjectFollowersAudience(actorsInvolvedInComment.concat(parentsCommentActors))
84 const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
86 // This was a reply, send it to the parent actors
87 const actorsException = [ byActor ]
88 await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException)
90 // Broadcast to our followers
91 await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
93 // Send to actors involved in the comment
94 if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
97 return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
100 async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) {
101 logger.info('Creating job to send view of %s.', video.url)
103 const url = getVideoViewActivityPubUrl(byActor, video)
104 const viewActivity = buildViewActivity(byActor, video)
106 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
109 if (video.isOwned() === false) {
110 const audience = getVideoAudience(video, actorsInvolvedInVideo)
111 const createActivity = buildCreateActivity(url, byActor, viewActivity, audience)
113 return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
117 const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
118 const createActivity = buildCreateActivity(url, byActor, viewActivity, audience)
120 // Use the server actor to send the view
121 const serverActor = await getServerActor()
122 const actorsException = [ byActor ]
123 return broadcastToFollowers(createActivity, serverActor, actorsInvolvedInVideo, t, actorsException)
126 async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
127 logger.info('Creating job to dislike %s.', video.url)
129 const url = getVideoDislikeActivityPubUrl(byActor, video)
130 const dislikeActivity = buildDislikeActivity(byActor, video)
132 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
135 if (video.isOwned() === false) {
136 const audience = getVideoAudience(video, actorsInvolvedInVideo)
137 const createActivity = buildCreateActivity(url, byActor, dislikeActivity, audience)
139 return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
143 const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
144 const createActivity = buildCreateActivity(url, byActor, dislikeActivity, audience)
146 const actorsException = [ byActor ]
147 return broadcastToFollowers(createActivity, byActor, actorsInvolvedInVideo, t, actorsException)
150 function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
151 if (!audience) audience = getAudience(byActor)
155 type: 'Create' as 'Create',
156 id: url + '/activity',
158 object: audiencify(object, audience)
164 function buildDislikeActivity (byActor: ActorModel, video: VideoModel) {
172 function buildViewActivity (byActor: ActorModel, video: VideoModel) {
180 // ---------------------------------------------------------------------------
188 buildDislikeActivity,
189 sendCreateVideoComment,