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'
9 async function processDeleteActivity (activity: ActivityDelete) {
10 const account = await getOrCreateAccountAndServer(activity.actor)
12 if (account.url === activity.id) {
13 return processDeleteAccount(account)
17 let videoObject = await VideoModel.loadByUrlAndPopulateAccount(activity.id)
18 if (videoObject !== undefined) {
19 return processDeleteVideo(account, videoObject)
24 let videoChannelObject = await VideoChannelModel.loadByUrl(activity.id)
25 if (videoChannelObject !== undefined) {
26 return processDeleteVideoChannel(account, videoChannelObject)
33 // ---------------------------------------------------------------------------
39 // ---------------------------------------------------------------------------
41 async function processDeleteVideo (account: AccountModel, videoToDelete: VideoModel) {
43 arguments: [ account, videoToDelete ],
44 errorMessage: 'Cannot remove the remote video with many retries.'
47 await retryTransactionWrapper(deleteRemoteVideo, options)
50 async function deleteRemoteVideo (account: AccountModel, videoToDelete: VideoModel) {
51 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
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)
58 await videoToDelete.destroy({ transaction: t })
61 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
64 async function processDeleteVideoChannel (account: AccountModel, videoChannelToRemove: VideoChannelModel) {
66 arguments: [ account, videoChannelToRemove ],
67 errorMessage: 'Cannot remove the remote video channel with many retries.'
70 await retryTransactionWrapper(deleteRemoteVideoChannel, options)
73 async function deleteRemoteVideoChannel (account: AccountModel, videoChannelToRemove: VideoChannelModel) {
74 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.uuid)
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)
81 await videoChannelToRemove.destroy({ transaction: t })
84 logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.uuid)
87 async function processDeleteAccount (accountToRemove: AccountModel) {
89 arguments: [ accountToRemove ],
90 errorMessage: 'Cannot remove the remote account with many retries.'
93 await retryTransactionWrapper(deleteRemoteAccount, options)
96 async function deleteRemoteAccount (accountToRemove: AccountModel) {
97 logger.debug('Removing remote account "%s".', accountToRemove.uuid)
99 await sequelizeTypescript.transaction(async t => {
100 await accountToRemove.destroy({ transaction: t })
103 logger.info('Remote account with uuid %s removed.', accountToRemove.uuid)