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