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'
11 audiencify, broadcastToActors, broadcastToFollowers, getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience,
12 getOriginVideoAudience, getOriginVideoCommentAudience,
16 async function sendCreateVideo (video: VideoModel, t: Transaction) {
17 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
19 const byActor = video.VideoChannel.Account.Actor
20 const videoObject = video.toActivityPubObject()
22 const audience = await getAudience(byActor, t, video.privacy === VideoPrivacy.PUBLIC)
23 const data = await createActivityData(video.url, byActor, videoObject, t, audience)
25 return broadcastToFollowers(data, byActor, [ byActor ], t)
28 async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) {
29 const url = getVideoAbuseActivityPubUrl(videoAbuse)
31 const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
32 const data = await createActivityData(url, byActor, videoAbuse.toActivityPubObject(), t, audience)
34 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
37 async function sendCreateVideoCommentToOrigin (comment: VideoCommentModel, t: Transaction) {
38 const byActor = comment.Account.Actor
39 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
40 const commentObject = comment.toActivityPubObject(threadParentComments)
42 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
43 actorsInvolvedInComment.push(byActor)
44 const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment)
46 const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
48 // This was a reply, send it to the parent actors
49 const actorsException = [ byActor ]
50 await broadcastToActors(data, byActor, threadParentComments.map(c => c.Account.Actor), t, actorsException)
52 // Broadcast to our followers
53 await broadcastToFollowers(data, byActor, [ byActor ], t)
56 return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl, t)
59 async function sendCreateVideoCommentToVideoFollowers (comment: VideoCommentModel, t: Transaction) {
60 const byActor = comment.Account.Actor
61 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
62 const commentObject = comment.toActivityPubObject(threadParentComments)
64 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
65 actorsInvolvedInComment.push(byActor)
67 const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment)
68 const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
70 // This was a reply, send it to the parent actors
71 const actorsException = [ byActor ]
72 await broadcastToActors(data, byActor, threadParentComments.map(c => c.Account.Actor), t, actorsException)
74 // Broadcast to our followers
75 await broadcastToFollowers(data, byActor, [ byActor ], t)
77 // Send to actors involved in the comment
78 return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
81 async function sendCreateViewToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
82 const url = getVideoViewActivityPubUrl(byActor, video)
83 const viewActivityData = createViewActivityData(byActor, video)
85 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
86 const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
87 const data = await createActivityData(url, byActor, viewActivityData, t, audience)
89 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
92 async function sendCreateViewToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
93 const url = getVideoViewActivityPubUrl(byActor, video)
94 const viewActivityData = createViewActivityData(byActor, video)
96 const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
97 const audience = getObjectFollowersAudience(actorsToForwardView)
98 const data = await createActivityData(url, byActor, viewActivityData, t, audience)
100 // Use the server actor to send the view
101 const serverActor = await getServerActor()
102 const actorsException = [ byActor ]
103 return broadcastToFollowers(data, serverActor, actorsToForwardView, t, actorsException)
106 async function sendCreateDislikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
107 const url = getVideoDislikeActivityPubUrl(byActor, video)
108 const dislikeActivityData = createDislikeActivityData(byActor, video)
110 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
111 const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
112 const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
114 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
117 async function sendCreateDislikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
118 const url = getVideoDislikeActivityPubUrl(byActor, video)
119 const dislikeActivityData = createDislikeActivityData(byActor, video)
121 const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
122 const audience = getObjectFollowersAudience(actorsToForwardView)
123 const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
125 const actorsException = [ byActor ]
126 return broadcastToFollowers(data, byActor, actorsToForwardView, t, actorsException)
129 async function createActivityData (
134 audience?: ActivityAudience
135 ): Promise<ActivityCreate> {
137 audience = await getAudience(byActor, t)
144 object: audiencify(object, audience)
148 function createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
156 function createViewActivityData (byActor: ActorModel, video: VideoModel) {
164 // ---------------------------------------------------------------------------
170 sendCreateViewToOrigin,
171 sendCreateViewToVideoFollowers,
172 sendCreateDislikeToOrigin,
173 sendCreateDislikeToVideoFollowers,
174 createDislikeActivityData,
175 sendCreateVideoCommentToOrigin,
176 sendCreateVideoCommentToVideoFollowers