]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-delete.ts
Fix delete activities
[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'
3fd3ab2d
C
4import { sequelizeTypescript } from '../../../initializers'
5import { AccountModel } from '../../../models/account/account'
50d6de9c 6import { ActorModel } from '../../../models/activitypub/actor'
3fd3ab2d
C
7import { VideoModel } from '../../../models/video/video'
8import { VideoChannelModel } from '../../../models/video/video-channel'
4cb6d457 9import { VideoCommentModel } from '../../../models/video/video-comment'
50d6de9c 10import { getOrCreateActorAndServerAndModel } from '../actor'
7a7724e6
C
11
12async function processDeleteActivity (activity: ActivityDelete) {
50d6de9c 13 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
7a7724e6 14
c3badc81 15 if (actor.url === activity.object) {
50d6de9c
C
16 if (actor.type === 'Person') {
17 if (!actor.Account) throw new Error('Actor ' + actor.url + ' is a person but we cannot find it in database.')
7a7724e6 18
50d6de9c
C
19 return processDeleteAccount(actor.Account)
20 } else if (actor.type === 'Group') {
21 if (!actor.VideoChannel) throw new Error('Actor ' + actor.url + ' is a group but we cannot find it in database.')
22
23 return processDeleteVideoChannel(actor.VideoChannel)
7a7724e6
C
24 }
25 }
26
27 {
c3badc81 28 const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(activity.object)
4cb6d457
C
29 if (videoCommentInstance) {
30 return processDeleteVideoComment(actor, videoCommentInstance)
31 }
32 }
33
34 {
c3badc81 35 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(activity.object)
4cb6d457
C
36 if (videoInstance) {
37 return processDeleteVideo(actor, videoInstance)
7a7724e6
C
38 }
39 }
40
79d5caf9 41 return
7a7724e6
C
42}
43
44// ---------------------------------------------------------------------------
45
46export {
47 processDeleteActivity
48}
49
50// ---------------------------------------------------------------------------
51
50d6de9c 52async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
7a7724e6 53 const options = {
50d6de9c 54 arguments: [ actor, videoToDelete ],
7a7724e6
C
55 errorMessage: 'Cannot remove the remote video with many retries.'
56 }
57
58 await retryTransactionWrapper(deleteRemoteVideo, options)
59}
60
50d6de9c 61async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) {
7a7724e6
C
62 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
63
3fd3ab2d 64 await sequelizeTypescript.transaction(async t => {
50d6de9c
C
65 if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
66 throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
7a7724e6
C
67 }
68
69 await videoToDelete.destroy({ transaction: t })
70 })
71
72 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
73}
74
50d6de9c 75async function processDeleteAccount (accountToRemove: AccountModel) {
7a7724e6 76 const options = {
50d6de9c
C
77 arguments: [ accountToRemove ],
78 errorMessage: 'Cannot remove the remote account with many retries.'
7a7724e6
C
79 }
80
50d6de9c 81 await retryTransactionWrapper(deleteRemoteAccount, options)
7a7724e6
C
82}
83
50d6de9c
C
84async function deleteRemoteAccount (accountToRemove: AccountModel) {
85 logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
7a7724e6 86
3fd3ab2d 87 await sequelizeTypescript.transaction(async t => {
50d6de9c 88 await accountToRemove.destroy({ transaction: t })
7a7724e6
C
89 })
90
50d6de9c 91 logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
7a7724e6
C
92}
93
50d6de9c 94async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
7a7724e6 95 const options = {
50d6de9c
C
96 arguments: [ videoChannelToRemove ],
97 errorMessage: 'Cannot remove the remote video channel with many retries.'
7a7724e6
C
98 }
99
50d6de9c 100 await retryTransactionWrapper(deleteRemoteVideoChannel, options)
7a7724e6
C
101}
102
50d6de9c
C
103async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel) {
104 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
7a7724e6 105
3fd3ab2d 106 await sequelizeTypescript.transaction(async t => {
50d6de9c 107 await videoChannelToRemove.destroy({ transaction: t })
7a7724e6
C
108 })
109
50d6de9c 110 logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
7a7724e6 111}
4cb6d457
C
112
113async function processDeleteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) {
114 const options = {
115 arguments: [ actor, videoComment ],
116 errorMessage: 'Cannot remove the remote video comment with many retries.'
117 }
118
119 await retryTransactionWrapper(deleteRemoteVideoComment, options)
120}
121
122function deleteRemoteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) {
123 logger.debug('Removing remote video comment "%s".', videoComment.url)
124
125 return sequelizeTypescript.transaction(async t => {
126 await videoComment.destroy({ transaction: t })
127
128 logger.info('Remote video comment %s removed.', videoComment.url)
129 })
130}