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