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
19 import { logger } from '../../../helpers/logger'
21 async function sendCreateVideo (video: VideoModel, t: Transaction) {
22 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
24 logger.info('Creating job to send video creation of %s.', video.url)
26 const byActor = video.VideoChannel.Account.Actor
27 const videoObject = video.toActivityPubObject()
29 const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
30 const data = createActivityData(video.url, byActor, videoObject, audience)
32 return broadcastToFollowers(data, byActor, [ byActor ], t)
35 async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) {
36 if (!video.VideoChannel.Account.Actor.serverId) return // Local
38 const url = getVideoAbuseActivityPubUrl(videoAbuse)
40 logger.info('Creating job to send video abuse %s.', url)
42 const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
43 const data = createActivityData(url, byActor, videoAbuse.toActivityPubObject(), audience)
45 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
48 async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
49 logger.info('Creating job to send comment %s.', comment.url)
51 const isOrigin = comment.Video.isOwned()
53 const byActor = comment.Account.Actor
54 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
55 const commentObject = comment.toActivityPubObject(threadParentComments)
57 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
58 actorsInvolvedInComment.push(byActor)
60 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
62 let audience: ActivityAudience
64 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
66 audience = getObjectFollowersAudience(actorsInvolvedInComment.concat(parentsCommentActors))
69 const data = createActivityData(comment.url, byActor, commentObject, audience)
71 // This was a reply, send it to the parent actors
72 const actorsException = [ byActor ]
73 await broadcastToActors(data, byActor, parentsCommentActors, actorsException)
75 // Broadcast to our followers
76 await broadcastToFollowers(data, byActor, [ byActor ], t)
78 // Send to actors involved in the comment
79 if (isOrigin) return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
82 return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
85 async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) {
86 logger.info('Creating job to send view of %s.', video.url)
88 const url = getVideoViewActivityPubUrl(byActor, video)
89 const viewActivityData = createViewActivityData(byActor, video)
91 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
94 if (video.isOwned() === false) {
95 const audience = getVideoAudience(video, actorsInvolvedInVideo)
96 const data = createActivityData(url, byActor, viewActivityData, audience)
98 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
102 const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
103 const data = createActivityData(url, byActor, viewActivityData, audience)
105 // Use the server actor to send the view
106 const serverActor = await getServerActor()
107 const actorsException = [ byActor ]
108 return broadcastToFollowers(data, serverActor, actorsInvolvedInVideo, t, actorsException)
111 async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
112 logger.info('Creating job to dislike %s.', video.url)
114 const url = getVideoDislikeActivityPubUrl(byActor, video)
115 const dislikeActivityData = createDislikeActivityData(byActor, video)
117 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
120 if (video.isOwned() === false) {
121 const audience = getVideoAudience(video, actorsInvolvedInVideo)
122 const data = createActivityData(url, byActor, dislikeActivityData, audience)
124 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
128 const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
129 const data = createActivityData(url, byActor, dislikeActivityData, audience)
131 const actorsException = [ byActor ]
132 return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, actorsException)
135 function createActivityData (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
136 if (!audience) audience = getAudience(byActor)
140 type: 'Create' as 'Create',
141 id: url + '/activity',
143 object: audiencify(object, audience)
149 function createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
157 function createViewActivityData (byActor: ActorModel, video: VideoModel) {
165 // ---------------------------------------------------------------------------
173 createDislikeActivityData,
174 sendCreateVideoComment