8f280d37fa44bf0763086c2c22e679c82ae7d86a
[oweals/peertube.git] / server / lib / activitypub / process / process-delete.ts
1 import { ActivityDelete } from '../../../../shared/models/activitypub'
2 import { logger, retryTransactionWrapper } from '../../../helpers'
3 import { sequelizeTypescript } from '../../../initializers'
4 import { AccountModel } from '../../../models/account/account'
5 import { VideoModel } from '../../../models/video/video'
6 import { VideoChannelModel } from '../../../models/video/video-channel'
7 import { getOrCreateAccountAndServer } from '../account'
8
9 async function processDeleteActivity (activity: ActivityDelete) {
10   const account = await getOrCreateAccountAndServer(activity.actor)
11
12   if (account.url === activity.id) {
13     return processDeleteAccount(account)
14   }
15
16   {
17     let videoObject = await VideoModel.loadByUrlAndPopulateAccount(activity.id)
18     if (videoObject !== undefined) {
19       return processDeleteVideo(account, videoObject)
20     }
21   }
22
23   {
24     let videoChannelObject = await VideoChannelModel.loadByUrl(activity.id)
25     if (videoChannelObject !== undefined) {
26       return processDeleteVideoChannel(account, videoChannelObject)
27     }
28   }
29
30   return
31 }
32
33 // ---------------------------------------------------------------------------
34
35 export {
36   processDeleteActivity
37 }
38
39 // ---------------------------------------------------------------------------
40
41 async function processDeleteVideo (account: AccountModel, videoToDelete: VideoModel) {
42   const options = {
43     arguments: [ account, videoToDelete ],
44     errorMessage: 'Cannot remove the remote video with many retries.'
45   }
46
47   await retryTransactionWrapper(deleteRemoteVideo, options)
48 }
49
50 async function deleteRemoteVideo (account: AccountModel, videoToDelete: VideoModel) {
51   logger.debug('Removing remote video "%s".', videoToDelete.uuid)
52
53   await sequelizeTypescript.transaction(async t => {
54     if (videoToDelete.VideoChannel.Account.id !== account.id) {
55       throw new Error('Account ' + account.url + ' does not own video channel ' + videoToDelete.VideoChannel.url)
56     }
57
58     await videoToDelete.destroy({ transaction: t })
59   })
60
61   logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
62 }
63
64 async function processDeleteVideoChannel (account: AccountModel, videoChannelToRemove: VideoChannelModel) {
65   const options = {
66     arguments: [ account, videoChannelToRemove ],
67     errorMessage: 'Cannot remove the remote video channel with many retries.'
68   }
69
70   await retryTransactionWrapper(deleteRemoteVideoChannel, options)
71 }
72
73 async function deleteRemoteVideoChannel (account: AccountModel, videoChannelToRemove: VideoChannelModel) {
74   logger.debug('Removing remote video channel "%s".', videoChannelToRemove.uuid)
75
76   await sequelizeTypescript.transaction(async t => {
77     if (videoChannelToRemove.Account.id !== account.id) {
78       throw new Error('Account ' + account.url + ' does not own video channel ' + videoChannelToRemove.url)
79     }
80
81     await videoChannelToRemove.destroy({ transaction: t })
82   })
83
84   logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.uuid)
85 }
86
87 async function processDeleteAccount (accountToRemove: AccountModel) {
88   const options = {
89     arguments: [ accountToRemove ],
90     errorMessage: 'Cannot remove the remote account with many retries.'
91   }
92
93   await retryTransactionWrapper(deleteRemoteAccount, options)
94 }
95
96 async function deleteRemoteAccount (accountToRemove: AccountModel) {
97   logger.debug('Removing remote account "%s".', accountToRemove.uuid)
98
99   await sequelizeTypescript.transaction(async t => {
100     await accountToRemove.destroy({ transaction: t })
101   })
102
103   logger.info('Remote account with uuid %s removed.', accountToRemove.uuid)
104 }