1 import { ActivityDelete } from '../../../shared/models/activitypub/activity'
2 import { getOrCreateAccount } from '../../helpers/activitypub'
3 import { retryTransactionWrapper } from '../../helpers/database-utils'
4 import { logger } from '../../helpers/logger'
5 import { database as db } from '../../initializers'
6 import { AccountInstance } from '../../models/account/account-interface'
7 import { VideoChannelInstance } from '../../models/video/video-channel-interface'
8 import { VideoInstance } from '../../models/video/video-interface'
10 async function processDeleteActivity (activity: ActivityDelete) {
11 const account = await getOrCreateAccount(activity.actor)
13 if (account.url === activity.id) {
14 return processDeleteAccount(account)
18 let videoObject = await db.Video.loadByUrl(activity.id)
19 if (videoObject !== undefined) {
20 return processDeleteVideo(account, videoObject)
25 let videoChannelObject = await db.VideoChannel.loadByUrl(activity.id)
26 if (videoChannelObject !== undefined) {
27 return processDeleteVideoChannel(account, videoChannelObject)
34 // ---------------------------------------------------------------------------
40 // ---------------------------------------------------------------------------
42 async function processDeleteVideo (account: AccountInstance, videoToDelete: VideoInstance) {
44 arguments: [ account, videoToDelete ],
45 errorMessage: 'Cannot remove the remote video with many retries.'
48 await retryTransactionWrapper(deleteRemoteVideo, options)
51 async function deleteRemoteVideo (account: AccountInstance, videoToDelete: VideoInstance) {
52 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
54 await db.sequelize.transaction(async t => {
55 if (videoToDelete.VideoChannel.Account.id !== account.id) {
56 throw new Error('Account ' + account.url + ' does not own video channel ' + videoToDelete.VideoChannel.url)
59 await videoToDelete.destroy({ transaction: t })
62 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
65 async function processDeleteVideoChannel (account: AccountInstance, videoChannelToRemove: VideoChannelInstance) {
67 arguments: [ account, videoChannelToRemove ],
68 errorMessage: 'Cannot remove the remote video channel with many retries.'
71 await retryTransactionWrapper(deleteRemoteVideoChannel, options)
74 async function deleteRemoteVideoChannel (account: AccountInstance, videoChannelToRemove: VideoChannelInstance) {
75 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.uuid)
77 await db.sequelize.transaction(async t => {
78 if (videoChannelToRemove.Account.id !== account.id) {
79 throw new Error('Account ' + account.url + ' does not own video channel ' + videoChannelToRemove.url)
82 await videoChannelToRemove.destroy({ transaction: t })
85 logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.uuid)
88 async function processDeleteAccount (accountToRemove: AccountInstance) {
90 arguments: [ accountToRemove ],
91 errorMessage: 'Cannot remove the remote account with many retries.'
94 await retryTransactionWrapper(deleteRemoteAccount, options)
97 async function deleteRemoteAccount (accountToRemove: AccountInstance) {
98 logger.debug('Removing remote account "%s".', accountToRemove.uuid)
100 await db.sequelize.transaction(async t => {
101 await accountToRemove.destroy({ transaction: t })
104 logger.info('Remote account with uuid %s removed.', accountToRemove.uuid)