]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-delete.ts
016811e6069e61255e751fb1ee2d6fe1138a0eaf
[github/Chocobozzz/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.loadActorsByVideoOwner(byActor.id, t)
35 actorsInvolved.push(byActor)
36
37 return broadcastToFollowers(activity, byActor, actorsInvolved, t)
38 }
39
40 async function sendDeleteVideoComment (videoComment: VideoCommentModel, t: Transaction) {
41 logger.info('Creating job to send delete of comment %s.', videoComment.url)
42
43 const isVideoOrigin = videoComment.Video.isOwned()
44
45 const url = getDeleteActivityPubUrl(videoComment.url)
46 const byActor = videoComment.Account.Actor
47 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, t)
48
49 const actorsInvolvedInComment = await getActorsInvolvedInVideo(videoComment.Video, t)
50 actorsInvolvedInComment.push(byActor) // Add the actor that commented the video
51
52 const audience = getVideoCommentAudience(videoComment, threadParentComments, actorsInvolvedInComment, isVideoOrigin)
53 const activity = buildDeleteActivity(url, videoComment.url, byActor, audience)
54
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)
58
59 // Broadcast to our followers
60 await broadcastToFollowers(activity, byActor, [ byActor ], t)
61
62 // Send to actors involved in the comment
63 if (isVideoOrigin) return broadcastToFollowers(activity, byActor, actorsInvolvedInComment, t, actorsException)
64
65 // Send to origin
66 return unicastTo(activity, byActor, videoComment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
67 }
68
69 async function sendDeleteVideoPlaylist (videoPlaylist: VideoPlaylistModel, t: Transaction) {
70 logger.info('Creating job to send delete of playlist %s.', videoPlaylist.url)
71
72 const byActor = videoPlaylist.OwnerAccount.Actor
73
74 const url = getDeleteActivityPubUrl(videoPlaylist.url)
75 const activity = buildDeleteActivity(url, videoPlaylist.url, byActor)
76
77 const serverActor = await getServerActor()
78 const toFollowersOf = [ byActor, serverActor ]
79
80 if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
81
82 return broadcastToFollowers(activity, byActor, toFollowersOf, t)
83 }
84
85 // ---------------------------------------------------------------------------
86
87 export {
88 sendDeleteVideo,
89 sendDeleteActor,
90 sendDeleteVideoComment,
91 sendDeleteVideoPlaylist
92 }
93
94 // ---------------------------------------------------------------------------
95
96 function buildDeleteActivity (url: string, object: string, byActor: ActorModel, audience?: ActivityAudience): ActivityDelete {
97 const activity = {
98 type: 'Delete' as 'Delete',
99 id: url,
100 actor: byActor.url,
101 object
102 }
103
104 if (audience) return audiencify(activity, audience)
105
106 return activity
107 }