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