1 import { Transaction } from 'sequelize'
2 import { ActivityAudience, ActivityDelete } from '../../../../shared/models/activitypub'
3 import { ActorModel } from '../../../models/activitypub/actor'
4 import { VideoModel } from '../../../models/video/video'
5 import { VideoCommentModel } from '../../../models/video/video-comment'
6 import { VideoShareModel } from '../../../models/video/video-share'
7 import { getDeleteActivityPubUrl } from '../url'
8 import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
9 import { audiencify, getActorsInvolvedInVideo, getVideoCommentAudience } from '../audience'
10 import { logger } from '../../../helpers/logger'
11 import { VideoPlaylistModel } from '../../../models/video/video-playlist'
12 import { getServerActor } from '../../../helpers/utils'
14 async function sendDeleteVideo (video: VideoModel, transaction: Transaction) {
15 logger.info('Creating job to broadcast delete of video %s.', video.url)
17 const byActor = video.VideoChannel.Account.Actor
19 const activityBuilder = (audience: ActivityAudience) => {
20 const url = getDeleteActivityPubUrl(video.url)
22 return buildDeleteActivity(url, video.url, byActor, audience)
25 return sendVideoRelatedActivity(activityBuilder, { byActor, video, transaction })
28 async function sendDeleteActor (byActor: ActorModel, t: Transaction) {
29 logger.info('Creating job to broadcast delete of actor %s.', byActor.url)
31 const url = getDeleteActivityPubUrl(byActor.url)
32 const activity = buildDeleteActivity(url, byActor.url, byActor)
34 const actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, t)
36 // In case the actor did not have any videos
37 const serverActor = await getServerActor()
38 actorsInvolved.push(serverActor)
40 actorsInvolved.push(byActor)
42 return broadcastToFollowers(activity, byActor, actorsInvolved, t)
45 async function sendDeleteVideoComment (videoComment: VideoCommentModel, t: Transaction) {
46 logger.info('Creating job to send delete of comment %s.', videoComment.url)
48 const isVideoOrigin = videoComment.Video.isOwned()
50 const url = getDeleteActivityPubUrl(videoComment.url)
51 const byActor = videoComment.Account.Actor
52 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, t)
54 const actorsInvolvedInComment = await getActorsInvolvedInVideo(videoComment.Video, t)
55 actorsInvolvedInComment.push(byActor) // Add the actor that commented the video
57 const audience = getVideoCommentAudience(videoComment, threadParentComments, actorsInvolvedInComment, isVideoOrigin)
58 const activity = buildDeleteActivity(url, videoComment.url, byActor, audience)
60 // This was a reply, send it to the parent actors
61 const actorsException = [ byActor ]
62 await broadcastToActors(activity, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
64 // Broadcast to our followers
65 await broadcastToFollowers(activity, byActor, [ byActor ], t)
67 // Send to actors involved in the comment
68 if (isVideoOrigin) return broadcastToFollowers(activity, byActor, actorsInvolvedInComment, t, actorsException)
71 return unicastTo(activity, byActor, videoComment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
74 async function sendDeleteVideoPlaylist (videoPlaylist: VideoPlaylistModel, t: Transaction) {
75 logger.info('Creating job to send delete of playlist %s.', videoPlaylist.url)
77 const byActor = videoPlaylist.OwnerAccount.Actor
79 const url = getDeleteActivityPubUrl(videoPlaylist.url)
80 const activity = buildDeleteActivity(url, videoPlaylist.url, byActor)
82 const serverActor = await getServerActor()
83 const toFollowersOf = [ byActor, serverActor ]
85 if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
87 return broadcastToFollowers(activity, byActor, toFollowersOf, t)
90 // ---------------------------------------------------------------------------
95 sendDeleteVideoComment,
96 sendDeleteVideoPlaylist
99 // ---------------------------------------------------------------------------
101 function buildDeleteActivity (url: string, object: string, byActor: ActorModel, audience?: ActivityAudience): ActivityDelete {
103 type: 'Delete' as 'Delete',
109 if (audience) return audiencify(activity, audience)