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 const url = getVideoAbuseActivityPubUrl(videoAbuse)
38 logger.info('Creating job to send video abuse %s.', url)
40 const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
41 const data = createActivityData(url, byActor, videoAbuse.toActivityPubObject(), audience)
43 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
46 async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
47 logger.info('Creating job to send comment %s.', comment.url)
49 const isOrigin = comment.Video.isOwned()
51 const byActor = comment.Account.Actor
52 const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
53 const commentObject = comment.toActivityPubObject(threadParentComments)
55 const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
56 actorsInvolvedInComment.push(byActor)
58 const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
60 let audience: ActivityAudience
62 audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
64 audience = getObjectFollowersAudience(actorsInvolvedInComment.concat(parentsCommentActors))
67 const data = createActivityData(comment.url, byActor, commentObject, audience)
69 // This was a reply, send it to the parent actors
70 const actorsException = [ byActor ]
71 await broadcastToActors(data, byActor, parentsCommentActors, actorsException)
73 // Broadcast to our followers
74 await broadcastToFollowers(data, byActor, [ byActor ], t)
76 // Send to actors involved in the comment
77 if (isOrigin) return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
80 return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
83 async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) {
84 logger.info('Creating job to send view of %s.', video.url)
86 const url = getVideoViewActivityPubUrl(byActor, video)
87 const viewActivityData = createViewActivityData(byActor, video)
89 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
92 if (video.isOwned() === false) {
93 const audience = getVideoAudience(video, actorsInvolvedInVideo)
94 const data = createActivityData(url, byActor, viewActivityData, audience)
96 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
100 const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
101 const data = createActivityData(url, byActor, viewActivityData, audience)
103 // Use the server actor to send the view
104 const serverActor = await getServerActor()
105 const actorsException = [ byActor ]
106 return broadcastToFollowers(data, serverActor, actorsInvolvedInVideo, t, actorsException)
109 async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
110 logger.info('Creating job to dislike %s.', video.url)
112 const url = getVideoDislikeActivityPubUrl(byActor, video)
113 const dislikeActivityData = createDislikeActivityData(byActor, video)
115 const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
118 if (video.isOwned() === false) {
119 const audience = getVideoAudience(video, actorsInvolvedInVideo)
120 const data = createActivityData(url, byActor, dislikeActivityData, audience)
122 return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
126 const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
127 const data = createActivityData(url, byActor, dislikeActivityData, audience)
129 const actorsException = [ byActor ]
130 return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, actorsException)
133 function createActivityData (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
134 if (!audience) audience = getAudience(byActor)
138 type: 'Create' as 'Create',
139 id: url + '/activity',
141 object: audiencify(object, audience)
147 function createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
155 function createViewActivityData (byActor: ActorModel, video: VideoModel) {
163 // ---------------------------------------------------------------------------
171 createDislikeActivityData,
172 sendCreateVideoComment