]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/send-delete.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-delete.ts
CommitLineData
54141398 1import { Transaction } from 'sequelize'
444c0a0e 2import { getServerActor } from '@server/models/application/application'
57e4e1c1 3import { ActivityAudience, ActivityDelete } from '@shared/models'
444c0a0e 4import { logger } from '../../../helpers/logger'
7d9ba5c0 5import { ActorModel } from '../../../models/actor/actor'
4cb6d457 6import { VideoCommentModel } from '../../../models/video/video-comment'
3fd3ab2d 7import { VideoShareModel } from '../../../models/video/video-share'
26d6bf65
C
8import { MActorUrl } from '../../../types/models'
9import { MCommentOwnerVideo, MVideoAccountLight, MVideoPlaylistFullSummary } from '../../../types/models/video'
57e4e1c1 10import { audiencify } from '../audience'
c3badc81 11import { getDeleteActivityPubUrl } from '../url'
57e4e1c1
C
12import { getActorsInvolvedInVideo, getVideoCommentAudience } from './shared'
13import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './shared/send-utils'
54141398 14
453e83ea 15async function sendDeleteVideo (video: MVideoAccountLight, transaction: Transaction) {
8e0fd45e
C
16 logger.info('Creating job to broadcast delete of video %s.', video.url)
17
50d6de9c 18 const byActor = video.VideoChannel.Account.Actor
54141398 19
a2377d15
C
20 const activityBuilder = (audience: ActivityAudience) => {
21 const url = getDeleteActivityPubUrl(video.url)
54141398 22
a2377d15
C
23 return buildDeleteActivity(url, video.url, byActor, audience)
24 }
54141398 25
a219c910 26 return sendVideoRelatedActivity(activityBuilder, { byActor, video, contextType: 'Delete', transaction })
54141398
C
27}
28
a219c910 29async function sendDeleteActor (byActor: ActorModel, transaction: Transaction) {
8e0fd45e
C
30 logger.info('Creating job to broadcast delete of actor %s.', byActor.url)
31
c3badc81 32 const url = getDeleteActivityPubUrl(byActor.url)
c48e82b5 33 const activity = buildDeleteActivity(url, byActor.url, byActor)
54141398 34
a219c910 35 const actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, transaction)
df0b219d
C
36
37 // In case the actor did not have any videos
38 const serverActor = await getServerActor()
39 actorsInvolved.push(serverActor)
40
f05a1c30
C
41 actorsInvolved.push(byActor)
42
a219c910
C
43 return broadcastToFollowers({
44 data: activity,
45 byActor,
46 toFollowersOf: actorsInvolved,
47 contextType: 'Delete',
48 transaction
49 })
54141398
C
50}
51
a219c910 52async function sendDeleteVideoComment (videoComment: MCommentOwnerVideo, transaction: Transaction) {
8e0fd45e
C
53 logger.info('Creating job to send delete of comment %s.', videoComment.url)
54
73c08093 55 const isVideoOrigin = videoComment.Video.isOwned()
4cb6d457 56
73c08093 57 const url = getDeleteActivityPubUrl(videoComment.url)
511765c9
C
58 const byActor = videoComment.isOwned()
59 ? videoComment.Account.Actor
60 : videoComment.Video.VideoChannel.Account.Actor
61
a219c910 62 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, transaction)
c883db6d 63 const threadParentCommentsFiltered = threadParentComments.filter(c => !c.isDeleted())
4cb6d457 64
a219c910 65 const actorsInvolvedInComment = await getActorsInvolvedInVideo(videoComment.Video, transaction)
c48e82b5 66 actorsInvolvedInComment.push(byActor) // Add the actor that commented the video
4cb6d457 67
c883db6d 68 const audience = getVideoCommentAudience(videoComment, threadParentCommentsFiltered, actorsInvolvedInComment, isVideoOrigin)
c48e82b5 69 const activity = buildDeleteActivity(url, videoComment.url, byActor, audience)
73c08093
C
70
71 // This was a reply, send it to the parent actors
72 const actorsException = [ byActor ]
a219c910
C
73 await broadcastToActors({
74 data: activity,
75 byActor,
76 toActors: threadParentCommentsFiltered.map(c => c.Account.Actor),
77 transaction,
78 contextType: 'Delete',
79 actorsException
80 })
73c08093
C
81
82 // Broadcast to our followers
a219c910
C
83 await broadcastToFollowers({
84 data: activity,
85 byActor,
86 toFollowersOf: [ byActor ],
87 contextType: 'Delete',
88 transaction
89 })
73c08093
C
90
91 // Send to actors involved in the comment
a219c910
C
92 if (isVideoOrigin) {
93 return broadcastToFollowers({
94 data: activity,
95 byActor,
96 toFollowersOf: actorsInvolvedInComment,
97 transaction,
98 contextType: 'Delete',
99 actorsException
100 })
101 }
73c08093
C
102
103 // Send to origin
a219c910
C
104 return transaction.afterCommit(() => {
105 return unicastTo({
106 data: activity,
107 byActor,
108 toActorUrl: videoComment.Video.VideoChannel.Account.Actor.getSharedInbox(),
109 contextType: 'Delete'
110 })
111 })
4cb6d457
C
112}
113
a219c910 114async function sendDeleteVideoPlaylist (videoPlaylist: MVideoPlaylistFullSummary, transaction: Transaction) {
418d092a
C
115 logger.info('Creating job to send delete of playlist %s.', videoPlaylist.url)
116
117 const byActor = videoPlaylist.OwnerAccount.Actor
118
119 const url = getDeleteActivityPubUrl(videoPlaylist.url)
120 const activity = buildDeleteActivity(url, videoPlaylist.url, byActor)
121
122 const serverActor = await getServerActor()
123 const toFollowersOf = [ byActor, serverActor ]
124
125 if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
126
a219c910
C
127 return broadcastToFollowers({
128 data: activity,
129 byActor,
130 toFollowersOf,
131 contextType: 'Delete',
132 transaction
133 })
418d092a
C
134}
135
54141398
C
136// ---------------------------------------------------------------------------
137
138export {
54141398 139 sendDeleteVideo,
4cb6d457 140 sendDeleteActor,
418d092a
C
141 sendDeleteVideoComment,
142 sendDeleteVideoPlaylist
54141398
C
143}
144
145// ---------------------------------------------------------------------------
146
453e83ea 147function buildDeleteActivity (url: string, object: string, byActor: MActorUrl, audience?: ActivityAudience): ActivityDelete {
73c08093
C
148 const activity = {
149 type: 'Delete' as 'Delete',
54141398 150 id: url,
c3badc81
C
151 actor: byActor.url,
152 object
54141398 153 }
73c08093
C
154
155 if (audience) return audiencify(activity, audience)
156
157 return activity
54141398 158}