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
20 async function sendCreateVideo (video: VideoModel, t: Transaction) {
21 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
23 const byActor = video.VideoChannel.Account.Actor
24 const videoObject = video.toActivityPubObject()
26 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
27 const data = createActivityData(video.url, byActor, videoObject, audience)
29 return broadcastToFollowers(data, byActor, [ byActor ], t)
32 async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) {
33 const url = getVideoAbuseActivityPubUrl(videoAbuse)
35 const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
36 const data = createActivityData(url, byActor, videoAbuse.toActivityPubObject(), audience)
38 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
41 async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
42 const isOrigin = comment.Video.isOwned()
44 const byActor = comment.Account.Actor
45 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
46 const commentObject = comment.toActivityPubObject(threadParentComments)
48 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
49 actorsInvolvedInComment.push(byActor)
51 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
53 let audience: ActivityAudience
55 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
57 audience = getObjectFollowersAudience(actorsInvolvedInComment.concat(parentsCommentActors))
60 const data = createActivityData(comment.url, byActor, commentObject, audience)
62 // This was a reply, send it to the parent actors
63 const actorsException = [ byActor ]
64 await broadcastToActors(data, byActor, parentsCommentActors, actorsException)
66 // Broadcast to our followers
67 await broadcastToFollowers(data, byActor, [ byActor ], t)
69 // Send to actors involved in the comment
70 if (isOrigin) return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
73 return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
76 async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) {
77 const url = getVideoViewActivityPubUrl(byActor, video)
78 const viewActivityData = createViewActivityData(byActor, video)
80 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
83 if (video.isOwned() === false) {
84 const audience = getVideoAudience(video, actorsInvolvedInVideo)
85 const data = createActivityData(url, byActor, viewActivityData, audience)
87 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
91 const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
92 const data = createActivityData(url, byActor, viewActivityData, audience)
94 // Use the server actor to send the view
95 const serverActor = await getServerActor()
96 const actorsException = [ byActor ]
97 return broadcastToFollowers(data, serverActor, actorsInvolvedInVideo, t, actorsException)
100 async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
101 const url = getVideoDislikeActivityPubUrl(byActor, video)
102 const dislikeActivityData = createDislikeActivityData(byActor, video)
104 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
107 if (video.isOwned() === false) {
108 const audience = getVideoAudience(video, actorsInvolvedInVideo)
109 const data = createActivityData(url, byActor, dislikeActivityData, audience)
111 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
115 const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
116 const data = createActivityData(url, byActor, dislikeActivityData, audience)
118 const actorsException = [ byActor ]
119 return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, actorsException)
122 function createActivityData (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 createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
144 function createViewActivityData (byActor: ActorModel, video: VideoModel) {
152 // ---------------------------------------------------------------------------
160 createDislikeActivityData,
161 sendCreateVideoComment