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