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 objectUrl = typeof activity.object === 'string' ? activity.object : activity.object.id
15 if (activity.actor === objectUrl) {
16 let actor = await ActorModel.loadByUrl(activity.actor)
19 if (actor.type === 'Person') {
20 if (!actor.Account) throw new Error('Actor ' + actor.url + ' is a person but we cannot find it in database.')
22 actor.Account.Actor = await actor.Account.$get('Actor') as ActorModel
23 return processDeleteAccount(actor.Account)
24 } else if (actor.type === 'Group') {
25 if (!actor.VideoChannel) throw new Error('Actor ' + actor.url + ' is a group but we cannot find it in database.')
27 actor.VideoChannel.Actor = await actor.VideoChannel.$get('Actor') as ActorModel
28 return processDeleteVideoChannel(actor.VideoChannel)
32 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
34 const videoCommentInstance = await VideoCommentModel.loadByUrlAndPopulateAccount(objectUrl)
35 if (videoCommentInstance) {
36 return processDeleteVideoComment(actor, videoCommentInstance)
41 const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(objectUrl)
43 return processDeleteVideo(actor, videoInstance)
50 // ---------------------------------------------------------------------------
56 // ---------------------------------------------------------------------------
58 async function processDeleteVideo (actor: ActorModel, videoToDelete: VideoModel) {
60 arguments: [ actor, videoToDelete ],
61 errorMessage: 'Cannot remove the remote video with many retries.'
64 await retryTransactionWrapper(deleteRemoteVideo, options)
67 async function deleteRemoteVideo (actor: ActorModel, videoToDelete: VideoModel) {
68 logger.debug('Removing remote video "%s".', videoToDelete.uuid)
70 await sequelizeTypescript.transaction(async t => {
71 if (videoToDelete.VideoChannel.Account.Actor.id !== actor.id) {
72 throw new Error('Account ' + actor.url + ' does not own video channel ' + videoToDelete.VideoChannel.Actor.url)
75 await videoToDelete.destroy({ transaction: t })
78 logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
81 async function processDeleteAccount (accountToRemove: AccountModel) {
83 arguments: [ accountToRemove ],
84 errorMessage: 'Cannot remove the remote account with many retries.'
87 await retryTransactionWrapper(deleteRemoteAccount, options)
90 async function deleteRemoteAccount (accountToRemove: AccountModel) {
91 logger.debug('Removing remote account "%s".', accountToRemove.Actor.uuid)
93 await sequelizeTypescript.transaction(async t => {
94 await accountToRemove.destroy({ transaction: t })
97 logger.info('Remote account with uuid %s removed.', accountToRemove.Actor.uuid)
100 async function processDeleteVideoChannel (videoChannelToRemove: VideoChannelModel) {
102 arguments: [ videoChannelToRemove ],
103 errorMessage: 'Cannot remove the remote video channel with many retries.'
106 await retryTransactionWrapper(deleteRemoteVideoChannel, options)
109 async function deleteRemoteVideoChannel (videoChannelToRemove: VideoChannelModel) {
110 logger.debug('Removing remote video channel "%s".', videoChannelToRemove.Actor.uuid)
112 await sequelizeTypescript.transaction(async t => {
113 await videoChannelToRemove.destroy({ transaction: t })
116 logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.Actor.uuid)
119 async function processDeleteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) {
121 arguments: [ actor, videoComment ],
122 errorMessage: 'Cannot remove the remote video comment with many retries.'
125 await retryTransactionWrapper(deleteRemoteVideoComment, options)
128 function deleteRemoteVideoComment (actor: ActorModel, videoComment: VideoCommentModel) {
129 logger.debug('Removing remote video comment "%s".', videoComment.url)
131 return sequelizeTypescript.transaction(async t => {
132 await videoComment.destroy({ transaction: t })
134 logger.info('Remote video comment %s removed.', videoComment.url)