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