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.loadActorsByVideoOwner(byActor.id, t)
35 actorsInvolved.push(byActor)
37 return broadcastToFollowers(activity, byActor, actorsInvolved, t)
40 async function sendDeleteVideoComment (videoComment: VideoCommentModel, t: Transaction) {
41 logger.info('Creating job to send delete of comment %s.', videoComment.url)
43 const isVideoOrigin = videoComment.Video.isOwned()
45 const url = getDeleteActivityPubUrl(videoComment.url)
46 const byActor = videoComment.Account.Actor
47 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, t)
49 const actorsInvolvedInComment = await getActorsInvolvedInVideo(videoComment.Video, t)
50 actorsInvolvedInComment.push(byActor) // Add the actor that commented the video
52 const audience = getVideoCommentAudience(videoComment, threadParentComments, actorsInvolvedInComment, isVideoOrigin)
53 const activity = buildDeleteActivity(url, videoComment.url, byActor, audience)
55 // This was a reply, send it to the parent actors
56 const actorsException = [ byActor ]
57 await broadcastToActors(activity, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
59 // Broadcast to our followers
60 await broadcastToFollowers(activity, byActor, [ byActor ], t)
62 // Send to actors involved in the comment
63 if (isVideoOrigin) return broadcastToFollowers(activity, byActor, actorsInvolvedInComment, t, actorsException)
66 return unicastTo(activity, byActor, videoComment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
69 async function sendDeleteVideoPlaylist (videoPlaylist: VideoPlaylistModel, t: Transaction) {
70 logger.info('Creating job to send delete of playlist %s.', videoPlaylist.url)
72 const byActor = videoPlaylist.OwnerAccount.Actor
74 const url = getDeleteActivityPubUrl(videoPlaylist.url)
75 const activity = buildDeleteActivity(url, videoPlaylist.url, byActor)
77 const serverActor = await getServerActor()
78 const toFollowersOf = [ byActor, serverActor ]
80 if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
82 return broadcastToFollowers(activity, byActor, toFollowersOf, t)
85 // ---------------------------------------------------------------------------
90 sendDeleteVideoComment,
91 sendDeleteVideoPlaylist
94 // ---------------------------------------------------------------------------
96 function buildDeleteActivity (url: string, object: string, byActor: ActorModel, audience?: ActivityAudience): ActivityDelete {
98 type: 'Delete' as 'Delete',
104 if (audience) return audiencify(activity, audience)