2017-12-12 10:53:50 -06:00
|
|
|
import { ActivityDelete } from '../../../../shared/models/activitypub'
|
|
|
|
import { logger, retryTransactionWrapper } from '../../../helpers'
|
|
|
|
import { sequelizeTypescript } from '../../../initializers'
|
|
|
|
import { AccountModel } from '../../../models/account/account'
|
|
|
|
import { VideoModel } from '../../../models/video/video'
|
|
|
|
import { VideoChannelModel } from '../../../models/video/video-channel'
|
2017-11-21 06:43:29 -06:00
|
|
|
import { getOrCreateAccountAndServer } from '../account'
|
2017-11-13 10:39:41 -06:00
|
|
|
|
|
|
|
async function processDeleteActivity (activity: ActivityDelete) {
|
2017-11-21 06:43:29 -06:00
|
|
|
const account = await getOrCreateAccountAndServer(activity.actor)
|
2017-11-13 10:39:41 -06:00
|
|
|
|
|
|
|
if (account.url === activity.id) {
|
|
|
|
return processDeleteAccount(account)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-12-12 10:53:50 -06:00
|
|
|
let videoObject = await VideoModel.loadByUrlAndPopulateAccount(activity.id)
|
2017-11-13 10:39:41 -06:00
|
|
|
if (videoObject !== undefined) {
|
|
|
|
return processDeleteVideo(account, videoObject)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-12-12 10:53:50 -06:00
|
|
|
let videoChannelObject = await VideoChannelModel.loadByUrl(activity.id)
|
2017-11-13 10:39:41 -06:00
|
|
|
if (videoChannelObject !== undefined) {
|
|
|
|
return processDeleteVideoChannel(account, videoChannelObject)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 08:52:26 -06:00
|
|
|
return
|
2017-11-13 10:39:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processDeleteActivity
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
async function processDeleteVideo (account: AccountModel, videoToDelete: VideoModel) {
|
2017-11-13 10:39:41 -06:00
|
|
|
const options = {
|
|
|
|
arguments: [ account, videoToDelete ],
|
|
|
|
errorMessage: 'Cannot remove the remote video with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
await retryTransactionWrapper(deleteRemoteVideo, options)
|
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
async function deleteRemoteVideo (account: AccountModel, videoToDelete: VideoModel) {
|
2017-11-13 10:39:41 -06:00
|
|
|
logger.debug('Removing remote video "%s".', videoToDelete.uuid)
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
await sequelizeTypescript.transaction(async t => {
|
2017-11-13 10:39:41 -06:00
|
|
|
if (videoToDelete.VideoChannel.Account.id !== account.id) {
|
|
|
|
throw new Error('Account ' + account.url + ' does not own video channel ' + videoToDelete.VideoChannel.url)
|
|
|
|
}
|
|
|
|
|
|
|
|
await videoToDelete.destroy({ transaction: t })
|
|
|
|
})
|
|
|
|
|
|
|
|
logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
|
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
async function processDeleteVideoChannel (account: AccountModel, videoChannelToRemove: VideoChannelModel) {
|
2017-11-13 10:39:41 -06:00
|
|
|
const options = {
|
|
|
|
arguments: [ account, videoChannelToRemove ],
|
|
|
|
errorMessage: 'Cannot remove the remote video channel with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
await retryTransactionWrapper(deleteRemoteVideoChannel, options)
|
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
async function deleteRemoteVideoChannel (account: AccountModel, videoChannelToRemove: VideoChannelModel) {
|
2017-11-13 10:39:41 -06:00
|
|
|
logger.debug('Removing remote video channel "%s".', videoChannelToRemove.uuid)
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
await sequelizeTypescript.transaction(async t => {
|
2017-11-13 10:39:41 -06:00
|
|
|
if (videoChannelToRemove.Account.id !== account.id) {
|
|
|
|
throw new Error('Account ' + account.url + ' does not own video channel ' + videoChannelToRemove.url)
|
|
|
|
}
|
|
|
|
|
|
|
|
await videoChannelToRemove.destroy({ transaction: t })
|
|
|
|
})
|
|
|
|
|
|
|
|
logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.uuid)
|
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
async function processDeleteAccount (accountToRemove: AccountModel) {
|
2017-11-13 10:39:41 -06:00
|
|
|
const options = {
|
|
|
|
arguments: [ accountToRemove ],
|
|
|
|
errorMessage: 'Cannot remove the remote account with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
await retryTransactionWrapper(deleteRemoteAccount, options)
|
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
async function deleteRemoteAccount (accountToRemove: AccountModel) {
|
2017-11-13 10:39:41 -06:00
|
|
|
logger.debug('Removing remote account "%s".', accountToRemove.uuid)
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
await sequelizeTypescript.transaction(async t => {
|
2017-11-13 10:39:41 -06:00
|
|
|
await accountToRemove.destroy({ transaction: t })
|
|
|
|
})
|
|
|
|
|
|
|
|
logger.info('Remote account with uuid %s removed.', accountToRemove.uuid)
|
|
|
|
}
|