Merge branch 'master' into develop
[oweals/peertube.git] / server / lib / activitypub / send / send-delete.ts
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'
13
14 async function sendDeleteVideo (video: VideoModel, transaction: Transaction) {
15   logger.info('Creating job to broadcast delete of video %s.', video.url)
16
17   const byActor = video.VideoChannel.Account.Actor
18
19   const activityBuilder = (audience: ActivityAudience) => {
20     const url = getDeleteActivityPubUrl(video.url)
21
22     return buildDeleteActivity(url, video.url, byActor, audience)
23   }
24
25   return sendVideoRelatedActivity(activityBuilder, { byActor, video, transaction })
26 }
27
28 async function sendDeleteActor (byActor: ActorModel, t: Transaction) {
29   logger.info('Creating job to broadcast delete of actor %s.', byActor.url)
30
31   const url = getDeleteActivityPubUrl(byActor.url)
32   const activity = buildDeleteActivity(url, byActor.url, byActor)
33
34   const actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, t)
35
36   // In case the actor did not have any videos
37   const serverActor = await getServerActor()
38   actorsInvolved.push(serverActor)
39
40   actorsInvolved.push(byActor)
41
42   return broadcastToFollowers(activity, byActor, actorsInvolved, t)
43 }
44
45 async function sendDeleteVideoComment (videoComment: VideoCommentModel, t: Transaction) {
46   logger.info('Creating job to send delete of comment %s.', videoComment.url)
47
48   const isVideoOrigin = videoComment.Video.isOwned()
49
50   const url = getDeleteActivityPubUrl(videoComment.url)
51   const byActor = videoComment.Account.Actor
52   const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, t)
53
54   const actorsInvolvedInComment = await getActorsInvolvedInVideo(videoComment.Video, t)
55   actorsInvolvedInComment.push(byActor) // Add the actor that commented the video
56
57   const audience = getVideoCommentAudience(videoComment, threadParentComments, actorsInvolvedInComment, isVideoOrigin)
58   const activity = buildDeleteActivity(url, videoComment.url, byActor, audience)
59
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)
63
64   // Broadcast to our followers
65   await broadcastToFollowers(activity, byActor, [ byActor ], t)
66
67   // Send to actors involved in the comment
68   if (isVideoOrigin) return broadcastToFollowers(activity, byActor, actorsInvolvedInComment, t, actorsException)
69
70   // Send to origin
71   return unicastTo(activity, byActor, videoComment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
72 }
73
74 async function sendDeleteVideoPlaylist (videoPlaylist: VideoPlaylistModel, t: Transaction) {
75   logger.info('Creating job to send delete of playlist %s.', videoPlaylist.url)
76
77   const byActor = videoPlaylist.OwnerAccount.Actor
78
79   const url = getDeleteActivityPubUrl(videoPlaylist.url)
80   const activity = buildDeleteActivity(url, videoPlaylist.url, byActor)
81
82   const serverActor = await getServerActor()
83   const toFollowersOf = [ byActor, serverActor ]
84
85   if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
86
87   return broadcastToFollowers(activity, byActor, toFollowersOf, t)
88 }
89
90 // ---------------------------------------------------------------------------
91
92 export {
93   sendDeleteVideo,
94   sendDeleteActor,
95   sendDeleteVideoComment,
96   sendDeleteVideoPlaylist
97 }
98
99 // ---------------------------------------------------------------------------
100
101 function buildDeleteActivity (url: string, object: string, byActor: ActorModel, audience?: ActivityAudience): ActivityDelete {
102   const activity = {
103     type: 'Delete' as 'Delete',
104     id: url,
105     actor: byActor.url,
106     object
107   }
108
109   if (audience) return audiencify(activity, audience)
110
111   return activity
112 }