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, 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 actorsInvolvedInVideo = await getActorsInvolvedInVideo(comment.Video, t)
43 const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInVideo)
45 const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
47 return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl, t)
50 async function sendCreateVideoCommentToVideoFollowers (comment: VideoCommentModel, t: Transaction) {
51 const byActor = comment.Account.Actor
52 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
53 const commentObject = comment.toActivityPubObject(threadParentComments)
55 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(comment.Video, t)
56 const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInVideo)
57 const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
59 const followersException = [ byActor ]
60 return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, followersException)
63 async function sendCreateViewToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
64 const url = getVideoViewActivityPubUrl(byActor, video)
65 const viewActivityData = createViewActivityData(byActor, video)
67 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
68 const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
69 const data = await createActivityData(url, byActor, viewActivityData, t, audience)
71 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
74 async function sendCreateViewToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
75 const url = getVideoViewActivityPubUrl(byActor, video)
76 const viewActivityData = createViewActivityData(byActor, video)
78 const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
79 const audience = getObjectFollowersAudience(actorsToForwardView)
80 const data = await createActivityData(url, byActor, viewActivityData, t, audience)
82 // Use the server actor to send the view
83 const serverActor = await getServerActor()
84 const followersException = [ byActor ]
85 return broadcastToFollowers(data, serverActor, actorsToForwardView, t, followersException)
88 async function sendCreateDislikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
89 const url = getVideoDislikeActivityPubUrl(byActor, video)
90 const dislikeActivityData = createDislikeActivityData(byActor, video)
92 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
93 const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
94 const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
96 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
99 async function sendCreateDislikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
100 const url = getVideoDislikeActivityPubUrl(byActor, video)
101 const dislikeActivityData = createDislikeActivityData(byActor, video)
103 const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
104 const audience = getObjectFollowersAudience(actorsToForwardView)
105 const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
107 const followersException = [ byActor ]
108 return broadcastToFollowers(data, byActor, actorsToForwardView, t, followersException)
111 async function createActivityData (
116 audience?: ActivityAudience
117 ): Promise<ActivityCreate> {
119 audience = await getAudience(byActor, t)
126 object: audiencify(object, audience)
130 function createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
138 function createViewActivityData (byActor: ActorModel, video: VideoModel) {
146 // ---------------------------------------------------------------------------
152 sendCreateViewToOrigin,
153 sendCreateViewToVideoFollowers,
154 sendCreateDislikeToOrigin,
155 sendCreateDislikeToVideoFollowers,
156 createDislikeActivityData,
157 sendCreateVideoCommentToOrigin,
158 sendCreateVideoCommentToVideoFollowers