1 import { ActivityDelete } from '../../../../shared/models/activitypub'
2 import { retryTransactionWrapper } from '../../../helpers/database-utils'
3 import { logger } from '../../../helpers/logger'
4 import { sequelizeTypescript } from '../../../initializers'
5 import { AccountModel } from '../../../models/account/account'
6 import { ActorModel } from '../../../models/activitypub/actor'
7 import { VideoModel } from '../../../models/video/video'
8 import { VideoChannelModel } from '../../../models/video/video-channel'
9 import { getOrCreateActorAndServerAndModel } from '../actor'
11 async function processDeleteActivity (activity: ActivityDelete) {
12 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
14 if (actor.url === activity.id) {
15 if (actor.type === 'Person') {
16 if (!actor.Account) throw new Error('Actor ' + actor.url + ' is a person but we cannot find it in database.')
18 return processDeleteAccount(actor.Account)
19 } else if (actor.type === 'Group') {
20 if (!actor.VideoChannel) throw new Error('Actor ' + actor.url + ' is a group but we cannot find it in database.')
22 return processDeleteVideoChannel(actor.VideoChannel)
27 let videoObject = await VideoModel.loadByUrlAndPopulateAccount(activity.id)
28 if (videoObject !== undefined) {
29 return processDeleteVideo(actor, videoObject)
36 // ---------------------------------------------------------------------------
42 // ---------------------------------------------------------------------------
44 async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
46 arguments: [ actor, videoToDelete ],
47 errorMessage: 'Cannot remove the remote video with many retries.'
50 await retryTransactionWrapper(deleteRemoteVideo, options)
53 async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) {
54 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
56 await sequelizeTypescript.transaction(async t => {
57 if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
58 throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
61 await videoToDelete.destroy({ transaction: t })
64 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
67 async function processDeleteAccount (accountToRemove: AccountModel) {
69 arguments: [ accountToRemove ],
70 errorMessage: 'Cannot remove the remote account with many retries.'
73 await retryTransactionWrapper(deleteRemoteAccount, options)
76 async function deleteRemoteAccount (accountToRemove: AccountModel) {
77 logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
79 await sequelizeTypescript.transaction(async t => {
80 await accountToRemove.destroy({ transaction: t })
83 logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
86 async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
88 arguments: [ videoChannelToRemove ],
89 errorMessage: 'Cannot remove the remote video channel with many retries.'
92 await retryTransactionWrapper(deleteRemoteVideoChannel, options)
95 async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel) {
96 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
98 await sequelizeTypescript.transaction(async t => {
99 await videoChannelToRemove.destroy({ transaction: t })
102 logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)