1 import { Transaction } from 'sequelize'
2 import { getServerActor } from '@server/models/application/application'
3 import { ActivityAudience, ActivityDelete } from '@shared/models'
4 import { logger } from '../../../helpers/logger'
5 import { ActorModel } from '../../../models/actor/actor'
6 import { VideoCommentModel } from '../../../models/video/video-comment'
7 import { VideoShareModel } from '../../../models/video/video-share'
8 import { MActorUrl } from '../../../types/models'
9 import { MCommentOwnerVideo, MVideoAccountLight, MVideoPlaylistFullSummary } from '../../../types/models/video'
10 import { audiencify } from '../audience'
11 import { getDeleteActivityPubUrl } from '../url'
12 import { getActorsInvolvedInVideo, getVideoCommentAudience } from './shared'
13 import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './shared/send-utils'
15 async function sendDeleteVideo (video: MVideoAccountLight, transaction: Transaction) {
16 logger.info('Creating job to broadcast delete of video %s.', video.url)
18 const byActor = video.VideoChannel.Account.Actor
20 const activityBuilder = (audience: ActivityAudience) => {
21 const url = getDeleteActivityPubUrl(video.url)
23 return buildDeleteActivity(url, video.url, byActor, audience)
26 return sendVideoRelatedActivity(activityBuilder, { byActor, video, contextType: 'Delete', transaction })
29 async function sendDeleteActor (byActor: ActorModel, transaction: Transaction) {
30 logger.info('Creating job to broadcast delete of actor %s.', byActor.url)
32 const url = getDeleteActivityPubUrl(byActor.url)
33 const activity = buildDeleteActivity(url, byActor.url, byActor)
35 const actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, transaction)
37 // In case the actor did not have any videos
38 const serverActor = await getServerActor()
39 actorsInvolved.push(serverActor)
41 actorsInvolved.push(byActor)
43 return broadcastToFollowers({
46 toFollowersOf: actorsInvolved,
47 contextType: 'Delete',
52 async function sendDeleteVideoComment (videoComment: MCommentOwnerVideo, transaction: Transaction) {
53 logger.info('Creating job to send delete of comment %s.', videoComment.url)
55 const isVideoOrigin = videoComment.Video.isOwned()
57 const url = getDeleteActivityPubUrl(videoComment.url)
58 const byActor = videoComment.isOwned()
59 ? videoComment.Account.Actor
60 : videoComment.Video.VideoChannel.Account.Actor
62 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, transaction)
63 const threadParentCommentsFiltered = threadParentComments.filter(c => !c.isDeleted())
65 const actorsInvolvedInComment = await getActorsInvolvedInVideo(videoComment.Video, transaction)
66 actorsInvolvedInComment.push(byActor) // Add the actor that commented the video
68 const audience = getVideoCommentAudience(videoComment, threadParentCommentsFiltered, actorsInvolvedInComment, isVideoOrigin)
69 const activity = buildDeleteActivity(url, videoComment.url, byActor, audience)
71 // This was a reply, send it to the parent actors
72 const actorsException = [ byActor ]
73 await broadcastToActors({
76 toActors: threadParentCommentsFiltered.map(c => c.Account.Actor),
78 contextType: 'Delete',
82 // Broadcast to our followers
83 await broadcastToFollowers({
86 toFollowersOf: [ byActor ],
87 contextType: 'Delete',
91 // Send to actors involved in the comment
93 return broadcastToFollowers({
96 toFollowersOf: actorsInvolvedInComment,
98 contextType: 'Delete',
104 return transaction.afterCommit(() => {
108 toActorUrl: videoComment.Video.VideoChannel.Account.Actor.getSharedInbox(),
109 contextType: 'Delete'
114 async function sendDeleteVideoPlaylist (videoPlaylist: MVideoPlaylistFullSummary, transaction: Transaction) {
115 logger.info('Creating job to send delete of playlist %s.', videoPlaylist.url)
117 const byActor = videoPlaylist.OwnerAccount.Actor
119 const url = getDeleteActivityPubUrl(videoPlaylist.url)
120 const activity = buildDeleteActivity(url, videoPlaylist.url, byActor)
122 const serverActor = await getServerActor()
123 const toFollowersOf = [ byActor, serverActor ]
125 if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
127 return broadcastToFollowers({
131 contextType: 'Delete',
136 // ---------------------------------------------------------------------------
141 sendDeleteVideoComment,
142 sendDeleteVideoPlaylist
145 // ---------------------------------------------------------------------------
147 function buildDeleteActivity (url: string, object: string, byActor: MActorUrl, audience?: ActivityAudience): ActivityDelete {
149 type: 'Delete' as 'Delete',
155 if (audience) return audiencify(activity, audience)