Fix player play exception on chromium
[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 { ActorModel } from '../../../models/activitypub/actor'
6 import { VideoModel } from '../../../models/video/video'
7 import { VideoChannelModel } from '../../../models/video/video-channel'
8 import { getOrCreateActorAndServerAndModel } from '../actor'
9
10 async function processDeleteActivity (activity: ActivityDelete) {
11   const actor = await getOrCreateActorAndServerAndModel(activity.actor)
12
13   if (actor.url === activity.id) {
14     if (actor.type === 'Person') {
15       if (!actor.Account) throw new Error('Actor ' + actor.url + ' is a person but we cannot find it in database.')
16
17       return processDeleteAccount(actor.Account)
18     } else if (actor.type === 'Group') {
19       if (!actor.VideoChannel) throw new Error('Actor ' + actor.url + ' is a group but we cannot find it in database.')
20
21       return processDeleteVideoChannel(actor.VideoChannel)
22     }
23   }
24
25   {
26     let videoObject = await VideoModel.loadByUrlAndPopulateAccount(activity.id)
27     if (videoObject !== undefined) {
28       return processDeleteVideo(actor, videoObject)
29     }
30   }
31
32   return
33 }
34
35 // ---------------------------------------------------------------------------
36
37 export {
38   processDeleteActivity
39 }
40
41 // ---------------------------------------------------------------------------
42
43 async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
44   const options = {
45     arguments: [ actor, videoToDelete ],
46     errorMessage: 'Cannot remove the remote video with many retries.'
47   }
48
49   await retryTransactionWrapper(deleteRemoteVideo, options)
50 }
51
52 async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) {
53   logger.debug('Removing remote video "%s".', videoToDelete.uuid)
54
55   await sequelizeTypescript.transaction(async t => {
56     if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
57       throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
58     }
59
60     await videoToDelete.destroy({ transaction: t })
61   })
62
63   logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
64 }
65
66 async function processDeleteAccount (accountToRemove: AccountModel) {
67   const options = {
68     arguments: [ accountToRemove ],
69     errorMessage: 'Cannot remove the remote account with many retries.'
70   }
71
72   await retryTransactionWrapper(deleteRemoteAccount, options)
73 }
74
75 async function deleteRemoteAccount (accountToRemove: AccountModel) {
76   logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
77
78   await sequelizeTypescript.transaction(async t => {
79     await accountToRemove.destroy({ transaction: t })
80   })
81
82   logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
83 }
84
85 async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
86   const options = {
87     arguments: [ videoChannelToRemove ],
88     errorMessage: 'Cannot remove the remote video channel with many retries.'
89   }
90
91   await retryTransactionWrapper(deleteRemoteVideoChannel, options)
92 }
93
94 async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel) {
95   logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
96
97   await sequelizeTypescript.transaction(async t => {
98     await videoChannelToRemove.destroy({ transaction: t })
99   })
100
101   logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
102 }