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