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 { VideoCommentModel } from '../../../models/video/video-comment'
10 import { getOrCreateActorAndServerAndModel } from '../actor'
12 async function processDeleteActivity (activity: ActivityDelete) {
13 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
14 const objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
16 if (actor.url === objectUrl) {
17 if (actor.type === 'Person') {
18 if (!actor.Account) throw new Error('Actor ' + actor.url + ' is a person but we cannot find it in database.')
20 return processDeleteAccount(actor.Account)
21 } else if (actor.type === 'Group') {
22 if (!actor.VideoChannel) throw new Error('Actor ' + actor.url + ' is a group but we cannot find it in database.')
24 return processDeleteVideoChannel(actor.VideoChannel)
29 const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(objectUrl)
30 if (videoCommentInstance) {
31 return processDeleteVideoComment(actor, videoCommentInstance)
36 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
38 return processDeleteVideo(actor, videoInstance)
45 // ---------------------------------------------------------------------------
51 // ---------------------------------------------------------------------------
53 async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
55 arguments: [ actor, videoToDelete ],
56 errorMessage: 'Cannot remove the remote video with many retries.'
59 await retryTransactionWrapper(deleteRemoteVideo, options)
62 async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) {
63 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
65 await sequelizeTypescript.transaction(async t => {
66 if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
67 throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
70 await videoToDelete.destroy({ transaction: t })
73 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
76 async function processDeleteAccount (accountToRemove: AccountModel) {
78 arguments: [ accountToRemove ],
79 errorMessage: 'Cannot remove the remote account with many retries.'
82 await retryTransactionWrapper(deleteRemoteAccount, options)
85 async function deleteRemoteAccount (accountToRemove: AccountModel) {
86 logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
88 await sequelizeTypescript.transaction(async t => {
89 await accountToRemove.destroy({ transaction: t })
92 logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
95 async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
97 arguments: [ videoChannelToRemove ],
98 errorMessage: 'Cannot remove the remote video channel with many retries.'
101 await retryTransactionWrapper(deleteRemoteVideoChannel, options)
104 async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel) {
105 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
107 await sequelizeTypescript.transaction(async t => {
108 await videoChannelToRemove.destroy({ transaction: t })
111 logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
114 async function processDeleteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) {
116 arguments: [ actor, videoComment ],
117 errorMessage: 'Cannot remove the remote video comment with many retries.'
120 await retryTransactionWrapper(deleteRemoteVideoComment, options)
123 function deleteRemoteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) {
124 logger.debug('Removing remote video comment "%s".', videoComment.url)
126 return sequelizeTypescript.transaction(async t => {
127 await videoComment.destroy({ transaction: t })
129 logger.info('Remote video comment %s removed.', videoComment.url)