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 getVideoCommentAudience,
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 sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
44 const isOrigin = comment.Video.isOwned()
46 const byActor = comment.Account.Actor
47 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
48 const commentObject = comment.toActivityPubObject(threadParentComments)
50 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
51 actorsInvolvedInComment.push(byActor)
53 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
55 let audience: ActivityAudience
57 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
59 audience = getObjectFollowersAudience(actorsInvolvedInComment.concat(parentsCommentActors))
62 const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
64 // This was a reply, send it to the parent actors
65 const actorsException = [ byActor ]
66 await broadcastToActors(data, byActor, parentsCommentActors, actorsException)
68 // Broadcast to our followers
69 await broadcastToFollowers(data, byActor, [ byActor ], t)
71 // Send to actors involved in the comment
72 if (isOrigin) return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
75 return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
78 async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) {
79 const url = getVideoViewActivityPubUrl(byActor, video)
80 const viewActivityData = createViewActivityData(byActor, video)
82 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
85 if (video.isOwned() === false) {
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)
93 const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
94 const data = await createActivityData(url, byActor, viewActivityData, t, audience)
96 // Use the server actor to send the view
97 const serverActor = await getServerActor()
98 const actorsException = [ byActor ]
99 return broadcastToFollowers(data, serverActor, actorsInvolvedInVideo, t, actorsException)
102 async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
103 const url = getVideoDislikeActivityPubUrl(byActor, video)
104 const dislikeActivityData = createDislikeActivityData(byActor, video)
106 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
109 if (video.isOwned() === false) {
110 const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
111 const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
113 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
117 const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
118 const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
120 const actorsException = [ byActor ]
121 return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, actorsException)
124 async function createActivityData (url: string,
128 audience?: ActivityAudience): Promise<ActivityCreate> {
130 audience = await getAudience(byActor, t)
134 type: 'Create' as 'Create',
135 id: url + '/activity',
137 object: audiencify(object, audience)
141 function createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
149 function createViewActivityData (byActor: ActorModel, video: VideoModel) {
157 // ---------------------------------------------------------------------------
165 createDislikeActivityData,
166 sendCreateVideoComment