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'
10 async function processDeleteActivity (activity: ActivityDelete) {
11 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
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.')
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.')
21 return processDeleteVideoChannel(actor.VideoChannel)
26 let videoObject = await VideoModel.loadByUrlAndPopulateAccount(activity.id)
27 if (videoObject !== undefined) {
28 return processDeleteVideo(actor, videoObject)
35 // ---------------------------------------------------------------------------
41 // ---------------------------------------------------------------------------
43 async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
45 arguments: [ actor, videoToDelete ],
46 errorMessage: 'Cannot remove the remote video with many retries.'
49 await retryTransactionWrapper(deleteRemoteVideo, options)
52 async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) {
53 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
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)
60 await videoToDelete.destroy({ transaction: t })
63 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
66 async function processDeleteAccount (accountToRemove: AccountModel) {
68 arguments: [ accountToRemove ],
69 errorMessage: 'Cannot remove the remote account with many retries.'
72 await retryTransactionWrapper(deleteRemoteAccount, options)
75 async function deleteRemoteAccount (accountToRemove: AccountModel) {
76 logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
78 await sequelizeTypescript.transaction(async t => {
79 await accountToRemove.destroy({ transaction: t })
82 logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
85 async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
87 arguments: [ videoChannelToRemove ],
88 errorMessage: 'Cannot remove the remote video channel with many retries.'
91 await retryTransactionWrapper(deleteRemoteVideoChannel, options)
94 async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel) {
95 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
97 await sequelizeTypescript.transaction(async t => {
98 await videoChannelToRemove.destroy({ transaction: t })
101 logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)