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'
14 getActorsInvolvedInVideo,
16 getObjectFollowersAudience,
17 getOriginVideoAudience,
18 getOriginVideoCommentAudience,
22 async function sendCreateVideo (video: VideoModel, t: Transaction) {
23 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
25 const byActor = video.VideoChannel.Account.Actor
26 const videoObject = video.toActivityPubObject()
28 const audience = await getAudience(byActor, t, video.privacy === VideoPrivacy.PUBLIC)
29 const data = await createActivityData(video.url, byActor, videoObject, t, audience)
31 return broadcastToFollowers(data, byActor, [ byActor ], t)
34 async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) {
35 const url = getVideoAbuseActivityPubUrl(videoAbuse)
37 const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
38 const data = await createActivityData(url, byActor, videoAbuse.toActivityPubObject(), t, audience)
40 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
43 async function sendCreateVideoCommentToOrigin (comment: VideoCommentModel, t: Transaction) {
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)
50 const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment)
52 const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
54 // This was a reply, send it to the parent actors
55 const actorsException = [ byActor ]
56 await broadcastToActors(data, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
58 // Broadcast to our followers
59 await broadcastToFollowers(data, byActor, [ byActor ], t)
62 return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
65 async function sendCreateVideoCommentToVideoFollowers (comment: VideoCommentModel, t: Transaction) {
66 const byActor = comment.Account.Actor
67 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
68 const commentObject = comment.toActivityPubObject(threadParentComments)
70 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
71 actorsInvolvedInComment.push(byActor)
73 const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment)
74 const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
76 // This was a reply, send it to the parent actors
77 const actorsException = [ byActor ]
78 await broadcastToActors(data, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
80 // Broadcast to our followers
81 await broadcastToFollowers(data, byActor, [ byActor ], t)
83 // Send to actors involved in the comment
84 return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
87 async function sendCreateViewToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
88 const url = getVideoViewActivityPubUrl(byActor, video)
89 const viewActivityData = createViewActivityData(byActor, video)
91 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
92 const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
93 const data = await createActivityData(url, byActor, viewActivityData, t, audience)
95 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
98 async function sendCreateViewToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
99 const url = getVideoViewActivityPubUrl(byActor, video)
100 const viewActivityData = createViewActivityData(byActor, video)
102 const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
103 const audience = getObjectFollowersAudience(actorsToForwardView)
104 const data = await createActivityData(url, byActor, viewActivityData, t, audience)
106 // Use the server actor to send the view
107 const serverActor = await getServerActor()
108 const actorsException = [ byActor ]
109 return broadcastToFollowers(data, serverActor, actorsToForwardView, t, actorsException)
112 async function sendCreateDislikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
113 const url = getVideoDislikeActivityPubUrl(byActor, video)
114 const dislikeActivityData = createDislikeActivityData(byActor, video)
116 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
117 const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
118 const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
120 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
123 async function sendCreateDislikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
124 const url = getVideoDislikeActivityPubUrl(byActor, video)
125 const dislikeActivityData = createDislikeActivityData(byActor, video)
127 const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
128 const audience = getObjectFollowersAudience(actorsToForwardView)
129 const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
131 const actorsException = [ byActor ]
132 return broadcastToFollowers(data, byActor, actorsToForwardView, t, actorsException)
135 async function createActivityData (
140 audience?: ActivityAudience
141 ): Promise<ActivityCreate> {
143 audience = await getAudience(byActor, t)
150 object: audiencify(object, audience)
154 function createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
162 function createViewActivityData (byActor: ActorModel, video: VideoModel) {
170 // ---------------------------------------------------------------------------
176 sendCreateViewToOrigin,
177 sendCreateViewToVideoFollowers,
178 sendCreateDislikeToOrigin,
179 sendCreateDislikeToVideoFollowers,
180 createDislikeActivityData,
181 sendCreateVideoCommentToOrigin,
182 sendCreateVideoCommentToVideoFollowers