]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/send-delete.ts
Add playlist rest tests
[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'
418d092a
C
11import { VideoPlaylistModel } from '../../../models/video/video-playlist'
12import { getServerActor } from '../../../helpers/utils'
54141398 13
a2377d15 14async function sendDeleteVideo (video: VideoModel, transaction: Transaction) {
8e0fd45e
C
15 logger.info('Creating job to broadcast delete of video %s.', video.url)
16
50d6de9c 17 const byActor = video.VideoChannel.Account.Actor
54141398 18
a2377d15
C
19 const activityBuilder = (audience: ActivityAudience) => {
20 const url = getDeleteActivityPubUrl(video.url)
54141398 21
a2377d15
C
22 return buildDeleteActivity(url, video.url, byActor, audience)
23 }
54141398 24
a2377d15 25 return sendVideoRelatedActivity(activityBuilder, { byActor, video, transaction })
54141398
C
26}
27
50d6de9c 28async function sendDeleteActor (byActor: ActorModel, t: Transaction) {
8e0fd45e
C
29 logger.info('Creating job to broadcast delete of actor %s.', byActor.url)
30
c3badc81 31 const url = getDeleteActivityPubUrl(byActor.url)
c48e82b5 32 const activity = buildDeleteActivity(url, byActor.url, byActor)
54141398 33
df0b219d
C
34 const actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, t)
35
36 // In case the actor did not have any videos
37 const serverActor = await getServerActor()
38 actorsInvolved.push(serverActor)
39
f05a1c30
C
40 actorsInvolved.push(byActor)
41
c48e82b5 42 return broadcastToFollowers(activity, byActor, actorsInvolved, t)
54141398
C
43}
44
4cb6d457 45async function sendDeleteVideoComment (videoComment: VideoCommentModel, t: Transaction) {
8e0fd45e
C
46 logger.info('Creating job to send delete of comment %s.', videoComment.url)
47
73c08093 48 const isVideoOrigin = videoComment.Video.isOwned()
4cb6d457 49
73c08093 50 const url = getDeleteActivityPubUrl(videoComment.url)
c3badc81 51 const byActor = videoComment.Account.Actor
73c08093 52 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, t)
4cb6d457 53
73c08093 54 const actorsInvolvedInComment = await getActorsInvolvedInVideo(videoComment.Video, t)
c48e82b5 55 actorsInvolvedInComment.push(byActor) // Add the actor that commented the video
4cb6d457 56
73c08093 57 const audience = getVideoCommentAudience(videoComment, threadParentComments, actorsInvolvedInComment, isVideoOrigin)
c48e82b5 58 const activity = buildDeleteActivity(url, videoComment.url, byActor, audience)
73c08093
C
59
60 // This was a reply, send it to the parent actors
61 const actorsException = [ byActor ]
c48e82b5 62 await broadcastToActors(activity, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
73c08093
C
63
64 // Broadcast to our followers
c48e82b5 65 await broadcastToFollowers(activity, byActor, [ byActor ], t)
73c08093
C
66
67 // Send to actors involved in the comment
c48e82b5 68 if (isVideoOrigin) return broadcastToFollowers(activity, byActor, actorsInvolvedInComment, t, actorsException)
73c08093
C
69
70 // Send to origin
c48e82b5 71 return unicastTo(activity, byActor, videoComment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
4cb6d457
C
72}
73
418d092a
C
74async function sendDeleteVideoPlaylist (videoPlaylist: VideoPlaylistModel, t: Transaction) {
75 logger.info('Creating job to send delete of playlist %s.', videoPlaylist.url)
76
77 const byActor = videoPlaylist.OwnerAccount.Actor
78
79 const url = getDeleteActivityPubUrl(videoPlaylist.url)
80 const activity = buildDeleteActivity(url, videoPlaylist.url, byActor)
81
82 const serverActor = await getServerActor()
83 const toFollowersOf = [ byActor, serverActor ]
84
85 if (videoPlaylist.VideoChannel) toFollowersOf.push(videoPlaylist.VideoChannel.Actor)
86
87 return broadcastToFollowers(activity, byActor, toFollowersOf, t)
88}
89
54141398
C
90// ---------------------------------------------------------------------------
91
92export {
54141398 93 sendDeleteVideo,
4cb6d457 94 sendDeleteActor,
418d092a
C
95 sendDeleteVideoComment,
96 sendDeleteVideoPlaylist
54141398
C
97}
98
99// ---------------------------------------------------------------------------
100
c48e82b5 101function buildDeleteActivity (url: string, object: string, byActor: ActorModel, audience?: ActivityAudience): ActivityDelete {
73c08093
C
102 const activity = {
103 type: 'Delete' as 'Delete',
54141398 104 id: url,
c3badc81
C
105 actor: byActor.url,
106 object
54141398 107 }
73c08093
C
108
109 if (audience) return audiencify(activity, audience)
110
111 return activity
54141398 112}