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