Split types and typings
[oweals/peertube.git] / server / lib / activitypub / send / send-delete.ts
1 import { Transaction } from 'sequelize'
2 import { getServerActor } from '@server/models/application/application'
3 import { ActivityAudience, ActivityDelete } from '../../../../shared/models/activitypub'
4 import { logger } from '../../../helpers/logger'
5 import { ActorModel } from '../../../models/activitypub/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, getActorsInvolvedInVideo, getVideoCommentAudience } from '../audience'
11 import { getDeleteActivityPubUrl } from '../url'
12 import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
13
14 async function sendDeleteVideo (video: MVideoAccountLight, 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: MCommentOwnerVideo, 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.isOwned()
52     ? videoComment.Account.Actor
53     : videoComment.Video.VideoChannel.Account.Actor
54
55   const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, t)
56   const threadParentCommentsFiltered = threadParentComments.filter(c => !c.isDeleted())
57
58   const actorsInvolvedInComment = await getActorsInvolvedInVideo(videoComment.Video, t)
59   actorsInvolvedInComment.push(byActor) // Add the actor that commented the video
60
61   const audience = getVideoCommentAudience(videoComment, threadParentCommentsFiltered, actorsInvolvedInComment, isVideoOrigin)
62   const activity = buildDeleteActivity(url, videoComment.url, byActor, audience)
63
64   // This was a reply, send it to the parent actors
65   const actorsException = [ byActor ]
66   await broadcastToActors(activity, byActor, threadParentCommentsFiltered.map(c => c.Account.Actor), t, actorsException)
67
68   // Broadcast to our followers
69   await broadcastToFollowers(activity, byActor, [ byActor ], t)
70
71   // Send to actors involved in the comment
72   if (isVideoOrigin) return broadcastToFollowers(activity, byActor, actorsInvolvedInComment, t, actorsException)
73
74   // Send to origin
75   t.afterCommit(() => unicastTo(activity, byActor, videoComment.Video.VideoChannel.Account.Actor.getSharedInbox()))
76 }
77
78 async function sendDeleteVideoPlaylist (videoPlaylist: MVideoPlaylistFullSummary, t: Transaction) {
79   logger.info('Creating job to send delete of playlist %s.', videoPlaylist.url)
80
81   const byActor = videoPlaylist.OwnerAccount.Actor
82
83   const url = getDeleteActivityPubUrl(videoPlaylist.url)
84   const activity = buildDeleteActivity(url, videoPlaylist.url, byActor)
85
86   const serverActor = await getServerActor()
87   const toFollowersOf = [ byActor, serverActor ]
88
89   if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
90
91   return broadcastToFollowers(activity, byActor, toFollowersOf, t)
92 }
93
94 // ---------------------------------------------------------------------------
95
96 export {
97   sendDeleteVideo,
98   sendDeleteActor,
99   sendDeleteVideoComment,
100   sendDeleteVideoPlaylist
101 }
102
103 // ---------------------------------------------------------------------------
104
105 function buildDeleteActivity (url: string, object: string, byActor: MActorUrl, audience?: ActivityAudience): ActivityDelete {
106   const activity = {
107     type: 'Delete' as 'Delete',
108     id: url,
109     actor: byActor.url,
110     object
111   }
112
113   if (audience) return audiencify(activity, audience)
114
115   return activity
116 }