]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/send-delete.ts
Basic video redundancy implementation
[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, unicastTo } from './utils'
9 import { audiencify, getActorsInvolvedInVideo, getVideoCommentAudience } from '../audience'
10 import { logger } from '../../../helpers/logger'
11
12 async function sendDeleteVideo (video: VideoModel, t: Transaction) {
13 logger.info('Creating job to broadcast delete of video %s.', video.url)
14
15 const url = getDeleteActivityPubUrl(video.url)
16 const byActor = video.VideoChannel.Account.Actor
17
18 const activity = buildDeleteActivity(url, video.url, byActor)
19
20 const actorsInvolved = await getActorsInvolvedInVideo(video, t)
21
22 return broadcastToFollowers(activity, byActor, actorsInvolved, t)
23 }
24
25 async function sendDeleteActor (byActor: ActorModel, t: Transaction) {
26 logger.info('Creating job to broadcast delete of actor %s.', byActor.url)
27
28 const url = getDeleteActivityPubUrl(byActor.url)
29 const activity = buildDeleteActivity(url, byActor.url, byActor)
30
31 const actorsInvolved = await VideoShareModel.loadActorsByVideoOwner(byActor.id, t)
32 actorsInvolved.push(byActor)
33
34 return broadcastToFollowers(activity, byActor, actorsInvolved, t)
35 }
36
37 async function sendDeleteVideoComment (videoComment: VideoCommentModel, t: Transaction) {
38 logger.info('Creating job to send delete of comment %s.', videoComment.url)
39
40 const isVideoOrigin = videoComment.Video.isOwned()
41
42 const url = getDeleteActivityPubUrl(videoComment.url)
43 const byActor = videoComment.Account.Actor
44 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, t)
45
46 const actorsInvolvedInComment = await getActorsInvolvedInVideo(videoComment.Video, t)
47 actorsInvolvedInComment.push(byActor) // Add the actor that commented the video
48
49 const audience = getVideoCommentAudience(videoComment, threadParentComments, actorsInvolvedInComment, isVideoOrigin)
50 const activity = buildDeleteActivity(url, videoComment.url, byActor, audience)
51
52 // This was a reply, send it to the parent actors
53 const actorsException = [ byActor ]
54 await broadcastToActors(activity, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
55
56 // Broadcast to our followers
57 await broadcastToFollowers(activity, byActor, [ byActor ], t)
58
59 // Send to actors involved in the comment
60 if (isVideoOrigin) return broadcastToFollowers(activity, byActor, actorsInvolvedInComment, t, actorsException)
61
62 // Send to origin
63 return unicastTo(activity, byActor, videoComment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
64 }
65
66 // ---------------------------------------------------------------------------
67
68 export {
69 sendDeleteVideo,
70 sendDeleteActor,
71 sendDeleteVideoComment
72 }
73
74 // ---------------------------------------------------------------------------
75
76 function buildDeleteActivity (url: string, object: string, byActor: ActorModel, audience?: ActivityAudience): ActivityDelete {
77 const activity = {
78 type: 'Delete' as 'Delete',
79 id: url,
80 actor: byActor.url,
81 object
82 }
83
84 if (audience) return audiencify(activity, audience)
85
86 return activity
87 }