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