]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-delete.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-delete.ts
CommitLineData
3fd3ab2d 1import { ActivityDelete } from '../../../../shared/models/activitypub'
da854ddd
C
2import { retryTransactionWrapper } from '../../../helpers/database-utils'
3import { logger } from '../../../helpers/logger'
80fdaf06 4import { sequelizeTypescript } from '../../../initializers/database'
7d9ba5c0 5import { ActorModel } from '../../../models/actor/actor'
3fd3ab2d 6import { VideoModel } from '../../../models/video/video'
4cb6d457 7import { VideoCommentModel } from '../../../models/video/video-comment'
df0b219d 8import { VideoPlaylistModel } from '../../../models/video/video-playlist'
26d6bf65 9import { APProcessorOptions } from '../../../types/activitypub-processor.model'
c24822a8
C
10import {
11 MAccountActor,
12 MActor,
13 MActorFull,
14 MActorSignature,
15 MChannelAccountActor,
16 MChannelActor,
17 MCommentOwnerVideo
18} from '../../../types/models'
57e4e1c1 19import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
1198edf4
C
20
21async function processDeleteActivity (options: APProcessorOptions<ActivityDelete>) {
22 const { activity, byActor } = options
7a7724e6 23
2890b615 24 const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
7a7724e6 25
f05a1c30 26 if (activity.actor === objectUrl) {
e587e0ec
C
27 // We need more attributes (all the account and channel)
28 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
f05a1c30 29
e587e0ec
C
30 if (byActorFull.type === 'Person') {
31 if (!byActorFull.Account) throw new Error('Actor ' + byActorFull.url + ' is a person but we cannot find it in database.')
7a7724e6 32
453e83ea
C
33 const accountToDelete = byActorFull.Account as MAccountActor
34 accountToDelete.Actor = byActorFull
35
36 return retryTransactionWrapper(processDeleteAccount, accountToDelete)
e587e0ec
C
37 } else if (byActorFull.type === 'Group') {
38 if (!byActorFull.VideoChannel) throw new Error('Actor ' + byActorFull.url + ' is a group but we cannot find it in database.')
50d6de9c 39
c24822a8
C
40 const channelToDelete = byActorFull.VideoChannel as MChannelAccountActor & { Actor: MActorFull }
41 channelToDelete.Actor = byActorFull
453e83ea 42 return retryTransactionWrapper(processDeleteVideoChannel, channelToDelete)
7a7724e6
C
43 }
44 }
45
46 {
511765c9 47 const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(objectUrl)
4cb6d457 48 if (videoCommentInstance) {
e587e0ec 49 return retryTransactionWrapper(processDeleteVideoComment, byActor, videoCommentInstance, activity)
4cb6d457
C
50 }
51 }
52
53 {
2890b615 54 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
4cb6d457 55 if (videoInstance) {
a2377d15
C
56 if (videoInstance.isOwned()) throw new Error(`Remote instance cannot delete owned video ${videoInstance.url}.`)
57
e587e0ec 58 return retryTransactionWrapper(processDeleteVideo, byActor, videoInstance)
7a7724e6
C
59 }
60 }
61
df0b219d
C
62 {
63 const videoPlaylist = await VideoPlaylistModel.loadByUrlAndPopulateAccount(objectUrl)
64 if (videoPlaylist) {
65 if (videoPlaylist.isOwned()) throw new Error(`Remote instance cannot delete owned playlist ${videoPlaylist.url}.`)
66
67 return retryTransactionWrapper(processDeleteVideoPlaylist, byActor, videoPlaylist)
68 }
69 }
70
bcec136e 71 return undefined
7a7724e6
C
72}
73
74// ---------------------------------------------------------------------------
75
76export {
77 processDeleteActivity
78}
79
80// ---------------------------------------------------------------------------
81
453e83ea 82async function processDeleteVideo (actor: MActor, videoToDelete: VideoModel) {
7a7724e6
C
83 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
84
3fd3ab2d 85 await sequelizeTypescript.transaction(async t => {
50d6de9c
C
86 if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
87 throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
7a7724e6
C
88 }
89
90 await videoToDelete.destroy({ transaction: t })
91 })
92
93 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
94}
95
453e83ea 96async function processDeleteVideoPlaylist (actor: MActor, playlistToDelete: VideoPlaylistModel) {
df0b219d
C
97 logger.debug('Removing remote video playlist "%s".', playlistToDelete.uuid)
98
99 await sequelizeTypescript.transaction(async t => {
100 if (playlistToDelete.OwnerAccount.Actor.id !== actor.id) {
101 throw new Error('Account ' + actor.url + ' does not own video playlist ' + playlistToDelete.url)
102 }
103
104 await playlistToDelete.destroy({ transaction: t })
105 })
106
107 logger.info('Remote video playlist with uuid %s removed.', playlistToDelete.uuid)
108}
109
453e83ea 110async function processDeleteAccount (accountToRemove: MAccountActor) {
57cfff78 111 logger.debug('Removing remote account "%s".', accountToRemove.Actor.url)
7a7724e6 112
3fd3ab2d 113 await sequelizeTypescript.transaction(async t => {
50d6de9c 114 await accountToRemove.destroy({ transaction: t })
7a7724e6
C
115 })
116
57cfff78 117 logger.info('Remote account %s removed.', accountToRemove.Actor.url)
7a7724e6
C
118}
119
453e83ea 120async function processDeleteVideoChannel (videoChannelToRemove: MChannelActor) {
57cfff78 121 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.url)
7a7724e6 122
3fd3ab2d 123 await sequelizeTypescript.transaction(async t => {
50d6de9c 124 await videoChannelToRemove.destroy({ transaction: t })
7a7724e6
C
125 })
126
57cfff78 127 logger.info('Remote video channel %s removed.', videoChannelToRemove.Actor.url)
7a7724e6 128}
4cb6d457 129
0d8de275
C
130function processDeleteVideoComment (byActor: MActorSignature, videoComment: MCommentOwnerVideo, activity: ActivityDelete) {
131 // Already deleted
9cc4b9c6 132 if (videoComment.isDeleted()) return Promise.resolve()
0d8de275 133
4cb6d457
C
134 logger.debug('Removing remote video comment "%s".', videoComment.url)
135
136 return sequelizeTypescript.transaction(async t => {
511765c9
C
137 if (byActor.Account.id !== videoComment.Account.id && byActor.Account.id !== videoComment.Video.VideoChannel.accountId) {
138 throw new Error(`Account ${byActor.url} does not own video comment ${videoComment.url} or video ${videoComment.Video.url}`)
12ba460e
C
139 }
140
eae0365b 141 videoComment.markAsDeleted()
69222afa 142
eae0365b 143 await videoComment.save({ transaction: t })
4cb6d457 144
73c08093
C
145 if (videoComment.Video.isOwned()) {
146 // Don't resend the activity to the sender
147 const exceptions = [ byActor ]
659edaa6 148 await forwardVideoRelatedActivity(activity, t, exceptions, videoComment.Video)
73c08093
C
149 }
150
4cb6d457
C
151 logger.info('Remote video comment %s removed.', videoComment.url)
152 })
153}